Untitled

 avatar
unknown
plain_text
2 months ago
4.7 kB
3
Indexable
import java.util.Scanner;

public class GrocerySystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double customerAmount = 0; // Initialize customer amount
        String receipt = "------ Grocery Receipt ------ ";
        double totalAmount = 0;
        
        int choice = 0;
        while (choice != 6) {
            System.out.println("PUREBROOZE GROCERY STORE MENU:");
            System.out.println("1. Fruits 2. Frozen Items 3. Drinks 4. Vegetables 5. Meat 6. Checkout ");
            choice = scanner.nextInt();
            
            if (choice == 6) {
                if (totalAmount == 0) {
                    System.out.println("You have not purchased anything.");
                } else {
                    System.out.print("Enter customer amount: $");
                    customerAmount = scanner.nextDouble();

                    if (customerAmount < totalAmount) {
                        System.out.println("Insufficient funds. Please enter a valid amount.");
                        continue;
                    }

                    System.out.println("Processing receipt...");
                }
                break;
            }
            
            System.out.println("Available Items:");
            if (choice == 1) {
                System.out.println("1. Apple - $1.5");
                System.out.println("2. Banana - $0.5");
                System.out.println("3. Orange - $2.0");
            } else if (choice == 2) {
                System.out.println("4. Ice Cream - $4.99");
                System.out.println("5. Frozen Pizza - $5.99");
            } else if (choice == 3) {
                System.out.println("6. Soda - $1.25");
                System.out.println("7. Water - $0.99");
            } else if (choice == 4) {
                System.out.println("8. Carrots - $2.50");
                System.out.println("9. Lettuce - $1.75");
            } else if (choice == 5) {
                System.out.println("10. Chicken - $6.99");
                System.out.println("11. Beef - $8.99");
            } else {
                System.out.println("Invalid choice. Please try again.");
                continue;
            }
            
            System.out.print("Enter item number to buy: ");
            int itemNumber = scanner.nextInt();
            double price = 0;
            String itemName = "empty";
            
            if (itemNumber == 1) {
                price = 1.5;
                itemName = "Apple";
            } else if (itemNumber == 2) {
                price = 0.5;
                itemName = "Banana";
            } else if (itemNumber == 3) {
                price = 2.0;
                itemName = "Orange";
            } else if (itemNumber == 4) {
                price = 4.99;
                itemName = "Ice Cream";
            } else if (itemNumber == 5) {
                price = 5.99;
                itemName = "Frozen Pizza";
            } else if (itemNumber == 6) {
                price = 1.25;
                itemName = "Soda";
            } else if (itemNumber == 7) {
                price = 0.99;
                itemName = "Water";
            } else if (itemNumber == 8) {
                price = 2.50;
                itemName = "Carrots";
            } else if (itemNumber == 9) {
                price = 1.75;
                itemName = "Lettuce";
            } else if (itemNumber == 10) {
                price = 6.99;
                itemName = "Chicken";
            } else if (itemNumber == 11) {
                price = 8.99;
                itemName = "Beef";
            } else {
                System.out.println("Invalid selection.");
                continue;
            }
            
            if (!itemName.equals("empty")) {
                System.out.print("Enter quantity to buy: ");
                int quantity = scanner.nextInt();
                double totalCost = price * quantity;
                totalAmount += totalCost;
                receipt += itemName + " x" + quantity + " - $" + totalCost + "  ";
                System.out.println("You bought " + quantity + " " + itemName + "(s) for $" + totalCost);
            }
        }
        
        if (totalAmount > 0) {
            double change = customerAmount - totalAmount;
            receipt += "--------------------------  Total: $" + totalAmount + "  Amount Paid: $" + customerAmount + "  Change: $" + change + "  Thank you for shopping! Have a great day!";
            System.out.println(receipt);
        }

        scanner.close();
    }
}
Editor is loading...
Leave a Comment