samedi 23 novembre 2019

Pizza price calculating using 'if' statements in java

I'm a beginner in java programming. I've tried to make a java code to communicate with the user. The code is about calculating Pizza price assuming the pizza price is final int and doesn't change. the only thing that affects the price is the Add on the customer wants on the pizza (tomato , mushrooms , cheddar cheese) I've tried to create a 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 i should 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);
    }
}

}

Aucun commentaire:

Enregistrer un commentaire