1
\$\begingroup\$

I am writing the Restaurant Management homework which must be following the MVC pattern. My Menu model has two parts: Drink menu and Food menu.

With Food menu, it separates into three different types of menu: Breakfast, Lunch and Dinner menu. About the Drink menu, it also divide into two different menu: Alcohol and Soft drink menu.

Each menu has the same CRUD operation therefore, I wrote one father class and extend five models from it, therefore, I only need to write one method for father class and change the parameter for methods and they will work the same way.

However, I don't know that my structure has been following the MVC structure or not? Do I need to write Menu controller class where the Menu Services in main class has already handle the logic? Thank for your support !!!

My father models :

public class DailyMenu implements Serializable {
        private List<MenuItem> menuItemList;
        public DailyMenu(List<MenuItem> menuItemList){
            this.menuItemList = menuItemList;
        }
        public DailyMenu(){
            this.menuItemList = new ArrayList<>();
        }
        public List<MenuItem> getMenuItemList() {
            return menuItemList;
        }
    
        public void setMenuItemList(List<MenuItem> menuItemList) {
            this.menuItemList = menuItemList;
        }
    }

Menu View:

 public class MenuView {
        private Scanner scanner = new Scanner(System.in);
        private DailyMenuServices dailyMenuServices = new DailyMenuServicesImpl();
    
    
        public DailyMenu getMenuTypes(Menu menu){;
            menu();
            int option = Integer.parseInt(scanner.nextLine());
            MenuTypes menuTypes = MenuTypes.get(option-1);
            switch (menuTypes){
                case FOODMENU -> {return getFoodMenuTypes(menu.getFoodMenu());}
                case DRINKMENU -> {return getDrinkMenuTypes(menu.getDrinkMenu());}
                default -> {return null;}
            }
        }
        public DailyMenu getFoodMenuTypes(FoodMenu foodMenu){
            foodMenu();
            int option = Integer.parseInt(scanner.nextLine());
            FoodMenuTypes foodMenuTypes = FoodMenuTypes.get(option-1);
    
            switch (foodMenuTypes){
                case BREAKFASTMENU -> {return foodMenu.getBreakfastMenu();}
                case LUNCHMENU -> {return foodMenu.getLunchMenu();}
                case DINNERMENU -> {return foodMenu.getDinnerMenu();}
                default -> {return null;}
            }
        }
        public DailyMenu getDrinkMenuTypes(DrinkMenu drinkMenu){
            drinkMenu();
            int option = Integer.parseInt(scanner.nextLine());
            DrinkMenuTypes drinkMenuTypes = DrinkMenuTypes.get(option-1);
            switch (drinkMenuTypes){
                case ALCOHOL -> { return drinkMenu.getAlcohol();}
                case SOFTDRINK -> {return drinkMenu.getSoftDrinks();}
                default -> {return null;}
            }
        }
        public MenuItem createMenuItem(){
            MenuItem menuItem = new MenuItem();
            System.out.print("\n Insert 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(Float.parseFloat(scanner.nextLine()));
            return menuItem;
        }
        public String getMenuItemName(){
            System.out.print("Insert Menu item you want to change: ");
            return scanner.nextLine();
        }
    
        public static void menu(){
            System.out.println("\n 1. Food Menu");
            System.out.println(" 2. Drink Menu");
            System.out.print(" Choose your option:");
        }
        public static void drinkMenu(){
            System.out.println("1. Alcohol Menu");
            System.out.println("2. Soft drink Menu");
            System.out.print("Choose which menu you want to make change:");
        }
        public static void foodMenu(){
            System.out.println("\n1. Breakfast Menu");
            System.out.println("2. Lunch Menu");
            System.out.println("3. Dinner Menu");
            System.out.print("\nChoose which menu you want to add:");
        }
    }

My Menu Service:

    public class DailyMenuServicesImpl implements DailyMenuServices {
        @Override
        public void addMenuItemsToMenu(DailyMenu dailyMenu,MenuItem menuItem) {
            List<MenuItem> menuItemList = dailyMenu.getMenuItemList();
            menuItemList.add(menuItem);
        }
    
        @Override
        public void updateMenu(DailyMenu dailyMenu,MenuItem updateMenuItem,String itemName) {
            dailyMenu.getMenuItemList().stream()
                                        .filter(menuItem -> menuItem.getNames().equals(itemName))
                                        .findFirst()
                                        .ifPresent(menuItem -> {
                                            menuItem.setNames(updateMenuItem.getNames());
                                            menuItem.setPrice(updateMenuItem.getPrice());
                                            menuItem.setDescription(updateMenuItem.getDescription());
                                            menuItem.setImage(updateMenuItem.getImage());
                                        });
        }
    
        @Override
        public void deleteMenu(DailyMenu dailyMenu,String itemName) {
            dailyMenu.getMenuItemList().removeIf(menuItem ->
                    menuItem.getNames().equals(itemName));
        }
    }

My Main method (where My services and View interact with each other):

public class Main {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        int option;
        Menu menu = new Menu();
        Bill bill = new Bill();
        while (true){
            System.out.println("\n1.Menu management");
            System.out.println("2.Bill management");
            System.out.print("Please choose which types Management you want to work with:");
            option = Integer.parseInt(scanner.nextLine());
            ManagementTypes types = ManagementTypes.get(option-1);
            switch (types){
                case MENU -> menuMain(menu);
                case BILL -> billMain(bill,menu);
                default -> {}
            }
        }
    }
    public static void billMain(Bill bill, Menu menu) throws IOException{
        int option = 0;
        BillServices billServices = new BillServicesImpl();
        BillPrinter billPrinter = new BillPrinterImpl();
        BillFileHandlingServices billFileHandlingServices = new BillFileHandlingServicesImpl();
        MenuView menuView = new MenuView();
        BillView billView = new BillView();
        MenuPrinter menuPrinter = new MenuPrinterImpl();
        try {
            while (option != 7){

                System.out.println("\nBill management");
                System.out.println("1. Create Bill ");
                System.out.println("2. Update Bill ");
                System.out.println("3. Delete Bill");
                System.out.println("4. Show Bill");
                System.out.println("5. Export bill");
                System.out.println("6. Import bill");
                System.out.println("7. To escape");
                System.out.print("\nChoose your option:");
                option = Integer.parseInt(scanner.nextLine());

                ManagementActions actions = ManagementActions.get(option-1);
                switch (actions){
                    case CREATE -> {
                        menuPrinter.printMenu(menu);
                        DailyMenu dailyMenu = menuView.getMenuTypes(menu);
                        menuPrinter.printMenu(dailyMenu);
                        BillItems billItems = billView.createBillItem(dailyMenu);
                        billServices.addBill(bill,billItems);
                    }
                    case UPDATE -> {
                        billPrinter.printBill(bill);
                        DailyMenu dailyMenu = menuView.getMenuTypes(menu);
                        BillItems billItems = billView.createBillItem(dailyMenu);
                        String itemName = billView.getBillItemName();
                        billServices.updateBill(bill,billItems,itemName);
                    }
                    case DELETE -> {
                        billPrinter.printBill(bill);
                        String itemName = billView.getBillItemName();
                        billServices.deleteBillItems(bill,itemName);
                    }
                    case SHOW -> billPrinter.printBill(bill);
                    case EXPORT -> billFileHandlingServices.exportBill(bill);
                    case IMPORT -> {
                        Bill importBill = billFileHandlingServices.importBill();
                        billPrinter.printBill(importBill);
                    }
                    case BREAK -> {}
                }
            }
        }catch (NullPointerException | IllegalStateException | InputMismatchException | NumberFormatException exception){
            System.out.println(exception.getMessage());
        }
    }
    public static void menuMain(Menu menu) throws IOException{
        int option = 0;
        DailyMenuServices dailyMenuServices = new DailyMenuServicesImpl();
        MenuView menuView = new MenuView();
        MenuPrinter menuPrinter = new MenuPrinterImpl();
        MenuFileHandlingServices menuFileHandlingServices = new MenuFileHandlingServicesImpl();
        try {
            while (option != 7) {
                System.out.println("\nMenu management");
                System.out.println("1. Create Menu Items");
                System.out.println("2. Update Menu Items");
                System.out.println("3. Delete Menu Items");
                System.out.println("4. Show Menu Items");
                System.out.println("5. Export Menu");
                System.out.println("6. Import Menu");
                System.out.println("7. To escape");
                System.out.print("\nChoose your option:");
                option = Integer.parseInt(scanner.nextLine());
                ManagementActions actions = ManagementActions.get(option - 1);
                switch (actions) {
                    case CREATE -> {
                        MenuItem menuItem = menuView.createMenuItem();
                        DailyMenu dailyMenu = menuView.getMenuTypes(menu);
                        dailyMenuServices.addMenuItemsToMenu(dailyMenu,menuItem);
                    }
                    case UPDATE -> {
                        menuPrinter.printMenu(menu);
                        MenuItem menuItem = menuView.createMenuItem();
                        DailyMenu dailyMenu = menuView.getMenuTypes(menu);
                        menuPrinter.printMenu(dailyMenu);
                        String itemName = menuView.getMenuItemName();
                        dailyMenuServices.updateMenu(dailyMenu,menuItem,itemName);
                    }
                    case DELETE -> {
                        menuPrinter.printMenu(menu);
                        DailyMenu dailyMenu = menuView.getMenuTypes(menu);
                        menuPrinter.printMenu(dailyMenu);
                        String itemName = menuView.getMenuItemName();
                        dailyMenuServices.deleteMenu(dailyMenu,itemName);
                    }
                    case SHOW -> menuPrinter.printMenu(menu);
                    case EXPORT -> menuFileHandlingServices.exportMenu(menu);
                    case IMPORT -> {
                        Menu importMenu = menuFileHandlingServices.importMenu();
                        menuPrinter.printMenu(importMenu);
                    }
                    case BREAK -> {}
                }
            }
        }catch (NullPointerException | IllegalStateException | NumberFormatException exception){
            System.out.println(exception.getMessage());
        }
    }
}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ In case you are just learning MVC, don't waste time on making such a complex model. Make it as simple as possible and concentrate on the architecture instead of the minute details of the data model. Once you have the architecture ready, you can start adding details to the model. You will also get better code reviews when you remove the parts that are not relevant to your question. At the moment people probably get a bit overwhelmed by the complicated input and menu handling and just skip your question. \$\endgroup\$ Dec 8, 2022 at 7:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.