我想弄清楚为什么我的toString()函数只会在添加另一个披萨时才能正常工作。这是第一个创建的披萨的输出,这是预期的并且运作良好
$ kitchen = new Kitchen()
$ pizza = new Pizza()
$ pizza.toString()
-> "no crust pizza with no toppings and no sauce: $0.00"
$ pizza.ingredients.add(kitchen.ingredients.get(1))
-> true
$ pizza.toString()
-> "Thick crust pizza with no toppings and no sauce: $3.50"
$ pizza.ingredients.add(kitchen.ingredients.get(2))
-> true
$ pizza.toString()
-> "Thick crust pizza with no toppings and Tomato sauce: $4.50"
$ pizza.ingredients.add(kitchen.ingredients.get(5))
-> true
$ pizza.toString()
-> "Thick crust pizza with Olives and Tomato sauce: $6.00"
$ pizza.ingredients.add(kitchen.ingredients.get(7))
-> true
$ pizza.toString()
-> "Thick crust pizza with Olives, Beef and Tomato sauce: $8.75"
$ pizza.ingredients.add(kitchen.ingredients.get(4))
-> true
$ pizza.toString()
-> "Thick crust pizza with Olives, Beef, Capsicum and Tomato sauce:
但另一个披萨的输出如下
EXPECTED OUTPUT Current output
Creating new pizza Creating new pizza
Ingredient(s): Thin Ingredient(s): Thin
Thin crust pizza with no toppings and no sauce: $3.00 <
Ingredient(s): Tomato Ingredient(s): Tomato
Thin crust pizza with no toppings and Tomato sauce: $4.00 <
Ingredient(s): Jalapenos Ingredient(s): Jalapenos
Thin crust pizza with Jalapenos and Tomato sauce: $5.00 <
Ingredient(s): Beef Ingredient(s): Beef
Thin crust pizza with Jalapenos, Beef and Tomato sauce: $7.75 <
=============================================== ==========================编辑
在分析我的代码之后,似乎没有修改Pizza类中的链接列表成分,因此问题将出现在我的测试方法中。
public void testing(Ingredient ingredient1){
for (Ingredient ingredient : ingredients){
if (ingredient1.ingtoString().equals(ingredient)){
ingredients.add(ingredient1);
}
}
}
这个方法由我的Kitchen类调用,就像这样。
public void nameMatch(String match){
Pizza pizza = new Pizza();
for (Ingredient ingredient : ingredients){
if (match.equals(ingredient.ingtoString())){
pizza.testing(ingredient);
}
}
pizza.toString();
}
最后,这就是用于创建披萨并调用这些方法的内容
public void create() {
System.out.println("Creating new pizza");
String ingredient1 = "";
while (!ingredient1.equals(".")){
System.out.print("Ingredient(s): ");
ingredient1 = (In.nextLine());
kitchen.nameMatch(ingredient1);
}
System.out.println("ORDER SUMMARY");
kitchen.nameMatch(ingredient1);
}
快速记录所有这些方法都在3个单独的类中
答案 0 :(得分:0)
每当您致电nameMatch
时,您都会创建一个新披萨,将该成分添加到 披萨中,然后将披萨扔掉。我想你想要制作一个披萨及其所有成分。
另外,nameMatch
实际上并没有打印任何东西。 pizza.toString()
会将披萨转换为字符串,但如果您想打印该字符串,则需要拨打System.out.println
- 例如System.out.println(pizza.toString());