-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheaterSeatManagementApp.java
192 lines (169 loc) · 10.4 KB
/
TheaterSeatManagementApp.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package gr.aueb.cf.ch10miniprojects;
import java.util.Scanner;
/**
* Theater booking management application. Uses characters for columns and numbers for rows. Example: Seat C2 is at second
* row , third column. The whole theater has 30 rows and 12 columns. Has methods for booking a row if it is not
* already booked. Method for booking cancellation for booked seats.
*/
public class TheaterSeatManagementApp {
public static void main(String[] args) {
boolean[][] seats = new boolean[12][30];
printCinemaLabel();
unBookAllSeats(seats);
printMenu(seats);
}
public static void printMenu(boolean[][] seats){
int selection = 0;
System.out.print("\nΚαλωσήρθατε στο σύστημα booking θέσεων του Coding Factory Cinema!! ");
do {
Scanner in = new Scanner(System.in);
System.out.println("\nΠαρακαλώ επιλέξτε ένα από τα παρακάτω: ");
System.out.printf("1. Εμφάνιση της κάτοψης του θεάτρου %n2. Έλεγχος διαθεσιμότητας για θέση και κράτηση %n3. Διαγραφή κράτησης %n4. Διαγραφή όλων των κρατήσεων %n5. Έξοδος%n");
if(in.hasNextInt()){
selection = in.nextInt();
}
if(selection == 1){
printVisualSeparator();
System.out.println("Επιλέξατε εμφάνιση κάτοψης.");
printVisualSeparator();
revealCinemaDiagram(seats);
}else if(selection == 2){
System.out.println("Επιλέξατε διαθεσιμότητα και κράτηση. Δώστε στήλη και σειρά θέσης.");
book(getUserInput(),seats);
}else if(selection == 3){
System.out.println("Επιλέξατε διαγραφή κράτησης Δώστε στήλη και σειρά θέσης.");
cancel(getUserInput(),seats);
}else if(selection == 4){
printVisualSeparator();
System.out.println("Επιλέξατε διαγραφή όλων των κρατήσεων.");
unBookAllSeats(seats);
System.out.println("Επιτυχημένη διαγραφή όλων των κρατήσεων");
}else if(selection <= 0 | selection>5){
printVisualSeparator();
System.out.println("Η επιλογή που δώσατε δεν ήταν σωστή!!!!!");
}
}while(selection != 5);
System.out.println("Ευχαριστούμε πολύ!");
}
/**
* Service layer. Manages the booking process , and provides feedback at the user. Has as input the seat
* of choice as an array of int and the theater as an array of boolean.
* @param seat The column-row seat selection as an array with length of two.
* @param seats The theater seats as a boolean array (columns , rows)
*/
public static void book(int[] seat , boolean[][] seats) {
if(seatIsAvailable(seat,seats)) {
seats[seat[0]][seat[1]] = true;
printVisualSeparator();
System.out.println("Η θέση : [" + ((char)( (seat[0]) + 65) ) + "][" + (seat[1] + 1) + "] ήταν διαθέσημη και μόλις την κλείσατε!");
printVisualSeparator();
}else{
printVisualSeparator();
System.out.println("Συγνώμη η θέση: [" + ((char)( (seat[0]) + 65) ) + "][" + (seat[1] + 1) + "] είναι ήδη κλεισμένη !");
printVisualSeparator();
}
}
/**
* Returns true if the selected seat is available. Has as signature an array with the selected seat and
* an array with the theater seats.
* @param seat The selected theater seat.
* @param seats The theater seats as a boolean array (columns , rows)
* @return True if the selected seat is available(if it is false)
*/
public static boolean seatIsAvailable(int[] seat , boolean[][] seats){
boolean available = false;
if (!seats[seat[0]][seat[1]]){
available = true;
}
return available;
}
/**
* Takes as signature an array with the selected seat column and rows as the two numbers in an array.
* Takes as signature the theater seats as a boolean array.
* @param seat The selected theater seat.
* @param seats The theater seats as a boolean array (columns , rows)
*/
public static void cancel(int[] seat , boolean[][] seats) {
if(!seatIsAvailable(seat,seats)) {
seats[seat[0]][seat[1]] = false;
printVisualSeparator();
System.out.println("Η θέση : [" + ((char) ((seat[0]) + 65) ) + "][" + (seat[1] + 1)+ "] ήταν κλεισμένη και μόλις την ακυρώσατε!");
printVisualSeparator();
}else{
printVisualSeparator();
System.out.println("Συγνώμη η θέση: [" + ((char) ((seat[0]) + 65)) + "][" + (seat[1] + 1) + "] ΔΕΝ είναι κλεισμένη");
printVisualSeparator();
}
}
/**
* Returns the two user inputs as an array with size of two. First for column , second for row.
* Handles exception in order to restore the state and provides message in case of invalid input.
* @return The array with the two user inputs.
*/
public static int[] getUserInput() {
int[] userInput = new int[2];
String letterNumber = "";
boolean invalidInput;
Scanner in = new Scanner(System.in);
do{
try{
invalidInput = false;
letterNumber = in.nextLine().trim().toUpperCase();
userInput[0] = letterNumber.charAt(0) - 65;
letterNumber = letterNumber.substring(1).trim();
userInput[1] = Integer.parseInt(letterNumber) - 1;
}catch (Exception e){
e.printStackTrace();
System.out.println("Λάθος εισαγωγή. Παρακαλώ δώστε ξανά τα στοιχεία της θέσης: ");
invalidInput = true;
}
if(userInput[0] > 11 || userInput[0] < 0 || userInput[1] < 0 || userInput[1] > 29) {
System.out.println("Λάθος εισαγωγή. Παρακαλώ δώστε ξανά τα στοιχεία της θέσης: ");
invalidInput = true;
}
}while(invalidInput);
return userInput;
}
/**
* Fills the seats array of booleans with false. indicating the empty bookings.
* @param seats The theater seats as a boolean array (columns , rows)
*/
public static void unBookAllSeats(boolean[][] seats) {
for (int i = 0; i < seats.length; i++){
for (int j = 0; j < seats[i].length; j++) {
seats[i][j] = false;
}
}
printVisualSeparator();
}
/**
* Prints on console (std output) the current state of the cinema seats as an array of booleans
* @param seats The theater seats as a boolean array (columns , rows)
*/
public static void revealCinemaDiagram(boolean[][] seats) {
for (int i = 0; i < seats.length; i++){
System.out.println();
for (int j = 0; j < seats[i].length; j++) {
System.out.print("["+ seats[i][j]+ "]");
}
}
}
/**
* Prints a visual separator on the std output wherever it is needed.
*/
public static void printVisualSeparator() {
System.out.println("-----------------------------------------------------");
}
/**
* Prints the Cinema label on the std output
*/
public static void printCinemaLabel() {
System.out.printf(" ╔═══╗ ╔═══╗ ╔═══╗ ╔══╗ ╔═╗ ╔╗ ╔═══╗ ╔═══╗ ╔═══╗ ╔═══╗ ╔════╗ ╔═══╗ ╔═══╗ ╔╗ ╔╗ ╔═══╗ ╔══╗ ╔═╗ ╔╗ ╔═══╗ ╔═╗╔═╗ ╔═══╗\n" +
" ║╔═╗║ ║╔═╗║ ╚╗╔╗║ ╚╣╠╝ ║║╚╗║║ ║╔═╗║ ║╔══╝ ║╔═╗║ ║╔═╗║ ║╔╗╔╗║ ║╔═╗║ ║╔═╗║ ║╚╗╔╝║ ║╔═╗║ ╚╣╠╝ ║║╚╗║║ ║╔══╝ ║║╚╝║║ ║╔═╗║\n" +
" ║║ ╚╝ ║║ ║║ ║║║║ ║║ ║╔╗╚╝║ ║║ ╚╝ ║╚══╗ ║║ ║║ ║║ ╚╝ ╚╝║║╚╝ ║║ ║║ ║╚═╝║ ╚╗╚╝╔╝ ║║ ╚╝ ║║ ║╔╗╚╝║ ║╚══╗ ║╔╗╔╗║ ║║ ║║\n" +
" ║║ ╔╗ ║║ ║║ ║║║║ ║║ ║║╚╗║║ ║║╔═╗ ║╔══╝ ║╚═╝║ ║║ ╔╗ ║║ ║║ ║║ ║╔╗╔╝ ╚╗╔╝ ║║ ╔╗ ║║ ║║╚╗║║ ║╔══╝ ║║║║║║ ║╚═╝║\n" +
" ║╚═╝║ ║╚═╝║ ╔╝╚╝║ ╔╣╠╗ ║║ ║║║ ║╚╩═║ ╔╝╚╗ ║╔═╗║ ║╚═╝║ ╔╝╚╗ ║╚═╝║ ║║║╚╗ ║║ ║╚═╝║ ╔╣╠╗ ║║ ║║║ ║╚══╗ ║║║║║║ ║╔═╗║\n" +
" ╚═══╝ ╚═══╝ ╚═══╝ ╚══╝ ╚╝ ╚═╝ ╚═══╝ ╚══╝ ╚╝ ╚╝ ╚═══╝ ╚══╝ ╚═══╝ ╚╝╚═╝ ╚╝ ╚═══╝ ╚══╝ ╚╝ ╚═╝ ╚═══╝ ╚╝╚╝╚╝ ╚╝ ╚╝\n" +
" \n");
}
}