William Landrum William Landrum - 17 days ago 6
Java Question

I am writing a program for a class that involves inputting and keeping track of totals and sub totals for each individual person in a restaurant

package restaurantMenu;

import java.util.Scanner;

public class RestaurantMenu {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String[] itemName = { " ", "Soup", "Wings", "Burger", "Chicken Sandwich", "Fries", "Pie", "Ice Cream",
"Soft Drink", "Coffee" };
double[] itemPrice = { 0, 2.50, .15, 4.95, 5.95, 1.99, 2.95, 2.99, 1.50, 1.00 };

double grandTotal = 0;
double total = 0;

System.out.println("How many people in the party? ");
int partyNumber = input.nextInt();

for (int i = 0; i < partyNumber; i++) {
System.out.println("Discount type:");
System.out.println("--------------");
System.out.println("1: Child");
System.out.println("2. Teen");
System.out.println("3: Senior");
System.out.println("4. None of the above");
int discount = input.nextInt();

for (int k = 0; k < itemName.length; k++) {
System.out.println(itemName[k] + " ");
}

for (int j = 1; j <= 3; j++) {

System.out.println("Please select menu item: ");
int itemNum = input.nextInt();

if (itemNum == 2) {
System.out.println("How many do you want? ");
int amount = input.nextInt();
itemPrice[2] = itemPrice[2] * amount;
}

total = total + itemPrice[itemNum];

}

System.out.println(total);

if (discount == 1)
total = total * 0;
else if (discount == 2 || discount == 3)
total = total * .75;
else
total = total + (total * .05);

grandTotal = total;

}
System.out.println(grandTotal);
}
}


So, what I'm thinking is the total is not resetting upon jumping out of the second for loop, but I don't necessarily know if I need it to. What I'm wondering is why can't I get the total to track properly, output the
grandTotal
, and discount everything properly for multiple people. If anyone needs the input data that was provided to me from the teacher let me know.

Answer

At the end of the englobing for loop, reset total, and grandTotal must add total.

grandTotal += total; total = 0;

Comments