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:
-
Add the
withDrink
method toMealBuilder
and store the size in aString
instance variable calleddrinkSize
-
Inside the
build()
method, add code to add the selected drink size to the meal order. This should be yourbuild()
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.
-
Make sure the
MealBuilderTest
tests pass -
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¶
-
Add the
drink
property to theMealOrderRequest
class. See above for a reminder about JavaBean properties. -
Add support in the
mealOrder
method in theMealOrderApiController
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"}