0% found this document useful (0 votes)
37 views

Scanner Menu: "Welcome /n1. Burger ($2.00) /n2. Fries ($1.50) /n3. Soda ($1.00) /n4. Done"

The document describes a Java program for a restaurant menu ordering system. It defines classes and methods for displaying a menu, getting an item and quantity selected, calculating subtotals and a running total. The main method contains a loop that runs the menu, gets selections, and calculates the running total, which is printed at the end along with an order completion message.

Uploaded by

Erwin Bajet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Scanner Menu: "Welcome /n1. Burger ($2.00) /n2. Fries ($1.50) /n3. Soda ($1.00) /n4. Done"

The document describes a Java program for a restaurant menu ordering system. It defines classes and methods for displaying a menu, getting an item and quantity selected, calculating subtotals and a running total. The main method contains a loop that runs the menu, gets selections, and calculates the running total, which is printed at the end along with an order completion message.

Uploaded by

Erwin Bajet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

Scanner;

public class Menu {


public double subTotal;
public static double runningTotal;
private static double itemPrice;
static boolean ordering = true;
static Scanner input = new Scanner(System.in);

public static void menu() {


System.out.println("Welcome \n1. Burger ($2.00) \n2. Fries ($1.50)\n3. Soda
($1.00) \n4. Done");
}

public static double itemPrice(int foodItem) {


if (foodItem == 1) {
// burger= $2.00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
// fries = $1.50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
// soda = $1.00
System.out.println("You've ordered a soda");
itemPrice = 1.00;
}
quantity();
return itemPrice;
}

public static double quantity() {


System.out.println("Enter quantity");
double quantity = input.nextDouble();
subTotal(quantity, itemPrice);
return quantity;
}

public static double subTotal(double quantity, double itemPrice) {


double subTotal = quantity * itemPrice;
System.out.println("Subtotal: " + subTotal);
return subTotal;
}

public static void done(double runningTotal) {


ordering = false;
System.out.println(runningTotal);
System.out.println("Enjoy your meal");
}
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
do {
double runningTotal = 0;
menu();
menuOption = input.nextInt();
switch (menuOption) {
case 1:
foodItem = 1;
itemPrice(foodItem);
break;
case 2:
foodItem = 2;
itemPrice(foodItem);
break;
case 3:
foodItem = 3;
itemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
} while (ordering);
{
subTotal(quantity(), itemPrice(foodItem));
runningTotal = runningTotal + subTotal(quantity(), itemPrice(foodItem));
}
}
}
share improve this question

askedOct 22 '15 at 3:15

Michael Romero Jr.


1●11 gold badge●11 silver badge●11 bronze badge

editedOct 22 '15 at 4:23


sparkhee93
1,296●33 gold badges●1717 silver badges●2525 bronze badges

3 Answers

order by active oldest votes

up vote1down vote

You are resetting the double runningTotal=0; in the while loop. Also the price returned by
the itemPrice needs to be added into the runningTotalvariable;
This is how your main method shoul
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
double runningTotal=0;
do{
menu();
menuOption = input.nextInt();
switch(menuOption){
case 1:
foodItem = 1;
runningTotal += itemPrice(foodItem);
break;
case 2:
foodItem = 2;
runningTotal += itemPrice(foodItem);
break;
case 3:
foodItem = 3;
runningTotal += itemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
} while(ordering);
System.out.println("Total amount: " + runningTotal);
}
Output:
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
1
You've ordered a burger
Enter quantity
2
Subtotal: 4.0
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
2
You've ordered fries
Enter quantity
1
Subtotal: 1.5
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
4
3.5
Enjoy your meal
Total amount: 3.5
shar

You might also like