import java.util.HashMap;  
import java.util.Scanner;  
  
public class Store {  
  
    // Scanner to get user input throughout the program  
    private static final Scanner scanner = new Scanner(System.in);  
  
    // Shopping cart using a HashMap to store item name and quantity pairs  
    private static final HashMap<String, Integer> ShoppingCart = new HashMap<>();  
  
    // Store inventory using a HashMap to store item name and price pairs  
    private static final HashMap<String, Float> StoreItems = new HashMap<>();  
  
    public static void main(String[] args) {  
  
        // Initialize the store inventory with a few items and their prices  
        StoreItems.put("Apple", 1.00F);  
        StoreItems.put("Banana", 0.50F);  
        StoreItems.put("Orange", 1.50F);  
        StoreItems.put("Grapes", 2.00F);  
        StoreItems.put("Pineapple", 3.00F);  
        StoreItems.put("Watermelon", 4.00F);  
        StoreItems.put("Strawberry", 1.30F);  
  
        // Welcome the customer and display the available items in a numbered list  
        System.out.println("Welcome to Jason's store");  
        System.out.println("Here are the items we have in stock:");  
  
        int index = 0;  
        for (String item : StoreItems.keySet()) {  
            System.out.println(index + ": " + item + " - $" + StoreItems.get(item));  
            index++;  
        }  
  
        // Initiate the purchase process  
        purchaseItem();  
  
        // Display the contents of the shopping cart, including item names, quantities, and individual prices  
        System.out.println("Here is your cart:");  
        float total = 0;  
        for (String item : ShoppingCart.keySet()) {  
            System.out.println(item + " - " + ShoppingCart.get(item) + " - at " + StoreItems.get(item) + "$ each");  
  
            // Calculate the running total cost of the items  
            total += ShoppingCart.get(item) * StoreItems.get(item);  
        }  
  
        // Calculate and display the tax (13%) rounded to 2 decimal places  
        float taxes = (float) (Math.round(total * 0.13 * 100.0) / 100.0);  
  
        // Round the subtotal and total cost to 2 decimal places  
        float subtotal = (float) (Math.round(total * 100.0) / 100.0);  
        float totalCost = (float) (Math.round((total + taxes) * 100.0) / 100.0);  
        System.out.println("Subtotal: $" + subtotal);  
        System.out.println("Tax (13%): $" + taxes);  
        System.out.println("Total: $" + totalCost);  
  
        // Thank the customer for their purchase  
        System.out.println("Thank you for shopping at Jason's store");  
  
        // Close the scanner to release system resources  
        scanner.close();  
    }  
  
    // Function to guide the user through purchasing a single item  
    public static void purchaseItem() {  
  
        // Prompt the user to enter the item number they want to buy  
        System.out.print("Please enter the item number you would like to purchase: ");  
        int itemIndex = scanner.nextInt();  
  
        // Retrieve the item name corresponding to the entered number  
        String ItemName = StoreItems.keySet().toArray()[itemIndex].toString();  
  
        // Prompt the user to enter the quantity of the item  
        System.out.print("Please enter the quantity you would like to purchase: ");  
        int ItemQuantity = scanner.nextInt();  
  
        // Update the shopping cart by adding the item or increasing its quantity if it's already there  
        if (ShoppingCart.containsKey(ItemName)) {  
            ShoppingCart.put(ItemName, ShoppingCart.get(ItemName) + ItemQuantity);  
        } else {  
            ShoppingCart.put(ItemName, ItemQuantity);  
        }  
        System.out.println("You have added " + ItemQuantity + " " + ItemName + " to your cart.");  
  
        // Ask if the user wants to buy anything else  
        System.out.print("Would you like to purchase anything else? (Y/N): ");  
        String response = scanner.next();  
  
        // Recursively call the function if the user wants to continue shopping  
        if (response.equalsIgnoreCase("Y")) {  
            purchaseItem();  
        }  
    }  
}

IPO Process

Input:

  • Store Inventory: The program starts with predefined item names and their prices stored in the StoreItems HashMap.
  • User Selections: The user provides input by selecting item numbers and specifying quantities through the console using the Scanner.

Process:

  • Display Inventory: The program displays a numbered list of available items and their prices to the user.
  • Item Selection and Quantity: The purchaseItem function repeatedly prompts the user to select an item by its number and enter the desired quantity.
  • Cart Update: The selected items and their quantities are added to the ShoppingCart HashMap. If an item is already in the cart, its quantity is increased.
  • Total Calculation: Once the user finishes shopping, the program iterates through the ShoppingCart, calculates the subtotal by multiplying item quantities by their prices, and adds a 13% tax.
  • Rounding: The subtotal and tax are rounded to two decimal places for a cleaner display.

Output:

  • Cart Contents: The program prints the contents of the shopping cart, showing each item, its quantity, and individual price.
  • Bill Details: The program then presents the subtotal, tax amount, and final total cost.
  • Thank You Message: A simple thank you message is displayed to conclude the shopping experience.

Example Output

Welcome to Jason's store
Here are the items we have in stock:
0: Apple - $1.0
1: Grapes - $2.0
2: Watermelon - $4.0
3: Strawberry - $1.3
4: Pineapple - $3.0
5: Orange - $1.5
6: Banana - $0.5
Please enter the item number you would like to purchase: 3
Please enter the quantity you would like to purchase: 7
You have added 7 Strawberry to your cart.
Would you like to purchase anything else? (Y/N): Y
Please enter the item number you would like to purchase: 4
Please enter the quantity you would like to purchase: 29
You have added 29 Pineapple to your cart.
Would you like to purchase anything else? (Y/N): N
Here is your cart:
Strawberry - 7 - at 1.3$ each
Pineapple - 29 - at 3.0$ each
Subtotal: $96.1
Tax (13%): $12.49
Total: $108.59
Thank you for shopping at Jason's store