# No clue how to do this.

• 02-28-2011, 10:20 PM
jjth39347
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, 10:29 PM
sunde887
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, 10:29 PM
NRitH
Help with the math? Basic subtraction? I hope for your sake that you mean "logic," not "math."

Pseudo code:

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, 10:32 PM
jjth39347
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, 10:37 PM
sunde887
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, 10:42 PM
jjth39347
yea but we get a bit or extra credit if we use Joptionpane for input and out put
• 02-28-2011, 11:00 PM
sunde887
Oh I see, well then, you are on the right track, follow the poster aboves pseudo code

You could also do
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```
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.
• 02-28-2011, 11:01 PM
jjth39347
okay this is what I have now, still getting two errors on lines 27 and 34

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, 11:09 PM
NRitH
I'm eyeballing the line numbers, but surely the errors are that you have a missing '+' after change in this line:

Code:

`JOptionPane.showMessageDialog(null, "Sorry, you need \$"+ change"more");`
and your string is malformed in this line:

Code:

`JOptionPane.showMessageDialog(null, "Come back in"[B])+ years("years")[/B];`
But you knew that already, because that's what the compiler told you.
• 02-28-2011, 11:12 PM
sunde887
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, 11:24 PM
jjth39347
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.

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.");                         }         } }```
• 03-01-2011, 12:02 AM
sunde887
• 03-01-2011, 12:18 AM
berkeleybross
"No BEAR for you!"?
I would hope not! :P
• 03-01-2011, 12:32 AM
Junky
[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?
• 03-01-2011, 12:37 AM
berkeleybross
[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:

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, 01:17 AM
TangentReq
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, 01:24 AM
Junky
Probably because they have only been shown how to use JOptionPane in class. JOptionPane only returns a String value.
• 03-02-2011, 06:11 AM
jjth39347
yea the people here are great,

Quote:

[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:
I tried it all on one line and I just like pissing the teacher off so I like my way :P