Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- from prettytable import PrettyTable
- import random
- from datetime import date
- connection=mysql.connector.connect(user='root', password='faheemcd',host='127.0.0.1', database='airport')
- cursor = connection.cursor()
- def generate_unique_passenger_number():
- while True:
- passenger_number = random.randint(1000, 9999)
- check_query = "select PassengerID from passengers where PassengerID = %s"
- cursor.execute(check_query, (passenger_number,))
- existing_passenger = cursor.fetchone()
- if not existing_passenger:
- return passenger_number
- def book_ticket(first_name, last_name, flight_no):
- passenger_number = generate_unique_passenger_number()
- fetch_query = "select FlightName, Date, From_Location, To_Location from flightinfo where FlightNo = %s"
- cursor.execute(fetch_query, (flight_no,))
- flight_info = cursor.fetchone()
- if flight_info:
- flight_name, flight_date, from_location, to_location = flight_info
- seat_no = f"Seat{random.randint(1, 100)}"
- ticket_date = date.today()
- meal = 'non-veg (chicken)'
- 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)"
- values = (passenger_number, first_name, last_name, flight_name, flight_no, flight_date, from_location, to_location, seat_no, ticket_date)
- cursor.execute(query, values)
- connection.commit()
- print("Ticket booked successfully!")
- print(f"Your ticket number is: {passenger_number}")
- else:
- print("Flight not found!")
- def display_flightinfo():
- query = "select * from flightinfo"
- cursor.execute(query)
- rows = cursor.fetchall()
- table=PrettyTable()
- table.field_names=[i[0] for i in cursor.description]
- for row in rows:
- table.add_row(row)
- print(table)
- def view_ticket(passenger_id):
- query = "select * from passengers where PassengerID = %s"
- cursor.execute(query, (passenger_id,))
- ticket_info = cursor.fetchone()
- if ticket_info:
- table = PrettyTable()
- table.field_names = ["Field", "Value"]
- table.add_row(["PassengerID", ticket_info[0]])
- table.add_row(["First Name", ticket_info[1]])
- table.add_row(["Last Name", ticket_info[2]])
- table.add_row(["Flight Name", ticket_info[3]])
- table.add_row(["Flight No", ticket_info[4]])
- table.add_row(["Date", ticket_info[5]])
- table.add_row(["From Location", ticket_info[6]])
- table.add_row(["To Location", ticket_info[7]])
- table.add_row(["Seat No", ticket_info[8]])
- table.add_row(["Ticket Date", ticket_info[9]])
- table.add_row(["Meal", ticket_info[10]])
- print("Ticket Details:")
- print(table)
- else:
- print("Passenger not found!")
- def cancel_ticket(passenger_id):
- cancel_query = "delete from passengers where PassengerID = %s"
- cursor.execute(cancel_query, (passenger_id,))
- connection.commit()
- if cursor.rowcount > 0:
- print("Ticket cancelled successfully!")
- else:
- print("Incorrect passenger ID. Ticket cancellation failed.")
- def show_flight_details(passenger_id):
- 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"
- cursor.execute(query, (passenger_id,))
- flight_info = cursor.fetchone()
- if flight_info:
- table = PrettyTable()
- table.field_names = ["Field", "Value"]
- table.add_row(["Flight No", flight_info[0]])
- table.add_row(["Flight Name", flight_info[1]])
- table.add_row(["Date", flight_info[2]])
- table.add_row(["From Location", flight_info[3]])
- table.add_row(["To Location", flight_info[4]])
- print("Flight Details:")
- print(table)
- else:
- print("Flight details not found.")
- def change_flight(passenger_id):
- fetch_passenger_query = "select FlightNo from passengers where PassengerID = %s"
- cursor.execute(fetch_passenger_query, (passenger_id,))
- current_flight = cursor.fetchone()
- if current_flight:
- current_flight_no = current_flight[0]
- print(f"Your current flight number is: {current_flight_no}")
- new_flight_no = input("Enter the new flight number you want to switch to: ")
- fetch_query = "select FlightName, Date, From_Location, To_Location FROM flightinfo where FlightNo = %s"
- cursor.execute(fetch_query, (new_flight_no,))
- flight_info = cursor.fetchone()
- if flight_info:
- flight_name, flight_date, from_location, to_location = flight_info
- update_query = "update passengers set FlightName = %s, FlightNo = %s where PassengerID = %s"
- update_values = (flight_name, new_flight_no, passenger_id)
- cursor.execute(update_query, update_values)
- connection.commit()
- print("Flight changed successfully!")
- else:
- print("New flight not found!")
- else:
- print("Passenger not found!")
- def change_seat(passenger_id):
- fetch_passenger_query = "select SeatNo from passengers where PassengerID = %s"
- cursor.execute(fetch_passenger_query, (passenger_id,))
- current_seat = cursor.fetchone()
- if current_seat:
- current_seat_no = current_seat[0]
- print(f"Your current seat number is: {current_seat_no}")
- new_seat_no = input("Enter the new seat number you want to switch to: ")
- update_query = "update passengers set SeatNo = %s where PassengerID = %s"
- update_values = (new_seat_no, passenger_id)
- cursor.execute(update_query, update_values)
- connection.commit()
- print("Seat changed successfully!")
- else:
- print("Passenger not found!")
- def change_meal(passenger_id):
- fetch_meal_query = "select meal from passengers where PassengerID = %s"
- cursor.execute(fetch_meal_query, (passenger_id,))
- current_meal = cursor.fetchone()
- if current_meal:
- current_meal = current_meal[0]
- print(f"Your current meal choice is: {current_meal}")
- print('')
- available_meals = {
- 1: "Non-veg (beef)",
- 2: "Veg-noodles",
- 3: "Fish and Chips",
- 4: "Idly and Dosa",
- 5: "Non-veg (chicken)",
- 6: "Chicken curry",
- 7: "Veg-Fried rice",
- }
- print("Available meals:")
- for meal_num, meal_name in available_meals.items():
- print(f"{meal_num}. {meal_name}")
- new_meal_choice = int(input("Enter your new meal choice: "))
- if new_meal_choice in available_meals:
- new_meal = available_meals[new_meal_choice]
- if new_meal != current_meal:
- update_query = "update passengers set meal = %s where PassengerID = %s"
- update_values = (new_meal, passenger_id)
- cursor.execute(update_query, update_values)
- connection.commit()
- print("Meal choice updated successfully!")
- else:
- print("Error: New meal choice is the same as the current meal.")
- else:
- print("Invalid meal choice.")
- else:
- print("Passenger not found!")
- while True:
- print('')
- print('AIRPORT TICKET BOOKING')
- print('1.Show availabile flights')
- print('2.Book your ticket')
- print('3.Modify your ticket')
- print('4.Show ticket details')
- print('5. Exit')
- print('')
- ch=int(input('Please enter your choice '))
- if ch==1:
- display_flightinfo()
- elif ch==2:
- fname=input('Please enter your first name ')
- lname=input('Please enter your last name ')
- flightnum=input('Please enter the flight no. (Available flights can be checked from homepage) ')
- book_ticket(fname,lname,flightnum)
- elif ch==3:
- print('')
- print('1.Cancel ticket')
- print('2.Change flight')
- print('3.Change seat number')
- print('4.Show flight details')
- print('5.Select meal')
- print('')
- modify_choice = int(input('Enter modification option: '))
- if modify_choice == 1:
- passenger_id = int(input("Enter your passenger ID: "))
- cancel_ticket(passenger_id)
- elif modify_choice == 2:
- passenger_id = int(input("Enter your passenger ID: "))
- change_flight(passenger_id)
- elif modify_choice == 3:
- passenger_id = int(input("Enter your passenger ID: "))
- change_seat(passenger_id)
- elif modify_choice == 4:
- passenger_id = int(input("Enter your passenger ID: "))
- show_flight_details(passenger_id)
- elif modify_choice == 5:
- passenger_id = int(input("Enter your passenger ID: "))
- change_meal(passenger_id)
- else:
- print("Invalid modification option.")
- elif ch==4:
- passenger_id=int(input("Enter your passenger ID: "))
- view_ticket(passenger_id)
- elif ch==5:
- print('Exiting the program')
- break
- else:
- print('Invalid choice. Please select a valid option')
- cursor.close()
- connection.close()
Add Comment
Please, Sign In to add comment