Skip to content

Lab 10 - Would You Like a Drink With That?

Add a Drink to Your Burger

In order to support adding a Drink to our meal order, we'll need to add code to the MealBuilder class to have that capability. Once we have that, we need to add that property to the request object, and have the Controller method process it.

Add Drink Support to MealBuilder

Starting with the test first, go to MealBuilderTest and add the following test:

@Test
public void orderWithBurgerNoneRegularDrinkCosts6() throws Exception {
  MealBuilder mealBuilder = new MealBuilder();
  mealBuilder.addBurgerString("none");
  mealBuilder.withDrink("regular");

  // build the meal order

  // assert that the price is 6
}

In order to make this test pass, you will have to:

  1. Add the withDrink method to MealBuilder and store the size in a String instance variable called drinkSize

  2. Inside the build() method, add code to add the selected drink size to the meal order. This should be your build() method, so add the drink where specified below:

    public MealOrder build() {
      Toppings toppings = parseToppings(burgerOrder);
    
      Burger burger = new Burger(toppings);
    
      MealOrder mealOrder = new MealOrder();
      mealOrder.addItem(burger);  
    
    **Add the drink to the mealOrder HERE**     
    
      return mealOrder;
    }      
    

    Warning

    You will have to handle the case for tests that didn't specify a drink!

    Do NOT change the tests to "fix" this problem.

  3. Make sure the MealBuilderTest tests pass

  4. Make sure all tests pass before continuing.


Add Drink to API Controller

Run All Tests

Make sure all of your tests pass before getting started.

Add Drink to MealOrderRequest

  1. Add the drink property to the MealOrderRequest class. See above for a reminder about JavaBean properties.

  2. Add support in the mealOrder method in the MealOrderApiController class so that if a drink is specified, it's added to the meal order.

Try it Out

Use curl:

curl -v -d '{"burger": "bacon", "drinkSize": "large"}' -H 'Content-Type: application/json' localhost:8080/api/mealorder

Or Postman, with the JSON:

{"burger": "bacon", "drinkSize": "large"}