Skip to content

MealOrder Code

Do not copy-n-paste this code, for reference only

MealOrder.java:

public class MealOrder {

  String burgerType = ""; // regular or cheeseburger
  String drinkSize = ""; // regular or large

  public static final String BURGER_CHEESE = "cheeseburger";
  public static final String BURGER_REGULAR = "regular";

  public int price() {
    return calcBurgerPrice() + calcDrinkPrice();
  }

  private int calcBurgerPrice() {
    if (burgerType.equals(BURGER_REGULAR)) {
      return 5;
    } else if (burgerType.equals("cheeseburger")) {
      return 6;
    }
    return 0;
  }

  private int calcDrinkPrice() {
    if (drinkSize.equals("regular")) {
      return 1;
    } else if (drinkSize.equals("large")) {
      return 2;
    }
    return 0;
  }
}

MealOrderTest.java

import org.junit.Test;

import static org.junit.Assert.*;

public class MealOrderTest {

  @Test
  public void mealWithRegularBurgerReturns5() throws Exception {
    // given a new meal order with a regular burger
    MealOrder mealOrder = new MealOrder();
    mealOrder.burgerType = MealOrder.BURGER_REGULAR;

    // when I ask for the price
    // then I expect it to be 5
    assertThat(mealOrder.price())
              .isEqualTo(5);
  }

  @Test
  public void mealWithCheeseburgerReturns6() throws Exception {
    MealOrder mealOrder = new MealOrder();
    mealOrder.burgerType = MealOrder.BURGER_CHEESE;
    assertThat(mealOrder.price())
              .isEqualTo(6);
  }

  @Test
  public void drinkSizeOfRegularReturns1() throws Exception {
    MealOrder mealOrder = new MealOrder();
    mealOrder.drinkSize = "regular";
    assertThat(mealOrder.price())
              .isEqualTo(1);
  }

  @Test
  public void drinkSizeOfLargeReturns2() throws Exception {
    MealOrder mealOrder = new MealOrder();
    mealOrder.drinkSize = "large";
    assertThat(mealOrder.price())
              .isEqualTo(2);
  }
}