+/EE422C

software design

git clone https://git.julia.blue/+/EE422C

Joseph RyanAdd the rest of the files7e47727

master
5.6 KiB206 linesraw
1package assignment6;
2
3import java.util.ArrayList;
4import java.util.List;
5
6public class Theater {
7
8    /**
9     * the delay time you will use when print tickets
10     */
11    private int printDelay = 50; // 50 ms.  Use it in your Thread.sleep()
12
13    public void setPrintDelay(int printDelay) {
14        this.printDelay = printDelay;
15    }
16
17    public int getPrintDelay() {
18        return printDelay;
19    }
20
21    private String show;
22    private int totalSeats;
23    private int seatsPerRow;
24    private int currentSeat;
25    private final List<Ticket> transactions;
26    private boolean notifiedAllOut = false;
27
28    /**
29     * Represents a seat in the theater
30     * A1, A2, A3, ... B1, B2, B3 ...
31     */
32    static class Seat implements Comparable<Seat> {
33        private int rowNum;
34        private int seatNum;
35
36        public Seat(int seatNum, int seatsPerRow) {
37            this.rowNum = seatNum / seatsPerRow;
38            this.seatNum = seatNum % seatsPerRow + 1;
39        }
40
41        public int getSeatNum() {
42            return seatNum;
43        }
44
45        public int getRowNum() {
46            return rowNum;
47        }
48
49        @Override
50        public String toString() {
51            String result = "";
52            int tempRowNumber = rowNum + 1;
53            do {
54                tempRowNumber--;
55                result = ((char) ('A' + tempRowNumber % 26)) + result;
56                tempRowNumber = tempRowNumber / 26;
57            } while (tempRowNumber > 0);
58            result += seatNum;
59            return result;
60        }
61
62        @Override
63        public int compareTo(Seat other) {
64            if (this.rowNum != other.rowNum) {
65                return Integer.compare(this.rowNum, other.rowNum);
66            }
67            return Integer.compare(this.seatNum, other.seatNum);
68        }
69    }
70
71    /**
72     * Represents a ticket purchased by a client
73     */
74    static class Ticket {
75        private String show;
76        private String boxOfficeId;
77        private Seat seat;
78        private int client;
79        public static final int ticketStringRowLength = 31;
80
81
82        public Ticket(String show, String boxOfficeId, Seat seat, int client) {
83            this.show = show;
84            this.boxOfficeId = boxOfficeId;
85            this.seat = seat;
86            this.client = client;
87        }
88
89        public Seat getSeat() {
90            return seat;
91        }
92
93        public String getShow() {
94            return show;
95        }
96
97        public String getBoxOfficeId() {
98            return boxOfficeId;
99        }
100
101        public int getClient() {
102            return client;
103        }
104
105        @Override
106        public String toString() {
107            String result, dashLine, showLine, boxLine, seatLine, clientLine, eol;
108
109            eol = System.getProperty("line.separator");
110
111            dashLine = new String(new char[ticketStringRowLength]).replace('\0', '-');
112
113            showLine = "| Show: " + show;
114            for (int i = showLine.length(); i < ticketStringRowLength - 1; ++i) {
115                showLine += " ";
116            }
117            showLine += "|";
118
119            boxLine = "| Box Office ID: " + boxOfficeId;
120            for (int i = boxLine.length(); i < ticketStringRowLength - 1; ++i) {
121                boxLine += " ";
122            }
123            boxLine += "|";
124
125            seatLine = "| Seat: " + seat.toString();
126            for (int i = seatLine.length(); i < ticketStringRowLength - 1; ++i) {
127                seatLine += " ";
128            }
129            seatLine += "|";
130
131            clientLine = "| Client: " + client;
132            for (int i = clientLine.length(); i < ticketStringRowLength - 1; ++i) {
133                clientLine += " ";
134            }
135            clientLine += "|";
136
137            result = dashLine + eol +
138                    showLine + eol +
139                    boxLine + eol +
140                    seatLine + eol +
141                    clientLine + eol +
142                    dashLine;
143
144            return result;
145        }
146    }
147
148    public Theater(int numRows, int seatsPerRow, String show) {
149        this.show = show;
150        this.seatsPerRow = seatsPerRow;
151        this.totalSeats = numRows * seatsPerRow;
152        currentSeat = 0;
153        transactions = new ArrayList<>();
154    }
155
156    /**
157     * Calculates the best seat not yet reserved
158     *
159     * @return the best seat or null if theater is full
160     */
161    public Seat bestAvailableSeat() {
162        if (currentSeat == totalSeats) {
163            if (!notifiedAllOut) {
164                System.out.println("Sorry, we are sold out!");
165                notifiedAllOut = true;
166            }
167            return null;
168        }
169        return new Seat(currentSeat, seatsPerRow);
170    }
171
172    /**
173     * Prints a ticket for the client after they reserve a seat
174     * Also prints the ticket to the console
175     *
176     * @param seat a particular seat in the theater
177     * @return a ticket or null if a box office failed to reserve the seat
178     */
179    public Ticket printTicket(String boxOfficeId, Seat seat, int client) {
180        Ticket t;
181        synchronized (transactions) {
182            if (transactions.size() > 0 && transactions.get(transactions.size() - 1).seat.compareTo(seat) >= 0) {
183                return null;
184            }
185            ++currentSeat;
186            t = new Ticket(show, boxOfficeId, seat, client);
187            transactions.add(t);
188        }
189        System.out.println(t);
190        try {
191            Thread.sleep(getPrintDelay());
192        } catch (InterruptedException e) {
193            e.printStackTrace();
194        }
195        return t;
196    }
197
198    /**
199     * Lists all tickets sold for this theater in order of purchase
200     *
201     * @return list of tickets sold
202     */
203    public List<Ticket> getTransactionLog() {
204        return transactions;
205    }
206}