Day 19(8/26) 좌석 예약 시스템

2016. 8. 29. 15:33Programming/Java

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class CinemaReserve {
    public static void main(String[] args) {
        String[][] seat = new String[4][8];
        String[] list = { "A""B""C""D" };
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 8; j++) {
                seat[i][j] = "□ ";
            }
        }
        
        while (true) {
            System.out.println("\n\n 예약 조회(1) | 예약 신청(2) | 예약 취소(3)");
            System.out.println("원하시는 번호를 입력해주세요.");
            Scanner scan = new Scanner(System.in);
            int select = scan.nextInt();
            switch (select) {
            // 예약 조회
            case 1:
                for (int i = 0; i < 4; i++) {
                    System.out.print("\n" + list[i] + " ");
                    for (int j = 0; j < 8; j++) {
                        System.out.print(seat[i][j]);
                    }
                }
                break;
            // 예약 신청
            case 2:
                System.out.println("원하시는 행을 입력해주세요.");
                int row = scan.nextInt() - 1;
                System.out.println("원하시는 열을 입력해주세요.");
                int col = scan.nextInt() - 1;
                seat[row][col] = "■ ";
                System.out.println("예약 신청되었습니다.");
                break;
 
            // 예약 취소
            case 3:
                System.out.println("원하시는 행을 입력해주세요.");
                int row_cancel = scan.nextInt() - 1;
                System.out.println("원하시는 열을 입력해주세요.");
                int col_cancel = scan.nextInt() - 1;
                seat[row_cancel][col_cancel] = "□ ";
                System.out.println("예약 취소되었습니다.");
                break;
            }
        }
    }
}
cs

 

 

예약 신청시 예외처리 추가

1) 열, 행값 초과하지 않게

2) 이미 예약된 자리 판별

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case 2:
                System.out.println("원하시는 행을 입력해주세요.");
                int row = scan.nextInt() - 1;
                while(row>3){
                    System.out.println("행 크기가 너무 큽니다. 다시 입력해주세요.");
                    row = scan.nextInt()-1;
                }
                System.out.println("원하시는 열을 입력해주세요.");
                int col = scan.nextInt() - 1;
                while(col>7){
                    System.out.println("열 크기가 너무 큽니다. 다시 입력해주세요.");
                    col = scan.nextInt()-1;
                }
                if(seat[row][col].equals("■ ")){
                    System.out.println("이미 예약된 자리입니다.다시 시도해주세요.");
                    break;
                }
                seat[row][col] = "■ ";
                System.out.println("예약 신청되었습니다.");
                break;
cs