I have an exercise requires me to write a service to management menu.
The requirement like this: Food menu includes: breakfast menu, lunch menu, dinner menu. The three menu haves list menu item( name, price, description, image). Write CRUD operation for food menu.
At first, I separate completely three types of food menu to each service and write service for each food menu:
My model ( at first):
Food Menu model:
public class FoodMenu{
private BreakfastMenu breakfastMenu;
private DinnerMenu dinnerMenu;
private LunchMenu lunchMenu;
public FoodMenu(BreakfastMenu breakfastMenu, DinnerMenu dinnerMenu, LunchMenu lunchMenu){
this.breakfastMenu = breakfastMenu;
this.dinnerMenu = dinnerMenu;
this.lunchMenu = lunchMenu;
}
}
Breakfast Menu model:
public class BreakfastMenu {
private List<MenuItem> menuItemList;
public BreakfastMenu(List<MenuItem> menuItemList){
this.menuItemList = menuItemList;
}
}
Dinner Menu model:
public class DinnerMenu {
public DinnerMenu(){}
private List<MenuItem> menuItemList;
public DinnerMenu(List<MenuItem> menuItemList){
this.menuItemList = menuItemList;
}
}
Lunch Menu model:
public class LunchMenu {
private List<MenuItem> menuItemList;
public LunchMenu(List<MenuItem> menuItemList){
this.menuItemList = menuItemList;
}
public LunchMenu(){}
}
My Service( at first): Breakfast Menu services ( another two service Lunch and Dinner Menu are the same they only different in parameters):
public class BreakfastMenuServicesImpl implements BreakfastMenuServices {
@Override
public BreakfastMenu addMenuItemsToBreakfastMenu(BreakfastMenu breakfastMenu) {
Scanner scanner = new Scanner(System.in);
MenuItem menuItem = new MenuItem();
List<MenuItem> menuItemList= breakfastMenu.getMenuItemList();
if(menuItemList == null){
menuItemList = new ArrayList<MenuItem>();
}
System.out.print("\nInsert food name: ");
menuItem.setNames(scanner.nextLine());
System.out.print("Insert food description: ");
menuItem.setDescription(scanner.nextLine());
System.out.print("Insert food image: ");
menuItem.setImage(scanner.nextLine());
System.out.print("Insert food price:");
menuItem.setPrice(scanner.nextFloat());
menuItemList.add(menuItem);
breakfastMenu.setMenuItemList(menuItemList);
return breakfastMenu;
}
@Override
public void showBreakFastMenu(BreakfastMenu breakfastMenu) {
System.out.println("\nBreakfast menu:");
breakfastMenu.getMenuItemList().forEach(System.out::println);
}
@Override
public void updateBreakfastMenu(BreakfastMenu breakfastMenu) {
Scanner scanner = new Scanner(System.in);
System.out.print("Insert food want to update:");
String foodItem = scanner.nextLine();
breakfastMenu.getMenuItemList().forEach((MenuItem menuItem)->{
if(menuItem.getNames().equals(foodItem)){
System.out.print("\nInsert food name: ");
menuItem.setNames(scanner.nextLine());
System.out.print("Insert food description: ");
menuItem.setDescription(scanner.nextLine());
System.out.print("Insert food image: ");
menuItem.setImage(scanner.nextLine());
System.out.print("Insert food price:");
menuItem.setPrice(scanner.nextFloat());
}
});
}
@Override
public void deleteBreakfastMenu(BreakfastMenu breakfastMenu) {
Scanner scanner = new Scanner(System.in);
System.out.print("Insert food want to delete:");
String foodItem = scanner.nextLine();
breakfastMenu.getMenuItemList().removeIf(menuItem ->
menuItem.getNames().equals(foodItem));
}
}
After finishing service, I realize that all the services share the same CRUD process and I can write one father class for model and one father service class for three types of food menu services:
My new model ( after): Food Menu model:
public class FoodMenu{
private BreakfastMenu breakfastMenu;
private DinnerMenu dinnerMenu;
private LunchMenu lunchMenu;
public FoodMenu(BreakfastMenu breakfastMenu, DinnerMenu dinnerMenu, LunchMenu lunchMenu){
this.breakfastMenu = breakfastMenu;
this.dinnerMenu = dinnerMenu;
this.lunchMenu = lunchMenu;
}
}
AbstractMenu model:
public class AbstractMenu {
private List<MenuItem> menuItemList;
public AbstractMenu(List<MenuItem> menuItemList){
this.menuItemList = menuItemList;
}
public AbstractMenu(){
}
}
Breakfast Menu model:
public class BreakfastMenu extends AbstractMenu{
}
Lunch Menu model:
public class LunchMenu extends AbstractMenu{
}
Dinner Menu model:
public class DinnerDMenu extends AbstractMenu{
}
My new Service(After)
AbstractMenuService:
public interface AbstractMenuService {
AbstractMenu addMenuItemsToMenu(AbstractMenu abstractMenu);
void showMenu(AbstractMenu abstractMenu);
void updateMenu(AbstractMenu abstractMenu);
void deleteMenu(AbstractMenu abstractMenu);
}
BreakfastMenuService:
public interface BreakfastMenuServices extends AbstractMenuService{
}
DinnerMenuService:
public interface DinnerMenuServices extends AbstractMenuService{
}
LunchMenuSerivce:
public interface LunchServices extends AbstractMenuService{
}
So which one is easier to read and maintain, furthermore, as a result of extending service from father class, all methods in BreakfastMenuImpl requires the override but I don't need to write different methods so I left it's blank. Does this have any negative effects?
My new BreakfastMenuServiceImpl:
public class BreakfastMenuServicesImpl implements BreakfastMenuServices {
@Override
public AbstractMenu addMenuItemsToMenu(AbstractMenu abstractMenu) {
return null;
}
@Override
public void showMenu(AbstractMenu abstractMenu) {
}
@Override
public void updateMenu(AbstractMenu abstractMenu) {
}
@Override
public void deleteMenu(AbstractMenu abstractMenu) {
}
}