我在为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);
}
}
答案 0 :(得分:0)
让我们将此问题分成多个问题,以使其更易于管理。
首先,让我们制作一个抽象类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());