I am new to java this is my programming Assignment , I will be grateful if anyone can show me or fixed my code that i placed below to produce the following output as shown here. We are only allowed to use control structures such as for loop, if else, if, do-while , while and switch statements for this program. We are not allowed to use methods.The problem i have with my code is that after the user enter its desired drinks and food from the food and drinks menu respectively, it return to the order menu but i want it to be able to return to the main menu when the user press the no '3' from the order menu, currently when the user press '3' the order menu just reappear!!
The following shows example output for this program that i want to achieved:
SJ Restaurant Ordering System
Main Menu
(1) Place Order
(2) Payment
(3) Quit
Enter your choice: 1
Order Menu
(1) Drinks
(2) Food
(3) Back to Main Menu
Enter your choice: 2
Food Menu
(1) Nasi Lemak RM5.00
(2) Roti Canai RM1.50
(3) Fried Noodle RM3.50
(4) Back to Order Menu
Enter order: 1
Enter quantity: 2
Food Menu
(1) Nasi Lemak RM5.00
(2) Roti Canai RM1.50
(3) Fried Noodle RM3.50
(4) Back to Order Menu
Enter order: 4
Order Menu
(1) Drinks
(2) Food
(3) Back to Main Menu
Enter your choice: 3
Main Menu
(1) Place Order
(2) Payment
(3) Quit
Enter your choice: 2
Order Quantity Unit Price Total
Nasi Lemak 2 5.00 10.00
Overall Total: 10.00
Payment: 50.00
Balance: 40.00
The current output of my code:
--------------------------------------------
SJ Restaurant Ordering System
--------------------------------------------
Main Menu
--------------
(1) Place Order
(2) Payment
(3) Quit
Enter your choice:1
Order Menu
------------
(1) Drinks
(2) Food
(3) Back to Main Menu)
Enter your choice:
1
Drinks Menu
--------------
(1)Milo + + RM5.00
(2) Coffee + RM1.50
(3) Tea+ RM3.50
(4) Back to Order Menu
Enter order:
1
Enter quantity:
2
Order Menu
------------
(1) Drinks
(2) Food
(3) Back to Main Menu)
Enter your choice:
2
Food Menu
--------------
(1)Burger + + RM5.00
(2) Pizza + RM1.50
(3) Nasilemak+ RM3.50
(4) Back to Order Menu
Enter order:
1
Enter quantity:
3
Order Menu
------------
(1) Drinks
(2) Food
(3) Back to Main Menu)
Enter your choice:
3
Order Menu
------------
(1) Drinks
(2) Food
(3) Back to Main Menu)
Enter your choice:
The problem as it can be seen here when the user press '3' it should return to the main menu but it does not
package assignment1; import java.util.Scanner; public class Assignment1Question2 { public static void main(String[] args) { Scanner read = new Scanner(System.in); int mainmenuchoice; int ordermenu; double quantity=0; double quantity2=0; int drinksorder=0; int foodorder=0; int order3=0; double subtotal=0; double subtotal2=0; double milo=5.50; double coffee=1.50; double tea=3.50; double burger=5.00; double pizza=1.50; doublenasilemak=3.50; double discount; double price; double discount2; double totalfinal; String output; String output2; double overalltotal; double total=0; double sum; double sum2; double total2=0; System.out.println("--------------------------------------------"); System.out.println("SJ Restaurant Ordering System"); System.out.println("--------------------------------------------"); System.out.println("Main Menu"); System.out.println("--------------"); System.out.println("(1) Place Order"); System.out.println("(2) Payment"); System.out.println("(3) Quit"); System.out.print("Enter your choice:"); choice=read.nextInt(); while(mainmenuchoice!=3){ if(mainmenuchoice==1) { System.out.println("Order Menu"); System.out.println("------------"); System.out.println("(1) Drinks"); System.out.println("(2) Food"); System.out.println("(3) Back to Main Menu)"); System.out.println("Enter your choice:"); ordermenu=read.nextInt(); if(ordermenu==1) { System.out.println("Drinks Menu"); System.out.println("--------------"); System.out.println("(1)Milo + \t\t + RM5.00"); System.out.println("(2) Coffee + \t\t RM1.50"); System.out.println("(3) Tea+ \t\t RM3.50"); System.out.println("(4) Back to Order Menu"); System.out.println("Enter order:"); drinksorder=read.nextInt(); switch (drinksorder) { case 1: System.out.println("Enter quantity:"); quantity=read.nextDouble(); subtotal=milo; total=(subtotal * quantity); sum=total; output="milo" +1; break; case 2: System.out.println("Enter quantity"); quantity=read.nextDouble(); subtotal=coffee; total=(subtotal * quantity); sum=total; output="coffee"+1; break; case 3: System.out.println("Enter quantity"); quantity=read.nextDouble(); subtotal=tea; total=(subtotal * quantity); sum=total; output="tea" +1; return; default: break; } }else if(ordermenu==2) { System.out.println("Food Menu"); System.out.println("--------------"); System.out.println("(1)Burger + \t\t + RM5.00"); System.out.println("(2) Pizza + \t\t RM1.50"); System.out.println("(3) Nasilemak+ \t\t RM3.50"); System.out.println("(4) Back to Order Menu"); System.out.println("Enter order:"); foodorder=read.nextInt(); while(foodorder!=4){ if (foodorder==1){ System.out.println("Enter quantity:"); quantity2=read.nextDouble(); subtotal2=burger; total2=(subtotal2*quantity2); sum2=total2; output2="burger"+1; break; }else if(foodorder2==2){ System.out.println("Enter quantity:"); quantity2=read.nextDouble(); subtotal2=pizza; total2=(subtotal2*quantity2); sum2=total2; output2="pizza"+1; break; }else if(foodorder==3){ System.out.println("Enter quantity:"); quantity2=read.nextDouble(); subtotal2=nasilemak; total2=(subtotal2*quantity2); sum2=total2; output2="nasilemak"+1; break; }else if(foodorder==4){ System.out.println("Food Menu"); System.out.println("--------------"); System.out.println("(1)Burger + \t\t + RM5.00"); System.out.println("(2) Pizza + \t\t RM1.50"); System.out.println("(3) Nasilemak+ \t\t RM3.50"); System.out.println("(4) Back to Order Menu"); System.out.println("Enter order:"); order3=read.nextInt(); break; }else if(foodorder!=0 || foodorder>4){ System.out.println("please enter a valid choice!!!"); break; } } }else if(mainmenuchoice==3){ System.out.println("Goodbye!!!"); } else if (mainmenuchoice==2) { System.out.println("Order \t\t Quantity\t\t Unit\t\t Price\t\t Total\t\t"); System.out.println("-----------------------------------------------"); System.out.println(drinksorder); System.out.println(quantity); System.out.println(subtotal); System.out.println(total); System.out.println(foodorder); System.out.println(quantity2); System.out.println(subtotal2); System.out.println(total2); overalltotal=total + total2; System.out.printf("Overall total is RM ",overalltotal); System.out.println(" Any Discount: "); discount=read.nextDouble(); discount2=discount/100; price=overalltotal-(discount2*overalltotal); System.out.println("Payment: "); double payment=read.nextDouble(); double balance = payment-price; System.out.println("Balance:" + balance); } } } }