メインクラスの中のOrderをアウトプットしようとしたのですが、うまくいきません。
なせ出力されないのでしょうか。
Pizzaクラス
package Pra3_3; import java.util.Scanner; // Class name should be "Pizza" though.... public class Pizza { private String size; private int cheese; private int pepperoni; private int ham; public int cost; public Pizza() {} public Pizza (String size, int cheese, int pepperoni, int ham) { this.size = size; this.cheese = cheese; this.pepperoni = pepperoni; this.ham = ham; } /** * @return the size */ public String getSize() { return size; } /** * @param size the size to set */ public void setSize(String size) { this.size = size; } /** * @return the cheese */ public int getCheese() { return cheese; } /** * @param cheese the cheese to set */ public void setCheese(int cheese) { this.cheese = cheese; } /** * @return the pepperoni */ public int getPepperoni() { return pepperoni; } /** * @param pepperoni the pepperoni to set */ public void setPepperoni(int pepperoni) { this.pepperoni = pepperoni; } /** * @return the ham */ public int getHam() { return ham; } /** * @param ham the ham to set */ public void setHam(int ham) { this.ham = ham; } public double calcCost() { int pizzaSize; int toppings; if ((getSize() == "small") && ((getCheese() >= 0) || (getPepperoni() >= 0) || (getHam() >= 0))) { pizzaSize = 10; toppings = 2 * getCheese() + 2 * getPepperoni() + 2 * getHam(); cost = pizzaSize + toppings; } else if((getSize() == "medium") && ((getCheese() > 0) || (getPepperoni() > 0) || (getHam() > 0))) { pizzaSize = 12; toppings = getCheese() * 2 + getPepperoni() * 2 + getHam() * 2; cost = pizzaSize + toppings; } else if((getSize() == "large") && ((getCheese() >= 0) || (getPepperoni() >= 0) || (getHam() >= 0))) { pizzaSize = 14; toppings = 2 * getCheese() + 2 * getPepperoni() + 2 * getHam(); cost = pizzaSize + toppings; } return cost; } public String getDescription() { return "Pizza size: " + getSize() + " Topping: " + (getCheese() + getPepperoni() + getHam()) + " Total cost is: " + calcCost(); } }
PizzaOrderクラス
package Pra3_4; public class PizzaOrder { private Pizza[] pizzas = new Pizza[3]; /* public PizzaOrder(Pizza[] pizzas) { this.pizzas = pizzas; } */ public void setPizzas(Pizza newPizza, int index) { pizzas[index] = newPizza; } public Pizza[] getPizzas() { return pizzas; } public double calcTotal() { double total = 0.0; for(int i = 0; i < pizzas.length; i++) { total += pizzas[i].calcCost(); } return total; } /* public int getNumPizzas() { } */ }
Mainクラス
package Pra3_4; import java.util.Arrays; public class Main { public static void main(String[] args) { Pizza pizza1 = new Pizza("large", 1, 0, 1); Pizza pizza2 = new Pizza("medium", 2, 2, 0); PizzaOrder order = new PizzaOrder(); order.setPizzas(pizza1,2); order.setPizzas(pizza2,0); //System.out.println(Arrays.toString(order[0].getPizzas())); for(int i = 0; i < order.getPizzas().length; i++) { System.out.println(order); } //double total = order.calcTotal(); //System.out.println(Arrays.toString(order)); //double total = order.calcTotal(); //System.out.printf("Pizza Order total cost $%.2f\n", total); } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/06 04:29
2021/04/06 04:33
2021/04/06 04:47
2021/04/06 04:57
2021/04/06 05:55
2021/04/06 06:37
2021/04/06 06:51
2021/04/06 07:34
2021/04/06 07:53 編集
2021/04/06 08:06
2021/04/06 08:29 編集
2021/04/06 09:43