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 12 of 12

Thread: If statement?

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default If statement?

    For this part :
    System.out.print("Do you want "+
    				thin +
    				"?");
    		input = keyboard.nextLine();
    		choice = input.charAt(0);
    		if (choice == 'Y' || choice == 'y')
    		{
    			numberOfthin += 1;
    			toppings = toppings + "Thin";
    		}
     
    		System.out.print("Do you want"+
    				deepDish +
    				"?");
    		input = keyboard.nextLine();
    		choice = input.charAt(0);
    		if (choice == 'Y' || choice == 'y')
    		{
    			numberOfdeepDish += 1;
    			toppings = toppings + "DeepDish ";
    		}
    In my program i have two bread types. However, the user can only opt for one. So say if the want the first one then the next one should be irrelevant, however if they say no to the first one then the second if statement should show. How could i do this ? , I'm guessing an If statement with boolean somehow? Cheers


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: If statement?

    If there are only two choices, and the user chooses "No" or anything but Yes/yes to the first, then the second is the choice by default. If the user chooses Yes/yes to the first, then the second choice is irrelevant. This logic is often done with an if/else construct. No boolean or flag is required.

  3. #3
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: If statement?

    Quote Originally Posted by GregBrannon View Post
    If there are only two choices, and the user chooses "No" or anything but Yes/yes to the first, then the second is the choice by default. If the user chooses Yes/yes to the first, then the second choice is irrelevant. This logic is often done with an if/else construct. No boolean or flag is required.
    Hi,

    Are you online now ?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: If statement?

    What's your question?

  5. #5
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: If statement?

    Quote Originally Posted by GregBrannon View Post
    If there are only two choices, and the user chooses "No" or anything but Yes/yes to the first, then the second is the choice by default. If the user chooses Yes/yes to the first, then the second choice is irrelevant. This logic is often done with an if/else construct. No boolean or flag is required.
    For my program i have different costs for different objects. The total cost will depend on the choices users make.

    At the end i've got :

    for(String s:l){

    totalCost += s.calcPrice();
    System.out.println("1 x " +s);

    }

    This lists all the objects that the user has chosen because for each topping chosen it has e.g.
    l.add ("Prawn");

    so if i make an object for prawn would it be possible to put .add(prawn) instead if the object is called prawn? because i'm required to use objects.

    Anyway,

    for the total cost, could i create another
    for(String s:l){

    totalCost += s.calcPrice();
    System.out.println("1 x " +s);

    }
    but using int rather than String, then list l.add(thin.B1Cost) if i have defined it. The more i'm typing here the more confusing it's becoming aha. Basically could i do the same as above to calculate the total cost as well?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: If statement?

    You're not yet thinking in objects, but you're trying, and that's good.

    The objects you're imagining should be instances of a class called (something like) PizzaTopping. The user is given a list of PizzaTopping objects by their attribute String name, and then after the user is done building the pizza, the double cost attribute of each PizzaTopping instance chosen is added to get the total topping price.

    So for your example, the instance of PizzaTopping would be named "Prawn" with a cost of $3.50. The for loops you've written above would be more like:
    // calculate the total price of all toppings chosen
    for ( PizzaTopping topping : toppingsChosen )
    {
        totalToppingPrice += topping.getCost();
    }
    Can you visualize the PizzaTopping class? Sketch out that class and post it if you want help.

  7. #7
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: If statement?

    Quote Originally Posted by GregBrannon View Post
    You're not yet thinking in objects, but you're trying, and that's good.

    The objects you're imagining should be instances of a class called (something like) PizzaTopping. The user is given a list of PizzaTopping objects by their attribute String name, and then after the user is done building the pizza, the double cost attribute of each PizzaTopping instance chosen is added to get the total topping price.

    So for your example, the instance of PizzaTopping would be named "Prawn" with a cost of $3.50. The for loops you've written above would be more like:
    // calculate the total price of all toppings chosen
    for ( PizzaTopping topping : toppingsChosen )
    {
        totalToppingPrice += topping.getCost();
    }
    Can you visualize the PizzaTopping class? Sketch out that class and post it if you want help.
    Hey, i didn't see this and wasn't trying to crack on! I feel like i'm making some progressive, but i'm not too sure. I'm attempting to use lists for the total, and i'm guessing that'll never work; since it'll always list the individual costs. Anyway here's what i mean


    List<String> l = new ArrayList<String>();
    List<Double> c = new ArrayList<Double>();

    for(String s:l){


    System.out.println("1 x " +s);

    }

    for(double f:c){

    System.out.println("total cost");
    System.out.println("£" +f);

    }

    output: VVCap Image

    Sorry i forgot how to format it into java code on this forum.

    I thought i'd be able to use the .add and then somehow total them up at the end, but i figured now this is impossible ?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: If statement?

    The Announcement topic at the top of the sub-forum shows to post code in code or highlight tags. There's almost always an FAQ that does, but sometimes they're hard to find.

    You're still thinking of storing data in parallel arrays, but you've evolved - if it can be called that - to using parallel ListArrays. It's not that it'll never work, but it's a lot harder than just using instances of well-designed classes as I described in my last post. It's time to shift your thinking. Write that PizzaTopping class.

  9. #9
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: If statement?

    I currently have a class named toppings. By the way, the program will also require to calculate the cost of the base, as well as the toppings. I have a separate class for that .Will this matter? I'll try it your way, thanks.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: If statement?

    Class names should begin with capital letters.

    Will it matter if there are multiple sources of cost which may exist in separate classes? No. I suggest designing your classes to take those factors into account. The top class might be called PizzaOrder. The PizzaOrder class may also include instances of PizzaBase and PizzaToppings. The total price of a PizzaOrder instance would then be calculated by adding up the parts pizzaBase and pizzaToppings in each pizzaOrder.

  11. #11
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: If statement?

    Quote Originally Posted by GregBrannon View Post
    Class names should begin with capital letters.

    Will it matter if there are multiple sources of cost which may exist in separate classes? No. I suggest designing your classes to take those factors into account. The top class might be called PizzaOrder. The PizzaOrder class may also include instances of PizzaBase and PizzaToppings. The total price of a PizzaOrder instance would then be calculated by adding up the parts pizzaBase and pizzaToppings in each pizzaOrder.
    Went for a little break. I think i've already partially done what you've mentioned because throughout my code i have written down :
    ex.:
    //Creating thick pizza base 
    	    PizzaBase e = new PizzaBase();
    	    e.type = "thick";
    	    e.B1Cost = 8.75;
       //Creating thin pizza base 
    	    PizzaBase thin = new PizzaBase();
    	    thin.type = "thin";
    	    thin.B1Cost = 6.75;
     
    ---------------------------------------
      Toppings cheese = new Toppings();
    	    cheese.type = "cheese";
    	    cheese.cost = 1.50;
     
    {
    }


    two examples are above are the stuff i have included?

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: If statement?

    You could be on the right track, but as soon as you start storing e and cheese from the above into an ArrayList, your train has left the tracks to OOP.

Similar Threads

  1. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  4. if....else statement
    By Appu14 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 2nd, 2012, 06:35 AM
  5. If statement help
    By Legion of Daughters in forum Loops & Control Statements
    Replies: 12
    Last Post: September 6th, 2011, 08:25 AM