import pandas as pd
import os
from random import randint
class TrainTicketSystem:
def __init__( self ) :
self . db
= mysql . connector
. connect
( host= "localhost" ,
user= "root" ,
password= "1234"
)
self . cursor = self . db. cursor( )
self . initialize_database( )
def initialize_database( self ) :
try:
# Create the database if it doesn't exist
self . cursor. execute( "CREATE DATABASE IF NOT EXISTS train_ticket_system" )
self . cursor. execute( "USE train_ticket_system" )
# Create tables
queries = [
"CREATE TABLE IF NOT EXISTS admins (name VARCHAR(100), mo_no VARCHAR(15), address VARCHAR(255), email VARCHAR(100), username VARCHAR(50) PRIMARY KEY, password VARCHAR(50))" ,
"CREATE TABLE IF NOT EXISTS customers (name VARCHAR(100), mo_no VARCHAR(15), address VARCHAR(255), email VARCHAR(100), username VARCHAR(50) PRIMARY KEY, password VARCHAR(50))" ,
"CREATE TABLE IF NOT EXISTS tickets (pnr BIGINT PRIMARY KEY, class VARCHAR(50), boarding_point VARCHAR(100), quota VARCHAR(50))" ,
"CREATE TABLE IF NOT EXISTS train_schedule (id INT AUTO_INCREMENT PRIMARY KEY, train_name VARCHAR(100), source VARCHAR(100), destination VARCHAR(100), time VARCHAR(50))"
]
for query in queries:
self . cursor. execute( query)
self . db. commit( )
except
mysql . connector
. Error
as err
: print ( "Error initializing database:" , err)
def admin_signup( self , name, mo_no, address, email, username, password) :
try:
self . cursor. execute(
"INSERT INTO admins (name, mo_no, address, email, username, password) VALUES (%s , %s , %s , %s , %s , %s )" ,
( name, mo_no, address, email, username, password)
)
self . db. commit( )
print ( "Admin account created." )
except
mysql . connector
. Error
as err
: print ( "Error:" , err)
def customer_signup( self , name, mo_no, address, email, username, password) :
try:
self . cursor. execute(
"INSERT INTO customers (name, mo_no, address, email, username, password) VALUES (%s , %s , %s , %s , %s , %s )" ,
( name, mo_no, address, email, username, password)
)
self . db. commit( )
print ( "Customer account created." )
except
mysql . connector
. Error
as err
: print ( "Error:" , err)
def book_ticket( self ) :
ticket_class = input( "Enter ticket class (e.g., Sleeper, AC): " )
boarding_point = input( "Enter boarding point: " )
quota = input( "Enter quota (e.g., General, Tatkal): " )
try:
pnr = randint( 1000000000 , 9999999999 )
self . cursor. execute(
"INSERT INTO tickets (pnr, class, boarding_point, quota) VALUES (%s , %s , %s , %s )" ,
( pnr, ticket_class, boarding_point, quota)
)
self . db. commit( )
print ( f"Ticket booked successfully with PNR: {pnr}" )
except
mysql . connector
. Error
as err
: print ( "Error:" , err)
def view_tickets( self ) :
self . cursor. execute( "SELECT * FROM tickets" )
tickets = self . cursor. fetchall( )
if tickets:
for ticket in tickets:
print ( f"PNR: {ticket[0]}, Class: {ticket[1]}, Boarding Point: {ticket[2]}, Quota: {ticket[3]}" )
else :
print ( "No tickets available." )
def add_train_schedule( self ) :
train_name = input( "Enter train name: " )
source = input( "Enter source station: " )
destination = input( "Enter destination station: " )
time = input
( "Enter departure time (HH:MM): " ) try:
self . cursor. execute(
"INSERT INTO train_schedule (train_name, source, destination, time) VALUES (%s , %s , %s , %s )" ,
( train_name
, source
, destination
, time ) )
self . db. commit( )
print ( "Train schedule added successfully." )
except
mysql . connector
. Error
as err
: print ( "Error:" , err)
def view_train_schedule( self ) :
self . cursor. execute( "SELECT * FROM train_schedule" )
schedules = self . cursor. fetchall( )
if schedules:
for schedule in schedules:
print ( f"Train: {schedule[1]}, Source: {schedule[2]}, Destination: { schedule[3]}, Time: {schedule[4]}" )
else :
print ( "No train schedules available." )
def export_to_excel( self , table_name) :
try:
self . cursor. execute( f"SELECT * FROM {table_name}" )
rows = self . cursor. fetchall( )
columns = [ desc[ 0 ] for desc in self . cursor. description]
df = pd. DataFrame( rows, columns= columns)
desktop
= os
. path
. join ( os
. path
. expanduser
( "~" ) , "Desktop" ) file_path
= os
. path
. join ( desktop
, f
"{table_name}.xlsx" )
df. to_excel( file_path, index= False )
print ( f"Data exported to {file_path}" )
except
mysql . connector
. Error
as err
: print ( "Error:" , err)
except Exception as ex:
print ( "Unexpected error:" , ex)
# Main Menu
def main( ) :
while True :
print ( '\n╔═[Main Menu]══════════════════╗' )
print ( ' 1. Admin SignUp' )
print ( ' 2. Customer SignUp' )
print ( ' 3. Book Ticket' )
print ( ' 4. View Tickets' )
print ( ' 5. Add Train Schedule' )
print ( ' 6. View Train Schedules' )
print ( ' 7. Export Data to Excel' )
print ( ' 8. Exit' )
print ( '╚══════════════════════════════╝' )
try:
choice = int( input( "Enter Your Choice: " ) )
except ValueError:
print ( "Invalid input. Please enter a number." )
continue
if choice == 1 :
system . admin_signup
( "Admin1" , "1234567890" , "Address1" , "admin@example.com" , "admin" , "password" ) elif choice == 2 :
system . customer_signup
( "Customer1" , "0987654321" , "Address2" , "cust@example.com" , "customer" , "password" ) elif choice == 3 :
elif choice == 4 :
elif choice == 5 :
elif choice == 6 :
elif choice == 7 :
table_name = input( "Enter table name to export (admins/customers/tickets/train_schedule): " )
system . export_to_excel
( table_name
) elif choice == 8 :
print ( "Exiting... Goodbye!" )
break
else :
print ( "Invalid choice. Please try again." )
main( )
aW1wb3J0IG15c3FsLmNvbm5lY3RvcgppbXBvcnQgcGFuZGFzIGFzIHBkCmltcG9ydCBvcwpmcm9tIHJhbmRvbSBpbXBvcnQgcmFuZGludAoKY2xhc3MgVHJhaW5UaWNrZXRTeXN0ZW06CiAgICBkZWYgX19pbml0X18oc2VsZik6CiAgICAgICAgc2VsZi5kYiA9IG15c3FsLmNvbm5lY3Rvci5jb25uZWN0KAogICAgICAgICAgICBob3N0PSJsb2NhbGhvc3QiLAogICAgICAgICAgICB1c2VyPSJyb290IiwKICAgICAgICAgICAgcGFzc3dvcmQ9IjEyMzQiCiAgICAgICAgKQogICAgICAgIHNlbGYuY3Vyc29yID0gc2VsZi5kYi5jdXJzb3IoKQogICAgICAgIHNlbGYuaW5pdGlhbGl6ZV9kYXRhYmFzZSgpCgogICAgZGVmIGluaXRpYWxpemVfZGF0YWJhc2Uoc2VsZik6CiAgICAgICAgdHJ5OgogICAgICAgICAgICAjIENyZWF0ZSB0aGUgZGF0YWJhc2UgaWYgaXQgZG9lc24ndCBleGlzdA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VksIHRyYWluX25hbWUgVkFSQ0hBUigxMDApLCBzb3VyY2UgVkFSQ0hBUigxMDApLCBkZXN0aW5hdGlvbiBWQVJDSEFSKDEwMCksIHRpbWUgVkFSQ0hBUig1MCkpIgogICAgICAgICAgICBdCiAgICAgICAgICAgIGZvciBxdWVyeSBpbiBxdWVyaWVzOgogICAgICAgICAgICAgICAgc2VsZi5jdXJzb3IuZXhlY3V0ZShxdWVyeSkKICAgICAgICAgICAgc2VsZi5kYi5jb21taXQoKQogICAgICAgIGV4Y2VwdCBteXNxbC5jb25uZWN0b3IuRXJyb3IgYXMgZXJyOgogICAgICAgICAgICBwcmludCgiRXJyb3IgaW5pdGlhbGl6aW5nIGRhdGFiYXNlOiIsIGVycikKCiAgICBkZWYgYWRtaW5fc2lnbnVwKHNlbGYsIG5hbWUsIG1vX25vLCBhZGRyZXNzLCBlbWFpbCwgdXNlcm5hbWUsIHBhc3N3b3JkKToKICAgICAgICB0cnk6CiAgICAgICAgICAgIHNlbGYuY3Vyc29yLmV4ZWN1dGUoCiAgICAgICAgICAgICAgICAiSU5TRVJUIElOVE8gYWRtaW5zIChuYW1lLCBtb19ubywgYWRkcmVzcywgZW1haWwsIHVzZXJuYW1lLCBwYXNzd29yZCkgVkFMVUVTICglcywgJXMsICVzLCAlcywgJXMsICVzKSIsCiAgICAgICAgICAgICAgICAobmFtZSwgbW9fbm8sIGFkZHJlc3MsIGVtYWlsLCB1c2VybmFtZSwgcGFzc3dvcmQpCiAgICAgICAgICAgICkKICAgICAgICAgICAgc2VsZi5kYi5jb21taXQoKQogICAgICAgICAgICBwcmludCgiQWRtaW4gYWNjb3VudCBjcmVhdGVkLiIpCiAgICAgICAgZXhjZXB0IG15c3FsLmNvbm5lY3Rvci5FcnJvciBhcyBlcnI6CiAgICAgICAgICAgIHByaW50KCJFcnJvcjoiLCBlcnIpCgogICAgZGVmIGN1c3RvbWVyX3NpZ251cChzZWxmLCBuYW1lLCBtb19ubywgYWRkcmVzcywgZW1haWwsI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IsCiAgICAgICAgICAgICAgICAocG5yLCB0aWNrZXRfY2xhc3MsIGJvYXJkaW5nX3BvaW50LCBxdW90YSkKICAgICAgICAgICAgKQogICAgICAgICAgICBzZWxmLmRiLmNvbW1pdCgpCiAgICAgICAgICAgIHByaW50KGYiVGlja2V0IGJvb2tlZCBzdWNjZXNzZnVsbHkgd2l0aCBQTlI6IHtwbnJ9IikKICAgICAgICBleGNlcHQgbXlzcWwuY29ubmVjdG9yLkVycm9yIGFzIGVycjoKICAgICAgICAgICAgcHJpbnQoIkVycm9yOiIsIGVycikKCiAgICBkZWYgdmlld190aWNrZXRzKHNlbGYpOgogICAgICAgIHNlbGYuY3Vyc29yLmV4ZWN1dGUoIlNFTEVDVCAqIEZST00gdGlja2V0cyIpCiAgICAgICAgdGlja2V0cyA9IHNlbGYuY3Vyc29yLmZldGNoYWxsKCkKICAgICAgICBpZiB0aWNrZXRzOgogICAgICAgICAgICBmb3IgdGlja2V0IGluIHRpY2tldHM6CiAgICAgICAgICAgICAgICBwcmludChmIlBOUjoge3RpY2tldFswXX0sIENsYXNzOiB7dGlja2V0WzFdfSwgQm9hcmRpbmcgUG9pbnQ6IHt0aWNrZXRbMl19LCBRdW90YToge3RpY2tldFszXX0iKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHByaW50KCJObyB0aWNrZXRzIGF2YWlsYWJsZS4iKQoKICAgIGRlZiBhZGRfdHJhaW5fc2NoZWR1bGUoc2VsZik6CiAgICAgICAgdHJhaW5fbmFtZSA9IGlucHV0KCJFbnRlciB0cmFpbiBuYW1lOiAiKQogICAgICAgIHNvdXJjZSA9IGlucHV0KCJFbnRlciBzb3VyY2Ugc3RhdGlvbjogIikKICAgICAgICBkZXN0aW5hdGlvbiA9IGlucHV0KCJFbnRlciBkZXN0aW5hdGlvbiBzdGF0aW9uOiAiKQogICAgICAgIHRpbWUgPSBpbnB1dCgiRW50ZXIgZGVwYXJ0dXJlIHRpbWUgKEhIOk1NKTogIikKICAgICAgICB0cn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ykKICAgICAgICBwcmludCgnIDMuIEJvb2sgVGlja2V0JykKICAgICAgICBwcmludCgnIDQuIFZpZXcgVGlja2V0cycpCiAgICAgICAgcHJpbnQoJyA1LiBBZGQgVHJhaW4gU2NoZWR1bGUnKQogICAgICAgIHByaW50KCcgNi4gVmlldyBUcmFpbiBTY2hlZHVsZXMnKQogICAgICAgIHByaW50KCcgNy4gRXhwb3J0IERhdGEgdG8gRXhjZWwnKQogICAgICAgIHByaW50KCcgOC4gRXhpdCcpCiAgICAgICAgcHJpbnQoJ+KVmuKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVkOKVnScpCgogICAgICAgIHRyeToKICAgICAgICAgICAgY2hvaWNlID0gaW50KGlucHV0KCJFbnRlciBZb3VyIENob2ljZTogIikpCiAgICAgICAgZXhjZXB0IFZhbHVlRXJyb3I6CiAgICAgICAgICAgIHByaW50KCJJbnZhbGlkIGlucHV0LiBQbGVhc2UgZW50ZXIgYSBudW1iZXIuIikKICAgICAgICAgICAgY29udGludWUKCiAgICAgICAgaWYgY2hvaWNlID09IDE6CiAgICAgICAgICAgIHN5c3RlbS5hZG1pbl9zaWdudXAoIkFkbWluMSIsICIxMjM0NTY3ODkwIiwgIkFkZHJlc3MxIiwgImFkbWluQGV4YW1wbGUuY29tIiwgImFkbWluIiwgInBhc3N3b3JkIikKICAgICAgICBlbGlmIGNob2ljZSA9PSAyOgogICAgICAgICAgICBzeXN0ZW0uY3VzdG9tZXJfc2lnbnVwKCJDdXN0b21lcjEiLCAiMDk4NzY1NDMyMSIsICJBZGRyZXNzMiIsICJjdXN0QGV4YW1wbGUuY29tIiwgImN1c3RvbWVyIiwgInBhc3N3b3JkIikKICAgICAgICBlbGlmIGNob2ljZSA9PSAzOgogICAgICAgICAgICBzeXN0ZW0uYm9va190aWNrZXQoKQogICAgICAgIGVsaWYgY2hvaWNlID09IDQ6CiAgICAgICAgICAgIHN5c3RlbS52aWV3X3RpY2tldHMoKQogICAgICAgIGVsaWYgY2hvaWNlID09IDU6CiAgICAgICAgICAgIHN5c3RlbS5hZGRfdHJhaW5fc2NoZWR1bGUoKQogICAgICAgIGVsaWYgY2hvaWNlID09IDY6CiAgICAgICAgICAgIHN5c3RlbS52aWV3X3RyYWluX3NjaGVkdWxlKCkKICAgICAgICBlbGlmIGNob2ljZSA9PSA3OgogICAgICAgICAgICB0YWJsZV9uYW1lID0gaW5wdXQoIkVudGVyIHRhYmxlIG5hbWUgdG8gZXhwb3J0IChhZG1pbnMvY3VzdG9tZXJzL3RpY2tldHMvdHJhaW5fc2NoZWR1bGUpOiAiKQogICAgICAgICAgICBzeXN0ZW0uZXhwb3J0X3RvX2V4Y2VsKHRhYmxlX25hbWUpCiAgICAgICAgZWxpZiBjaG9pY2UgPT0gODoKICAgICAgICAgICAgcHJpbnQoIkV4aXRpbmcuLi4gR29vZGJ5ZSEiKQogICAgICAgICAgICBicmVhawogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHByaW50KCJJbnZhbGlkIGNob2ljZS4gUGxlYXNlIHRyeSBhZ2Fpbi4iKQoKbWFpbigp
stdout
import mysql.connector
import pandas as pd
import os
from random import randint
class TrainTicketSystem:
def __init__(self):
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="1234"
)
self.cursor = self.db.cursor()
self.initialize_database()
def initialize_database(self):
try:
# Create the database if it doesn't exist
self.cursor.execute("CREATE DATABASE IF NOT EXISTS train_ticket_system")
self.cursor.execute("USE train_ticket_system")
# Create tables
queries = [
"CREATE TABLE IF NOT EXISTS admins (name VARCHAR(100), mo_no VARCHAR(15), address VARCHAR(255), email VARCHAR(100), username VARCHAR(50) PRIMARY KEY, password VARCHAR(50))",
"CREATE TABLE IF NOT EXISTS customers (name VARCHAR(100), mo_no VARCHAR(15), address VARCHAR(255), email VARCHAR(100), username VARCHAR(50) PRIMARY KEY, password VARCHAR(50))",
"CREATE TABLE IF NOT EXISTS tickets (pnr BIGINT PRIMARY KEY, class VARCHAR(50), boarding_point VARCHAR(100), quota VARCHAR(50))",
"CREATE TABLE IF NOT EXISTS train_schedule (id INT AUTO_INCREMENT PRIMARY KEY, train_name VARCHAR(100), source VARCHAR(100), destination VARCHAR(100), time VARCHAR(50))"
]
for query in queries:
self.cursor.execute(query)
self.db.commit()
except mysql.connector.Error as err:
print("Error initializing database:", err)
def admin_signup(self, name, mo_no, address, email, username, password):
try:
self.cursor.execute(
"INSERT INTO admins (name, mo_no, address, email, username, password) VALUES (%s, %s, %s, %s, %s, %s)",
(name, mo_no, address, email, username, password)
)
self.db.commit()
print("Admin account created.")
except mysql.connector.Error as err:
print("Error:", err)
def customer_signup(self, name, mo_no, address, email, username, password):
try:
self.cursor.execute(
"INSERT INTO customers (name, mo_no, address, email, username, password) VALUES (%s, %s, %s, %s, %s, %s)",
(name, mo_no, address, email, username, password)
)
self.db.commit()
print("Customer account created.")
except mysql.connector.Error as err:
print("Error:", err)
def book_ticket(self):
ticket_class = input("Enter ticket class (e.g., Sleeper, AC): ")
boarding_point = input("Enter boarding point: ")
quota = input("Enter quota (e.g., General, Tatkal): ")
try:
pnr = randint(1000000000, 9999999999)
self.cursor.execute(
"INSERT INTO tickets (pnr, class, boarding_point, quota) VALUES (%s, %s, %s, %s)",
(pnr, ticket_class, boarding_point, quota)
)
self.db.commit()
print(f"Ticket booked successfully with PNR: {pnr}")
except mysql.connector.Error as err:
print("Error:", err)
def view_tickets(self):
self.cursor.execute("SELECT * FROM tickets")
tickets = self.cursor.fetchall()
if tickets:
for ticket in tickets:
print(f"PNR: {ticket[0]}, Class: {ticket[1]}, Boarding Point: {ticket[2]}, Quota: {ticket[3]}")
else:
print("No tickets available.")
def add_train_schedule(self):
train_name = input("Enter train name: ")
source = input("Enter source station: ")
destination = input("Enter destination station: ")
time = input("Enter departure time (HH:MM): ")
try:
self.cursor.execute(
"INSERT INTO train_schedule (train_name, source, destination, time) VALUES (%s, %s, %s, %s)",
(train_name, source, destination, time)
)
self.db.commit()
print("Train schedule added successfully.")
except mysql.connector.Error as err:
print("Error:", err)
def view_train_schedule(self):
self.cursor.execute("SELECT * FROM train_schedule")
schedules = self.cursor.fetchall()
if schedules:
for schedule in schedules:
print(f"Train: {schedule[1]}, Source: {schedule[2]}, Destination: { schedule[3]}, Time: {schedule[4]}")
else:
print("No train schedules available.")
def export_to_excel(self, table_name):
try:
self.cursor.execute(f"SELECT * FROM {table_name}")
rows = self.cursor.fetchall()
columns = [desc[0] for desc in self.cursor.description]
df = pd.DataFrame(rows, columns=columns)
desktop = os.path.join(os.path.expanduser("~"), "Desktop")
file_path = os.path.join(desktop, f"{table_name}.xlsx")
df.to_excel(file_path, index=False)
print(f"Data exported to {file_path}")
except mysql.connector.Error as err:
print("Error:", err)
except Exception as ex:
print("Unexpected error:", ex)
# Main Menu
def main():
system = TrainTicketSystem()
while True:
print('\n╔═[Main Menu]══════════════════╗')
print(' 1. Admin SignUp')
print(' 2. Customer SignUp')
print(' 3. Book Ticket')
print(' 4. View Tickets')
print(' 5. Add Train Schedule')
print(' 6. View Train Schedules')
print(' 7. Export Data to Excel')
print(' 8. Exit')
print('╚══════════════════════════════╝')
try:
choice = int(input("Enter Your Choice: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if choice == 1:
system.admin_signup("Admin1", "1234567890", "Address1", "admin@example.com", "admin", "password")
elif choice == 2:
system.customer_signup("Customer1", "0987654321", "Address2", "cust@example.com", "customer", "password")
elif choice == 3:
system.book_ticket()
elif choice == 4:
system.view_tickets()
elif choice == 5:
system.add_train_schedule()
elif choice == 6:
system.view_train_schedule()
elif choice == 7:
table_name = input("Enter table name to export (admins/customers/tickets/train_schedule): ")
system.export_to_excel(table_name)
elif choice == 8:
print("Exiting... Goodbye!")
break
else:
print("Invalid choice. Please try again.")
main()