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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: Ugent pls help

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ugent pls help

    The owner of the ‘Lunch Café’ aims to sell daily a reasonably-priced three-course lunch to the local student community. To save costs, each day there is only a single choice of starter, main course and dessert. As with most establishments of its kind, the largest profit comes from selling the main courses. Unfortunately, the local students being frugal, hard-working and cost-conscious tend to only order a starter and/or a dessert.
    As an incentive to order main courses, the owner has decided – where customers order a main course – to provide, free of charge:
    • a soft drink if they also order a starter;
    • coffee if they also order a dessert;
    • both a soft drink and a coffee if all 3 courses are ordered.
    If customers do not order a main course or if they only order a main course, then there are no drinks provided.
    You are to write a program that advises staff what to provide. The program should ask what course(s) the customer is ordering and output what is to be provided. The choices are:
    • starter at RM3
    • main course at RM6
    • dessert at RM4
    • soft drink at RM1.5 (free only if both a starter & a main course have been ordered)
    • coffee at RM2 (free only of both a dessert & a main course have been ordered)






    Menu:
    Do you want to order starter? (true/false): true
    No of starter: 1

    Do you want to order main course? (true/false): true
    No of main course: 2
    Do you want to order dessert? (true/false): true
    No of dessert:1
    You have ordered 1 starters, 2 main course, 1 dessert,1 soft drink, 1 coffee
    Total: RM19
    Press 1 to next order and 0 to exit: __1





    here is what i have done so far, i really dont know how to put the if and else statement


     //
     
    import java.util.Scanner;
     
    public class courseWork {
    	static int no_starter,  no_mainCourse, no_desert, no_softDrink, no_coffee ;
       public static void main(String[] args) {
     
     
          int input;
          Scanner stdio = new Scanner( System.in );
     
          double Total = 0; // Total value of all the orders.
     
          /* Ask the user for the number of each type of course. */
     
          System.out.println("Do you want to order starter? ");
          input = stdio.nextInt();
     
     
          if(input == 1){
        	  System.out.print("Enter the number of starter:  ");
          no_starter = stdio.nextInt();
          stdio.nextLine();}
     
     
          System.out.println("Do you want to order main course?");
          input = stdio.nextInt();
     
          if(input == 1){
          System.out.print("Enter the number of main course: ");
          no_mainCourse = stdio.nextInt();
          stdio.nextLine();
          }
     
          System.out.println("Do you want to order desert?");
          input = stdio.nextInt();
     
          if(input == 1){
          System.out.print("Enter the number of desert: ");
          no_desert = stdio.nextInt();
          stdio.nextLine();
          }
     
          System.out.println("Do you want to order soft drink?");
          input = stdio.nextInt();
     
          if(input == 1){
          System.out.print("Enter the number of soft drink: ");
          no_softDrink = stdio.nextInt();
          stdio.nextLine();
          }
     
          System.out.println("Do you want to order coffee? ");
          input = stdio.nextInt();
          if(input == 1){
     
          System.out.print("Enter the number of coffee: ");
          no_coffee = stdio.nextInt();
          stdio.nextLine();
          }
          /* Add up the prices of the ordered courses. */
          double p_starter=(3*no_starter), p_mainCourse=(6*no_mainCourse), p_desert=(4*no_desert) , p_softDrink=(1.5*no_softDrink), p_coffee=(2*no_coffee);
    //


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    i don't really know how to do that. wrap my code how?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    Please edit your post and wrap your code with

    [code=java] //<<<<<<<< This before

    <YOUR CODE HERE>

    [/code] //<<<<<<<< This after
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    oke now got it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    how to put the if and else statement
    How is the program supposed to work? It looks like the user must make the same answer ( a 1) to all of the questions he is asked.
    Some of the questions look like they would require a response of "yes" vs 1.
    The Scanner class has methods for reading Strings like "yes" in addition to the method you are using: nextInt().
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    yh it is suppose to look like ask if the user wants to order starter if "yes" then enter the amount, but i tried using the scanner to enter input as y which will be yes but its giving me error each time

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    its giving me error each time
    Please copy the full text of the error message and paste it here.
    What Scanner methods are you using to read the user's input? Read the Scanner class's API doc to get the correct method to use for reading Strings vs ints.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    this is the code that i was using input as string.

    //
    import java.util.*;
     
    public class Course_work {
     
      Scanner use = new Scanner(System.in);
     
     public  void main(String[] args) {
     
    		int no_starter,  no_mainCourse, no_desert;
    		String input,t = null,f= null;
    		double total;
     
     
     
            System.out.println("Do you want to order starter?");
    		input= use.nextLine();
     
     
    		if (input == t)
    		{
    			System.out.println("Enter No of starter: ");
    			no_starter= use.nextInt();
    			use.nextLine();
    			}
    		System.out.println("Do you want to order main course? ");
    		input= use.nextLine();
     
    		if (input == t){
    			System.out.println("Enter No of main course: ");
    		no_mainCourse= use.nextInt();
    		use.nextLine();
    		}
     
    		System.out.println("Do you want to order desert? ");
    		input= use.nextLine();
     
    		if (input == t){
     
    			System.out.println("Enter No of desert: ");
    		no_desert = use.nextInt();
    		use.nextLine();
    		}
     
    		System.out.println("You have oedered " +no_starter +"Starter," +no_mainCourse + "main course" +no_desert + "desert");
     
    		}
     
            }
    //

    and this is the error
    Do you want to order starter?
    y
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at courseWork.main(courseWork.java:16)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    Strange that the message: Enter No of starter:
    was NOT printed before the call to the nextInt() method.

    The posted code has errors when compiled that need to be fixed so the code can be executed.
    Copy the full text of the compiler's error messages and post them here.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    that was the only error message it showed coz im using eclipse.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    There are several compiler errors that need to be fixed before the code will compile and execute for testing.

    How many versions of the source code do you have? The code in post#1 has several differences from the code in post#9
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    this is the one that im currently working with, if u could just help me ajust it in such a way that i use the 'y' as input using the scanner instead of 1. And my if and else are too much and when the program execute it gives me an incorrect value as the Total.

    //
     
    import java.util.Scanner;
     
    public class courseWork {
    	static int no_starter,  no_mainCourse, no_desert, no_softDrink, no_coffee ;
       public static void main(String[] args) {
     
     
          int input;
          Scanner stdio = new Scanner( System.in );
     
          double Total = 0; // Total value of all the orders.
     
          /* Ask the user for the number of each type of course. */
     
          System.out.println("Do you want to order starter? ");
          input = stdio.nextInt();
     
     
          if(input == 1){
        	  System.out.print("Enter the number of starter:  ");
          no_starter = stdio.nextInt();
          stdio.nextLine();}
     
     
          System.out.println("Do you want to order main course?");
          input = stdio.nextInt();
     
          if(input == 1){
          System.out.print("Enter the number of main course: ");
          no_mainCourse = stdio.nextInt();
          stdio.nextLine();
          }
     
          System.out.println("Do you want to order desert?");
          input = stdio.nextInt();
     
          if(input == 1){
          System.out.print("Enter the number of desert: ");
          no_desert = stdio.nextInt();
          stdio.nextLine();
          }
     
          System.out.println("Do you want to order soft drink?");
          input = stdio.nextInt();
     
          if(input == 1){
          System.out.print("Enter the number of soft drink: ");
          no_softDrink = stdio.nextInt();
          stdio.nextLine();
          }
     
          System.out.println("Do you want to order coffee? ");
          input = stdio.nextInt();
          if(input == 1){
     
          System.out.print("Enter the number of coffee: ");
          no_coffee = stdio.nextInt();
          stdio.nextLine();
          }
          /* Add up the prices of the ordered courses. */
          double p_starter=(3*no_starter), p_mainCourse=(6*no_mainCourse), p_desert=(4*no_desert) , p_softDrink=(1.5*no_softDrink), p_coffee=(2*no_coffee);
     
     
     
     
     
     
           if(no_starter >= 1 && no_mainCourse >=1 || no_softDrink>=1){
        	  Total = p_starter + p_mainCourse;
          }
     
          else if(no_starter >= 1 && no_mainCourse >=1 && no_coffee >=1){
        	  Total = p_starter + p_mainCourse + p_coffee;
          }
     
          else if(no_starter >=1 && no_mainCourse >=1 && no_desert >=1 || no_softDrink >=1 || no_coffee >=1)
          {
        	  Total =  p_starter + p_mainCourse + p_desert;
          }
          else if(no_desert >= 1 && no_mainCourse >=1 || no_coffee>=1){
        	  Total = p_mainCourse + p_desert;
          }
          else if(no_mainCourse >= 1 && no_softDrink >= 1 && no_softDrink >= 1){
        	  Total =  p_mainCourse + p_softDrink + p_softDrink;
          }
     
          else if(no_softDrink >=1 && no_coffee >=1)
          {
        	  Total=   p_softDrink +  p_coffee;
          }
          else if(no_softDrink >=1 && no_coffee >=1 && no_desert >=1)
          {
        	  Total=   p_softDrink +  p_coffee + p_desert;
          }
          else if(no_softDrink >=1 && no_coffee >=1 && no_mainCourse >=1)
          {
        	  Total=   p_softDrink +  p_coffee + p_mainCourse;
          }
     
          else if(no_softDrink >=1 && no_coffee >=1 && no_starter >=1)
          {
        	  Total=   p_softDrink +  p_coffee + p_starter;
          }
     
          else{
        	  System.out.println("nothing has been ordered");
        	  Total = 0;
          }
     
          /* Report the result back to the user. */
     
          System.out.println("You have oedered " + no_starter + " Starter " + no_mainCourse + " main course " + no_desert + " desert " + no_softDrink + " soft drink" + no_coffee + " coffee ");
          System.out.println();
          System.out.print("The total ");
          System.out.print(Total);  // Formatted output!
          System.out.println();
     
       }  // end main()
     
    }  // end class
    //

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    The code in post#9 had the correct method for reading String input from the user. The code in posts #1 and #13 do not. Use the Scanner method from post#9 to read a String.

    After reading the user input into a String you will need to use one of the String class methods to test its contents. Don't use the == operator to compare the contents of a String object.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    oke i have change the method of using string to scan the string, but what operator should i use here "if (input == t)" coz i tried using this" if (input != t)" and it work but it doesont matter weather i press t or anything it still skips and ask me to enter the number of course.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    If the variable: input is a String object you need to use one of the String class's methods for comparing Strings or the equals() method for an exact match.
    See the API doc for the String class for its methods:
    Java Platform SE 7

    If input is an int, then the == operator is OK.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    it seems alittle advance for me tho i checked through the API doc but i couldn't pull out anything as i'm not even sure what i'm looking for, pls help norm.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    What are you trying to do?
    If you want to compare String objects for equality use the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    all i want to do is, if the program ask if i want t order starter the i press t, then it ask me to enter the number of staters i want, but if i press n then it jumps that and straight away ask me if i want to order main course

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    Take it one step at a time and test that each step is done correctly
    Ask a question << does the code do this OK?
    read the response << what is read? Add a println to print out what was read so you can see it
    ??? What is the next step for the program? What does the code do with what was entered by the user?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    ok, firstly it asks the user if he wants to order a starter if yes then it goes on to ask for how many starter he wants to order after her puts the number the program goes to the next and ask the user again if he wants to order a main course if yes then ask for how many main course he wants to order it keeps asking till it reaches the last one which is coffee, after that it will print out how many of each course has he ordered the calculates the amount for each and displays the total. e.g
    Menu:
    Do you want to order starter? (true/false): true
    No of starter: 1

    Do you want to order main course? (true/false): true
    No of main course: 2
    Do you want to order dessert? (true/false): true
    No of dessert:1
    You have ordered 1 starters, 2 main course, 1 dessert,1 soft drink, 1 coffee
    Total: RM19
    Press 1 to next order and 0 to exit: __1

    then not forgetting that
    starter at RM3
    • main course at RM6
    • dessert at RM4
    • soft drink at RM1.5 (free only if both a starter & a main course have been ordered)
    • coffee at RM2 (free only of both a dessert & a main course have been ordered)

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    What is the one thing the code must do after the user enters the response to the first question? Don't try to do it all at once. One step at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    it must save that response in other to determine the last answers, and must execute the next step which is another question.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ugent pls help

    save that response in other
    Is that an assignment statement, like this:
    other = response;  // save response in other
    next step which is another question.
    I assume the user's response to the question will be read and saved in a variable

    then what?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ugent pls help

    not an assignment statement, i meant literary it will take the response to know what to to do next, which is if t is been entered then the next thing is to ask the user of how many if not then skip to the next and request a responce of t again only at the last response the it shows all what has been entered.

Page 1 of 2 12 LastLast

Similar Threads

  1. Help Pls..
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2013, 12:41 PM
  2. HI PLS HELP
    By Virat Pandey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 23rd, 2012, 06:12 AM
  3. pls.pls.pls.help with this
    By izzahmed in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 25th, 2011, 11:30 AM
  4. Pls help me
    By rose_brit3 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 28th, 2010, 09:57 AM
  5. Pls Help
    By randika11 in forum JDBC & Databases
    Replies: 3
    Last Post: July 20th, 2010, 05:52 AM