Results 1 to 18 of 18
  1. #1
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default 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. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    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. #4
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    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. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #6
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    yea but we get a bit or extra credit if we use Joptionpane for input and out put

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #8
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    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. #9
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    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. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #11
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

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

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad you solved it. Hope you found people here helpful, mark your thread solved with the thread tools at the top.

  13. #13
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

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

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    [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. #15
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    [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. #16
    TangentReq is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    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. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

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

  18. #18
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    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

Similar Threads

  1. I have no clue what this is... Please Help
    By 4rch in forum New To Java
    Replies: 3
    Last Post: 12-29-2010, 08:34 AM

Posting Permissions

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