Object-Oriented MealOrder Code¶
Do not copy-n-paste this code, for reference only
MealOrder.java
import java.util.ArrayList;
import java.util.List;
public class MealOrder {
List<MenuItem> items = new ArrayList<>();
public MealOrder() {
}
public void addBurger(String burgerType) {
items.add(new Burger(burgerType));
}
public void addDrink(String drinkType) {
items.add(new Drink(drinkType));
}
public MealOrder(String burgerType) {
addBurger(burgerType);
}
public MealOrder(String burgerType, String drinkSize) {
addBurger(burgerType);
addDrink(drinkSize);
}
public int price() {
// loop over all items and sum their price
int price = 0;
for (MenuItem item: items) {
price = price + item.price();
}
return price;
}
}
Burger.java
public class Burger extends MenuItem {
public static final String BURGER_CHEESE = "cheeseburger";
public static final String BURGER_REGULAR = "regular";
private String burgerType = "";
public Burger(String newBurgerType) {
burgerType = newBurgerType;
}
@Override
public int price() {
if (burgerType.equals(BURGER_REGULAR)) {
return 5;
} else if (burgerType.equals(BURGER_CHEESE)) {
return 6;
}
return 0;
}
}
Drink.java
public class Drink extends MenuItem {
public static final String DRINK_REGULAR = "regular";
public static final String DRINK_LARGE = "large";
private String drinkSize = ""; // regular or large
public Drink() {
}
public Drink(String theDrinkSize) {
drinkSize = theDrinkSize;
}
@Override
public int price() {
if (drinkSize.equals(DRINK_REGULAR)) {
return 1;
} else if (drinkSize.equals(DRINK_LARGE)) {
return 2;
}
return 0;
}
}
MenuItem.java
public class MenuItem {
public int price() {
return 0;
}
}