#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Java if-else statements pizza order


    Hello all, I have a problem with my pizza order code. I have two problems rights now, one problem is that the final output price is like $100.00+. Also I want the toppings of the pizza to be displayed IF the user has input "y" or "Y". However it outputs all the toppings out, even if they did not select that topping.
    Here is my code:

    import java.util.Scanner;

    public class pizza
    {
    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner(System.in);

    // Variables
    String firstName; // first name of user
    char crustType; // code for type
    String crust; // name of crust
    int inches; // pizza size
    double cost = 0.0; // pizza cost
    final double taxRate = 0.08; // amount tax owed
    double tax; // tax amount
    boolean discount; // $2.00 discount
    double total; // total of pizz + toppings
    double subTotal;
    double lastTotal; // total of everything
    int numberOfToppings = 0;
    String toppings = "Cheese";

    // Prompts for name & determines discount
    System.out.println("Enter your first name? " );
    firstName = keyboard.nextLine();

    if (firstName.equalsIgnoreCase("Mike")
    || firstName.equalsIgnoreCase("Diane")
    || firstName.equalsIgnoreCase("Michael")
    || firstName.equalsIgnoreCase("Diane") )
    discount = true;
    else
    discount = false;

    // Prompts user for pizza size
    System.out.print("What size of pizza would you like? (10, 12, 14, or 16) " );
    inches = keyboard.nextInt();
    if (inches == 10 )
    cost = 10.99;
    else if (inches == 12)
    cost = 12.99;
    else if (inches == 14)
    cost = 14.99;
    else if (inches == 16)
    cost = 16.99;
    else if (inches != 10 && inches != 12 && inches != 14 && inches != 16)
    System.out.println("The number you have entered is illegal, your pizza size will be set to 12 inches. " );
    cost = 12;
    keyboard.nextLine();

    // Prompts user for type of crust
    System.out.print("What type of crust do you want? (H)and-Tossed, (T)hin-crust, or (D)eep-dish (enter H, T, or D,): " );
    crustType = keyboard.nextLine().charAt(0);

    if (crustType == 'H' || crustType == 'h' )
    crust = "Hand-Tossed";
    else if (crustType == 'T' || crustType == 't' )
    crust = "Thin-Crust";
    else if (crustType == 'D' || crustType == 'd' )
    crust = "Deep-Dish";
    else if (crustType != 'H' && crustType != 'h' && crustType != 'T' && crustType != 't' && crustType != 'D' && crustType != 'd' )
    System.out.println("The crust type you have entered is illegal, your crust type will be set to hand-tossed. " );
    crust = "Hand-Tossed";

    // Prompts user for additonal toppings
    System.out.println("All pizzas come with cheese." );
    System.out.println("Additional toppings are $1.25 each, choose from Pepperoni, Sausage, Onion, or Mushroom." );

    // Pepperoni
    System.out.println("Do you want Pepperoni? (Y/N)" );
    numberOfToppings = keyboard.nextLine().charAt(0);
    if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
    numberOfToppings = numberOfToppings + 1;
    toppings = toppings + " Pepperoni";

    // Sausage
    System.out.println("Do you want Sausage? (Y/N)" );
    numberOfToppings = keyboard.nextLine().charAt(0);
    if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
    numberOfToppings = numberOfToppings + 1;
    toppings = toppings + " Sausage";

    // Onion
    System.out.println("Do you want Onion? (Y/N)" );
    numberOfToppings = keyboard.nextLine().charAt(0);
    if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
    numberOfToppings = numberOfToppings + 1;
    toppings = toppings + " Onion";

    // Mushroom
    System.out.println("Do you want Mushroom? (Y/N)" );
    numberOfToppings = keyboard.nextLine().charAt(0);
    if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
    numberOfToppings = numberOfToppings + 1;
    toppings = toppings + " Mushroom";

    // Calculations
    total = (cost) + (numberOfToppings * 1.25);
    if (discount == true )
    total = total - 2;
    else
    total = total;
    tax = total * taxRate;
    lastTotal = total * ( 1 + taxRate );

    // Payment Confirmation
    System.out.println( firstName + ", your order is as follows:" );
    System.out.println( inches + " inch pizza" );
    System.out.println( crust );
    System.out.println( toppings );
    if ( discount == true )
    System.out.println("You are eligible for a $2.00 discount" );
    else
    System.out.println("You are not eligible for a $2.00 discount" );
    System.out.println("The cost of your order is: $" + total );
    System.out.println("The tax is: $" + tax );
    System.out.println("The total due is: $" + lastTotal );
    System.out.println("Your order will be ready for pickup in 30 minutes." );
    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    80
    Rep Power
    8
    Code:
    	// Prompts user for additonal toppings
    	System.out.println("All pizzas come with cheese." );
    	System.out.println("Additional toppings are $1.25 each, choose from Pepperoni, Sausage, Onion, or Mushroom." );
    	
    	// Pepperoni
    	System.out.println("Do you want Pepperoni? (Y/N)" );
    	numberOfToppings = keyboard.nextLine().charAt(0);
    	if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
    		numberOfToppings = numberOfToppings + 1; 
    		toppings = toppings + " Pepperoni";
    	
    	// Sausage
    	System.out.println("Do you want Sausage? (Y/N)" );
    	numberOfToppings = keyboard.nextLine().charAt(0);
    	if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
    		numberOfToppings = numberOfToppings + 1;
    		toppings = toppings + " Sausage";
    	
    	// Onion
    	System.out.println("Do you want Onion? (Y/N)" );
    	numberOfToppings = keyboard.nextLine().charAt(0);
    	if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
    		numberOfToppings = numberOfToppings + 1;
    		toppings = toppings + " Onion";
    	
    	// Mushroom
    	System.out.println("Do you want Mushroom? (Y/N)" );
    	numberOfToppings = keyboard.nextLine().charAt(0);
    	if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
    		numberOfToppings = numberOfToppings + 1;
    		toppings = toppings + " Mushroom";
    So basically, this is where the problem is. What is happening is that you're not using { } brackets on your if statements. Normally, that is ok, but only if the following statement is one line. If you're using a multi-line if-statement, you need to enclose it in the { }. Right now, the way you have it, the user can input their choice, and it'll increment the numberOfToppings, but then, regardless of their input, it'll add that ingredient to toppings.

    Enclose those if statements in those brackets and it'll solve your problem.

IMN logo majestic logo threadwatch logo seochat tools logo