Languages/Java

[자바 객체 지향 프로그래밍] 06. 기말고사: 자바 실무 프로젝트

Dev다D 2021. 3. 4. 13:14
반응형

영화관 '코드박스' 예약 시스템 pt. 2

 

Theater 클래스

(1) public boolean reserve(String name, char rowChar, int col, int numSeat)

예약 정보(이름, 열, 행, 예약 좌석 수)를 파라미터로 받아 예약하는 메소드입니다.

  • 존재하지 않는 행 또는 열을 입력하면 false를 리턴하고 메소드를 종료합니다.
  • 만약 1열부터 9열까지밖에 없는데, D7부터 네 좌석을 예약하면 false를 리턴하고 메소드를 종료합니다. 아무 좌석도 예약되면 안 됩니다!
  • D3부터 네 좌석(D3, D4, D5, D6)을 예약 하려고 하는데 D6가 이미 예약된 자리라면, 나머지 좌석들(D3, D4, D5)도 예약되면 안 됩니다.
  • 문제가 없는 경우, 실제로 예약을 하고 true를 리턴합니다.

입력받은 rowChar를 배열의 index로 사용하기 위해 getRowIndex 메소드를 이용하세요!

 

(2) public int cancel(String name)

이름 name으로 예약된 자리를 취소하고, 취소된 좌석의 수를 리턴합니다. 예를 들어 "김신의" 이름으로 예약된 좌석이 여덟 자리이면 t.cancel("김신의")는 8 리턴하는 것이죠. 만약 예약된 자리가 없다면 0을 리턴하겠죠?

 

(3) public int cancel(char rowChar, int col, int numSeat)

메소드 오버로딩으로 구현한 또 다른 cancel 메소드입니다. 이번에는 파라미터로 받는 '열', '행', '좌석 수'에 해당되는 모든 좌석의 예약을 취소합니다. 그리고 위의 cancel 메소드와 마찬가지로 총 취소된 좌석 수를 리턴합니다.

만약 G2부터 G4까지 세 자리가 예약된 상황에서 t.cancel('G', 3, 4)는 G3부터 G6까지 네 자리중, 예약된 G3, G4를 취소하고 2를 리턴해야 합니다. 그런데 만약 t.cancel('G', 3, 10000) 이런 식으로 열의 범위를 초과하는 좌석 개수(10000)가 지정된다면 해당 행의 가장 오른쪽 끝 열까지 존재하는 모든 좌석의 예약 내역을 취소하면 됩니다. 이 부분도 코드로 잘 표현해보세요.

 

(4) public int getNumberOfReservedSeat()

예약된 모든 좌석 수를 리턴하는 메소드입니다.

 

(5) private int getRowIndex(char uppercaseChar)

영화관 행을 그에 해당하는 정수로 변환하는 메소드입니다. 예를들어 'A'0, 'D'3으로 변환해줍니다. 템플릿으로 제공됩니다!

 

 


package Ex0602;

public class Theater {
    private Seat[][] seats;

    private int rowCount, colCount;

    public Theater(int rowCount, int colCount) {
        if (rowCount > 26) {
            rowCount = 26; // number of alphabets
        }
        seats = new Seat[rowCount][colCount];
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                seats[i][j] = new Seat();
            }
        }

        this.rowCount = rowCount;
        this.colCount = colCount;
    }

    public boolean reserve(String name, char rowChar, int col, int numSeat) {

        int row = getRowIndex(rowChar) + 1;

        if (row > rowCount || col > colCount || col + numSeat -1 > colCount) {
            return false;
        }

        for (int i = 0; i < numSeat; i++) {
            Seat current = seats[row - 1][col - 1 + i];

            if (current.isOccupied()) {
                for (int j = 0; j < i; j++) {
                    seats[row - 1][col - 1 + j].cancel();
                }
                return false;
            }
            current.reserve(name);
        }
        return true;
    }


    public int cancel(String name) {
        int canceledCount = 0;
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                Seat seat = seats[i][j];
                if (seat.isOccupied() && seat.match(name)) {
                    seat.cancel();
                    canceledCount++;
                }
            }
        }
        return canceledCount;
    }

    public int cancel(char rowChar, int col, int numSeat) {
        int row = getRowIndex(rowChar) +1;

        if (row > rowCount || col > colCount) {
            return 0;
        }
        if (col + numSeat - 1 > colCount) {
            numSeat = colCount - col + 1;
        }

        int canceledCount = 0;

        for (int i = 0; i < numSeat; i++) {
            if (seats[row - 1][col - 1 + i].isOccupied()) {
                seats[row - 1][col - 1 + i].cancel();
                canceledCount++;
            }
        }
        return canceledCount;
    }


    public int getNumberOfReservedSeat() {
        int reservedCount = 0;
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                Seat seat = seats[i][j];
                if (seat.isOccupied()) {
                    reservedCount++;
                }
            }
        }
        return reservedCount;
    }

    public void printSeatMatrix() {
        System.out.print("  ");
        for (int i = 1; i <= 9; i++) {
            System.out.print("  " + i);
        }
        System.out.println();

        for (int i = 0; i < rowCount; i++) {
            System.out.print((char) ('A' + i) + ": ");
            for (int j = 0; j < colCount; j++) {
                Seat s = seats[i][j];
                if (s.isOccupied()) {
                    System.out.print("[O]");
                } else {
                    System.out.print("[ ]");
                }
            }
            System.out.println();
        }
    }

    private int getRowIndex(char uppercaseChar) {
        return uppercaseChar - 'A';
    }
}

본 내용은 Codeit의  '자바 기초' 강의를
참고하여 작성한 내용입니다.

 

반응형