• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help For Assignment (Problem with Loop Control Structure)  RSS feed

 
calvin choi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

TASK 2 : A Simple Restaurant Ordering Application Write a program that simulates a simple food ordering system. The program should start with displaying a menu with the following options:

Main Menu
(1) Place Order
(2) Payment
(3) Quit

If the user chooses Place Order option, the program should display a sub-menu to choose either to order drinks or food. The sub-menu may look like follows:

Order Menu
(1) Drinks
(2) Food
(3) Back to Main Menu

Each option (Drinks or Food) should display several options of food/drinks together with their prices. The following shown an example food menu. You may list your own food and prices in your program.

Food Menu
(1) Nasi Lemak RM5.00
(2) Roti Canai RM1.50
(3) Fried Noodle RM3.50
(4) Back to Order Menu

The user should be allowed to order as many food/drink as he/she wants. For each order, the user need to enter the quantity needed.

If the user chooses Payment option, the program should display the total price need to be paid.

Then prompt the user whether there is a discount given. If there is a discount, read the discount rate in percentage (e.g. enter 5 for 5% discount) and deduct the discount amount from the total price.

Continue to read an amount paid by the user, and display the balance need to be paid back to the user. After the payment is done, allow user to go back to the main menu so that another order can be made.
Remember to re-initialize all variables after payment is done before a new order is made.

The following shows example output for this program:
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


package assignment1;

import java.util.Scanner;

public class Assignment1Question2 {

public static void main(String[] args)

{

Scanner read = new Scanner(System.in);

int choice;

int choice2;

double quantity=0;

double quantity2=0;

int order=0;

int order2=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(choice!=3){

if(choice==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:");
choice2=read.nextInt();
if(choice2==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:");
order=read.nextInt();
switch (order) {
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(choice2==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:");
order2=read.nextInt();

while(order!=4){
if (order2==1){
System.out.println("Enter quantity:");
quantity2=read.nextDouble();
subtotal2=burger;
total2=(subtotal2*quantity2);
sum2=total2;
output2="burger"+1;
break;

}else
if(order2==2){
System.out.println("Enter quantity:");
quantity2=read.nextDouble();
subtotal2=pizza;
total2=(subtotal2*quantity2);
sum2=total2;
output2="pizza"+1;
break;

}else
if(order==3){
System.out.println("Enter quantity:");
quantity2=read.nextDouble();
subtotal2=nasilemak;
total2=(subtotal2*quantity2);
sum2=total2;
output2="nasilemak"+1;
break;

}else
if(order==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(order!=0 || order>4){
System.out.println("please enter a valid choice!!!");
break;
}
}

}else
if(choice==3){
System.out.println("Goodbye!!!");

} else
if (choice==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(order);
System.out.println(quantity);
System.out.println(subtotal);
System.out.println(total);

System.out.println(order2);
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);

}
if(choice==3)
{
System.out.println("Goobye!!");
}
}
}
}
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
calvin choi wrote:. . . . We are not allowed to use methods. . . . .
Welcome to the Ranch

You cannot write a program without using at least one method. It does annoy us to see assignments which don't test good programming, but rather bad programming. But, until you find somebody who will test good programming, you are stuck with this, so please go back to the code, add code tags as described here, get rid of the excess blank lines, and then tell us what you think the program shou‍ld do and what seems to be going wrong. As long as we aren't actually doing the assignment, the more details you provide, the more help we can give.
 
calvin choi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
calvin choi wrote: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 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.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you really got all that code up against the left margin like that? Have you not been taught to indent your code? You cannot find errors in your code if you don't indent it. I suggest you read our suggestions about code formatting. Also use a decent text editor (if you use Windows®, I recommend NotePad++) and set up the following options:-
  • Automatic indentation
  • Automatic conversion of 1 tab → 4 spaces
  • Syntax colouring; you may be able to set options for colours different from the defualts.
  • Bracket matching
  • All those features will help you write the code so you can read it, and help you find potential errors. Also remove the blank lines between the successive variables, because they don't help legibility.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!