ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [개인프로젝트] 피드백1 이해완료
    스파르타코딩클럽/자바 개인프로젝트 - Kiosk 2023. 10. 30. 17:54

     

     

    1. 객체지향을 만들어가는 데에 있어서

    action (method)와 factor(variable)을 분리해서 생각하는 게 중요한데요.

    각 클래스 안에 print method를 넣는 것,

    예를 들면 Order 안에 order 현황을 프린트 하도록

    모두 넣다보니, 메인에서 각 클래스를 호출하는 부분이

    어색하게 느껴지는 거 같습니다.

    Order 객체 안에는 Order가 필요한 값들

    (메뉴 종류 등등)만을 변수로 가지도록 하고

    printer 클래스와 같은 프린트를 해주는 클래스를 만들어 설계하면 어땠을까 생각합니다 !

     

    이  피드백을 항상 머릿속에 넣어두고 어떤식으로 했어야할까를 지난 며칠간 생각하고 있었다.

    그런데 문득 정석 예제를 풀어 보던 중 print 메소드를 따로 작성해둔 것이 보였고 

    혹시 이런식으로 하면 되는가 하여

    튜터님께 질문을 드렸다.

     

     

    package kiosk;
    
    import java.util.*;
    
    import static kiosk.main.choiceMenu;
    
    public class Order extends OrderResult {
        private void addOrderMenu(ArrayList<? extends Menu> menuList, Scanner sc, int choice) {
            System.out.println(menuList.get(choice));
            System.out.println("     주문목록에 담으시겠습니까?");
            System.out.println("       1.네    2.아니오");
            int chooseMenu = sc.nextInt();
            sc.nextLine();
            if (chooseMenu == 1) {
                System.out.println("주문목록에 담았습니다.");
                addOrder(menuList.get(choice), 1);
            } else {
                System.out.println("취소되었습니다.");
    
            }
        }
    
        public void pickMenu(ArrayList<? extends Menu> menuList) {
            Scanner sc = new Scanner(System.in);
            int inputChoice = sc.nextInt();
            int choice = inputChoice - 1;
            sc.nextLine();
            if (choice < 0 || choice > 2) {
                return;
            }
            addOrderMenu(menuList, sc, choice);
        }
    
        
        public void pickMainMenu(ArrayList<? extends Menu> menuList) {
            Scanner sc = new Scanner(System.in);
            int inputChoice = sc.nextInt();
            int choice = inputChoice - 1;
            sc.nextLine();
            if (choice < 0 || choice > 2) {
                return;
            }
            addOrderMainMenu(menuList, sc, choice);
        }
    
        private void addOrderMainMenu(ArrayList<? extends Menu> menuList, Scanner sc, int choice) {
            System.out.println(menuList.get(choice));
            System.out.println("     주문목록에 담으시겠습니까?");
            System.out.println("       1.네    2.아니오");
            int chooseMenu = sc.nextInt();
            sc.nextLine();
            if (chooseMenu == 1) {
                System.out.println("주문목록에 담았습니다.");
                addOrder(menuList.get(choice), 1);
                System.out.println("\n선택하신 상품에 메뉴를 추가하시겠습니까?");
                System.out.println("원하시는 상품이 없다면 5번을 눌러주세요\n");
                choiceMenu(main.topping);
                plusMenu(main.topping);
            } else {
                System.out.println("취소되었습니다.");
    
            }
        }
    
        public void plusMenu(ArrayList<? extends Menu> menuList) {
            Scanner sc = new Scanner(System.in);
            int inputChoice = sc.nextInt();
            int choice = inputChoice - 1;
            sc.nextLine();
    
            if (choice == 4) {
                Menu.printMenu();
                return;
            }
            if (choice < 0 || choice > 2) {
                return;
            }
            addOrderMenu(menuList, sc, choice);
        }

     

     private Map<Menu, Integer> orderMap = new HashMap<>();
    
        public void addOrder(Menu menu, int num) {
            if (orderMap.containsKey(menu)) {
                int menuNum = orderMap.get(menu);
                orderMap.put(menu, menuNum + num);
            } else {
                orderMap.put(menu, num);
            }
            System.out.println(menu.getName() + "  " + num + "개");
        }
    
        public void printOrder() {
            System.out.println(" 주문목록입니다. ");
            int totalPrice = 0;
    
            for (Map.Entry<Menu, Integer> entry : orderMap.entrySet()) {
                Menu menu = entry.getKey();
                int dishNum = entry.getValue();
                int menuTotalPrice = dishNum * menu.getPrice();
    
                System.out.println(menu.getName() + "  " + dishNum + "개");
                totalPrice += menuTotalPrice;
            }
            System.out.println("총 금액은 " + totalPrice + " 원 입니다.");
            System.out.println("     \n주문하시겠습니까?");
            System.out.println("    1.주문하기    2.돌아가기");
            try {
            Scanner sc = new Scanner(System.in);
            int orderOk = sc.nextInt();
            sc.nextLine();
    
                if (orderOk == 1) {
                    for (Map.Entry<Menu, Integer> entry : orderMap.entrySet()) {
                        Menu menu = entry.getKey();
                        OrderResult.setSelledMenu(menu);
                    }
                    setTotalSales(totalPrice);
                    endOrder();
                } else if (orderOk == 2) {
                    Menu.printMenu();
                } else {
                    System.out.println("이거 누르시면 안되는데 이번만 봐드립니다!");
                }
            } catch (InputMismatchException e) {
                System.out.println("화면에 있는 숫자를 눌러주세요. 제발ㅠㅠ");
            }
        }
    
    
        int orderNumber = 1;
    
        public void endOrder() {
    
            if (!orderMap.isEmpty()) {
                System.out.println(orderNumber + "번 주문이 완료되었습니다.");
                orderMap.clear();
            } else if (orderMap.isEmpty()) {
                System.out.println("주문 목록이 없습니다.");
                return;
            }
            orderNumber++;
    
            try {
                Thread.sleep(3 * 1000);
            } catch (InterruptedException e) {
                System.out.println("메인메뉴로 돌아갑니다.");
            }
        }
    
        public void cancelOrder() {
            orderMap.clear();
        }
    
    }

     

    내 Order 클래스는 이렇게  정보를 저장하는 부분과 정보를 가져와서 출력하는 부분이  혼재해 있었고

    피드백의 주 골자는 두 부분을 각기 다른 클래스로 만드는 것이 더 좋다는 것을 말씀하셨던 것이다.

     

    피드백에 따라 코드블럭을 두 분류로 나누어 보았고

    각각 다른 클래스로 만드는 것이 훨씬 더 보기 좋은 코드라고 한다.

     

     

     

Designed by Tistory.