java - For loop printing invalid value? -
i new java programming. developed pizza class takes parameters , outputs description , cost. developed pizzaorderarray
class stores pizza orders in array. have class containing main method also.
i trying print out cost of order 4
, built-in error message claims pizza_size invalid , prints out value 0.0
.
i not understand how value of order 4
's pizza_size printed above yet pizza_size becomes invalid.
what 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 + " 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 + " 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 + " 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 + " 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 " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings , " + pizza_ham + " ham toppings "; } public string getdescription(){ return pizza_size + " pizza " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings , " + 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; (int = 0; < 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 = 0; < index; i++) { s= (s + pizza[i].tostring()); } return s; } public double calctotal(){ double r = 0.0; for(int = 0; < 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 = 0; < 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 program system.out.println("test: total order 4 is: " + order4.calctotal()); system.out.println(); system.out.println("test: order1 equal order2?"); system.out.println(order1.equals(order2)); system.out.println(); system.out.println("test: order2 equal order4?"); system.out.println(order2.equals(order4)); system.out.println(); }//end of main class }//end of v4_project_15_page_418 class
you comparing strings using ==
, compares instance identity, not equality. should use string.equals
instead.
alternatively, can use 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 using enum represent pizza size:
enum size { small, medium, large }
string not choice, because not can represent size different case (you have case-insensitive comparison , lower-casing throughout code), passed anything else. if limit inputs these few sizes, code gets lot simpler. and, can compare enum values using ==
.
Comments
Post a Comment