import javax.swing.JOptionPane;
public class cafe2
{
public static void main(String args[])
{
String name,ch;
double price = 0.00;
double sum=0.00;
int cho=0;

name=JOptionPane.showInputDialog("Enter
Customer Name:");

do{
ch=JOptionPane.showInputDialog(
" Food Name "+
"\n-----------------------------------------------------"+
"\n1) French Fries $ 3.50"+
"\n2) Fish and Chips $ 5.50"+
"\n3) Goat Cheese Pizza $20.70"+
"\n4) Chicken Salad $ 8.90"+
"\n5) Spaghetti $ 9.00"+
"\n6) Chicken Chop $ 9.50"+
"\n7) Lamb Chop $10.50"+
"\n\n Beverages "+
"\n-----------------------------------------------------"+
"\n8) Ice Blended Mocha $ 5.60"+
"\n9) Cafe Classic Martini $13.00"+
"\n0) EXIT"+
"\n\n PLEASE Enter Item No. :");
cho=Integer.parseInt (ch);

switch (cho) {
case 1: price= 3.50;
break;
case 2: price= 5.50;
break;
case 3: price= 20.70;
break;
case 4: price= 8.90;
break;
case 5: price= 9.00;
break;
case 6: price= 9.50;
break;
case 7: price= 10.50;
break;
case 8: price= 5.60;
break;
case 9: price= 10.50; break;
default: System.out.println("Invalid
choice.");
}
}

while(cho!=0);
sum = sum + price;

JOptionPane.showMessageDialog(null,
"RECEIPT"+
"\n*********"+
"\nCustomer Name: "+name+
"\n\nTotal Price: $"+sum);


System.exit(0);
}
}

//*** PROBLEM IS I CANT GET THE TOTAL IN
//THE END

WELLLL, one of your major problems in your code is that you have an infinite loop.
Translation --- Your telling your code to add price to your sum until cho equals zero, which it never does because you never update your condition.

The correct way to use a loop:

while(choice != 0)
{ 
     sum = sum + price;
     choice = choice - 1;    // or choice--;
}

while(cho!=0);
sum = sum + price;

WELLLL, one of your major problems in your code is that you have an infinite loop.
Translation --- Your telling your code to add price to your sum until cho equals zero, which it never does because you never update your condition.

The correct way to use a loop:

while(choice != 0)
{ 
     sum = sum + price;
     choice = choice - 1;    // or choice--;
}

thanks for the reply, but i've tried altering it to

while(cho != 0);
{
sum = sum + price;
cho = cho --; // or choice--;
}

JOptionPane.showMessageDialog(null,
"RECEIPT"+
"\n*********"+
"\nCustomer Name: "+name+
"\n\nTotal Price: RM"+sum);

and

while(cho != 0);
{
sum = sum + price;
cho = cho --; // or choice--;


JOptionPane.showMessageDialog(null,
"RECEIPT"+
"\n*********"+
"\nCustomer Name: "+name+
"\n\nTotal Price: RM"+sum);
}

and i still get the same thing...wierd..have been trying to figure it out for almost a week tho

Correct me if im wrong, but for what your trying to do I don't think you need a while loop.
You want to add up the total amount of money that was choosen from each menu choice.
All you need is to do is check one condition, is choice = 0. If not then do this, if yes then continue.

Another thing when your writing code you want to try and make your code readable and effiecent. You have a couple of options here, add that statement that's in the while loop in your switch body after each case, or use if-else statments which would probably be more elegent. I think you need to redesign your program, using the most effiecent coding style. One more thing I will point out.

On your menu options you have a choice of 0 which exits the program but in your switch body you dont take that into account, so when your program runs your always going to get the message

default: System.out.println("Invalidchoice.");

, everytime a user choose to exit the program. You dont need choice = choice-- , the two dashes after a variable (choice--) is equivalent to choice = choice - 1, so use one or the other.

switch (cho) {
case 1: price= 3.50;
break;
case 2: price= 5.50;
break;
case 3: price= 20.70;
break;
case 4: price= 8.90;
break;
case 5: price= 9.00;
break;
case 6: price= 9.50;
break;
case 7: price= 10.50;
break;
case 8: price= 5.60;
break;
case 9: price= 10.50; break;
default: System.out.println("Invalid
choice.");
}

if you still are using the code you posted at the first post, then the problem can be solved by simply moving a line of your code, and adding one more.
the line where you do sum should be moved, and u should reset the value of price.

if you still are using the code you posted at the first post, then the problem can be solved by simply moving a line of your code.
the line where you do sum.

i still have the code...erm,move it where? i moved it after the switch and after the while..but no change... an example would help..

edited the post:)

hi guys..thanks alot for the help..at last i got if figured out..might look messy,but it works... yup..its the sum line. here's my new line:

case 1: price= 3.50;
sum = sum + price;
break;
case 2: price= 5.50;
sum = sum + price;
break;
case 3: price= 20.70;
sum = sum + price;
break;
case 4: price= 8.90;
sum = sum + price;
break;
case 5: price= 9.00;
sum = sum + price;
break;


now to figure out how to get 2 decimal pts :)

let's test it, before you move further
try inputing this sequence:
1
1
1
2
5
7
9
you should get total sum = 48.5
also, one thing just pointed out. In case 9, price should be 13.
try it and then move.

thanks for the correction... typo mistake there.. yup i got 48.5 as my output

i decided to remove my default message, assuming the user knows wht they are doing.

case 9: price= 13.00;
sum = sum + price;
break;
case 0:
JOptionPane.showMessageDialog(null,"Thank You");
break;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.