所以我有一个任务,并且我已经为它创建了大部分代码,但我无法弄清楚如何为浇头添加价格。这不是我的主要代码,这是针对比萨尺寸的价格,但我还需要在浇头中添加更多价格。我需要添加两个单独的价格,一个用于中型比萨和一个用于大比萨。将价格添加到浇头
package assignment1;
public class Pizza {
private int diam;
private int numOfPizza;
private double price;
private String toppings;
Pizza(int size, String input) {
diam = size;
toppings = input;
}
public int getDiameter(){
return diam;
}
public int getPizzaCount(){
return numOfPizza;
}
public double getPrice(){
return price;
}
public String getToppings(){
return toppings;
}
public void setDiameter(int size){
if (size == 12)
diam = 12;
else if (size == 16)
diam = 16;
else
diam = 0;
}
public void setPizzaCount(int pizzaCount){
numOfPizza = pizzaCount;
}
public void setPrice(double total){
price = total;
}
public void setToppings(String input){
if ("Ham".equalsIgnoreCase(input))
toppings = "Ham";
else if ("Mozarella".equalsIgnoreCase(input))
toppings = "Mozarella" ;
else if ("Olives".equalsIgnoreCase(input))
toppings = "Olives";
else if ("Pineapple".equalsIgnoreCase(input))
toppings = "Pineapple";
else if ("Spinach".equalsIgnoreCase(input))
toppings = "Spinach";
else
toppings = "Plain";
}
private double calculatePrice(int size, String input){
int total;
if(!(toppings).equalsIgnoreCase("plain")) {
total = 1;
} else {
total = 0;
}
if(size == 12) {
total += 4.00;
}
else if(size == 16) {
total += 5.00;
}
return total;
}
public String toString(){
String pizzaString ="You have ordered a "+diam + " inch pizza with
"+toppings +" toppings and a price of £"+ calculatePrice(diam, toppings);
return pizzaString;
}
}
在'calculatePrice'中,您应该在'setToppings'方法中重复相同的'if-elseif',根据顶部名称添加不同的价格。你也可以使用'switch',这样读起来更清晰一些。您也可以使用顶部名称作为键和价格作为价值的地图,但'switch'就好了。啊,顺便说一句,它是Mo ** zz ** arella :) – BackSlash
但对于本网站更重要 - 如果你发布了一个问题,你应该询问一个实际的问题。您刚刚发布了代码和广泛的要求,并且不符合本网站的质量标准。请仔细阅读[帮助]中的“如何提问”部分,详细了解本网站的工作方式以及如何提问以获得赞赏和答案。 –
感谢您的帮助 –