0

I am new to Java programming. I developed a Pizza class that takes for parameters and outputs the description and cost. I developed a PizzaOrderArray class that stores the pizza orders in an array. I have a class containing the main method also.

I am trying to print out the cost of order 4, but the a built-in error message claims that the pizza_size is invalid and prints out that the value is 0.0.

I do not understand how the value of order 4's pizza_size could be printed above it yet the pizza_size becomes invalid.

What am I missing?

Pizza.java

import java.text.NumberFormat; 
import java.util.Locale;

public class Pizza {

    public Pizza(String size, int numCheeseTop, int numPepTop, int numHamTop){
        if(!setPizzaSize(size)){
            System.out.println(size + " is invalid size." + "Use small, medium or large.");
        }
        setNumCheese(numCheeseTop);
        setNumPep(numPepTop);
        setNumHam(numHamTop);
    }

    public Pizza(String size, int numPepTop, int numHamTop){
         if(!setPizzaSize(size)){
                        System.out.println(size + " is invalid size." + "Use small, medium or large.");
                }
        pizza_cheese = 0;
        setNumPep(numPepTop);
        setNumHam(numHamTop);
    }

    public Pizza(String size, int numHamTop){
         if(!setPizzaSize(size)){
                        System.out.println(size + " is invalid size." + "Use small, medium or large.");
                }
        pizza_pep = 0;
        setNumHam(numHamTop);
        pizza_cheese = 0;
    }

    public Pizza(String size){
         if(!setPizzaSize(size)){
                        System.out.println(size + " is invalid size." + "Use small, medium or large.");
                }
        pizza_cheese = 0;
        pizza_pep = 0;
        pizza_ham = 0;
    }

    public Pizza(){
        pizza_size = "small";
        pizza_cheese = 0;
        pizza_pep = 0;
        pizza_ham = 0;
    }

    public Pizza(Pizza copyPizza)
    {
        pizza_size = copyPizza.getPizzaSize();
        pizza_cheese = copyPizza.getNumCheese();
        pizza_pep = copyPizza.getNumPep();
        pizza_ham = copyPizza.getNumHam();
    }


    //Setters

    public boolean setPizzaSize(String size){
        if(size.equalsIgnoreCase("small") || (size.equalsIgnoreCase("medium") || (size.equalsIgnoreCase("large")))){
            pizza_size = size.toLowerCase();
            return true;
        }
        return false;
    }

    public void setNumCheese(int numCheeseTop){
        pizza_cheese = numCheeseTop;
    }

    public void setNumPep(int numPepTop){
        pizza_pep = numPepTop;
    }

    public void setNumHam(int numHamTop){
        pizza_ham = numHamTop;
    }
    //End of setters

    //Getters

    public String getPizzaSize(){
        return pizza_size;
    }

    public int getNumCheese(){
        return pizza_cheese;
    }

    public int getNumPep(){
        return pizza_pep;
    }

    public int getNumHam(){
        return pizza_ham;
    }

    //End of getters

    public double calcCost(){
        if(pizza_size.toLowerCase() == "small"){
            return 10 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() == "medium"){
            return 12 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() == "large"){
            return 14 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() != "small" && pizza_size.toLowerCase() != "medium" && pizza_size.toLowerCase() != "large"){
            System.out.println("Inside of calcCost if");
            System.out.println("Invalid pizza size");
            return 0;
        }
    return 0;
    }


    public String toString(){
        return pizza_size + " pizza with " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings and "
             + pizza_ham + " ham toppings ";        }

    public String getDescription(){
        return pizza_size + " pizza with " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings and "
             + pizza_ham + " ham toppings ";  
        }

    public String pizza_size;
    public int pizza_cheese, pizza_pep, pizza_ham;

} //End of Pizza class  

PizzaOrderArray.java

import static java.lang.System.out;

public class PizzaOrderArray{

    public String pizza_size;
    public int pizza_cheese, pizza_pep, pizza_ham; 

    private Pizza[] pizza;
    private int index = 0;


    public PizzaOrderArray(){
        index = 1;
        pizza = new Pizza[index];
    }

    public PizzaOrderArray(int i){
        index = 1;
        pizza = new Pizza[index];
    }

    public PizzaOrderArray(PizzaOrderArray poa){
        pizza = new Pizza[poa.index];
        index = poa.index;
        for (int i = 0; i < poa.index; i++){
            pizza[i] = new Pizza(poa.pizza[i]);
        }
    }

    public void setPizza(int index1, Pizza newpizza){
        pizza[index1] = new Pizza(newpizza);
    }

    public String getPizzaSize(){
               return pizza_size;
       }

       public int getNumCheese(){
               return pizza_cheese;
        }

        public int getNumPep(){
                return pizza_pep;
        }

        public int getNumHam(){
                return pizza_ham;
        }

    public String toString() {
        String s = "";
            for(int i = 0; i < index; i++) {
            s= (s + pizza[i].toString());
        }
        return s;
    }

    public double calcTotal(){
        double r = 0.0;
        for(int i = 0; i < index; i++){
            System.out.println("PizzaOrderArray-calcTotal()");
            r = r + pizza[i].calcCost();
        }
    return r;
    }

    public boolean equals(PizzaOrderArray orderarray) {
        boolean r = false;
        if(orderarray.pizza.length != pizza.length) {
            return r;
        }

        for(int i = 0; i < orderarray.pizza.length; i++) {
            if(pizza[i].equals(orderarray.pizza[i])) { 
                r = true;
            }
            else {
                return false;
            }
        }
        return r;
    }   


} //End of PizzaOrderArray class



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

public class V4_Project_15_page_418 {

    public static void main (String args[]){

        //Order1
        PizzaOrderArray order1 = new PizzaOrderArray();
        Pizza pizzaone = new Pizza("Medium", 1, 1, 2);
        Pizza pizzatwo = new Pizza("Small", 1, 2, 1);
        order1.setPizza(0, pizzaone);
        System.out.println("Order 1: ");
        System.out.println(order1);
        System.out.println();

        //Order2
        Pizza pizzathree = new Pizza(pizzatwo);
        PizzaOrderArray order2 = new PizzaOrderArray(2);
        order2.setPizza(0, pizzaone);
            order2.setPizza(0, pizzatwo);
        System.out.println("Order 2: ");
        System.out.println(order2);
        System.out.println();

        //Order3
        PizzaOrderArray order3 = new PizzaOrderArray(1);
        order3.setPizza(0, pizzaone);
        order3.setPizza(0, pizzatwo);
        System.out.println("Order 3: ");
        System.out.println(order3);
        System.out.println();

        //Order4
        PizzaOrderArray order4 = new PizzaOrderArray(order1);
        System.out.println("Order4: ");
        System.out.println(order4); 
        System.out.println();

        //TEST THE PROGRAM
        System.out.println("TEST:  The total for order 4 is: " + order4.calcTotal());
        System.out.println();

        System.out.println("TEST: Is order1 equal to order2?");
        System.out.println(order1.equals(order2));
        System.out.println();

        System.out.println("TEST: Is order2 equal to order4?");
        System.out.println(order2.equals(order4));
        System.out.println();



    }//End of main class

}//End of V4_Project_15_page_418 class
6
  • 1
    Can I recommend that you look into using an enum for your pizza size instead of a string? Commented Sep 29, 2015 at 20:33
  • @AndyTurner Can you please explain why this would be a better option and if it would possibly solve the issue? Thanks! Commented Sep 29, 2015 at 20:37
  • Because String can contain any value representable by a string, including "String" and "Justin", which are obviously not pizza sizes. If you use an enum, you can limit it to just valid sizes. Commented Sep 29, 2015 at 20:38
  • And why does it solve your problem? Because you shouldn't compare strings using ==, you use .equals(). You can use == with enum values. Commented Sep 29, 2015 at 20:39
  • 1
    possible duplicate of How do I compare strings in Java? Commented Sep 29, 2015 at 20:51

2 Answers 2

2

You are comparing strings using ==, which compares the instance identity, not equality. You should use String.equals instead.

Alternatively, you can use a switch:

public double calcCost(){
  switch (pizza_size.toLowerCase()) {
    case "small":
        return 10 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
    case "medium":
        return 12 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
    case "large":
        return 14 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
    default:
        System.out.println("Inside of calcCost if");
        System.out.println("Invalid pizza size");
        return 0;
    }
}

You should also look into using an enum to represent the pizza size:

enum Size { SMALL, MEDIUM, LARGE }

String is not a good choice, because not only can you represent a size by the different case (you have case-insensitive comparison and lower-casing throughout this code), but you could also be passed anything else. If you limit your inputs to just these few sizes, your code gets a lot simpler. And, you can compare enum values using ==.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thank you, dear sir. I will also continue to look into using an enum. I appreciate your help.
1

Do not test String equal using '==', use equals instead.

   public double calcCost(){
        if(pizza_size.toLowerCase() == "small"){
            return 10 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() == "medium"){
            return 12 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() == "large"){
            return 14 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() != "small" && pizza_size.toLowerCase() != "medium" && pizza_size.toLowerCase() != "large"){
            System.out.println("Inside of calcCost if");
            System.out.println("Invalid pizza size");
            return 0;
        }
        return 0;
    }

Because:

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

See this question for reference.

4 Comments

And why is that? Maybe you should add some description as to why.
I know why. You can google why. But just stating "a instead of b" and copy paste his code isn't a very good answer, it is better suited as a comment. And please do not just post that link in the answer. Post any relevant information from it onto your answer.
Understood, I will pay attention to this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.