Skip to content

Coffee Vending Machine

The goal of this lab is to write code that will provide the price for a cup of coffee, similar to the MealOrder code we wrote in class. For reference, that code is here.

The coffee order has the following options with pricing:

Category Choices Price (cents)
Size Small 100
Medium 150
Large 200
Creamer None 0
Milk 25
Half-n-half 35

A. Create CoffeeOrder Class

Create a new, empty class CoffeeOrder under the src/main/java/com/welltestedlearning/cvm directory.

  1. Open up the Project Tool Window on the left side, press Command + 1.
  2. Right-click (Ctrl + Click) on the cvm directory and choose New > and then Java Class.
  3. Name it CoffeeOrder

B. Write the Test First

  1. Create a new test class called CoffeeOrderTest under the test directory: src/test/java/com/welltestedlearning/cvm

    1. Right-click on the cvm underneath test and choose New > and then Java Class.
    2. Name it CoffeeOrderTest
  2. Add the following import statements above the class name:

    import org.junit.Test;
    
    import static org.assertj.core.api.Assertions.assertThat;
    
  3. Create a test method smallCoffeeIs100 and write test code that asserts a new CoffeeOrder with a "small" coffee has a price is 100. It should look something like this:

    @Test
    public void smallCoffeeIs100() throws Exception {
    
    }
    
  4. Inside the test method:

    • Instantiate the CoffeeOrder class (this is the "given" part)
    • Call a method named coffeeSize() and pass in "small" -- you'll need to create this method in the class, but do not implement any code yet.
    • Now assert that calling price() returns 100

C. Test Fail and Pass

  1. Run the test (right-click on the test and run it) and it should fail.

  2. Write the minimum amount of code in CoffeeOrder to make the test pass.

D. Create More Size Tests

  1. Create tests for the other size options (Medium and Large), writing the test first and then writing the minimum amount of code to make each test pass.

    • Make sure to use the Price List at the top of this lab.
  2. Create a test for an "empty" Coffee order (just a new order where you haven't specified a size) that should have a price of 0.

  3. Run all of the tests in CoffeeOrderTest and don't move until until they all pass.

Note

You should have at least 4 test methods, one for each size and one for an empty order.


Once you've completed the above steps, do not move on until you have checked in with the instructor.


E. Support Creamer

  1. Add tests for Creamer options and then write the code to support that. For example:

    @Test
    public void milkCreamerIs25() throws Exception {
      CoffeeOrder coffeeOrder = new CoffeeOrder();
    
      coffeeOrder.creamer("milk");
    
      assertThat(coffeeOrder.price())
          .isEqualTo(25);
    }
    
  2. Make sure you have tests for the other creamer options: none and half-n-half.

F. Support Size + Creamer

Write tests that exercise having both the Size and Creamer options set, e.g., a coffee order with size Medium and Milk must return 175.


Once you've completed the above steps, check in with the instructor.