1

I am having trouble creating a mutator method for my Java class and I'm looking for a little help. These are the instructions for the method;

 * 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  

Where I'm running into trouble is adding the $2 discount to each pizza. So far I've tried this but I get an error message saying bad operand types for binary operator.

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;
}

I've also tried this code, which complies but returns 'null' instead of the cost of the pizza plus the discount:

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);

}

Any suggestions?

Here is the full code:

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);
    }
}
aaaidan
  • 7,093
  • 8
  • 66
  • 102
AlephZed
  • 31
  • 4
  • 1
    Please post more code - we need to know the types of variables `smallPizza`, `mediumPizza`, `largePizza` and what method `valueOf` does. The line on which you are getting compile-time error would also help. By the way, the second method is `void`, it does not return anything, not even `null`. – Jaroslaw Pawlak Nov 09 '15 at 00:54
  • 1
    `smallPizza = smallPizza.valueOf(8 + 1 * numberToppings);` That can't be a good design. Your converting the number of pizzas (I assume) to a cost, and so you've lost the number. – John3136 Nov 09 '15 at 00:57
  • 1
    I've posted the rest of the code and yes, the valueOf converts the number of pizzas to a cost. Thank you for your feedback, what would you suggest instead of the conversion? – AlephZed Nov 09 '15 at 01:59

1 Answers1

0

Let's split this problem into multiple ones to make it more manageable.

First, let's make an abstract class 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);
    }
}

Then let's make a class per size of Pizza you offer, for example I'll define the SmallPizza.

public class SmallPizza extends Pizza {
    sizeName =  "Small";
    basePrice = 8;
    toppingPrice = 1;
}

Now, to make an order, we can make a List of pizzas. This is important, because we can calculate the price of each individual pizza, and tally the total.

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.

We can get the order total with:

public int getTotal(List<Pizza> order) {
    int total =  0;
    for(Pizza pizza : order) { //iterate over every Pizza in order.
        total += pizza.getPrice();
    }
    return total;
}

Displaying a receipt might look something like this:

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;
}

Then to test, we can:

System.out.println(getReceipt());
MeetTitan
  • 3,383
  • 1
  • 13
  • 26