MealOrder Tests & Code¶
Do not copy-n-paste this code, it is for reference only
MealOrderTest.java¶
This is the test class for the MealOrder
code.
package com.spiralburger.meal;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MealOrderTest {
@Test
public void emptyMealOrderPriceIsZero() throws Exception {
MealOrder mealOrder = new MealOrder();
assertThat(mealOrder.price())
.isZero();
}
@Test
public void mealWithRegularBurgerCosts5() throws Exception {
MealOrder mealOrder = new MealOrder();
mealOrder.burgerType("regular");
int price = mealOrder.price();
assertThat(price)
.isEqualTo(5);
}
@Test
public void mealWithCheeseBurgerCosts6() throws Exception {
MealOrder mealOrder = new MealOrder();
mealOrder.burgerType("cheese");
int price = mealOrder.price();
assertThat(price)
.isEqualTo(6);
}
@Test
public void mealWithRegularSizeDrinkCosts2() throws Exception {
MealOrder mealOrder = new MealOrder();
mealOrder.drinkSize("regular");
assertThat(mealOrder.price())
.isEqualTo(2);
}
@Test
public void mealOrderWithLargeSizeDrinkCosts3() throws Exception {
MealOrder mealOrder = new MealOrder();
mealOrder.drinkSize("large");
assertThat(mealOrder.price())
.isEqualTo(3);
}
@Test
public void mealWithRegularBurgerAndRegularDrinkCosts7() throws Exception {
MealOrder mealOrder = new MealOrder();
mealOrder.burgerType("regular");
mealOrder.drinkSize("regular");
assertThat(mealOrder.price())
.isEqualTo(7);
}
}
MealOrder.java:¶
This is the "production" code:
package com.spiralburger.meal;
public class MealOrder {
// instance variables: initialized to empty strings
private String burgerType = "";
private String drinkSize = "";
public int price() {
return burgerPrice() + drinkPrice();
}
public void burgerType(String burger) {
burgerType = burger;
}
public void drinkSize(String size) {
drinkSize = size;
}
private int burgerPrice() {
int price = 0;
if (burgerType.equals("regular")) {
price = 5;
} else if (burgerType.equals("cheese")) {
price = 6;
}
return price;
}
private int drinkPrice() {
if (drinkSize.equals("regular")) {
return 2; // can just do a "direct return"
} else if (drinkSize.equals("large")) {
return 3;
}
return 0; // for no match
}
}