Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Decision Making Exercise

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Airlie Beach, Queensland, AUS.
    Posts
    6
    My Mood
    Happy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Decision Making Exercise

    Ok so I have been at this for 2 days now and even though I know I could probably find the answer online somewhere and copy/paste that won't help me learn it. So, if someone could help me figure out whats wrong and maybe explain it like I'm five, that would be awesome and extremely helpful.
    P.S were are not up to loops or arrays yet.

    Write an application that displays a menu of three items for the Jivin’ Java Coffee
    Shop as follows:
    (1) American 1.99
    (2) Espresso 2.50
    (3) Latte 2.15
    Prompt the user to choose an item using the number (1, 2, or 3) that corresponds to
    the item, or to enter 0 to quit the application. After the user makes the first selection,
    if the choice is 0, display a total bill of $0. Otherwise, display the menu again. The user
    should respond to this prompt with another item number to order or 0 to quit. If the
    user types 0, display the cost of the single requested item. If the user types 1, 2, or 3,
    add the cost of the second item to the first, and then display the menu a third time. If
    the user types 0 to quit, display the total cost of the two items; otherwise, display the
    total for all three selections.
    import java.util.Scanner;
     
     
        public class Coffee
        {
        	//private static double orderStop;
        	private static int item;
        	private static double price;
        	private static Scanner keyboard;
    		private static int menuNo;
     
        	public static void main(String[] args) {
        		// TODO Auto-generated method stub
     
     
        		price = price += service();
     
     
        		if (menuNo >0 || menuNo < 4)
        		{ 
        			price = service();
        		}
        		else
        			if (menuNo == 0)
        		{
        			System.out.println("Number of purchases " + item + "."
        					+ "\nYour total is $" + price + "." );
        		}
        			else
        			{
            			System.out.println("Sorry. An error has occured with your order" );
            		}
        	}
        		public static double service()
        		{
        			//int menuNo;
     
        				System.out.println("Jivin’ Java CoffeeShop");
        				System.out.println("MENU");
        				System.out.println("(1) American $1.99");
        				System.out.println("(2) Espresso $2.50");
        				System.out.println("(3) Latte $2.15");
     
        				keyboard = new Scanner(System.in);
     
        				System.out.println("Please place your order. "
        						+ "\nEnter \n1 for American, "
        						+ "\n2 for Espresso, "
        						+ "\n3 for Latte "
        						+ "\nor "
        						+ "\n0 to complete and total your order.");
     
        				menuNo = keyboard.nextInt();
     
     
        				if(menuNo == 1)
        				{
        					//orderStop = 0;
        					item += 1;
        					price +=1.99;
        					return 1.99;
        				}
        				else
        					if(menuNo == 2)
        					{
        						//orderStop = 0;
        						item += 1;
        						price += 2.50;
        						return 2.50;
        					}
        				else
        					if(menuNo == 3)
        					{
        						//orderStop = 0;
        						item += 1;
        						price += 2.15;
        						return 2.15;
        					}
        				else
        					//orderStop += 1;
        					return 0;
     
        	}
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Decision Making Exercise

    Did you have any specific questions or problems with the program you are trying to write?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Wooth (January 26th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Airlie Beach, Queensland, AUS.
    Posts
    6
    My Mood
    Happy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Decision Making Exercise

    I figure it's somewhere in this part of the code. I would like it to allow re ordering until '0' is pressed but I don't know if I can make that happen without looping. If I can't make that happen do I need to add another variable(s) to enable it to allow reordering to a set number of times?
    price = price += service();
     
     
        		if (menuNo >0 || menuNo < 4)
        		{ 
        			price = service();
        		}
        		else
        			if (menuNo == 0)
        		{
        			System.out.println("Number of purchases " + item + "."
        					+ "\nYour total is $" + price + "." );
        		}
        			else
        			{
            			System.out.println("Sorry. An error has occurred with your order" );
            		}
    Last edited by Wooth; January 26th, 2014 at 02:24 PM. Reason: Fixed highlight tags.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Decision Making Exercise

    I would like it to allow re ordering
    To "redo" steps in a program requires looping. A few redos can be done by having nested if statements, one nested if for each redo. For example 2 nested ifs would allow 2 redos, but not 3.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Wooth (January 26th, 2014)

  7. #5
    Junior Member
    Join Date
    Jan 2014
    Location
    Airlie Beach, Queensland, AUS.
    Posts
    6
    My Mood
    Happy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Decision Making Exercise

    I think I have it. Could you just give it a glance to double check please.
    import java.util.Scanner;
     
        public class Coffee
        {
        	//fields
        	private static int item;
        	private static double price;
        	private static int menuNo;
        	private static Scanner keyboard;
     
     
        	public static void main(String[] args) {
     
        		// 3 nested if's allows for 3 additional orders
        		service();
     
        		if (menuNo > 0 && price > 1.95)
        			service();
     
        		if (menuNo > 0 && price > 3.95)
        			service();
     
        		if (menuNo > 0 && price > 5.95)
        			service();
     
    			display();
     
        	} //end main
     
        	public static void display()
    		{
        		//rounded to fix a strange addition problem
        		price = price * 100;
    			long roundedPrice = Math.round(price);
    			price = roundedPrice / 100.0;
     
    			//prints total purchases and price
        		System.out.println("Number of purchases " + item + "."
        					+ "\nYour total is $" + price + "." );
    		} //end display
     
        		public static double service()
        		{
        			// displays menu 
        				System.out.println("Jivin’ Java CoffeeShop");
        				System.out.println("MENU");
        				System.out.println("(1) American $1.99");
        				System.out.println("(2) Espresso $2.50");
        				System.out.println("(3) Latte $2.15");
     
        			//displays choices and asks for input	
        				keyboard = new Scanner(System.in);
     
        				System.out.println("Please place your order. "
        						+ "\nEnter \n1 for American, "
        						+ "\n2 for Espresso, "
        						+ "\n3 for Latte "
        						+ "\nor "
        						+ "\n0 to complete and total your order.");
     
        				menuNo = keyboard.nextInt();
     
        			// determines what happens with given selection
        				if(menuNo == 1)
        				{
        					item += 1;
        					price +=1.99;
        					return 1.99;
        				}
        				else
        					if(menuNo == 2)
        					{
        						item += 1;
        						price += 2.50;
        						return 2.50;
        					}
        				else
        					if(menuNo == 3)
        					{
        						item += 1;
        						price += 2.15;
        						return 2.15;
        					}
        				else
        					return 0;
     
        	} //end service
        } //end class

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Decision Making Exercise

    Does it compile, execute and give you the expected results?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Jan 2014
    Location
    Airlie Beach, Queensland, AUS.
    Posts
    6
    My Mood
    Happy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Decision Making Exercise

    Yes sir. It even stops and gives the price when '0' is entered and I got rid of 1 'if' so as to only allow the 3 orders asked for.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Decision Making Exercise

    One loose end I see is that the service() method returns a value that is not saved in a variable and used for anything.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Wooth (January 26th, 2014)

  12. #9
    Junior Member
    Join Date
    Jan 2014
    Location
    Airlie Beach, Queensland, AUS.
    Posts
    6
    My Mood
    Happy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Decision Making Exercise

    Thanks I missed that from when I was trying out different things
    got rid of returns and changed to void
    public static void service()
        		{
        			// displays menu 
        				System.out.println("Jivin’ Java CoffeeShop");
        				System.out.println("MENU");
        				System.out.println("(1) American $1.99");
        				System.out.println("(2) Espresso $2.50");
        				System.out.println("(3) Latte $2.15");
     
        			//displays choices and asks for input	
        				keyboard = new Scanner(System.in);
     
        				System.out.println("Please place your order. "
        						+ "\nEnter \n1 for American, "
        						+ "\n2 for Espresso, "
        						+ "\n3 for Latte "
        						+ "\nor "
        						+ "\n0 to complete and total your order.");
     
        				menuNo = keyboard.nextInt();
     
        			// determines what happens with given selection
        				if(menuNo == 1)
        				{
        					item += 1;
        					price +=1.99;
     
        				}
        				else
        					if(menuNo == 2)
        					{
        						item += 1;
        						price += 2.50;
     
        					}
        				else
        					if(menuNo == 3)
        					{
        						item += 1;
        						price += 2.15;
     
        					}
     
     
        	} //end service

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Alternate Decision Process Needed
    By brooksr13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 12th, 2013, 04:27 AM
  4. Help with java decision structure program
    By kingsnans in forum Object Oriented Programming
    Replies: 4
    Last Post: November 5th, 2011, 10:54 AM
  5. Ordered Binary Decision Tree
    By nilay in forum Member Introductions
    Replies: 1
    Last Post: October 7th, 2011, 06:12 AM

Tags for this Thread