# If statement?

• November 29th, 2013, 04:40 AM
maths94
If statement?
For this part :
Code Java:

```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
• November 29th, 2013, 04:50 AM
GregBrannon
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.
• November 29th, 2013, 10:26 AM
maths94
Re: If statement?
Quote:

Originally Posted by GregBrannon
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 ?
• November 29th, 2013, 10:49 AM
GregBrannon
Re: If statement?
• November 29th, 2013, 11:23 AM
maths94
Re: If statement?
Quote:

Originally Posted by GregBrannon
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.

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?
• November 29th, 2013, 11:48 AM
GregBrannon
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:
Code java:

```// 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.
• November 29th, 2013, 12:42 PM
maths94
Re: If statement?
Quote:

Originally Posted by GregBrannon
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:
Code java:

```// 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 ?
• November 29th, 2013, 12:58 PM
GregBrannon
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.
• November 29th, 2013, 01:05 PM
maths94
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.
• November 29th, 2013, 01:12 PM
GregBrannon
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.
• November 29th, 2013, 03:54 PM
maths94
Re: If statement?
Quote:

Originally Posted by GregBrannon
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.:
Code Java:

```//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?
• November 29th, 2013, 04:40 PM
GregBrannon
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.