-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_io.py
278 lines (220 loc) · 8.48 KB
/
command_io.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from cinema_database import CinemaDatabase
from hall import Hall
from settings import DB_NAME
class CommandIO:
@staticmethod
def start():
database = CinemaDatabase(DB_NAME)
database.prepare()
CommandIO.welcome()
CommandIO.menu()
CommandIO.input_command(database)
@staticmethod
def welcome():
print()
print('-' * 44)
print("Welcome to CINEMA RESERVATION SYSTEM. Enjoy!")
print('-' * 44)
@staticmethod
def menu():
print()
print("Please choose a command:")
print()
print("show_movies")
print("show_movie_projections")
print("make_reservation")
print("cancel_reservation")
print("menu")
print("help")
print("exit")
print()
@staticmethod
def input_command(database):
command = ""
while command != "exit":
command = input("command>")
commands = CommandIO.command_parser(command)
CommandIO.command_choice(database, *commands)
@staticmethod
def command_parser(command):
commands = command.split()
return (tuple(commands))
@staticmethod
def command_choice(database, command, *keywords):
if command == "show_movies":
CommandIO.show_movies(database)
elif command == "show_movie_projections":
CommandIO.show_movie_projections(database, *keywords)
elif command == "make_reservation":
CommandIO.make_reservation(database)
elif command == "cancel_reservation":
CommandIO.cancel_reservation(database, *keywords)
elif command == "menu":
CommandIO.menu()
elif command == "help":
CommandIO.help()
elif command == "exit":
CommandIO.exit(database)
else:
print("Wrong choice! Please try again!")
@staticmethod
def show_movies(database):
print("Current movies with their rating:")
movies = database.show_all_movies()
for movie in movies:
movie_id = movie[0]
name = movie[1]
rating = movie[2]
print("[{}] - {} --- {}".format(movie_id, name, rating))
@staticmethod
def show_movie_projections(database, movie_id, *date):
movie_name = database.get_movie_name(movie_id)
print("Projections for movie: '{}':".format(movie_name))
projections = database.show_movie_projections(movie_id, *date)
for projection in projections:
projection_id = projection[0]
projection_type = projection[1]
date = projection[2]
time = projection[3]
busy_seats = database.count_non_available_seats(projection_id)
free_seats = Hall.SEATS - busy_seats
first_part = "[{}] - {} - {} - ".format(projection_id, date, time)
second_part = "({}) - ".format(projection_type)
third_part = "Available Seats: {}".format(free_seats)
message = first_part + second_part + third_part
print(message)
@staticmethod
def cancel_reservation(database, name):
database.delete_reservation(name)
print("{} you reservation was deleted!".format(name))
print("Have a nice day!")
@staticmethod
def help():
print()
print ("1. show_movies")
print("Show movies ordered by rating descending!")
print ("2. show_movie_projections <movie_id> [<date>]")
print("Date is not mandatory! But you must type movie_id!")
print ("3. make_reservation")
print("You must follow the steps and everything will be okay!")
print ("4. cancel_reservation <name>")
print("Delete your reservation from our system!")
print ("5. help")
print("This command will show you this menu!")
print ("6. exit")
print("You will exit our system! Be carefull!")
print()
@staticmethod
def exit(database):
database.close_connection()
print("\nRservation System closed doors!")
print("We will see you next time! Goodbye!\n")
@staticmethod
def input_name():
name = input("Please enter your name>")
return name
@staticmethod
def input_tickets():
tickets = int(input("Please enter number of tickets>"))
while tickets > Hall.SEATS:
print("Maximum seats are 100!")
tickets = int(input("Please enter number of tickets>"))
return tickets
@staticmethod
def input_movie_id():
movie_id = int(input("Please enter id of movie you want to watch>"))
return movie_id
@staticmethod
def input_projection_id():
projection_id = int(input("Please enter id of the projection>"))
return projection_id
@staticmethod
def check_seats(database, projection_id, number_of_tickets):
busy_seats = database.count_non_available_seats(projection_id)
free_seats = Hall.SEATS - busy_seats
if free_seats < number_of_tickets:
return False
else:
return True
@staticmethod
def append_busy_seats(hall, seats):
for seat in seats:
row = seat[0]
col = seat[1]
hall.set_busy_seat(row, col)
@staticmethod
def choose_seats(busy_seats, number_of_tickets):
print("Choose row and column for every ticket!")
all_seats = []
while number_of_tickets > 0:
row = None
col = None
is_inside_hall = False
seat_taken = False
while not is_inside_hall or not seat_taken:
is_inside_hall = False
seat_taken = False
row = int(input("Please choose row>"))
if row < Hall.MIN_ROW or row > Hall.MAX_ROW:
print("Such row didn't exists!")
is_inside_hall = False
continue
col = int(input("Please choose col>"))
if col < Hall.MIN_COL or col > Hall.MAX_COL:
print("Such column didn't exists!")
is_inside_hall = False
continue
is_inside_hall = True
if (row, col) in busy_seats:
print("The seats are already taken!")
seat_taken = False
continue
seat_taken = True
all_seats.append((row, col))
number_of_tickets -= 1
print("Tickets left: {}".format(number_of_tickets))
return all_seats
@staticmethod
def print_movie_name(database, movie_id):
current_movie = database.get_movie_name(movie_id)
print("Movie: {}".format(current_movie))
@staticmethod
def print_projection_info(database, projection_id, seats):
current_projection = database.show_projection(projection_id)
date = current_projection[0]
time = current_projection[1]
type = current_projection[2]
print("Date and Time: {} {} ({})".format(date, time, type))
print("Seats: {}".format(seats))
@staticmethod
def finalize(database, name, projection_id, all_seats):
finalize = input("type 'finalize'>")
if finalize == "finalize":
for seat in all_seats:
database.add_reservation(name, projection_id, seat[0], seat[1])
print("Thanks")
@staticmethod
def make_reservation(database):
print("Step 1:")
name = CommandIO.input_name()
tickets = CommandIO.input_tickets()
print("Step 2:")
CommandIO.show_movies(database)
movie_id = CommandIO.input_movie_id()
print("Step 3:")
is_seats = False
while not is_seats:
CommandIO.show_movie_projections(database, movie_id)
projection_id = CommandIO.input_projection_id()
is_seats = CommandIO.check_seats(database, projection_id, tickets)
busy_seats = database.get_non_available_seats(projection_id)
new_hall = Hall()
CommandIO.append_busy_seats(new_hall, busy_seats)
new_hall.print_places()
print("Step 4: ")
choosen_seats = CommandIO.choose_seats(busy_seats, tickets)
CommandIO.append_busy_seats(new_hall, choosen_seats)
CommandIO.print_movie_name(database, movie_id)
CommandIO.print_projection_info(database, projection_id, choosen_seats)
print("Step 5:")
CommandIO.finalize(database, name, projection_id, choosen_seats)