【问题标题】:Discount method for pizza program比萨计划的折扣方法
【发布时间】:2015-11-09 00:49:56
【问题描述】:

我在为我的 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);
    }
}

【问题讨论】:

  • 请发布更多代码 - 我们需要知道变量smallPizzamediumPizzalargePizza 的类型以及valueOf 的方法。您收到编译时错误的那一行也会有所帮助。对了,第二种方法是void,它什么都不返回,甚至null也不返回。
  • smallPizza = smallPizza.valueOf(8 + 1 * numberToppings); 这不是一个好的设计。您将比萨饼的数量(我假设)转换为成本,因此您丢失了数量。
  • 我已经发布了其余的代码,是的,valueOf 将比萨饼的数量转换为成本。感谢您的反馈,您会建议什么来代替转换?

标签: java methods value-of


【解决方案1】:

让我们将此问题拆分为多个问题,以使其更易于管理。

首先,我们创建一个抽象类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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多