Search code examples
javamethodsswitch-statementbluej

How do I print the bought items at the time of bill?


I want to print what I've ordered in the starters method with the quantity when printing the bill. Any suggestions as to how I will do it? I have only written an instance of my program since it the entire problem is very large.

Thanks in advance.

    static void Bill()
    {

        System.out.println("\f");
        System.out.println("*********************************************************");
        System.out.println("*********************************************************");
        System.out.println("**************************BILL***************************");
        System.out.println(ob);
        System.out.println("Your phone number - "+j);
        int Z = (int)(Math.random()*10000000);
        System.out.println("Bill number is "+Z);
        System.out.println("The total bill amount is Rs."+sum);
        System.out.println();
        System.out.println("Hope you enjoyed. Visit us again soon!");
        Food=0; 
    }

        static void Starters()
    {
        System.out.println("You have selected Starters");
        System.out.println();
        System.out.println("1. Spring Rolls               - Rs 350");
        System.out.println("2. Onion Rings                - Rs 350");
        System.out.println("3. Fried Raviolli             - Rs 400");
        System.out.println("4. Gorgonzola                 - Rs 400");
        System.out.println("5. Fresh Ricotta Crostini     - Rs 475");
        System.out.println("6. Potato Fritters            - Rs 500");
        System.out.println();
        System.out.println("Enter your desired option");
        starterOption=sc.nextInt();
        System.out.println("Enter quantity");
        quantity=sc.nextInt();
        switch(starterOption)
        {
            case 1:
            s1 = quantity * 350;
            break;
            case 2:
            s2 = quantity * 350;
            break;
            case 3:
            s3 = quantity * 400;
            break;
            case 4:
            s4 = quantity * 400;
            break;
            case 5:
            s5 = quantity * 475;
            break;
            case 6:
            s6 = quantity * 500;
            break;
            default:
            System.out.println("You haven't ordered anything");
            break;
        }
        System.out.println("Do you want to order anything else? Y/N");
        A = sc1.next();
        if(A.equalsIgnoreCase("Y"))
        {
            System.out.println("Do you want to order from the same section? Y/N ");
            F=sc.next();
            if(F.equalsIgnoreCase("Y"))
            {
                sum=s1+s2+s3+s4+s5+s6;
                Starters();
            }
            else
            {
                sum=s1+s2+s3+s4+s5+s6;
                Food_Items();
            }
        }
        else
        {
            sum=s1+s2+s3+s4+s5+s6;
            Bill();
        }
    }

Solution

  • There are several improvement you could do to your code. You have to store what you have already order, if you want to show all the elements on the bill. Each time you order, you can add the element to a List

    You need to store objects in the list, so you could create some objects for your dishes. Enums seems to be appropiate to your application, this could be some example for the starters:

    enum Starters {
        SPRING_ROLLS("Spring Rolls", "Rs 350", 5),
        FRIED_RAVIOLLI("Fried Raviolli", "Rs 400", 9),
        ... // All other starters...
    
        String name;
        String code;
        int price;
    
        public Starters(String name, String code, String price) {
            this.name = name;
            this.code = code;
            this.price = price;
        }
    
        public String getName() {
            return name;
        }
    
        public String getCode(){
            return code;
        }
    
        public int getPrice(){
            return price;
        }
    }
    

    Then you display your menus like this:

    for (Starters starter : Starters.values()) {
        System.out.println(starter.getName() + " " starter.getCode()); // Just an example, modify to fit your format
    }
    

    You are going to have different types of enums and you want to add objects of the same type to the list. You can use an interface for this and make all enums to implement the interface:

    public interface Orderable {
        public String getName();
        public String getCode();
        public String getPrice();
    }
    

    You can initialize the list like this:

    List<Orderable> orders = new ArrayList<>();
    

    And add elements to the list like this (The enums need to implement the Orderable interface):

    orders.add(Starters.SPRING_ROLLS);
    order.add(Desserts.CHEESE_CAKE);
    ...
    

    When you want to display the bill, you can iterate over the list and display the orders:

    int totalPrice = 0;
    for (Orderable order : orders) {
        System.out.println(order.getName() + " - " order.getPrice());
        totalPrice += order.getPrice();
    }
    System.out.println("Total price: ");