Skip to content

Quantity Discount

We want to offer a discount based on the number of options added to the coffee order.

But first, let's leverage the Enum to make it easier to calculate the price of the SizeOption.

  • Modify your Size class to use SizeOption for the price by creating a method called cost() that returns the private variable cost, where the values are defined in the call to the constructor. For an Enum example, see this class:
public enum Priority {  
  HIGH(3),
  MEDIUM(2),
  LOW(1);

  private final int code;

  Priority(int code) {
    this.code = code;
  }

  public int code() {
    return code;
  }
}

Once you've updated your Size.price() method to use the SizeOption's cost() method, ensure all tests continue to pass.


The Discounts

Creamer and Sweetener will have quantity discounts, but the quantity that triggers the discount is different for Creamer vs. Sweetener.

Creamer Discount

If 2 (two) or more Creamers are added, then we want a discount of 15 cents. For example, 2 Milk options normally costs 50 cents, but with the discount, should cost 35 cents (50 - 15). Adding only 1 Milk (or Half-n-Half) would not be discounted.

Sweetener Discount:

If 3 (three) or more Sweeteners are added, then the discount is 5 cents. For example, 3 Sugars normally costs 30 cents, but with the discount, it should cost 25 cents (30 - 5). Adding only one or two sweeteners would not be discounted.

Failing Creamer Test

Here's a failing Creamer test that you can start with.

  @Test
  public void twoMilksShouldBeDiscountedTo35Cents() throws Exception {
    Creamer creamer = new Creamer("milk", 2);

    assertThat(creamer.price())
        .isEqualTo(35);
  }

Make it pass, then make sure all other tests pass.

Failing Sweetener Test

Remember, for Sweetener, you need quantity of 3 to get the discount.

  @Test
  public void threeSugarsShouldBeDiscountedTo25Cents() throws Exception {
    Sweetener sugar = new Sweetener("sugar", 3);

    assertThat(sugar.price())
        .isEqualTo(25);
  }

The above test should fail, so make it pass, but also make sure all other existing tests pass!


Once you've completed the above steps, check in with the instructor to review your code.