Instructions
Requirements and Specifications
Source Code
MAIN
from MyModule import *
# Creathe the cinema
cinema = Cinema(15, 5)
cinema.add_movie("Spiderman: No Way Home", "16:00", 16, "New York")
cinema.add_movie("Spiderman: No Way Home", "13:00", 16, "Miami")
cinema.add_movie("Spiderman: No Way Home", "18:00", 16, "Texas")
cinema.add_movie("Avengers: Endgame", "21:00", 13, "Florida")
cinema.add_movie("Avatar", "22:00", 15, "New York")
cinema.add_movie("Avatar", "21:00", 15, "Texas")
cinema.add_movie("Avengers: Endgame", "22:00", 15, "New York")
cinema.add_movie("Avengers: Endgame", "21:00", 15, "Miami")
cinema.add_movie("Avengers: Endgame", "20:00", 15, "Florida")
if __name__ == '__main__':
# Start program
option = -1
while option != 6:
option = menu()
if option == 1:
print(str(cinema))
elif option == 2: # buy ticket
# Ask for movie name
name = input("Enter movie name: ")
if cinema.movie_exist(name):
# Ask for age
age = getInt("Enter age: ")
# Ask for time
time = input("Enter time: ")
# Ask for location
location = input("Enter location: ")
# Get movie
movie = cinema.get_movie_loc_time(name, location, time)
# Check if the movie can be seen
if movie:
if age >= movie.age and time == movie.time:
# Ask for seat
movie.print_seats()
row = getInt("Please enter row: ")
col = getInt("Please enter column: ")
if movie.is_free(row-1, col-1):
movie.sell_seat(row-1, col-1)
print(f"You bought the ticket at position ({row},{col})")
else:
print("Sorry, this seat is not available.")
else:
print("Sorry, you don't have the enough age or the movie is not playing at that time.")
else:
print("Sorry, that movie is not playing in the chosen location.")
else:
print("Sorry, there is no movie with that name.")
elif option == 3:
# get name
name = input("Enter movie name: ")
location = input("Enter location: ")
time = input("Enter time: ")
if cinema.movie_exist(name):
movie = cinema.get_movie_loc_time(name, location, time)
movie.print_seats()
else:
print("Sorry, there is no movie with that name.")
elif option == 4:
location = input("Enter location: ")
# Check movies with that location
found = False
ret = ""
for movie in cinema.movies:
if movie.location.lower() == location.lower():
ret += str(movie)+"\n"
found = True
if found:
print("The movies playing on " + location + " are:")
print(ret)
else:
print("Sorry, there are no movies playing at that location")
elif option == 5: # Movies by age
age = getInt("Enter age: ")
found = False
ret = ""
for movie in cinema.movies:
if age >= movie.age:
ret += str(movie) + "\n"
found = True
if found:
print(f"The movies for age {age}+ are:")
print(ret)
else:
print("Sorry, there are no movies for that age.")
elif option == 6:
print("Good bye!")
break
print()
MODULE
class Movie:
"""
This class represents a movie object with name, time and seats
"""
def __init__(self, name: str, time: str, age: int, location: str, nrows: int, ncols: int):
self.name = name
self.time = time
self.age = age
self.rows = nrows
self.location = location
self.columns = ncols
# Initialize a list of elements that represents the seats
self.seats = []
for i in range(self.rows):
row = [0 for j in range(self.columns)]
self.seats.append(row)
self.free_seats = self.rows * self.columns
def is_free(self, row: int, column: int) -> bool:
return self.seats[row][column] == 0
def sell_seat(self, row:int, column: int):
self.seats[row][column] = 1
def print_seats(self):
ret = "\t"
for k in range(1, self.columns + 1):
ret += "{:>5}".format(k)
ret += "\n"
for i in range(1, self.rows + 1):
ret += str(i) + "\t"
for j in range(1, self.columns + 1):
if self.is_free(i - 1, j - 1):
ret += "{:>5}".format('-')
else:
ret += "{:>5}".format('X')
ret += "\n"
print(ret)
def __str__(self):
"""
This function returns a string representation of the cinema
:return: string
"""
ret = f"Movie: {self.name}\n\t\tTime: {self.time}\n\t\tLocation: {self.location}\n\t\tAge: {self.age}+\n"
return ret
class Cinema:
"""
This class represents a Cinema that contains movies
"""
def __init__(self, nrows: int, ncols: int):
"""
This function received the number of rows and columns representing the size of the cinema (number of seats)
:param nrows: number of rows
:param ncols: number of columns
"""
self.rows = nrows
self.columns = ncols
self.movies = []
def add_movie(self, name: str, time: str, age: int, location: str):
self.movies.append(Movie(name, time, age, location, self.rows, self.columns))
def movie_exist(self, name: str):
"""
Check if a movie exists given its name
:param name: str
:return: bool
"""
for movie in self.movies:
if movie.name.lower() == name.lower():
return True
return False
def get_movie(self, name: str) -> Movie:
"""
Get movie by name
:param name: name of the movie
:return: Movie object or None
"""
for movie in self.movies:
if movie.name.lower() == name.lower():
return movie
return None
def get_movie_loc(self, name: str, location: str) -> Movie:
"""
Get movie by name and location
:param name: name of the movie
:param location: location
:return: Movie object or None
"""
for movie in self.movies:
if movie.name.lower() == name.lower() and movie.location.lower() == location.lower():
return movie
return None
def get_movie_loc_time(self, name: str, location: str, time: str) -> Movie:
"""
Get movie by name, location and time
:param name: name of the movie
:param location: location
_param time: str
:return: Movie object or None
"""
for movie in self.movies:
if movie.name.lower() == name.lower() and movie.location.lower() == location.lower() and time == movie.time:
return movie
return None
def __str__(self):
"""
Returns info about the current movies
:return:
"""
ret = ""
if len(self.movies) > 0:
ret = "The movies playing are:\n"
for i, movie in enumerate(self.movies):
ret += f"\t{i+1}) " + str(movie) + "\n"
else:
ret = "There are no movies playing."
return ret
"""
This file contains helper functions
"""
def getInt(message: str) -> int:
"""
This function will request an integer from user
The function will keep prompting the user until s/he enters a valid value
:param message: str
:return: int
"""
while True:
try:
value = int(input(message))
if value > 0:
return value
else:
print("Please enter a positive number.")
except:
print("Please enter a valid number.")
def menu() -> int:
"""
Displays menu and ask for menu option
:return: int
"""
print("1) List all movies")
print("2) Buy ticket")
print("3) See available seats for movie")
print("4) Show movies by location")
print("5) Show movies by age")
print("6) Exit")
while True:
try:
option = int(input("Enter option: "))
if 1 <= option <= 6:
return option
else:
print("Please enter a valid option.")
except:
print("Please enter a valid option.")