본문 바로가기
TIL/내배캠 과제

23.05.29

by J1-H00N 2023. 5. 29.

개인과제인 키오스크만들기를 진행

 

메가커피의 키오스크를 참고하기로 함

 

구현하고자 하는 기능

메인 메서드에서 메인 메뉴판을 출력하고 입력에 따른 상세 메뉴판을 추가로 출력

장바구니에 해당하는 번호를 누르면 장바구니(지금까지 추가한 메뉴를 출력) 출력

취소에 해당하는 번호를 누르면 확인 문장 출력 후 확인 시 장바구니 초기화

상세 메뉴판에서 해당 메뉴에 해당하는 번호를 입력하면 구매 확인 문장 출력

확인 시 장바구니에 추가와 함께 메인 메뉴로 돌아옴

취소 시 장바구니에 추가하지 않고 메인 메뉴로 돌아옴 (위 두 문장은 해당 상세 메뉴판으로 돌아오게 수정 가능하면 수정)

장바구니에서는 추가한 메뉴 목록과 전체 가격 출력

장바구니에서 주문 완료(또는 해당 번호) 입력 시 주문 번호 출력과 함께 장바구니 초기화 + 메인 메뉴

장바구니에서 장바구니 목록 유지하고 메인 메뉴판으로 이동 가능

이후 추가 기능은 코드리뷰 등을 거치며 결정

 

문제 발생

메인 메뉴에서 각 상세 메뉴 혹은 장바구니나 취소 버튼을 클릭했을 때 발생하는 이벤트를 결정하기 위해 switch문을 이용했는데, 이 기능을 각 메뉴판 마다 넣으려고 하니 너무 비효율적이라 생각됨

더보기
public static void main(String[] args) {
        // 메인 메뉴판에서 사용자가 선태할 수 있는 사항
        String[] mainMenu = {"커피 & 콜드브루", "라떼", "초코", "에이드 & 모히또", "스무디 & 프라페", "티 & 주스", "디저트"};
        // 장바구니, 주문 초기화 메뉴
        String[] OrderMenu = {"장바구니", "주문 취소"};

        while (true) {
            // 선택할 수 있는 대상에 번호 추가
            int i = 1;
            // 이 부분은 각 메뉴판에서도 출력할 것이기 때문에 나중에 메서드화
            System.out.println("MegaCoffee에 오신 것을 환영합니다!!");
            System.out.println("아래에서 메뉴판을 보고 주문하실 메뉴를 골라주세요!\n");

            System.out.println("[메가커피 메뉴]");
            System.out.println("+------------------------");
            for (String mainMenuSelect : mainMenu) {
                System.out.println("| " + i + ". " + mainMenuSelect);
                i++;
            }
            System.out.println("+------------------------");
            System.out.println("\n[장바구니 확인, 주문 취소]");
            for (String orderMenuSelect : OrderMenu) {
                System.out.println(i + ". " + orderMenuSelect);
                i++;
            }
            Scanner orderNumber = new Scanner(System.in);
            int selectedOrderNumber = orderNumber.nextInt();
            switch (selectedOrderNumber) {
                case 1 :
                    System.out.println("커피 메뉴 출력");
                    break;
                case 2 :
                    System.out.println("라떼 메뉴 출력");
                    break;
                case 3 :
                    System.out.println("초코 메뉴 출력");
                    break;
                case 4 :
                    System.out.println("에이드 메뉴 출력");
                    break;
                case 5 :
                    System.out.println("스무디 메뉴 출력");
                    break;
                case 6 :
                    System.out.println("티 메뉴 출력");
                    break;
                case 7 :
                    System.out.println("디저트 메뉴 출력");
                    break;
                case 8 :
                    System.out.println("장바구니 출력");
                    break;
                case 9 :
                    System.out.println("주문을 취소하였습니다.");
                    System.out.println();
                    continue;
                default:
                    System.out.println("잘못 입력하셨습니다. 다시 입력해주세요.");
                    System.out.println();
            }
        }
    }

1. 어차피 각 메뉴의 개수를 수정하지 않을 것이기 때문에 현재 방법 유지

2. HashMap에 번호와 메서드(이를 위해선 해당 메서드를 람다식으로 구현해야 한다.)를 저장해 구현

 

일단은 현상태를 유지하고 후에 충분한 시간이 있거나 유지보수를 해야할 필요성을 느낀다면 그때 2번 방법을 사용할 생각

 

일단 커피 & 콜드브루 메뉴판을 임시로 생성했다.

더보기
class CoffeeAndColdBrewMenu {
    public static void callSelectableMenu() {
        // 각 메뉴판마다 바뀌는 것 중 겹치는 것 객체화
        // 메뉴판 이름
        String menuName = "커피 & 콜드브루";
        // 커피 & 콜드브루 메뉴
        String[] CoffeeAndColdBrewMenu = {"커피1", "커피2", "커피3", "커피4", "커피5", "커피6"};
        // 장바구니, 주문 초기화 메뉴
        String[] OrderMenu = {"장바구니", "주문 취소"};

        while (true) {
            // 선택할 수 있는 대상에 번호 추가
            int i = 1;
            // 이 부분은 각 메뉴판에서도 출력할 것이기 때문에 나중에 메서드화
            System.out.println("MegaCoffee에 오신 것을 환영합니다!!");
            System.out.println("아래에서 메뉴판을 보고 주문하실 메뉴를 골라주세요!\n");

            System.out.println("[" + menuName + " 메뉴]");
            System.out.println("+------------------------");
            for (String mainMenuSelect : CoffeeAndColdBrewMenu) { // 각 메뉴마다 원하는 메뉴리스트만 불러올 수 있는 방법 찾아보기
                System.out.println("| " + i + ". " + mainMenuSelect);
                i++;
            }
            System.out.println("+------------------------");
            System.out.println("\n[장바구니 확인, 주문 취소]");
            for (String orderMenuSelect : OrderMenu) {
                System.out.println(i + ". " + orderMenuSelect);
                i++;
            }
            Scanner orderNumber = new Scanner(System.in);
            int selectedOrderNumber = orderNumber.nextInt();
            if (selectedOrderNumber >= 1 && selectedOrderNumber <= CoffeeAndColdBrewMenu.length) {
                System.out.println("장바구니 추가 확인 메서드 출력");
            } else if (selectedOrderNumber == CoffeeAndColdBrewMenu.length + 1) {
                System.out.println("장바구니 출력");
                break;
            } else if (selectedOrderNumber == CoffeeAndColdBrewMenu.length + 2) {
                System.out.println("주문을 취소하였습니다.");
                System.out.println();
                continue;
            } else {
                System.out.println("잘못 입력하셨습니다. 다시 입력해주세요.");
                System.out.println();
                continue;
            }
            // 장바구니 추가 확인 메서드로 구현할 부분
            System.out.println(CoffeeAndColdBrewMenu[selectedOrderNumber - 1]);
            System.out.println("위 메뉴를 장바구니에 추가하시겠습니까?");
            System.out.println("1. 확인        2. 취소");
            Scanner scanner = new Scanner(System.in);
            int check = scanner.nextInt();
            if (check == 1) {
                System.out.println("확인");
            } else if (check == 2) {
                System.out.println("취소");
            } else {
                System.out.println("잘못 입력하였습니다.");
            }
            //
        }
    }
}

다른 메서드에서 구현해야할 부분도 있고, 다른 메뉴판 클래스들을 만든 뒤 추상 클래스로 모을 부분도 있지만 앞으로 다른 클래스들을 만들 때는 수월할 것이다.

 

장바구니 추가 확인 메서드

더보기
class CheckShoppingList {
    // 장바구니 추가 확인 메서드
    public static void checkShoppingList(String[] menuName, int orderNumber) {
        System.out.println(menuName[orderNumber - 1]);
        System.out.println("위 메뉴를 장바구니에 추가하시겠습니까?");
        System.out.println("1. 확인        2. 취소");
        Scanner scanner = new Scanner(System.in);
        int check = scanner.nextInt();
        if (check == 1) {
            System.out.println("확인");
        } else if (check == 2) {
            System.out.println("취소");
        } else {
            System.out.println("잘못 입력하였습니다.");
        }
    }
}

 각 메뉴판 메서드에서 메뉴 리스트의 이름과 사용자 입력값만 전달하면 된다.

 

위 커피&콜드브루 메뉴판을 참조하여 나머지 메뉴판들을 생서했고, 이제 장바구니 클래스와 장바구니 초기화, 구매 기능만 추가하면 기본 적인 성능 구현은 거의 마무리다.

 

코드 리뷰 중 과제 필수 요구 사항 중 일부를 놓쳐서 코드를 다시 수정해야 된다는 것을 알게되었다.

또한 이번 경험을 통해 미리 어떤 방식으로 구현할지 로드맵 등을 먼저 만들어보는 습관을 들이도록 할 것이다.

객체지향적 코딩 신경쓸것

메인 메서드에 로직이 있는 것을 지양할 것

https://kellis.tistory.com/127

 

Static 사용을 피해야 하는 이유

Java에는 static이라는 키워드가 존재하며, 이는 static으로 지시된 특정한 멤버가 해당 클래스의 인스턴스가 아니라 클래스 자체에 속해 있음을 나타냅니다. 즉, 클래스의 모든 인스턴스에서 공유

kellis.tistory.com

 

메뉴 이름과 설명 필드를 가지는 메뉴 클래스 생성

public class Menu {
    private String menuName;
    private String menuDesc;

    public Menu(String menuName, String menuDesc) {
        this.menuName = menuName;
        this.menuDesc = menuDesc;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public String getMenuDesc() {
        return menuDesc;
    }

    public void setMenuDesc(String menuDesc) {
        this.menuDesc = menuDesc;
    }
}

 

메뉴 클래스를 상속받는 제품 클래스 만들기

public class Product extends Menu{
    private int menuPrice;

    public int getMenuPrice() {
        return menuPrice;
    }

    public void setMenuPrice(int menuPrice) {
        this.menuPrice = menuPrice;
    }

    public Product(String menuName, String menuDesc, int menuPrice) {
        super(menuName, menuDesc);
        this.menuPrice = menuPrice;
    }
}

 

메뉴 클래스에 메가 커피 메뉴 하드 코딩 완료

'TIL > 내배캠 과제' 카테고리의 다른 글

23.06.03  (0) 2023.06.05
23.06.02  (0) 2023.06.02
23.06.01  (0) 2023.06.01
23.05.31  (0) 2023.05.31
23.05.30  (0) 2023.05.30