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

23.06.02

by J1-H00N 2023. 6. 2.

옵션 추가 기능을 위해 새로운 클래스를 만들거나 기존의 자료형들을 다시 만들어야 하나 고민하던 중 총 결산 기능을 만들 때 기존의 메서드와 클래스, 변수들을 재활용했던 경험을 살려 옵션도 기존의 객체들을 재활용해서 구현하려고 한다.

 

더보기
    public boolean addOption(String menuName) { // 메뉴 카테고리만 확인
        boolean option = true;
        String optionName;
        int optionPrice = 0;
        int optionCount = 1;
        if (!menuCollection.menuList().get(6).getMenuName().equals(menuName)) { // 디저트가 아니라면 옵션 추가
            if (menuCollection.menuList().get(0).getMenuName().equals(menuName) || menuCollection.menuList().get(1).getMenuName().equals(menuName) || menuCollection.menuList().get(2).getMenuName().equals(menuName)) {
                // 메뉴가 커피 & 콜드브루 or 라떼 or 초코
                while (true) { // HOT, ICED 선택 가능
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                    System.out.println("| HOT or ICED를 선택해주세요.");
                    System.out.println("| 1. HOT");
                    System.out.println("| 2. ICED");
                    System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                    System.out.println();
                    System.out.println("주문을 취소하고 싶으시다면 0을 눌러주세요.");
                    System.out.print("선택해 주세요 : ");
                    int check = sc.nextInt();
                    if (check == 1) {
                        optionName = "+ HOT";
                        addOrder(optionName, optionPrice, optionCount);
                        System.out.println(optionName);
                        break;
                    } else if (check == 2) {
                        optionName = "+ ICED";
                        addOrder(optionName, optionPrice, optionCount);
                        break;
                    } else if (check == 0) {
                        return !option;
                    } else {
                        System.out.println("제대로 선택해 주세요.");
                    }
                }
            }
            if (menuCollection.menuList().get(0).getMenuName().equals(menuName) || menuCollection.menuList().get(1).getMenuName().equals(menuName)) {
                // 메뉴가 커피 & 콜드브루 or 라떼
                while (true) { // 샷 추가 얼마든지 가능
                    optionPrice = 0; // 옵션 선택할 때마다 옵션 가격 초기화
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                    System.out.println("| 샷 옵션을 선택해 주세요");
                    System.out.println("| 1. 샷 추가 (+ 500)");
                    System.out.println("| 2. 연하게 (주의!! : 샷 추가 가격은 받습니다!!)");
                    System.out.println("|");
                    System.out.println("|");
                    System.out.println("| 3. 확인");
                    System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                    System.out.println();
                    System.out.println("주문을 취소하고 싶으시다면 0을 눌러주세요.");
                    System.out.print("선택해 주세요 : ");
                    int check = sc.nextInt();
                    if (check == 1) {
                        optionName = "+ 샷 추가";
                        optionPrice = 500;
                        addOrder(optionName, optionPrice, optionCount);
                    } else if (check == 2) {
                        optionName = "+ 연하게";
                        addOrder(optionName, optionPrice, optionCount);
                        break;
                    } else if (check == 3) {
                        break;
                    } else if (check == 0) {
                        return !option;
                    } else {
                        System.out.println("제대로 선택해 주세요.");
                    }
                }
                while (true) { // 시럽 추가
                    optionPrice = 0; // 옵션 선택할 때마다 옵션 가격 초기화
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                    System.out.println("| 시럽 옵션을 선택해 주세요");
                    System.out.println("| 1. 바닐라 시럽 추가 (+ 500)");
                    System.out.println("| 2. 헤이즐넛 시럽 추가 (+ 500)");
                    System.out.println("|");
                    System.out.println("|");
                    System.out.println("| 3. 확인");
                    System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                    System.out.println();
                    System.out.println("주문을 취소하고 싶으시다면 0을 눌러주세요.");
                    System.out.print("선택해 주세요 : ");
                    int check = sc.nextInt();
                    if (check == 1) {
                        optionName = "+ 바닐라 시럽 추가";
                        optionPrice = 500;
                        addOrder(optionName, optionPrice, optionCount);
                    } else if (check == 2) {
                        optionName = "+ 헤이즐넛 시럽 추가";
                        optionPrice = 500;
                        addOrder(optionName, optionPrice, optionCount);
                    } else if (check == 3) {
                        break;
                    } else if (check == 0) {
                        return !option;
                    } else {
                        System.out.println("제대로 선택해 주세요.");
                    }
                }
            }
            while (true) { // 모든 메뉴 휘핑크림, 알로에, 타피오카 얼마든지 추가가능
                optionPrice = 0; // 옵션 선택할 때마다 옵션 가격 초기화
                System.out.println();
                System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                System.out.println("| 추가 옵션을 선택해 주세요");
                System.out.println("| 1. 휘핑크림 추가 (+ 500)");
                System.out.println("| 2. 알로에 펄 추가 (+ 500)");
                System.out.println("| 3. 타피오카 펄 추가 (+ 500)");
                System.out.println("|");
                System.out.println("|");
                System.out.println("| 4. 확인");
                System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                System.out.println();
                System.out.println("주문을 취소하고 싶으시다면 0을 눌러주세요.");
                System.out.print("선택해 주세요 : ");
                int check = sc.nextInt();
                if (check == 1) {
                    optionName = "+ 휘핑크림 추가";
                    optionPrice = 500;
                    addOrder(optionName, optionPrice, optionCount);
                } else if (check == 2) {
                    optionName = "+ 알로에 펄 추가";
                    optionPrice = 500;
                    addOrder(optionName, optionPrice, optionCount);
                } else if (check == 3) {
                    optionName = "+ 타피오카 펄 추가";
                    optionPrice = 500;
                    addOrder(optionName, optionPrice, optionCount);
                } else if (check == 4) {
                    break;
                } else if (check == 0) {
                    return !option;
                } else {
                    System.out.println("제대로 선택해 주세요.");
                }
            }
            while (true) { // 개인 컵 여부
                optionPrice = 0; // 옵션 선택할 때마다 옵션 가격 초기화
                System.out.println();
                System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                System.out.println("| 일회용을 사용하시겠습니까?");
                System.out.println("| 1. 일회용 컵 포장(일회용 컵 반환 시 돌려드립니다.) (+ 500)");
                System.out.println("| 2. 개인 컵 사용");
                System.out.println("| 3. 매장 내 이용");
                System.out.println("+---------------------------------------------------------------------------------------------------------------------------");
                System.out.println();
                System.out.println("주문을 취소하고 싶으시다면 0을 눌러주세요.");
                System.out.print("선택해 주세요 : ");
                int check = sc.nextInt();
                if (check == 1) {
                    optionName = "+ 일회용 컵 사용";
                    optionPrice = 500;
                    addOrder(optionName, optionPrice, optionCount);
                    break;
                } else if (check == 2) {
                    optionName = "+ 개인 컵 사용";
                    addOrder(optionName, optionPrice, optionCount);
                    break;
                } else if (check == 3) {
                    optionName = "+ 매장";
                    addOrder(optionName, optionPrice, optionCount);
                    break;
                } else if (check == 0) {
                    return !option;
                } else {
                    System.out.println("제대로 선택해 주세요.");
                }
            }
        }
        return option;
    }

위와 같이 만들었고, 만들다 보니 가격이 없는 메뉴는 W 0이라고 출력되는게 거슬려 toString문을 조금 수정했다.

public String toString() {
        if (price == 0) { // 가격을 선택 안하는 옵션 선택 시 메뉴와 개수만 입력
            return String.format("%-13s           %d",menuName, count);
        }
        return String.format("%-13s   ₩ %d    %d", menuName, (price * count), count);
    }

또한 옵션 추가문이 장바구니에 메뉴 다음에 출력되도록 수정했다.

if (check == 1) { // 옵션 추가 메서드가 boolean을 반환하게 해서 true 시 장바구니 추가, 아니면 탈출하도록
                personalOrder.addPersonalOrder(menuCollection.productMap().get(menuName).get(orderNumber - 1).getMenuName(), menuCollection.productMap().get(menuName).get(orderNumber - 1).getMenuPrice(), 1);
                if (personalOrder.addOption(menuName)) {
                    System.out.print("");
                } else if (!personalOrder.addOption(menuName)) {
                    System.out.println("주문을 취소하였습니다.");
                    return;
                }
                System.out.println("장바구니에 추가하였습니다.");
                System.out.println();
            }

그리고 같은 메뉴를 장바구니에 담았을 때 옵션들도 합쳐지는 문제가 생겨 이 또한 장바구니용 추가 메서드를 만들었다.

public void addPersonalOrder(String menuName, int price, int count) {
        Cart cart = new Cart(menuName, price, count);
        order.add(cart);
    }

기존 메서드

public void addOrder(String menuName, int price, int count) {
        Cart plusCount = plusCount(menuName);
        if (plusCount != null) {
            plusCount.plusCount(count);
        } else {
            Cart cart = new Cart(menuName, price, count);
            order.add(cart);
        }
    }

이렇게 만들어 장바구니에 추가할 때는 같은 메뉴라도 따로 따로 추가되게 만들었고, 옵션이나 총 결산에서는 다같이 출력되게 만들었다.

 

출력 예시

장바구니

[주문 내역]
아메리카노           ₩ 1500    1
+ HOT                   1
+ 샷 추가          ₩ 1000    2
+ 바닐라 시럽 추가     ₩ 1000    2
+ 알로에 펄 추가      ₩ 1000    2
+ 일회용 컵 사용      ₩ 500    1
아메리카노           ₩ 1500    1
+ ICED                  1
+ 연하게                   1
+ 헤이즐넛 시럽 추가    ₩ 500    1
+ 매장                    1

총 주문 액수 : ₩ 7000

총 결산

[판매 내역]
아메리카노           ₩ 3000    2
+ HOT                   1
+ 샷 추가          ₩ 1000    2
+ 바닐라 시럽 추가     ₩ 1000    2
+ 알로에 펄 추가      ₩ 1000    2
+ 일회용 컵 사용      ₩ 500    1
+ ICED                  1
+ 연하게                   1
+ 헤이즐넛 시럽 추가    ₩ 500    1
+ 매장                    1

총 판매 액수 : ₩ 7000

위 문제를 해결할 때 메뉴들이 합쳐지지 않아 구현에 쓰이지 않았던 '실패한' 메서드가 끝에서 문제를 해결하는 메서드로 쓰이는 것을 보고 특별하고 값진 경험을 한 느낌이 들어 특히 좋았다.

 

조금 더 깔끔한 줄 맞춤을 위해 ㅡ로 길이를 비슷하게 맞췄다.

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

23.06.05  (0) 2023.06.05
23.06.03  (0) 2023.06.05
23.06.01  (0) 2023.06.01
23.05.31  (0) 2023.05.31
23.05.30  (0) 2023.05.30