Untitled

 avatar
unknown
plain_text
2 months ago
4.6 kB
1
Indexable
import java.util.Scanner;

public class GrocerySystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double customerAmount;
        String receipt = "------ Grocery Receipt ------ ";
        double totalAmount = 0;
        
        System.out.print("Enter customer amount: $");
        customerAmount = scanner.nextDouble();
        
        int choice = 0;
        while (choice != 6) {
            System.out.println("Grocery System Menu:");
            System.out.println("1. Fruits");
            System.out.println("2. Frozen Items");
            System.out.println("3. Drinks");
            System.out.println("4. Vegetables");
            System.out.println("5. Meat");
            System.out.println("6. Checkout");
            System.out.print("Choose an option: ");
            choice = scanner.nextInt();
            
            if (choice == 6) {
                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 = "";
            
            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;
            }
            
            System.out.print("Enter quantity to buy: ");
            int quantity = scanner.nextInt();
            double totalCost = price * quantity;
            
            if (totalCost > customerAmount) {
                System.out.println("Insufficient funds.");
            } else {
                customerAmount -= totalCost;
                totalAmount += totalCost;
                receipt += itemName + " x" + quantity + " - $" + totalCost + "  ";
                System.out.println("You bought " + quantity + " " + itemName + "(s) for $" + totalCost);
                System.out.println("Remaining balance: $" + customerAmount);
            }
        }
        
        receipt += "--------------------------  Total: $" + totalAmount + "  Paid: $" + (totalAmount + customerAmount) + "  Change: $" + customerAmount + "  Thank you for shopping! Have a great day!";
        System.out.println(receipt);
    }
}
Editor is loading...
Leave a Comment