Results 1 to 18 of 18
Thread: No clue how to do this.
- 02-28-2011, 09:20 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
No clue how to do this.
Write a Java program to help a convenience store clerk decide whether or not to sell beer to a customer. Beer can only be sold to somebody who is 21 years old or more, and has enough money (beer costs $5.00). If the customer is too young, tell them how many years they must wait before returning. If they have too little money, tell them how much more is needed.
I know I need to do nested if else statements. Not asking you guys to do it just a general idea on how to do the math.
- 02-28-2011, 09:29 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
No math is really needed, simply ask him for Age, then use an if to test if they are old enough. You honestly don't need any nested loops either, show me some code and I'll tell you if you are heading in the right direction.
- 02-28-2011, 09:29 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
Help with the math? Basic subtraction? I hope for your sake that you mean "logic," not "math."
Pseudo code:
Java Code:[B]if[/B] customer.age < 21 then [B]if[/B] customer.money >= 5.00 then sell that beer [B]else[/B] tell them they need (5.00 - customer.money) to buy beer [B]end[/B] [B]else[/B] tell them they need to come back in (21 - customer.age) years [B]end[/B]
- 02-28-2011, 09:32 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
yea I mean logic lol. I just started it a few mins ago, this is what I have, the thing I stuck on is the change and how many more years they need
import javax.swing.JOptionPane;
public class Lab6
{
public static void main(String [] args)
{
String age;
String money;
age = JOptionPane.showInputDialog(null, "How old are you?");
money = JOptionPane.showInputDialog(null, "And how much monies you got?");
if age >=21 && money >=5
JOptionPane.showMessageDialog(null, ");
}
}
- 02-28-2011, 09:37 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That's not proper syntax for if statements, it's close though. The pseudo code does a perfect job giving you the answer. Just follow it. Try using System.out.println instead of JOptionPane.
- 02-28-2011, 09:42 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
yea but we get a bit or extra credit if we use Joptionpane for input and out put
- 02-28-2011, 10:00 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Oh I see, well then, you are on the right track, follow the poster aboves pseudo code
You could also do
With this example, if they are too young it shouldn't even bother with anything else, since they are too young. If they are old enough then it will determine whether they have enough money.Java Code:if age < 21 tell customer they are too young end if else if money < 5 tell them how much money they need end if else sell them beer end
- 02-28-2011, 10:01 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
okay this is what I have now, still getting two errors on lines 27 and 34
Java Code:import javax.swing.JOptionPane; public class Lab6 { public static void main(String [] args) { String age; String money; Double change; Double years; age = JOptionPane.showInputDialog(null, "How old are you?"); money = JOptionPane.showInputDialog(null, "And how much monies you got?"); change = (money - 5); years = (age - 21); if (age >= 21) { if (money >= 5.00) { JOptionPane.showMessageDialog(null, "Congratulations, you can have some beer."); JOptionPane.showMessageDialog(null, "Thank you for your patronage."); } else { JOptionPane.showMessageDialog(null, "Sorry, you need $"+ change"more"); JOptionPane.showMessageDialog(null, "Thank you for your patronage."); } } else { JOptionPane.showMessageDialog(null, "No Bear for you!"); JOptionPane.showMessageDialog(null, "Come back in")+ years("years"); JOptionPane.showMessageDialog(null, "Thank you for your patronage."); } } }
- 02-28-2011, 10:09 PM #9
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
I'm eyeballing the line numbers, but surely the errors are that you have a missing '+' after change in this line:
and your string is malformed in this line:Java Code:JOptionPane.showMessageDialog(null, "Sorry, you need $"+ change"more");
But you knew that already, because that's what the compiler told you.Java Code:JOptionPane.showMessageDialog(null, "Come back in"[B])+ years("years")[/B];
- 02-28-2011, 10:12 PM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, you stored age and money as Strings, I don't see you converting them to ints/doubles but you are making conditional tests, and subtracting from them. You need to convert them first.
Check out Integer.parseInt(String)
- 02-28-2011, 10:24 PM #11
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
I think I do have it now, I've run through the 3 out comes a few times for testing and so far so good, thanks guys.
Java Code:import javax.swing.JOptionPane; public class Lab6 { public static void main(String [] args) { String age; String money; Double change; Double years; age = JOptionPane.showInputDialog(null, "How old are you?"); money = JOptionPane.showInputDialog(null, "And how much monies you got?"); Double dAge= Double.parseDouble(age); Double dMoney= Double.parseDouble(money); years = (dAge - 21); change = (dMoney - 5); if (dAge >= 21) { if (dMoney >= 5.00) { JOptionPane.showMessageDialog(null, "Congratulations, you can have some beer."); JOptionPane.showMessageDialog(null, "Thank you for your patronage."); } else { JOptionPane.showMessageDialog(null, "Sorry, you need $"+change+"more"); JOptionPane.showMessageDialog(null, "Thank you for your patronage."); } } else { JOptionPane.showMessageDialog(null, "No Bear for you!"); JOptionPane.showMessageDialog(null, "Come back in" +years+ "years"); JOptionPane.showMessageDialog(null, "Thank you for your patronage."); } } }
- 02-28-2011, 11:02 PM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Glad you solved it. Hope you found people here helpful, mark your thread solved with the thread tools at the top.
- 02-28-2011, 11:18 PM #13
"No BEAR for you!"?
I would hope not! :P
- 02-28-2011, 11:32 PM #14
[nitpick]
Since in each branch you display the "Thank you" message, why not just have a single line at the end of your code instead of repeating it everywhere?
- 02-28-2011, 11:37 PM #15
[also nitpick]
I think a better approach would be to display a single dialog, with multiple lines. Much more user friendly, as the user only has to close one dialog.
Its not tested, but perhaps this would work better:
Java Code:if (dAge >= 21) { if (dMoney >= 5.00){ JOptionPane.showMessageDialog(null, "Congratulations, you can have some beer.\nThank you for your patronage."); } else { JOptionPane.showMessageDialog(null, "Sorry, you need $"+change+" more\nThank you for your patronage."); } } else { JOptionPane.showMessageDialog(null, "No Bear for you!\nCome back in" +years+ "years\nThank you for your patronage."); }
- 03-01-2011, 12:17 AM #16
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
First post, as I am learning how to program, but I was just confused on this. Why don't you just read the values in as an integer and double in the first place?
- 03-01-2011, 12:24 AM #17
Probably because they have only been shown how to use JOptionPane in class. JOptionPane only returns a String value.
- 03-02-2011, 05:11 AM #18
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
yea the people here are great,
I tried it all on one line and I just like pissing the teacher off so I like my way :P[also nitpick]
I think a better approach would be to display a single dialog, with multiple lines. Much more user friendly, as the user only has to close one dialog.
Its not tested, but perhaps this would work better:
Similar Threads
-
I have no clue what this is... Please Help
By 4rch in forum New To JavaReplies: 3Last Post: 12-29-2010, 08:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks