Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.33 KB | None | 0 0
  1. datetime_list = []
  2. class Flights:
  3.  
  4. def __init__(self, flightid, day, month, capacity, date_time):
  5. self.__flight_id = flightid
  6. self.__flight_day = day
  7. self.__flight_month = month
  8. self.__flight_date = str(self.__flight_day) + "." + str(self.__flight_month) + "."
  9. self.__datetime = date_time
  10. self.__capacity = int(capacity)
  11. self.__amount_booked = 0
  12. self.__cancelled = False
  13. self.__passenger_list = []
  14. self.__identity = str(flightid + self.__flight_date)
  15.  
  16. def return_date(self):
  17. return self.__flight_date
  18.  
  19. def add_datetime_list(self):
  20. list1 = [self.__flight_id, self.__datetime]
  21. datetime_list.append(list1)
  22.  
  23. def get_identity(self):
  24. thing = f_dictionary[self]
  25. return thing
  26.  
  27. def can_you_book(self):
  28. if self.__amount_booked < self.__capacity:
  29. return True
  30.  
  31. def has_anyone_booked(self):
  32. if self.__amount_booked == 0:
  33. return False
  34.  
  35. def add_passenger(self, name):
  36. self.__passenger_list.append(name)
  37. self.__amount_booked += 1
  38.  
  39. def cancel_flight(self):
  40. self.__cancelled = True
  41. return self.__passenger_list
  42.  
  43. def cancel_bookings(self):
  44. self.__passenger_list = []
  45.  
  46. def cancelled_maybe(self):
  47. return self.__cancelled
  48.  
  49. def find_new_flight(self):
  50. rebook_list = []
  51. only_date_list = []
  52. flight_month = str(self.__datetime.month)
  53. flight_day = str(self.__datetime.day)
  54. for i in range(len(datetime_list)):
  55. if datetime_list[i][0] == self.__flight_id:
  56. if datetime_list[i][1] != self.__datetime:
  57. rebook_list.append(datetime_list[i])
  58. for t in range(len(rebook_list)):
  59. new_flight_month = str(rebook_list[t][1].month)
  60. if new_flight_month > flight_month:
  61. only_date_list.append(rebook_list[t][1])
  62. new_flight_day = str(rebook_list[t][1].day)
  63. if new_flight_month == flight_month:
  64. if new_flight_day >= flight_day:
  65. only_date_list.append(rebook_list[t][1])
  66. date = self.__datetime
  67. closest_date = min(only_date_list, key=lambda d: abs(d - date))
  68. for r in range(len(rebook_list)):
  69. if rebook_list[r][1] == closest_date:
  70. new_id = rebook_list[r][0]
  71. return closest_date, new_id, self.__passenger_list, self.__flight_date
  72.  
  73. def move_passengers(self, old_flight, movables_list):
  74. for p in range(len(movables_list)):
  75. self.__passenger_list.append(movables_list[p])
  76. old_flight.__passenger_list = []
  77. old_flight.__amount_booked = 0
  78. self.__amount_booked = len(self.__passenger_list)
  79. return self.__passenger_list
  80.  
  81. def print_passengers(self):
  82. amount = self.__capacity - self.__amount_booked
  83. return self.__passenger_list, amount
  84.  
  85.  
  86. class Passengers:
  87. def __init__(self, name):
  88. self.__passenger_name = name
  89. self.__booked_flights = []
  90.  
  91.  
  92. def be_rebooked(self, the_id, old_time, new_time):
  93. old_list = []
  94. old_list.append(the_id)
  95. old_list.append(old_time)
  96. old_flight = str(old_list)
  97. for b in range(len(self.__booked_flights)):
  98. a_booked_flight = str(self.__booked_flights[b])
  99. if a_booked_flight == old_flight:
  100. self.__booked_flights.remove(self.__booked_flights[b])
  101.  
  102. def book_flight(self, flight_id, date):
  103. flight_list = [flight_id, date]
  104. self.__booked_flights.append(flight_list)
  105.  
  106. def cancel_a_flight(self, c_id, c_date):
  107. c_flight = [c_id, c_date]
  108. for h in range(len(self.__booked_flights)):
  109. if self.__booked_flights[h] == c_flight:
  110. self.__booked_flights.remove(self.__booked_flights[h])
  111.  
  112. def print_all_flights(self):
  113. return self.__booked_flights
  114.  
  115.  
  116. import datetime
  117. f_dictionary = {}
  118. datetime_list = []
  119. all_passengers = []
  120. all_ids = []
  121. f_dictionary = {}
  122. p_dictionary = {}
  123.  
  124. def get_name(flight_id, date2):
  125. flight_key = str(flight_id + date2)
  126. name = f_dictionary[flight_key]
  127. return name
  128.  
  129. def get_name_name(p_name):
  130. p_name = str(p_name)
  131. namename = p_dictionary[p_name]
  132. return namename
  133.  
  134. def create_flight(clist):
  135. success = False
  136. try:
  137. f_id = clist[1]
  138. day = clist[2]
  139. month = clist[3]
  140. capacity = clist[4]
  141. except:
  142. print("There wasn't enough items in this input.")
  143. input()
  144. return success
  145. id_list = []
  146. correct1 = True
  147. correct2 = True
  148. correct3 = True
  149. correct4 = True
  150. for f in f_id:
  151. id_list.append(f)
  152. count = 0
  153. try:
  154. for p in id_list:
  155. test = p
  156. if count < 2:
  157. if test.isalpha() == False:
  158. correct1 = False
  159. elif count > 2:
  160. test = int(test)
  161. count += 1
  162. except:
  163. correct1 = False
  164.  
  165. if len(id_list) != 5:
  166. correct1 = False
  167. try:
  168. day2 = float(day)
  169. if day2 < 1 or day2 > 30:
  170. correct2 = False
  171. except:
  172. correct2 = False
  173. try:
  174. month2 = float(month)
  175. if month2 < 1 or month2 > 12:
  176. correct3 = False
  177. except:
  178. correct3 = False
  179. try:
  180. capacity2 = float(capacity)
  181. if capacity2 < 0:
  182. correct4 = False
  183. except:
  184. correct4 = False
  185. if correct1 == False or correct2 == False or correct3 == False or correct4 == False:
  186. print("Flight couldn't be added to the database.")
  187. print("Following inputs were invalid:")
  188. print("")
  189. if correct1 == False:
  190. print("- Flight ID")
  191. if correct2 == False:
  192. print("- Day")
  193. if correct3 == False:
  194. print("- Month")
  195. if correct4 == False:
  196. print("- Capacity")
  197. input()
  198. else:
  199. all_ids.append(f_id)
  200. year = 2019
  201. datetime_object = datetime.date(year, int(month), int(day))
  202. flight = Flights(f_id, day, month, capacity, datetime_object)
  203. date = str(day) + "." + str(month) + "."
  204. identity = str(f_id + date)
  205. f_dictionary[identity] = flight
  206. flight.add_datetime_list()
  207. print("Flight {:s} on {:s} has been added to the database.".format(f_id, date))
  208. input()
  209.  
  210. def add_passenger(c_list):
  211. success = False
  212. try:
  213. flight_id2 = c_list[1]
  214. date = c_list[2]
  215. passenger_name = c_list[3]
  216. except:
  217. print("There wasn't enough items in this input.")
  218. input()
  219. return success
  220. try:
  221. flight_self = get_name(flight_id2, date)
  222. available = flight_self.can_you_book()
  223. if available == True:
  224. flight_self.add_passenger(passenger_name)
  225. if passenger_name not in all_passengers:
  226. passenger = Passengers(passenger_name)
  227. p_dictionary[passenger_name] = passenger
  228. all_passengers.append(passenger_name)
  229. passenger.book_flight(flight_id2, date)
  230. else:
  231. passengerr = get_name_name(passenger_name)
  232. passengerr.book_flight(flight_id2, date)
  233. print("Passenger {:s} has been booked to flight {:s} on {:s}".format(passenger_name, flight_id2, date))
  234. input()
  235. else:
  236. print("Unfortunately this flight is fully booked.")
  237. input()
  238. except:
  239. print("Flight with this ID and date doesn't exist.")
  240. input()
  241.  
  242. def cancel_flight_and_shit(c_list):
  243. success = False
  244. try:
  245. flight_id = c_list[1]
  246. date = c_list[2]
  247. except:
  248. print("There wasn't enough items in this input.")
  249. input()
  250. return success
  251. valid = True
  252. try:
  253. flight = get_name(flight_id, date)
  254. any_passengers = flight.has_anyone_booked()
  255. cancelled = flight.cancelled_maybe()
  256. if cancelled == True:
  257. print("Flight {:s} has already been cancelled.".format(flight_id))
  258. input()
  259. except:
  260. valid = False
  261. print("This flight doesn't exist.")
  262. input()
  263.  
  264. if valid == True:
  265. try:
  266. newdate, newid, movables, old_date = flight.find_new_flight()
  267. newdate = str(newdate.day)+ "." + str(newdate.month) + "."
  268. new_flight = get_name(newid, newdate)
  269. if any_passengers == False:
  270. flight.cancel_flight()
  271. print("Flight {:s} on {:s} has been cancelled. There weren't any bookings on this flight".format(flight_id, date))
  272. input()
  273. else:
  274. new_flight.move_passengers(flight, movables)
  275. for s in range(len(movables)):
  276. moving_passenger = get_name_name(movables[s])
  277. moving_passenger.be_rebooked(flight_id, old_date, newdate)
  278. moving_passenger.book_flight(flight_id, newdate)
  279. flight.cancel_flight()
  280. print("Flight {:s} on {:s} has been cancelled.".format(flight_id, date))
  281. print("Passengers on this flight have been moved to another {:s} flight on {:s}".format(flight_id, newdate))
  282. input()
  283. except:
  284. print("Unfortunately there aren't any other flights with this ID.")
  285. if any_passengers == False:
  286. 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))
  287. else:
  288. 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")
  289. options = ["Yes", "No"]
  290. while what_next not in options:
  291. what_next = input("Enter Yes or No\n")
  292.  
  293. if what_next == "Yes":
  294. if any_passengers == False:
  295. flight.cancel_flight()
  296. print("")
  297. print("Flight {:s} on {:s} has been cancelled.".format(flight_id, date))
  298. input()
  299. else:
  300. cancel_these = flight.cancel_flight()
  301. for w in range(len(cancel_these)):
  302. cancel_name = cancel_these[w]
  303. cancel_code = get_name_name(cancel_name)
  304. cancel_code.cancel_a_flight(flight_id, date)
  305. flight.cancel_bookings()
  306. flight.cancel_flight()
  307. print("")
  308. print("Flight {:s} and all of it's bookings have been cancelled.".format(flight_id))
  309. input()
  310. else:
  311. print("")
  312. print("The flight wasn't cancelled.")
  313. input()
  314.  
  315. def my_passengers(c_list):
  316. success = False
  317. try:
  318. flight_id = c_list[1]
  319. date = c_list[2]
  320. except:
  321. print("There wasn't enough items in this input.")
  322. input()
  323. return success
  324. try:
  325. flight_self = get_name(flight_id, date)
  326. p_list, amount = flight_self.print_passengers()
  327. cancelled = flight_self.cancelled_maybe()
  328. any_bookings = flight_self.has_anyone_booked()
  329. if cancelled == False:
  330. if any_bookings == False:
  331. print("No bookings have been to flight {:s} on {:s} yet.".format(flight_id, date))
  332. input()
  333. else:
  334. print("Passengers that have been booked to flight {:s} on {:s}:".format(flight_id, date))
  335. print("")
  336. for g in p_list:
  337. print("- ", g)
  338. print("")
  339. print("There are {:d} vacant seats left on this flight.".format(amount))
  340. input()
  341. else:
  342. print("This flight has been cancelled.")
  343. input()
  344. except:
  345. print("This flight doesn't exist.")
  346. input()
  347.  
  348. def my_flights(c_list):
  349. p_name = c_list[1]
  350. try:
  351. the_code = get_name_name(p_name)
  352. try:
  353. flight_list = the_code.print_all_flights()
  354. print("")
  355. print("Flights booked by {:s}:".format(p_name))
  356. for c in range(len(flight_list)):
  357. print1 = str(flight_list[c][0])
  358. print2 = str(flight_list[c][1])
  359. print("- Flight {:s} on {:s}".format(print1, print2))
  360. input()
  361. except:
  362. print("This passenger hasn't booked any flights.")
  363. input()
  364. except:
  365. print("This passenger hasn't booked any flights.")
  366. input()
  367.  
  368. def start_menu():
  369. print("")
  370. print("1. CREATE A FLIGHT:")
  371. print("- Enter 1, FlightID (two letters and three numbers), day (1-30), month (1-12) and capacity (over zero)")
  372. print("")
  373. print("2. BOOK PASSENGER ON A FLIGHT:")
  374. print("- Enter 2, FlightID, date (for example 1.1.) and name of the passenger")
  375. print("")
  376. print("3. CANCEL FLIGHT AND TRANSFER BOOKINGS TO ANOTHER ONE:")
  377. print("- Enter 3, FlightID and date (for example 1.1.)")
  378. print("")
  379. print("4. LIST OF PASSENGERS ON A FLIGHT:")
  380. print("- Enter 4, Flight ID and date (for example 1.1.)")
  381. print("")
  382. print("5. LIST OF BOOKINGS A PASSENGER HAS:")
  383. print("- Enter 5 and name of the passenger")
  384. print("")
  385. print("6. TO EXIT PROGRAM enter exit.")
  386. choiselist = []
  387. choise1 = input()
  388. print("")
  389. choise1 = choise1.split(",")
  390. for g in choise1:
  391. choiselist.append(g)
  392. choise0 = choiselist[0]
  393. return choiselist, choise0
  394.  
  395. def main():
  396. print("")
  397. print("Welcome to Alma Airlines booking system!")
  398. input()
  399. print("Please choose a number corresponding to the action you want take and enter the needed information as follows (separate all items with ',':")
  400. input()
  401. choise_list, choise = start_menu()
  402. actions = ["1", "2", "3", "4", "5", "6", "exit", "Exit", "EXIT"]
  403. while choise != "exit" and choise != "Exit" and choise != "EXIT" and choise != "6":
  404. if choise not in actions or len(choise_list) < 2:
  405. print("This isn't a valid input. Press enter to continue.\n")
  406. input()
  407. choise_list, choise = start_menu()
  408. else:
  409. if choise == "1":
  410. create_flight(choise_list)
  411. elif choise == "2":
  412. add_passenger(choise_list)
  413. elif choise == "3":
  414. cancel_flight_and_shit(choise_list)
  415. elif choise == "4":
  416. my_passengers(choise_list)
  417. elif choise == "5":
  418. my_flights(choise_list)
  419. print("WHAT NEXT?")
  420. choise_list, choise = start_menu()
  421. if choise == "6":
  422. print("You jumped from a plane.")
  423. print("Your life has ended.")
  424. input()
  425. print("Budget airlines aren't always a safe choise.")
  426. input()
  427.  
  428. else:
  429. print("Program has ended.")
  430.  
  431.  
  432. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement