Guest User

Python code

a guest
Aug 26th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. import mysql.connector
  2. from prettytable import PrettyTable
  3. import random
  4. from datetime import date
  5. connection=mysql.connector.connect(user='root', password='faheemcd',host='127.0.0.1', database='airport')
  6. cursor = connection.cursor()
  7. def generate_unique_passenger_number():
  8. while True:
  9. passenger_number = random.randint(1000, 9999)
  10. check_query = "select PassengerID from passengers where PassengerID = %s"
  11. cursor.execute(check_query, (passenger_number,))
  12. existing_passenger = cursor.fetchone()
  13.  
  14. if not existing_passenger:
  15. return passenger_number
  16. def book_ticket(first_name, last_name, flight_no):
  17. passenger_number = generate_unique_passenger_number()
  18. fetch_query = "select FlightName, Date, From_Location, To_Location from flightinfo where FlightNo = %s"
  19. cursor.execute(fetch_query, (flight_no,))
  20. flight_info = cursor.fetchone()
  21.  
  22. if flight_info:
  23. flight_name, flight_date, from_location, to_location = flight_info
  24. seat_no = f"Seat{random.randint(1, 100)}"
  25. ticket_date = date.today()
  26. meal = 'non-veg (chicken)'
  27. query = "insert into passengers (PassengerID, FirstName, LastName, FlightName, FlightNo, Date, FromLocation, ToLocation, SeatNo, TicketDate) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
  28. values = (passenger_number, first_name, last_name, flight_name, flight_no, flight_date, from_location, to_location, seat_no, ticket_date)
  29.  
  30.  
  31. cursor.execute(query, values)
  32. connection.commit()
  33.  
  34. print("Ticket booked successfully!")
  35. print(f"Your ticket number is: {passenger_number}")
  36. else:
  37. print("Flight not found!")
  38. def display_flightinfo():
  39. query = "select * from flightinfo"
  40. cursor.execute(query)
  41. rows = cursor.fetchall()
  42. table=PrettyTable()
  43. table.field_names=[i[0] for i in cursor.description]
  44. for row in rows:
  45. table.add_row(row)
  46. print(table)
  47. def view_ticket(passenger_id):
  48. query = "select * from passengers where PassengerID = %s"
  49. cursor.execute(query, (passenger_id,))
  50. ticket_info = cursor.fetchone()
  51.  
  52. if ticket_info:
  53. table = PrettyTable()
  54. table.field_names = ["Field", "Value"]
  55. table.add_row(["PassengerID", ticket_info[0]])
  56. table.add_row(["First Name", ticket_info[1]])
  57. table.add_row(["Last Name", ticket_info[2]])
  58. table.add_row(["Flight Name", ticket_info[3]])
  59. table.add_row(["Flight No", ticket_info[4]])
  60. table.add_row(["Date", ticket_info[5]])
  61. table.add_row(["From Location", ticket_info[6]])
  62. table.add_row(["To Location", ticket_info[7]])
  63. table.add_row(["Seat No", ticket_info[8]])
  64. table.add_row(["Ticket Date", ticket_info[9]])
  65. table.add_row(["Meal", ticket_info[10]])
  66. print("Ticket Details:")
  67. print(table)
  68. else:
  69. print("Passenger not found!")
  70. def cancel_ticket(passenger_id):
  71. cancel_query = "delete from passengers where PassengerID = %s"
  72. cursor.execute(cancel_query, (passenger_id,))
  73. connection.commit()
  74.  
  75. if cursor.rowcount > 0:
  76. print("Ticket cancelled successfully!")
  77. else:
  78. print("Incorrect passenger ID. Ticket cancellation failed.")
  79. def show_flight_details(passenger_id):
  80. query = "select p.FlightNo, f.FlightName, f.Date, f.From_Location, f.To_Location from passengers p join flightinfo f on p.FlightNo = f.FlightNo where p.PassengerID = %s"
  81. cursor.execute(query, (passenger_id,))
  82. flight_info = cursor.fetchone()
  83.  
  84. if flight_info:
  85. table = PrettyTable()
  86. table.field_names = ["Field", "Value"]
  87. table.add_row(["Flight No", flight_info[0]])
  88. table.add_row(["Flight Name", flight_info[1]])
  89. table.add_row(["Date", flight_info[2]])
  90. table.add_row(["From Location", flight_info[3]])
  91. table.add_row(["To Location", flight_info[4]])
  92. print("Flight Details:")
  93. print(table)
  94. else:
  95. print("Flight details not found.")
  96. def change_flight(passenger_id):
  97. fetch_passenger_query = "select FlightNo from passengers where PassengerID = %s"
  98. cursor.execute(fetch_passenger_query, (passenger_id,))
  99. current_flight = cursor.fetchone()
  100.  
  101. if current_flight:
  102. current_flight_no = current_flight[0]
  103. print(f"Your current flight number is: {current_flight_no}")
  104.  
  105. new_flight_no = input("Enter the new flight number you want to switch to: ")
  106. fetch_query = "select FlightName, Date, From_Location, To_Location FROM flightinfo where FlightNo = %s"
  107. cursor.execute(fetch_query, (new_flight_no,))
  108. flight_info = cursor.fetchone()
  109.  
  110. if flight_info:
  111. flight_name, flight_date, from_location, to_location = flight_info
  112. update_query = "update passengers set FlightName = %s, FlightNo = %s where PassengerID = %s"
  113. update_values = (flight_name, new_flight_no, passenger_id)
  114. cursor.execute(update_query, update_values)
  115. connection.commit()
  116.  
  117. print("Flight changed successfully!")
  118. else:
  119. print("New flight not found!")
  120. else:
  121. print("Passenger not found!")
  122. def change_seat(passenger_id):
  123. fetch_passenger_query = "select SeatNo from passengers where PassengerID = %s"
  124. cursor.execute(fetch_passenger_query, (passenger_id,))
  125. current_seat = cursor.fetchone()
  126.  
  127. if current_seat:
  128. current_seat_no = current_seat[0]
  129. print(f"Your current seat number is: {current_seat_no}")
  130.  
  131. new_seat_no = input("Enter the new seat number you want to switch to: ")
  132. update_query = "update passengers set SeatNo = %s where PassengerID = %s"
  133. update_values = (new_seat_no, passenger_id)
  134. cursor.execute(update_query, update_values)
  135. connection.commit()
  136.  
  137. print("Seat changed successfully!")
  138. else:
  139. print("Passenger not found!")
  140. def change_meal(passenger_id):
  141. fetch_meal_query = "select meal from passengers where PassengerID = %s"
  142. cursor.execute(fetch_meal_query, (passenger_id,))
  143. current_meal = cursor.fetchone()
  144.  
  145. if current_meal:
  146. current_meal = current_meal[0]
  147. print(f"Your current meal choice is: {current_meal}")
  148. print('')
  149. available_meals = {
  150. 1: "Non-veg (beef)",
  151. 2: "Veg-noodles",
  152. 3: "Fish and Chips",
  153. 4: "Idly and Dosa",
  154. 5: "Non-veg (chicken)",
  155. 6: "Chicken curry",
  156. 7: "Veg-Fried rice",
  157. }
  158.  
  159. print("Available meals:")
  160. for meal_num, meal_name in available_meals.items():
  161. print(f"{meal_num}. {meal_name}")
  162.  
  163. new_meal_choice = int(input("Enter your new meal choice: "))
  164.  
  165. if new_meal_choice in available_meals:
  166. new_meal = available_meals[new_meal_choice]
  167.  
  168. if new_meal != current_meal:
  169. update_query = "update passengers set meal = %s where PassengerID = %s"
  170. update_values = (new_meal, passenger_id)
  171. cursor.execute(update_query, update_values)
  172. connection.commit()
  173.  
  174. print("Meal choice updated successfully!")
  175. else:
  176. print("Error: New meal choice is the same as the current meal.")
  177. else:
  178. print("Invalid meal choice.")
  179. else:
  180. print("Passenger not found!")
  181.  
  182. while True:
  183. print('')
  184. print('AIRPORT TICKET BOOKING')
  185. print('1.Show availabile flights')
  186. print('2.Book your ticket')
  187. print('3.Modify your ticket')
  188. print('4.Show ticket details')
  189. print('5. Exit')
  190. print('')
  191. ch=int(input('Please enter your choice '))
  192. if ch==1:
  193. display_flightinfo()
  194. elif ch==2:
  195. fname=input('Please enter your first name ')
  196. lname=input('Please enter your last name ')
  197. flightnum=input('Please enter the flight no. (Available flights can be checked from homepage) ')
  198. book_ticket(fname,lname,flightnum)
  199. elif ch==3:
  200. print('')
  201. print('1.Cancel ticket')
  202. print('2.Change flight')
  203. print('3.Change seat number')
  204. print('4.Show flight details')
  205. print('5.Select meal')
  206. print('')
  207. modify_choice = int(input('Enter modification option: '))
  208.  
  209. if modify_choice == 1:
  210. passenger_id = int(input("Enter your passenger ID: "))
  211. cancel_ticket(passenger_id)
  212.  
  213. elif modify_choice == 2:
  214. passenger_id = int(input("Enter your passenger ID: "))
  215. change_flight(passenger_id)
  216. elif modify_choice == 3:
  217. passenger_id = int(input("Enter your passenger ID: "))
  218. change_seat(passenger_id)
  219. elif modify_choice == 4:
  220. passenger_id = int(input("Enter your passenger ID: "))
  221. show_flight_details(passenger_id)
  222. elif modify_choice == 5:
  223. passenger_id = int(input("Enter your passenger ID: "))
  224. change_meal(passenger_id)
  225. else:
  226. print("Invalid modification option.")
  227. elif ch==4:
  228. passenger_id=int(input("Enter your passenger ID: "))
  229. view_ticket(passenger_id)
  230. elif ch==5:
  231. print('Exiting the program')
  232. break
  233. else:
  234. print('Invalid choice. Please select a valid option')
  235. cursor.close()
  236. connection.close()
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
Add Comment
Please, Sign In to add comment