NapoleanBraa NapoleanBraa - 6 months ago 55
Java Question

Not adding the customers total correctly

So when I run this it does not add the customer total all together. It just keeps displaying the last item entered as the customer total.

import java.util.Scanner;

public class dinerBill {

public static void main(String[] args) {


double taxRate = 0, customerTotal=0, discountType = 0, grandTotal= 0;


Scanner in = new Scanner(System.in);
String [] itemName = { "0) Soup", "1) Wing", "2) Burger", "3) Chicken Sandwich", "4) Fries", "5) Pie", "6) Ice Cream", "7) Soft drink", "8) Coffee"};
double [] itemPrice= {2.50 , .15 , 4.95, 5.95, 1.99, 2.95, 2.99, 1.50, 1.00};

System.out.println("Enter the number of people in the party");
int numberOfPeople = in.nextInt();

while (numberOfPeople >0) {

System.out.println("Enter 1 if customer recieves teen or eldery discount ");
System.out.println("Enter 2 if the customer recieves no discount");
System.out.println("Enter 3 if the customer is under 5");
discountType = in.nextInt();

if (discountType == 1) {
discountType = .85;
taxRate = 1;
}
if (discountType ==2) {
discountType = 1;
taxRate = 1.05;
}
if (discountType ==3)
discountType = 0;



System.out.printf("%-24s", "Menu");
System.out.print("Prices" + "\n");
System.out.println("--------------------------------");
for (int i = 0; i < itemName.length; i++) {

System.out.printf("%-24.21s" ,itemName[i]);
System.out.print(itemPrice[i] +"\n");

}
System.out.println("Enter the corresponding number");
for (int choices=3; choices > 0; choices--) {
double choicePrice = 0 , customerTotalBeforeDiscount = 0;

System.out.println("Enter customers item");
int customerItem = in.nextInt();

if (customerItem ==1) {
System.out.println("How many wings ordered?");
int wingsOrdered = in.nextInt();
double priceOfWings = wingsOrdered * itemPrice[1];
choicePrice = priceOfWings;}
else
choicePrice = itemPrice[customerItem];

customerTotalBeforeDiscount +=choicePrice;
double customerTotalBeforeTax = customerTotalBeforeDiscount * discountType;
customerTotal = customerTotalBeforeTax * taxRate;
}

System.out.print("The total for the customer is $" );
System.out.printf("%.2f \n" , customerTotal );

grandTotal += customerTotal;


numberOfPeople--;
}

System.out.print("The total is $");
System.out.printf("%.2f", grandTotal);

in.close();

System.exit(0);
}

}


the results i'm getting are:
Enter the number of people in the party
1
Enter 1 if customer recieves teen or eldery discount
Enter 2 if the customer recieves no discount
Enter 3 if the customer is under 5
2

Menu Prices



0) Soup 2.5
1) Wing 0.15
2) Burger 4.95
3) Chicken Sandwich 5.95
4) Fries 1.99
5) Pie 2.95
6) Ice Cream 2.99
7) Soft drink 1.5
8) Coffee 1.0
Enter the corresponding number
Enter customers item
0
Enter customers item
0
Enter customers item
0
The total for the customer is $2.63
The total is $2.63

Answer Source

Your problem is in

double choicePrice = 0 , customerTotalBeforeDiscount = 0;

because it resets customerTotalBeforeDiscount on each iteration. Instead do:

double customerTotalBeforeDiscount = 0;
for (int choices=3; choices > 0; choices--) {
    double choicePrice = 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download