Reputation: 11
I have an assignment and I have to write a program that shows a restaurant menu to the user, the idea of this assignment is using loops in Java.
One of the requirements for the program is that the program should check if the user has ordered something from the food menu before choosing the delivery menu or the summary, and if the user did that, I should give him a message that says "you have to order first" or "you have not ordered yet".
But I can't think of an advanced way to do that because I'm still a student and a beginner programmer and I'm allowed only to use loops, if and switch statements and some other basic methods.
Here is some of what I have done:
do {
System.out.println(" ------------------------------------ ");
System.out.println(" WELCOM ");
System.out.println(" ------------------------------------ ");
System.out.println(" a. Food Ordering ");
System.out.println(" b. Delivery ");
System.out.println(" c. Summary And Exit ");
System.out.print(" Please enter your choice : ");
choice = x.nextLine();
C = choice.charAt(0);
if (C == 'a' || C == 'b' || C == 'c' || C == 'A' || C == 'B' || C == 'C'){
switch (C) {
case 'a':
case 'A':
int qua ;
int CHOICE ;
int choicee = 0 ;
int SUM = 0 ;
do {
System.out.println(" ------------------------------------ ");
System.out.println(" Food Menu ");
System.out.println(" ------------------------------------ ");
System.out.println("1. Chicken Burger 12 SR ");
System.out.println("2. Beef Burger 15 SR ");
System.out.println("3. Mac And Cheese 10 SR ");
System.out.println("4. Cesar Salad 11 SR ");
System.out.println("5. House Salad 10 SR ");
System.out.println("6. Fries 7 SR ");
System.out.println("7. Soft Drink 3 SR ");
System.out.println(" Enter your choice (1 to 7) or -1 to exit : ");
CHOICE = x.nextInt() ;
switch (CHOICE){
case 1 : choicee = 12;break;
case 2 : choicee = 15;break;
case 3 : choicee = 10;break;
case 4 : choicee = 11;break;
case 5 : choicee = 10;break;
case 6 : choicee = 7;break;
case 7 : choicee = 3;break;
}
if ( 1 <= CHOICE && CHOICE <= 7) {
SUM += choicee ;
do{
System.out.print( " Enter quantities : " );
qua = x.nextInt();
}while( qua <= 0 );
SUM *= qua ;
}else if ( CHOICE == -1 ){
System.out.println( SUM );
break ;
}else if ( CHOICE != -1 || 1 <= CHOICE && CHOICE <= 7){
System.out.println(" ********** Wrong Entry.Try Again ********** ");
}
}while(true);
break;
case 'b':
case 'B':
}
The program is not completed yet, this is only part of it.
Upvotes: 1
Views: 243
Reputation: 592
First of all welcome to SO.
To answer your question directly, you can use booleans to track what menu the customer has used, as @Carlos López Marí commented on you question, or @Joakim Danielson's solution.
On top of that, allow me to offer you some recommendations as well, since you mention you're still inexperienced:
A. Use methods to encapsulate the bulk of your print statements, i.e. a method to print the "Welcome" and first batch of messages, an other one to print the menu items, etc. The same goes for any piece of logic that can be on its own or repeatable i.e. the logic after selecting A, B or C and then for the sum Imagine having the main method read out like the steps that the customer will take. This will make your code easier to read and find errors if there any. For example:
...
do{
printWelcomeMessage();
choice = x.nextLine().toLowercase();
C = choice.charAt(0);
if(C == 'a' || C == 'b' || C == 'c'){
chooseFoodItems();
}
}...
B. Use a collection for the price catalog. If you can use a HashMap to pair items with prices, it would made your printing and cost assigning look better and easier to calculate. If you can't use a HashMap, you can instead use 2 tables, one with names and one with prices.
C. You can change youre x.nextLine();
to x.nextLine().toUppercase()
(or lower case) so that you can simplify your switch statements.
D. While not that important, it's convention when comparing numbers to do so 0 < CHOICE && CHOICE < 8
instead of 1 <= CHOICE && CHOICE <= 7
( for example in a for loop we usually do it as for(int i=0; i < limit; i++)
E. Try and simplify your code's logic as much as you can. It'll be easier to write it, read through it and work with it in general.
Upvotes: 1
Reputation: 51993
Change the bottom of your while loop to
} else if ( CHOICE == -1 ){
if (SUM > 0) {
System.out.println( SUM );
break ;
} else {
System.out.println( "You have to order first" );
}
} else if ( CHOICE != -1 || 1 <= CHOICE && CHOICE <= 7){
System.out.println(" ********** Wrong Entry.Try Again ********** ");
}
} while(true);
This can certainly be improved further but it should be good enough to help you move forward
Upvotes: 0