StudyLover
  • Home
  • Study Zone
  • Features
    • Workout Interval Timer
    • Typing Tutor
  • Profiles
  • Contact us
  • Sign in
StudyLover The Decision Hub
Download
  1. Java
  2. Programs
The System Architect : The Loop Mechanic
Programs
Initiative: To master multi-path branching using if-else and the modern JDK 21 switch expressions.
import java.util.Scanner; // Import Scanner class for capturing user console input

public class DecisionHub { // Define the class for logic and decision handling
    public static void main(String[] args) { // Main method starting the program execution
        try (Scanner sc = new Scanner(System.in)) { // Open Scanner in try-with-resources for auto-closing
            System.out.print("Enter Purchase Amount: "); // Prompt the user to input a dollar amount
            if (!sc.hasNextDouble()) { // Validate if the input is actually a valid number
                System.out.println("Invalid input!"); // Print error if input is not numeric
                return; // Terminate the program execution early
            } // End of the validation if-block
            
            double amount = sc.nextDouble(); // Store the valid numeric input into variable 'amount'
            System.out.print("Membership (Gold/Silver/None): "); // Prompt user for their membership type
            String type = sc.next().toLowerCase(); // Read input and convert to lowercase for matching
            
            double discount = 0.0; // Initialize the discount percentage variable at zero
            
            if (amount >= 5000) { // Check if the purchase qualifies for the high-tier range
                discount = 10.0; // Assign a 10 percent base discount for large purchases
            } else if (amount >= 1000) { // Check if purchase falls into the mid-tier range
                discount = 5.0; // Assign a 5 percent base discount for mid-tier purchases
            } else { // Handle all purchases that fall below the 1000 threshold
                discount = 0.0; // No base discount for smaller purchases
            } // End of the range-based if-else ladder
            
            double extra = switch (type) { // Use JDK 21 switch expression to calculate extra discount
                case "gold" -> 15.0; // Return 15 percent extra if the user is a Gold member
                case "silver" -> 10.0; // Return 10 percent extra if the user is a Silver member
                case "none" -> 0.0; // Return zero extra for non-members
                default -> { // Handle any unexpected or misspelled input strings
                    System.out.println("Unknown type, no extra bonus."); // Log the mismatch
                    yield 0.0; // Return zero using the yield keyword in a block
                } // End of the default case block
            }; // End of the switch expression assignment
            
            double totalDiscount = discount + extra; // Sum the base and membership discounts together
            double savings = (totalDiscount / 100) * amount; // Calculate the actual money saved
            
            System.out.println("Total Discount: " + totalDiscount + "%"); // Print total percentage to user
            System.out.println("Savings: $" + savings); // Print the monetary savings to the user
            System.out.println("Final: $" + (amount - savings)); // Print the final price after the discount
        } // End of the try-with-resources block (Scanner closes here)
    } // End of the main method
} // End of the DecisionHub class
The System Architect The Loop Mechanic
Our Products & Services
  • Home
Connect with us
  • Contact us
  • info.studylover@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website