Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- datetime_list = []
- class Flights:
- def __init__(self, flightid, day, month, capacity, date_time):
- self.__flight_id = flightid
- self.__flight_day = day
- self.__flight_month = month
- self.__flight_date = str(self.__flight_day) + "." + str(self.__flight_month) + "."
- self.__datetime = date_time
- self.__capacity = int(capacity)
- self.__amount_booked = 0
- self.__cancelled = False
- self.__passenger_list = []
- self.__identity = str(flightid + self.__flight_date)
- def return_date(self):
- return self.__flight_date
- def add_datetime_list(self):
- list1 = [self.__flight_id, self.__datetime]
- datetime_list.append(list1)
- def get_identity(self):
- thing = f_dictionary[self]
- return thing
- def can_you_book(self):
- if self.__amount_booked < self.__capacity:
- return True
- def has_anyone_booked(self):
- if self.__amount_booked == 0:
- return False
- def add_passenger(self, name):
- self.__passenger_list.append(name)
- self.__amount_booked += 1
- def cancel_flight(self):
- self.__cancelled = True
- return self.__passenger_list
- def cancel_bookings(self):
- self.__passenger_list = []
- def cancelled_maybe(self):
- return self.__cancelled
- def find_new_flight(self):
- rebook_list = []
- only_date_list = []
- flight_month = str(self.__datetime.month)
- flight_day = str(self.__datetime.day)
- for i in range(len(datetime_list)):
- if datetime_list[i][0] == self.__flight_id:
- if datetime_list[i][1] != self.__datetime:
- rebook_list.append(datetime_list[i])
- for t in range(len(rebook_list)):
- new_flight_month = str(rebook_list[t][1].month)
- if new_flight_month > flight_month:
- only_date_list.append(rebook_list[t][1])
- new_flight_day = str(rebook_list[t][1].day)
- if new_flight_month == flight_month:
- if new_flight_day >= flight_day:
- only_date_list.append(rebook_list[t][1])
- date = self.__datetime
- closest_date = min(only_date_list, key=lambda d: abs(d - date))
- for r in range(len(rebook_list)):
- if rebook_list[r][1] == closest_date:
- new_id = rebook_list[r][0]
- return closest_date, new_id, self.__passenger_list, self.__flight_date
- def move_passengers(self, old_flight, movables_list):
- for p in range(len(movables_list)):
- self.__passenger_list.append(movables_list[p])
- old_flight.__passenger_list = []
- old_flight.__amount_booked = 0
- self.__amount_booked = len(self.__passenger_list)
- return self.__passenger_list
- def print_passengers(self):
- amount = self.__capacity - self.__amount_booked
- return self.__passenger_list, amount
- class Passengers:
- def __init__(self, name):
- self.__passenger_name = name
- self.__booked_flights = []
- def be_rebooked(self, the_id, old_time, new_time):
- old_list = []
- old_list.append(the_id)
- old_list.append(old_time)
- old_flight = str(old_list)
- for b in range(len(self.__booked_flights)):
- a_booked_flight = str(self.__booked_flights[b])
- if a_booked_flight == old_flight:
- self.__booked_flights.remove(self.__booked_flights[b])
- def book_flight(self, flight_id, date):
- flight_list = [flight_id, date]
- self.__booked_flights.append(flight_list)
- def cancel_a_flight(self, c_id, c_date):
- c_flight = [c_id, c_date]
- for h in range(len(self.__booked_flights)):
- if self.__booked_flights[h] == c_flight:
- self.__booked_flights.remove(self.__booked_flights[h])
- def print_all_flights(self):
- return self.__booked_flights
- import datetime
- f_dictionary = {}
- datetime_list = []
- all_passengers = []
- all_ids = []
- f_dictionary = {}
- p_dictionary = {}
- def get_name(flight_id, date2):
- flight_key = str(flight_id + date2)
- name = f_dictionary[flight_key]
- return name
- def get_name_name(p_name):
- p_name = str(p_name)
- namename = p_dictionary[p_name]
- return namename
- def create_flight(clist):
- success = False
- try:
- f_id = clist[1]
- day = clist[2]
- month = clist[3]
- capacity = clist[4]
- except:
- print("There wasn't enough items in this input.")
- input()
- return success
- id_list = []
- correct1 = True
- correct2 = True
- correct3 = True
- correct4 = True
- for f in f_id:
- id_list.append(f)
- count = 0
- try:
- for p in id_list:
- test = p
- if count < 2:
- if test.isalpha() == False:
- correct1 = False
- elif count > 2:
- test = int(test)
- count += 1
- except:
- correct1 = False
- if len(id_list) != 5:
- correct1 = False
- try:
- day2 = float(day)
- if day2 < 1 or day2 > 30:
- correct2 = False
- except:
- correct2 = False
- try:
- month2 = float(month)
- if month2 < 1 or month2 > 12:
- correct3 = False
- except:
- correct3 = False
- try:
- capacity2 = float(capacity)
- if capacity2 < 0:
- correct4 = False
- except:
- correct4 = False
- if correct1 == False or correct2 == False or correct3 == False or correct4 == False:
- print("Flight couldn't be added to the database.")
- print("Following inputs were invalid:")
- print("")
- if correct1 == False:
- print("- Flight ID")
- if correct2 == False:
- print("- Day")
- if correct3 == False:
- print("- Month")
- if correct4 == False:
- print("- Capacity")
- input()
- else:
- all_ids.append(f_id)
- year = 2019
- datetime_object = datetime.date(year, int(month), int(day))
- flight = Flights(f_id, day, month, capacity, datetime_object)
- date = str(day) + "." + str(month) + "."
- identity = str(f_id + date)
- f_dictionary[identity] = flight
- flight.add_datetime_list()
- print("Flight {:s} on {:s} has been added to the database.".format(f_id, date))
- input()
- def add_passenger(c_list):
- success = False
- try:
- flight_id2 = c_list[1]
- date = c_list[2]
- passenger_name = c_list[3]
- except:
- print("There wasn't enough items in this input.")
- input()
- return success
- try:
- flight_self = get_name(flight_id2, date)
- available = flight_self.can_you_book()
- if available == True:
- flight_self.add_passenger(passenger_name)
- if passenger_name not in all_passengers:
- passenger = Passengers(passenger_name)
- p_dictionary[passenger_name] = passenger
- all_passengers.append(passenger_name)
- passenger.book_flight(flight_id2, date)
- else:
- passengerr = get_name_name(passenger_name)
- passengerr.book_flight(flight_id2, date)
- print("Passenger {:s} has been booked to flight {:s} on {:s}".format(passenger_name, flight_id2, date))
- input()
- else:
- print("Unfortunately this flight is fully booked.")
- input()
- except:
- print("Flight with this ID and date doesn't exist.")
- input()
- def cancel_flight_and_shit(c_list):
- success = False
- try:
- flight_id = c_list[1]
- date = c_list[2]
- except:
- print("There wasn't enough items in this input.")
- input()
- return success
- valid = True
- try:
- flight = get_name(flight_id, date)
- any_passengers = flight.has_anyone_booked()
- cancelled = flight.cancelled_maybe()
- if cancelled == True:
- print("Flight {:s} has already been cancelled.".format(flight_id))
- input()
- except:
- valid = False
- print("This flight doesn't exist.")
- input()
- if valid == True:
- try:
- newdate, newid, movables, old_date = flight.find_new_flight()
- newdate = str(newdate.day)+ "." + str(newdate.month) + "."
- new_flight = get_name(newid, newdate)
- if any_passengers == False:
- flight.cancel_flight()
- print("Flight {:s} on {:s} has been cancelled. There weren't any bookings on this flight".format(flight_id, date))
- input()
- else:
- new_flight.move_passengers(flight, movables)
- for s in range(len(movables)):
- moving_passenger = get_name_name(movables[s])
- moving_passenger.be_rebooked(flight_id, old_date, newdate)
- moving_passenger.book_flight(flight_id, newdate)
- flight.cancel_flight()
- print("Flight {:s} on {:s} has been cancelled.".format(flight_id, date))
- print("Passengers on this flight have been moved to another {:s} flight on {:s}".format(flight_id, newdate))
- input()
- except:
- print("Unfortunately there aren't any other flights with this ID.")
- if any_passengers == False:
- what_next = print("Do you still want to cancel the flight {:s} on {:s}(Yes/No)? No bookings have been made to this flight.".format(flight_id, date))
- else:
- what_next = input("Do you want to cancel the flight anyway(Yes/No)? If so, all the bookings on this flight will be cancelled.\n")
- options = ["Yes", "No"]
- while what_next not in options:
- what_next = input("Enter Yes or No\n")
- if what_next == "Yes":
- if any_passengers == False:
- flight.cancel_flight()
- print("")
- print("Flight {:s} on {:s} has been cancelled.".format(flight_id, date))
- input()
- else:
- cancel_these = flight.cancel_flight()
- for w in range(len(cancel_these)):
- cancel_name = cancel_these[w]
- cancel_code = get_name_name(cancel_name)
- cancel_code.cancel_a_flight(flight_id, date)
- flight.cancel_bookings()
- flight.cancel_flight()
- print("")
- print("Flight {:s} and all of it's bookings have been cancelled.".format(flight_id))
- input()
- else:
- print("")
- print("The flight wasn't cancelled.")
- input()
- def my_passengers(c_list):
- success = False
- try:
- flight_id = c_list[1]
- date = c_list[2]
- except:
- print("There wasn't enough items in this input.")
- input()
- return success
- try:
- flight_self = get_name(flight_id, date)
- p_list, amount = flight_self.print_passengers()
- cancelled = flight_self.cancelled_maybe()
- any_bookings = flight_self.has_anyone_booked()
- if cancelled == False:
- if any_bookings == False:
- print("No bookings have been to flight {:s} on {:s} yet.".format(flight_id, date))
- input()
- else:
- print("Passengers that have been booked to flight {:s} on {:s}:".format(flight_id, date))
- print("")
- for g in p_list:
- print("- ", g)
- print("")
- print("There are {:d} vacant seats left on this flight.".format(amount))
- input()
- else:
- print("This flight has been cancelled.")
- input()
- except:
- print("This flight doesn't exist.")
- input()
- def my_flights(c_list):
- p_name = c_list[1]
- try:
- the_code = get_name_name(p_name)
- try:
- flight_list = the_code.print_all_flights()
- print("")
- print("Flights booked by {:s}:".format(p_name))
- for c in range(len(flight_list)):
- print1 = str(flight_list[c][0])
- print2 = str(flight_list[c][1])
- print("- Flight {:s} on {:s}".format(print1, print2))
- input()
- except:
- print("This passenger hasn't booked any flights.")
- input()
- except:
- print("This passenger hasn't booked any flights.")
- input()
- def start_menu():
- print("")
- print("1. CREATE A FLIGHT:")
- print("- Enter 1, FlightID (two letters and three numbers), day (1-30), month (1-12) and capacity (over zero)")
- print("")
- print("2. BOOK PASSENGER ON A FLIGHT:")
- print("- Enter 2, FlightID, date (for example 1.1.) and name of the passenger")
- print("")
- print("3. CANCEL FLIGHT AND TRANSFER BOOKINGS TO ANOTHER ONE:")
- print("- Enter 3, FlightID and date (for example 1.1.)")
- print("")
- print("4. LIST OF PASSENGERS ON A FLIGHT:")
- print("- Enter 4, Flight ID and date (for example 1.1.)")
- print("")
- print("5. LIST OF BOOKINGS A PASSENGER HAS:")
- print("- Enter 5 and name of the passenger")
- print("")
- print("6. TO EXIT PROGRAM enter exit.")
- choiselist = []
- choise1 = input()
- print("")
- choise1 = choise1.split(",")
- for g in choise1:
- choiselist.append(g)
- choise0 = choiselist[0]
- return choiselist, choise0
- def main():
- print("")
- print("Welcome to Alma Airlines booking system!")
- input()
- print("Please choose a number corresponding to the action you want take and enter the needed information as follows (separate all items with ',':")
- input()
- choise_list, choise = start_menu()
- actions = ["1", "2", "3", "4", "5", "6", "exit", "Exit", "EXIT"]
- while choise != "exit" and choise != "Exit" and choise != "EXIT" and choise != "6":
- if choise not in actions or len(choise_list) < 2:
- print("This isn't a valid input. Press enter to continue.\n")
- input()
- choise_list, choise = start_menu()
- else:
- if choise == "1":
- create_flight(choise_list)
- elif choise == "2":
- add_passenger(choise_list)
- elif choise == "3":
- cancel_flight_and_shit(choise_list)
- elif choise == "4":
- my_passengers(choise_list)
- elif choise == "5":
- my_flights(choise_list)
- print("WHAT NEXT?")
- choise_list, choise = start_menu()
- if choise == "6":
- print("You jumped from a plane.")
- print("Your life has ended.")
- input()
- print("Budget airlines aren't always a safe choise.")
- input()
- else:
- print("Program has ended.")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement