2

I have been using a search tool to look for an answer but I am now stuck after being able to fix several of my previous problems. I was able to compare and make adjustments but now I don't understand what is wrong with my program.

I am in my first year learning computer programming starting with Java. I have read many people saying to use equalsIgnoreCase but we have yet to learn it and this is how I was taught as of this moment. I also read a post saying if (discount) maybe better than if (discount == true) but I have never used an if statement without displaying the variable next to it yet.

At first, it was giving the discount to every name I input, including those not in the 4 names designated to give the discount. Now it is not giving a discount to any of the 4 names (Mike, mike, Diane, diane).

I feel like it is a very small error but I just can't seem to to figure it out.

import java.util.Scanner;
import java.text.DecimalFormat;

public class PizzaOrder
{
public static void main (String [] args)
{
    DecimalFormat formatter = new DecimalFormat("#0.00");

    Scanner keyboard = new Scanner (System.in);

    String firstName;                   
    boolean discount = false;       
    int inches;                     
    char crustType;             
    String crust = "Hand-tossed"; 
    double cost = 12.99;            
    final double TAX_RATE = .08;    
    double tax;                     
    char choice;                        
    String input;                   
    String toppings = "Cheese ";    
    int numberOfToppings = 0;       

    System.out.println("Welcome to Mike and Diane's Pizza");
    System.out.print("Enter your first name:  ");
    firstName = keyboard.nextLine();

    if (firstName == "Mike" || firstName == "mike")
    {
        discount = true;
    }
    if (firstName =="Diane" || firstName == "diane")
    {
        discount = true;
    }

    System.out.println("Pizza Size (inches)   Cost");
    System.out.println("        10            $10.99");
    System.out.println("        12            $12.99");
    System.out.println("        14            $14.99");
    System.out.println("        16            $16.99");
    System.out.println("What size pizza would you like?"); 
    System.out.print("10, 12, 14, or 16 (enter the number only): ");
    inches = keyboard.nextInt();

    if (inches == 10)
    {
        cost = 10.99;
    }
    if (inches == 12)
    {
        cost = 12.99;
    }
    if (inches == 14)
    {
        cost = 14.99;
    }   
    if (inches == 16)
    {
        cost = 16.99;
    }
    else
        System.out.println("You have selected incorrect pizza size.");
        System.out.println("You have been assigned 12 inch pizza by default.");

    keyboard.nextLine();    

    System.out.println("What type of crust do you want? ");
    System.out.print("(H)Hand-tossed, (T) Thin-crust, or " +
        "(D) Deep-dish (enter H, T, or D): ");
    input = keyboard.nextLine();
    crustType = input.charAt(0);

    switch (crustType)
    {
        case 'H':
        case 'h':
            System.out.println("You have selected Hand-tossed crust.");
            break;
        case 'T':
        case 't':
            System.out.println("You have selected Thin-crust.");
            break;
        case 'D':
        case 'd':
            System.out.println("You have selected Deep-dish crust.");
            break;
        default:
            System.out.println("You have selected invalid type of crust.");
            System.out.println("You have been assigned Hand-tossed crust by default.");
            break;
    }

    System.out.println("All pizzas come with cheese."); 
    System.out.println("Additional toppings are $1.25 each,"
            +" choose from");
    System.out.println("Pepperoni, Sausage, Onion, Mushroom");

    System.out.print("Do you want Pepperoni?  (Y/N):  ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
        numberOfToppings += 1;
        toppings = toppings + "Pepperoni ";
    }
    System.out.print("Do you want Sausage?  (Y/N):  ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
        numberOfToppings += 1;
        toppings = toppings + "Sausage ";
    }
    System.out.print("Do you want Onion?  (Y/N):  ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
        numberOfToppings += 1;
        toppings = toppings + "Onion ";
    }
    System.out.print("Do you want Mushroom?  (Y/N):  ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
        numberOfToppings += 1;
        toppings = toppings + "Mushroom ";
    }

    cost = cost + (1.25*numberOfToppings);

    System.out.println();
    System.out.println("Your order is as follows: ");
    System.out.println(inches + " inch pizza");
    System.out.println(crust + " crust");
    System.out.println(toppings);       

    if (discount)
    {
        System.out.println("You are eligible for $2.00 discount.");
        cost -= 2.00;
    }

    System.out.println("The cost of your order is: $" + formatter.format(cost));

    tax = cost * TAX_RATE;
    System.out.println("The tax is:  $" + formatter.format(tax));
    System.out.println("The total due is:  $" + formatter.format((tax+cost)));

    System.out.println("Your order will be ready for pickup in 30 minutes.");
}   
}
Marcus Rickert
  • 4,138
  • 3
  • 24
  • 29
James
  • 47
  • 3
  • 8

3 Answers3

1

To compare strings you need to use .equals()

firstName.equals("Mike") || firstName.equals("mike")

or even better

firstName.equalsIgnoreCase("mike")
David
  • 3,388
  • 2
  • 21
  • 25
1

You're missing a bracket here

else
    System.out.println("You have selected incorrect pizza size.");
    System.out.println("You have been assigned 12 inch pizza by default.");

Should be

else {
    System.out.println("You have selected incorrect pizza size.");
    System.out.println("You have been assigned 12 inch pizza by default.");
}

You also forgot to set the default pizza size. However, you should use a switch statement for the size and set the default in the default case. Like this:

switch(inches) {
    case 10: 
        cost = 10.99;
    break;
    case 12:
        cost = 12.99;
    break;
    case 14:
        cost = 14.99;
    break;
    case 16:
        cost = 16.99;
    break;
    default:
        System.out.println("You have selected incorrect pizza size.");
        System.out.println("You have been assigned 12 inch pizza by default.");
        inches = 12;
        cost = 12.99;
}

The other answers here suggesting to use the String.equals or String.equalsIgnoreCase are also correct. See How do I compare strings in java?. I highly doubt your professor will have an issue with you doing things the correct way. I'm not certain whether == will work in this case, but it's the wrong way.

Community
  • 1
  • 1
Justin
  • 1,972
  • 13
  • 28
0

You'll need to use equals() to compare Strings instead of ==.

This is because Strings in Java are objects and == checks if the objects are one and the same, whereas equals() has been designed to look at the contents of the String instead.

For example:

String s1 = new String("abc");
String s2 = new String("abc");

System.out.println(s1 == s2);      //prints false
System.out.println(s1.equals(s2)); //prints true
Abe Fehr
  • 729
  • 9
  • 23