-1

I've tried to make Java code to communicate with the user.

The code is about calculating a pizza price assuming the pizza price is a final int and doesn't change. The only thing that affects the price is the add-on that the customer wants on the pizza (tomato, mushrooms, cheddar cheese). I've tried to create code that covers every option the customer picks using 'if' statements, but I think there is easier way to do it. (I want the program to calculate the price given only the add-on name.)

For example, the customer picks Mushroom and Tomato so the pizza price will be the pizza price + tomato price + mushroom price.

Is there any easier way to solve it? Or should I cover every option the customer picks with if/else statements?

public static void main(String[] args){
    Scanner s= new Scanner(System.in);
    final int pizzaPrice= 12;
    int Mushrooms = 2;
    int Tomato= 2;
    int Corn = 2;
    int Olives = 2;
    int CheddarCheese=3;
    int bulgaritCheese=3;
    int yellowCheese= 3;

    String cheapAddOns ="Mushrooms , Tomato, Corn, Olives";
    String expensiveAddOns = "Cheddar cheese , Bulgarit cheese , Yellow cheese";

    System.out.println("Please enter first AddOns(Mushrooms , Tomato , Corn, Olives): ");
    cheapAddOns=s.next();
    System.out.println("Please enter second AddOns (Cheddar cheese , Bulgarit cheese ,Yellow cheese)");
    expensiveAddOns=s.next();

    if( cheapAddOns== "Mushrooms" || expensiveAddOns=="Cheddar cheese"  ) {
        System.out.println("Your Pizza price is: $" + pizzaPrice + Mushrooms + CheddarCheese );
    }
    else if (cheapAddOns=="Tomato" || expensiveAddOns=="Bulgarit cheese") {
        System.out.println("Your Pizza price is: $" +pizzaPrice+ Tomato + bulgaritCheese);
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Tal Ouzan
  • 75
  • 1
  • 5
  • 1
    Comparing strings with `==` is not going to work. – khelwood Nov 23 '19 at 19:32
  • You might consider putting your add-ons and their prices in a map. That would simplify your code. Also you can calculate the price for the first add-on, then the price for the second add-on, and then add them up, instead of dealing with them in pairs. – khelwood Nov 23 '19 at 19:43
  • You are using "||" when you should be using "&&". – FredK Nov 23 '19 at 19:45
  • You are also clobbering your initialization of cheapAddOns and expensiveAddOns when you read in the user input. – FredK Nov 23 '19 at 19:54

2 Answers2

1

First of all when you name your variables, don't start with a capital letter int mushrooms, not int Mushrooms. Second thing, when you compare Strings == operator will not work. you must use stringName.equals(). In your case it would look like:

cheapAddOns = s.next();
if(cheapAddOns.equals("tomato") || cheapAddOns.equals("mushrooms") || ...){
   //this way you can get one if for all cheap addons;
   pizzaPrice += 2; //read below to undrstand why i would add price this way
}

And the same check for expensive addons.
What you do when you initiate cheapAddOns and expensiveAddOns is incorrect, I mean that you initiate them with start variable, and next you read them from standard input. Initiate them with no value:

String cheapAddOns;
String expensiveAddOns;

And for this example, you dont have to use final int, better initiate it as an normal integer, and if any statment is true, add to this value. It would look like this:

int pizzaPrice = 12;
int cheapAddonPrice = 2;
int expensiveAddonPrice = 3;
if(anyStatement){
   pizzaPrice += 2; //2 for cheap, 3 for expensiv addon
}
System.out.println("Your pizza costs: $" + pizzaPrice) 

It works only when all cheap addons cost $2 and all expensive addons cost $3. If each addon has another price, you will need more if statements to calculate price. But calculate price in as many statements as you want and print it once (until you print only price (without addons list).
You made a lot of simple mistakes here so i think that you just start your programming adventure. Don't learn bad habbits, try to imporove your code with each day.

Czachodym
  • 245
  • 2
  • 13
0

if all cheap add-ons have the same price, one way could be:

String cheapAddOns = "Mushrooms, Tomato, ...";
int cheapPrices = 2;

System.out.println("Please enter first AddOns(Mushrooms , Tomato , Corn, Olives): ");
String cheap = s.next();
int price = pizzaPrice;
if ( cheapAddOns.indexOf(cheap) >= 0 ) {
   price += cheapPrice;
}

then repeat the code using expensiveAddOns. Note that you will need to consume the CR before calling s.next() to get the user's next input.

FredK
  • 4,094
  • 1
  • 9
  • 11