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.

When I tried to print the values of the orders, nothing prints yet debugging shows that the proper methods and loops were entered.

What am I doing incorrect? I have invested many hours and am still very confused. Any suggestions, please? Thank you! I appreciate it.

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("Invalid pizza size");
            return 0;
        }
        return 0;
    }

    public String getDescription() {
        return pizza_size + " pizza with " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings and "
                + pizza_ham + " ham toppings ";  //+ " which is " + money.format(pizza2.calcCost());
    }

    //private String pizza_size;
    //private int pizza_cheese, pizza_pep, pizza_ham;  
    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 String pizza_size;
    //private int pizza_cheese; pizza_pep; pizza_ham;
    private Pizza[] pizza;
    private int index = 0;

    public PizzaOrderArray() {
        System.out.println("PizzaOrderArray()");
        index = 1;
        pizza = new Pizza[index];
    }

    public PizzaOrderArray(int i) {
        System.out.println("PizzaOrderArray(int i)");
        index = 1;
        pizza = new Pizza[index];
    }

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

    public void setPizza(int index1, Pizza newpizza) {
        System.out.println("Inside of setPizza");
        pizza[index1] = new Pizza(newpizza);
    }

    public String getPizzaSize() {
        System.out.println("Inside of getPizzaSize");
        return pizza_size;
    }

    public int getNumCheese() {
        System.out.println("Inside of getNumCheese");
        return pizza_cheese;
    }

    public int getNumPep() {
        System.out.println("Inside of getNumPep");
        return pizza_pep;
    }

    public int getNumHam() {
        System.out.println("Inside of getNumHam");
        return pizza_ham;
    }

    public String toString() {
        String s = "";
        int indexUsed = 0;
        System.out.println("Inside of toString");
        for (int i = 0; i < indexUsed; i++) {
            s = (s + pizza[i].toString());
        }
        System.out.println("Inside of toString for loop");

        return s;
    }

    public double calcTotal() {
        double r = 0.0;
        System.out.println("Inside of calcTotal");

        for (int i = 0; i < index; i++) {
            System.out.println("Inside of calcTotal for loop");
            r = r + pizza[i].calcCost();
        }
        return r;
    }

    public boolean equals(PizzaOrderArray orderarray) {
        boolean r = false;
        System.out.println("Inside of equals");
        if (orderarray.pizza.length != pizza.length) {
            System.out.println("Inside of equals if");
            return r;
        }

        for (int i = 0; i < orderarray.pizza.length; i++) {
            if (pizza[i].equals(orderarray.pizza[i])) {
                System.out.println("Inside of equals for-if");
                r = true;
            } else {
                System.out.println("Inside of equals for-else");
                return false;
            }
        }
        System.out.println("Return of equals");
        return r;
    }

} //End of PizzaOrderArray class

V4_Project_15_page_418.java

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", 0, 0, 0);
        Pizza pizzatwo = new Pizza("Small", 1, 0, 0);
        order1.setPizza(0, pizzaone);
        System.out.println("Order 1: ");
        System.out.println(order1.toString());
        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.toString());
        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.toString());
        System.out.println(order3);
        System.out.println();

        //Order4
        PizzaOrderArray order4 = new PizzaOrderArray(order3);
        System.out.println("Order 4: ");
        System.out.println(order4.toString());
        System.out.println(order4);
        //TEST THE PROGRAM
        System.out.println("TEST:  The total for order 4 is: " + order4.calcTotal());
        System.out.println();

        //Order5
        PizzaOrderArray order5 = new PizzaOrderArray(order1);
        System.out.println("Order5: ");
        System.out.println(order5);
        System.out.println();

    }//End of main class

}//End of V4_Project_15_page_418 class

Output:

PizzaOrderArray()
Inside of setPizza
Order 1:
Inside of toString
Inside of toString for loop

Inside of toString
Inside of toString for loop


PizzaOrderArray(int i)
Inside of setPizza
Inside of setPizza
Order 2:
Inside of toString
Inside of toString for loop

Inside of toString
Inside of toString for loop


PizzaOrderArray(int i)
Inside of setPizza
Inside of setPizza
Order 3:
Inside of toString
Inside of toString for loop

Inside of toString
Inside of toString for loop


PizzaOrderArray(PizzaOrderArray poa)
PizzaOrderArray(PizzaOrderArray poa) for loop
Order 4:
Inside of toString
Inside of toString for loop

Inside of toString
Inside of toString for loop

Inside of calcTotal
Inside of calcTotal for loop
Invalid pizza size
TEST:  The total for order 4 is: 0.0

PizzaOrderArray(PizzaOrderArray poa)
PizzaOrderArray(PizzaOrderArray poa) for loop
Order5:
Inside of toString
Inside of toString for loop
Channa Jayamuni
  • 1,876
  • 1
  • 18
  • 29
  • `if(pizza_size.toLowerCase() == "small")` -> [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Sep 29 '15 at 18:58
  • But do you understand difference between `==` and `equals` method? – Pshemo Sep 29 '15 at 19:03

4 Answers4

3

Take a close look at the condition in this for loop, it isn't going to ever print anything since the condition is never true since i is never less than indexUsed which is 0.

public String toString() {
    String s = "";
    int indexUsed = 0;
    System.out.println("Inside of toString");
    for(int i = 0; i < indexUsed; i++) 
        s= (s + pizza[i].toString());
        System.out.println("Inside of toString for loop");

    return s;
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Jacob Briscoe
  • 252
  • 1
  • 8
  • Hmm ... Unfortunately, there is another bug in this code snippet. Do you spot it? – Seelenvirtuose Sep 29 '15 at 19:06
  • Yes! Wow..thank you, dear sir. Now I am printing the values out, but the values are in a hex looking form like this: Pizza@437d51a6. How can I get the values to a more normal looking string? Also, pizza size in order4 is invalid which I will have to look into. But, again, how can I get the values into a more normal looking string? Thank you again. – Justin_Finland Sep 29 '15 at 19:08
  • @Justin If you want to ask about different problem post different question. – Pshemo Sep 29 '15 at 19:09
  • @Pshemo I understand. Thank you again sir. I will post a different question and look into the printing more. – Justin_Finland Sep 29 '15 at 19:10
  • @Justin Good to hear it. But remember that there is a limit of how many questions you can ask per hour/month so try to first do your own research before asking next one. – Pshemo Sep 29 '15 at 19:12
  • @Justin The "hex looking form" is because there's no `toString()` method in `Pizza`. – David Conrad Sep 29 '15 at 19:21
  • @DavidConrad Ahhh, I see now. Great, I added a toString() method in the Pizza class. I understand 100% now of why that would be needed in that specific class. Thank you! I appreciate it. – Justin_Finland Sep 29 '15 at 19:38
1

Something also need pay attention to:

for(int i = 0; i < indexUsed; i++) 
    s= (s + pizza[i].toString());
    System.out.println("Inside of toString for loop");

means:

for(int i = 0; i < indexUsed; i++) {
    s= (s + pizza[i].toString());
}
    System.out.println("Inside of toString for loop");

So this is System.out.println just misleading you, you are never "inside of" the for loop.

I think it's better to always use the braces '{}' with for/while loop.

Hong Duan
  • 4,234
  • 2
  • 27
  • 50
0

The snippet

int indexUsed = 0; System.out.println("Inside of toString"); for(int i = 0; i < indexUsed; i++) s= (s + pizza[i].toString()); is wrong, your for loop is never executed since indexUsed is 0

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Objects always have `toString()` method. It is inherited from `Object` class, or other closer supertype. Our class can simply override it. – Pshemo Sep 29 '15 at 18:59
  • if there's no toString() it will use the default toString() which will usually print the memory address of the object. You will always get something printed – dkatzel Sep 29 '15 at 18:59
  • I am getting absolutely no printed values...not even hex values. Can you please explain why? Thanks. – Justin_Finland Sep 29 '15 at 19:00
0

In your toString method, the for loop condition never becomes true (before the first iteration itself, 0<0 becomes false & loop terminates without executing once) so the loop never executes.

You can try changing the for loop statement to:

 for(int i = 0; i < index; i++) 
Sumedh
  • 404
  • 3
  • 11