Cinema Booking Challenge

A cinema has created a booking system for their main theatre which consists of 48 seats, organised in 6 rows of 8 seats.

To store information as to whether a seat is booked or available, the program uses a 2-dimensional array (in python a list of lists). Each cell of the array contains the value 1 if the seat is booked or 0 if it is empty:

The following code is used to check whether a specific seat (identified by its row and column number) is already booked or not:

Challenge #1: Booking Options


Add a menu system to this code with 4 otpions:

  • Option 1: Book a seat by row/column
  • Option 2: Book a seat close to the front
  • Option 3: Book a seat close to the back
  • Option X: Exit
View Solution
#Cinema Booking Challenge - www.101computing.net/cinema-booking-challenge
seats = []
seats.append([0,0,1,1,0,1,1,1])
seats.append([0,1,1,0,0,1,0,1])
seats.append([1,0,0,1,0,1,1,0])
seats.append([0,1,1,1,0,0,0,1])
seats.append([0,0,1,1,0,1,0,0])
seats.append([1,0,1,1,0,0,1,1])

def displayBookings():
  #Display Bookings
  print("")
  print("======================================")
  print("")
  for row in seats:
    print(row)
  print("")
  print("======================================")

def checkSeat():
  row = int(input("Enter a row number (between 0 and 5)"))
  column = int(input("Enter a column number (between 0 and 7)"))
  
  if seats[row][column]==1:
    print("This seat is already booked.")
  else:
    print("This seat is empty.")

def bookSeat():
  print("Booking a Seat by Row/Column")
  #....
    
def bookSeatAtFront():
  print("Booking seat at the front")
  #....
  
def bookSeatAtBack():
  print("Booking seat at the back")
  #....
  

#Main Program Starts Here
print("+============================+")
print("+   CINEMA BOOKING SYSTEM    +")
print("+============================+")
print("")
print("1 - Book a seat by row/column")
print("2 - Book a seat at the front")
print("3 - Book a seat at the back")
print("x - Exit")

choice = input("Your Choice?")
if choice=="1":
  bookSeat()
  displayBookings()
elif choice=="2":
  bookSeatAtFront()
  displayBookings()
elif choice=="3":
  bookSeatAtBack()
  displayBookings()
elif choice=="x":
  print("Good Bye!")
else:
  print("Invalid Menu Option")
  print("Good Bye!")

Option 1:

Let the user enter a row and column number. If the seat is available, book it by changing the corresponding value in the array to a 1. If it’s not available carry on asking for a row and column number until a free seat is found.

View Solution
def bookSeat():
  booked = False
  while booked == False:
    row = int(input("Enter a row number (between 0 and 5)"))
    column = int(input("Enter a column number (between 0 and 7)"))

    if seats[row][column]==1:
      print("This seat is already booked.")
    else:
      print("This seat is empty.")
      print("Booking seat...")
      seats[row][column]=1
      print("We have now booked this seat for you.")
      booked=True

Option 2:

If the user chooses this option the program should automatically book the first seat available starting from the front row (row 0) from the left (column 0), and scanning each seat one by one until a free seat is found. The program should inform the user which seat (row/column) has been booked.

View Solution
def bookSeatAtFront():
  print("Booking seat at the front")
  for row in range(0,6):
    for column in range(0,8):
      if seats[row][column]==0:
        print("Booking seat...")
        print("Row: " + str(row))
        print("Column: " + str(column))
        seats[row][column]=1
        print("We have now booked this seat for you.")
        #Stop Searching
        return True
  #We scanned the whole theatre without finding an empty seat:
  print("Sorry the theatre is full - Cannot make a booking")
  return False 

Option 3:

If the user chooses this option the program should automatically book the first seat available starting from the back row (row 5) from the right (column 7), and scanning each seat one by one until a free seat is found. The program should inform the user which seat (row/column) has been booked.

View Solution
def bookSeatAtBack():
  print("Booking seat at the back")
  for row in range(5,-1,-1):
    for column in range(7,-1,-1):
      if seats[row][column]==0:
        print("Booking seat...")
        print("Row: " + str(row))
        print("Column: " + str(column))
        seats[row][column]=1
        print("We have now booked this seat for you.")
        #Stop Searching
        return True
  #We scanned the whole theatre without finding an empty seat:
  print("Sorry the theatre is full - Cannot make a booking")
  return False   

Challenge #2: Saving Bookings in a CSV file


Create a CSV file containing the following data:
0,0,1,1,0,1,1,1
0,1,1,0,0,1,0,1
1,0,0,1,0,1,1,0
0,1,1,1,0,0,0,1
0,0,1,1,0,1,0,0
1,0,1,1,0,0,1,1

  • When the program starts, the seats array should be initialised by reading the content of the CSV file.
  • View Solution
    def loadBookings():
      file = open("seats.csv","r")
      row = 0
      for line in file:
        data = line.split(",")
              if len(data)==8: #Only process lines which contain 8 values
                 for column in range (0,8):
                    seats[row][column] = int(data[column])
                 row = row + 1
      file.close()
  • When the user exit the program (option X), the content of the CSV file should be replaced with the content of the seats array.
  • View Solution
    def saveBookings():
      file = open("seats.csv","w")
      for row in range(0,6):
        line=""
        for column in range(0,8):
          line = line + str(seats[row][column]) + ","
        line = line[:-1] + ("\n") #Remove last comma and add a new line
        file.write(line)    
      file.close()

To complete this challenge you will need to read more about how to read through a CSV file.

Challenge #3: Resetting the Array


Add an extra menu option (option 4). If this option is chosen, the seats array should automatically be reset so that all seats are reset to 0.

Challenge #4: Improvements


Can you think of any other features that could be useful to improve this cinema booking system? This could include:

  • A login screen so that only authorised staff can access the booking system,
  • An option to cancel a booking,
  • A message to inform the end-user of how many seats are left,
  • A message to inform the end-user when the theatre is full (all the seats have been booked),
  • Input validation (e.g. row number between 0 and 5, column number between 0 and 7),
  • Multiple bookings where the computer can identify if the user wants to book several (e.g. 2 or 3) consecutive seats on the same row,
  • etc.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.7 / 5. Vote count: 22

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , , , , , , ,