cant get the final sum

Thread Solved

Join Date: Feb 2008
Posts: 5
Reputation: ndrux is an unknown quantity at this point 
Solved Threads: 0
ndrux ndrux is offline Offline
Newbie Poster

cant get the final sum

 
0
  #1
Feb 9th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 55
Reputation: WhYuLoOkIn is an unknown quantity at this point 
Solved Threads: 3
WhYuLoOkIn's Avatar
WhYuLoOkIn WhYuLoOkIn is offline Offline
Junior Poster in Training

Re: cant get the final sum

 
0
  #2
Feb 10th, 2008
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:
  1. while(choice != 0)
  2. {
  3. sum = sum + price;
  4. choice = choice - 1; // or choice--;
  5. }

Originally Posted by ndrux View Post
while(cho!=0);
sum = sum + price;
Doing what they say couldn't be done!!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: ndrux is an unknown quantity at this point 
Solved Threads: 0
ndrux ndrux is offline Offline
Newbie Poster

Re: cant get the final sum

 
0
  #3
Feb 10th, 2008
Originally Posted by WhYuLoOkIn View Post
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:
  1. while(choice != 0)
  2. {
  3. sum = sum + price;
  4. choice = choice - 1; // or choice--;
  5. }
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 55
Reputation: WhYuLoOkIn is an unknown quantity at this point 
Solved Threads: 3
WhYuLoOkIn's Avatar
WhYuLoOkIn WhYuLoOkIn is offline Offline
Junior Poster in Training

Re: cant get the final sum

 
0
  #4
Feb 10th, 2008
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
  1. 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.");
}
Last edited by WhYuLoOkIn; Feb 10th, 2008 at 8:16 am.
Doing what they say couldn't be done!!!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: striker3344 is an unknown quantity at this point 
Solved Threads: 2
striker3344 striker3344 is offline Offline
Newbie Poster

Re: cant get the final sum

 
0
  #5
Feb 11th, 2008
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.
Last edited by striker3344; Feb 11th, 2008 at 12:09 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: ndrux is an unknown quantity at this point 
Solved Threads: 0
ndrux ndrux is offline Offline
Newbie Poster

Re: cant get the final sum

 
0
  #6
Feb 11th, 2008
Originally Posted by striker3344 View Post
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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: striker3344 is an unknown quantity at this point 
Solved Threads: 2
striker3344 striker3344 is offline Offline
Newbie Poster

Re: cant get the final sum

 
0
  #7
Feb 11th, 2008
edited the post
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: ndrux is an unknown quantity at this point 
Solved Threads: 0
ndrux ndrux is offline Offline
Newbie Poster

Re: cant get the final sum

 
0
  #8
Feb 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: striker3344 is an unknown quantity at this point 
Solved Threads: 2
striker3344 striker3344 is offline Offline
Newbie Poster

Re: cant get the final sum

 
0
  #9
Feb 11th, 2008
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.
Last edited by striker3344; Feb 11th, 2008 at 12:31 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: ndrux is an unknown quantity at this point 
Solved Threads: 0
ndrux ndrux is offline Offline
Newbie Poster

Re: cant get the final sum

 
0
  #10
Feb 11th, 2008
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;
Last edited by ndrux; Feb 11th, 2008 at 1:05 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1142 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC