# Thread: No clue how to do this.

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.

2. 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.

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]```

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, ");

}
}

5. 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.

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

7. Oh I see, well then, you are on the right track, follow the poster aboves pseudo code

You could also do
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```
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.

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.");

}
}
}```

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:

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

Java Code:
`JOptionPane.showMessageDialog(null, "Come back in"[B])+ years("years")[/B];`
But you knew that already, because that's what the compiler told you.

10. 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)

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.");

}
}
}```

13. "No BEAR for you!"?
I would hope not! :P

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?

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.");
}```

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?

17. Probably because they have only been shown how to use JOptionPane in class. JOptionPane only returns a String value.

18. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
yea the people here are great,

[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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•