我在为Java类创建mutator方法时遇到麻烦,并且正在寻找一些帮助。这些是该方法的说明。
* Mutator method that calculates the cost of a pizza.
* Small pizza = $8, toppings = $1/each
* Medium pizza = $10, toppings = $2/each
* Large pizza = $12, toppings = $3/each
* 4 or more toppings = $2 discount regardless of pizza size
我遇到麻烦的地方是给每个披萨加2美元的折扣。到目前为止,我已经尝试过此操作,但收到一条错误消息,指出二进制运算符的操作数类型错误。
public void setCost()
{
smallPizza = smallPizza.valueOf(8 + 1 * numberToppings);
mediumPizza = mediumPizza.valueOf(10 + 2 * numberToppings);
largePizza = largePizza.valueOf(12 + 3 * numberToppings);
if(numberToppings >= 4) {
smallPizza = smallPizza - 2;
mediumPizza = mediumPizza - 2;
largePizza = largePizza - 2;
}
我也尝试过此代码,该代码合规但返回“ null”,而不是披萨的价格加上折扣:
public void setCost()
{
smallPizza = smallPizza.valueOf(8 + 1 * numberToppings);
mediumPizza = mediumPizza.valueOf(10 + 2 * numberToppings);
largePizza = largePizza.valueOf(12 + 3 * numberToppings);
if(numberToppings >= 4) {
smallPizza = smallPizza.valueOf(6 + 1 * numberToppings);
mediumPizza = mediumPizza.valueOf(8 + 2 * numberToppings);
largePizza = largePizza.valueOf(10 + 3 * numberToppings);
}
有什么建议么?
这是完整的代码:
public class Pizza
{
private String customerName;
private String pizzaSize;
private int numberToppings;
private int pizzaCost;
private String smallPizza;
private String mediumPizza;
private String largePizza;
/**
* Constructor for objects of class Pizza
*/
public Pizza(String CustomerName, String PizzaSize, int NumberToppings)
{
customerName = CustomerName;
numberToppings = NumberToppings;
pizzaSize = PizzaSize;
}
/**
* Constructor for selecting pizza size.
*/
public void pizzaSize(String small, String medium, String large)
{
smallPizza = small;
mediumPizza = medium;
largePizza = large;
}
/**
* Accessor method that returns the name of an order
*/
public String getName()
{
return customerName;
}
/**
* Mutator method that calculates the cost of a pizza.
* Small pizza = $8, toppings = $1/each
* Medium pizza = $10, toppings = $2/each
* Large pizza = $12, toppings = $3/each
* 4 or more toppings = $2 discount regardless of pizza size
*/
public void setCost()
{
smallPizza = smallPizza.valueOf(8 + 1 * numberToppings);
mediumPizza = mediumPizza.valueOf(10 + 2 * numberToppings);
largePizza = largePizza.valueOf(12 + 3 * numberToppings);
if(numberToppings >= 4) {
smallPizza = smallPizza.valueOf(6 + 1 * numberToppings);
mediumPizza = mediumPizza.valueOf(8 + 2 * numberToppings);
largePizza = largePizza.valueOf(10 + 3 * numberToppings);
}
}
/**
* Accessor method that returns the cost of a pizza.
*/
public void getCost()
{
System.out.println(smallPizza);
System.out.println(mediumPizza);
System.out.println(largePizza);
}
}
最佳答案
让我们将此问题分解为多个问题,使其更易于管理。
首先,让我们创建一个抽象类Pizza
。
public abstract class Pizza {
protected String sizeName;
protected int basePrice;
protected int toppingPrice;
protected int toppingCount;
protected Pizza(int toppingCount) {
this.toppingCount = toppingCount;
}
protected int applyApplicableDiscounts(int price) {
if(toppingCount >= 4)
price -= 2;
return price;
}
protected int getPrice() {
return applyApplicableDiscounts(basePrice)+(toppingCount*toppingPrice);
}
}
然后让我们根据您提供的披萨大小来分类,例如,我将定义
SmallPizza
。public class SmallPizza extends Pizza {
sizeName = "Small";
basePrice = 8;
toppingPrice = 1;
}
现在,要下订单,我们可以制作
List
披萨。这很重要,因为我们可以计算每个披萨的价格,并计算总和。final List<Pizza> order = new ArrayList<>();
order.add(new SmallPizza(2)); //hypothetical order of a small pizza with 2 toppings. We can add any type of pizza to this list.
我们可以通过以下方式获得订单总数:
public int getTotal(List<Pizza> order) {
int total = 0;
for(Pizza pizza : order) { //iterate over every Pizza in order.
total += pizza.getPrice();
}
return total;
}
显示收据可能看起来像这样:
public int receiptCharWidth = 30;
public String getReceipt(List<Pizza> order) {
String receipt = "";
for(Pizza Pizza : order) {
String pizzaString = String.format("%s %d topping:", pizza.sizeName, pizza.toppingCount);
String priceString = String.format("$%d.00", pizza.getPrice());
receipt += pizzaString;
for(int i = 0; i < receiptCharWidth-(pizzaString.length()+priceString.length())) { //insert spacing equal to the difference of String lengths minus receipt length
receipt += ' ';
}
receipt += ' \n';
for(int i = 0; i < receiptCharWidth; i++) { //insert dashes equal to receiptCharWidth
receipt += '-';
}
receipt += priceString;
}
return receipt;
}
然后进行测试,我们可以:
System.out.println(getReceipt());