Skip to content

Refactoring - 메뉴 로직 간결화 #34

@ggujin

Description

@ggujin

메뉴 로직 간결화

  • 메뉴의 중복되는 3부분을 1개의 메소드로 해결하여 코드 간결화

ViewUtil 클래스를 추가

static int displayMenu(String menusName, String[] menus) {
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("******비트를 타쇼 " + menusName + " ******");
            System.out.println("        원하는 메뉴의 번호를 입력하세요");
            for(int i = 0; i < menus.length; i ++) {
                System.out.printf("%d. %s%n", i+1, menus[i]);
            }
            System.out.println("**********************************");
            try {
                int menu = Integer.parseInt(scanner.nextLine());
                if(menu >= 1 && menu <= menus.length) {
                    continue;
                } else {
                    throw new Exception("메뉴 번호 오류");
                }
            } catch (Exception e) {
                System.out.println("오류발생: " + e.getMessage());
                System.out.println("1~4 중 다시 입력해주세요");
            }
        } while (true);
    }

메뉴 method 예시

public void showUserPage() {
        bikeService.addBike(BikeType.Single);

        int menu = ViewUtil.displayMenu("자전거 대여 시스템" , new String[] {
                "대여하기 (1인용)",
                "대여하기 (2인용)",
                "결제 및 반납하기",
                "이전메뉴"
        });
    }

결과

image

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions