import sqlite3
# Database connection
conn = sqlite3.connect('railway_management.db')
cursor = conn.cursor()
# Create tables for train and reservation data
cursor.execute("""
CREATE TABLE IF NOT EXISTS trains (
train_id INTEGER PRIMARY KEY AUTOINCREMENT,
train_name TEXT,
source TEXT,
destination TEXT,
departure_time TEXT,
available_seats INTEGER
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS reservations (
reservation_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_name TEXT,
train_id INTEGER,
seat_count INTEGER,
status TEXT,
FOREIGN KEY (train_id) REFERENCES trains (train_id)
)
""")
conn.commit()
# Class representing a train
class Train:
def __init__(self, train_name, source, destination, departure_time, available_seats):
self.train_name = train_name
self.source = source
self.destination = destination
self.departure_time = departure_time
self.available_seats = available_seats
def add_train(self):
cursor.execute("""
INSERT INTO trains (train_name, source, destination, departure_time, available_seats)
VALUES (?, ?, ?, ?, ?)
""", (self.train_name, self.source, self.destination, self.departure_time, self.available_seats))
conn.commit()
def show_trains(self):
cursor.execute("SELECT * FROM trains")
for row in cursor.fetchall():
print(f"Train ID: {row[0]}, Name: {row[1]}, Source: {row[2]}, Destination: {row[3]}, Departure: {row[4]}, Available Seats: {row[5]}")
# Class to manage reservations
class Reservation:
def __init__(self, user_name, train_id, seat_count):
self.user_name = user_name
self.train_id = train_id
self.seat_count = seat_count
def book_ticket(self):
cursor.execute("SELECT available_seats FROM trains WHERE train_id = ?", (self.train_id,))
available_seats = cursor.fetchone()[0]
if available_seats >= self.seat_count:
cursor.execute("INSERT INTO reservations (user_name, train_id, seat_count, status) VALUES (?, ?, ?, ?)",
(self.user_name, self.train_id, self.seat_count, 'Booked'))
cursor.execute("UPDATE trains SET available_seats = available_seats - ? WHERE train_id = ?",
(self.seat_count, self.train_id))
conn.commit()
print("Ticket booked successfully!")
else:
print("Not enough available seats.")
def cancel_ticket(self, reservation_id):
cursor.execute("SELECT seat_count, train_id FROM reservations WHERE reservation_id = ?", (reservation_id,))
reservation = cursor.fetchone()
if reservation:
seat_count, train_id = reservation
cursor.execute("UPDATE trains SET available_seats = available_seats + ? WHERE train_id = ?",
(seat_count, train_id))
cursor.execute("DELETE FROM reservations WHERE reservation_id = ?", (reservation_id,))
conn.commit()
print("Ticket cancelled successfully!")
else:
print("Reservation not found.")
# Function to display available trains
def display_available_trains():
print("\nAvailable Trains:")
train.show_trains()
# Function to search for trains
def search_trains_by_source_destination(source, destination):
cursor.execute("SELECT * FROM trains WHERE source = ? AND destination = ?", (source, destination))
rows = cursor.fetchall()
if rows:
for row in rows:
print(f"Train ID: {row[0]}, Name: {row[1]}, Source: {row[2]}, Destination: {row[3]}, Departure: {row[4]}, Available Seats: {row[5]}")
else:
print("No trains found for this route.")
# Function to view reservations
def view_reservations():
cursor.execute("SELECT * FROM reservations")
for row in cursor.fetchall():
print(f"Reservation ID: {row[0]}, User: {row[1]}, Train ID: {row[2]}, Seat Count: {row[3]}, Status: {row[4]}")
# Sample Train Data
train1 = Train("Express 101", "New York", "Boston", "2025-01-10 10:00", 50)
train1.add_train()
train2 = Train("Express 102", "Los Angeles", "Chicago", "2025-01-12 09:00", 30)
train2.add_train()
# Sample Reservation Data
reservation1 = Reservation("John Doe", 1, 2)
reservation1.book_ticket()
reservation2 = Reservation("Jane Smith", 2, 3)
reservation2.book_ticket()
# Menu-driven interface
def main():
while True:
print("\n--- Railway Management System ---")
print("1. View Available Trains")
print("2. Book Ticket")
print("3. Cancel Ticket")
print("4. Search Trains by Source and Destination")
print("5. View All Reservations")
print("6. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
display_available_trains()
elif choice == 2:
user_name = input("Enter your name: ")
train_id = int(input("Enter train ID: "))
seat_count = int(input("Enter number of seats to book: "))
reservation = Reservation(user_name, train_id, seat_count)
reservation.book_ticket()
elif choice == 3:
reservation_id = int(input("Enter reservation ID to cancel: "))
reservation = Reservation("", 0, 0)
reservation.cancel_ticket(reservation_id)
elif choice == 4:
source = input("Enter source station: ")
destination = input("Enter destination station: ")
search_trains_by_source_destination(source, destination)
elif choice == 5:
view_reservations()
elif choice == 6:
print("Exiting system.")
break
else:
print("Invalid choice, please try again.")
if __name__ == "__main__":
main()
# Close the database connection at the end
conn.close()
aW1wb3J0IHNxbGl0ZTMKCiMgRGF0YWJhc2UgY29ubmVjdGlvbgpjb25uID0gc3FsaXRlMy5jb25uZWN0KCdyYWlsd2F5X21hbmFnZW1lbnQuZGInKQpjdXJzb3IgPSBjb25uLmN1cnNvcigpCgojIENyZWF0ZSB0YWJsZXMgZm9yIHRyYWluIGFuZCByZXNlcnZhdGlvbiBkYXRhCmN1cnNvci5leGVjdXRlKCIiIgogICAgQ1JFQVRFIFRBQkxFIElGIE5PVCBFWElTVFMgdHJhaW5zICgKICAgICAgICB0cmFpbl9pZCBJTlRFR0VSIFBSSU1BUlkgS0VZIEFVVE9JTkNSRU1FTlQsCiAgICAgICAgdHJhaW5fbmFtZSBURVhULAogICAgICAgIHNvdXJjZSBURVhULAogICAgICAgIGRlc3RpbmF0aW9uIFRFWFQsCiAgICAgICAgZGVwYXJ0dXJlX3RpbWUgVEVYVCwKICAgICAgICBhdmFpbGFibGVfc2VhdHMgSU5URUdFUgogICAgKQoiIiIpCgpjdXJzb3IuZXhlY3V0ZSgiIiIKICAgIENSRUFURSBUQUJMRSBJRiBOT1QgRVhJU1RTIHJlc2VydmF0aW9ucyAoCiAgICAgICAgcmVzZXJ2YXRpb25faWQgSU5URUdFUiBQUklNQVJZIEtFWSBBVVRPSU5DUkVNRU5ULAogICAgICAgIHVzZXJfbmFtZSBURVhULAogICAgICAgIHRyYWluX2lkIElOVEVHRVIsCiAgICAgICAgc2VhdF9jb3VudCBJTlRFR0VSLAogICAgICAgIHN0YXR1cyBURVhULAogICAgICAgIEZPUkVJR04gS0VZICh0cmFpbl9pZCkgUkVGRVJFTkNFUyB0cmFpbnMgKHRyYWluX2lkKQogICAgKQoiIiIpCmNvbm4uY29tbWl0KCkKCiMgQ2xhc3MgcmVwcmVzZW50aW5nIGEgdHJhaW4KY2xhc3Mg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AgICAgY29ubi5jb21taXQoKQogICAgICAgICAgICBwcmludCgiVGlja2V0IGJvb2tlZCBzdWNjZXNzZnVsbHkhIikKICAgICAgICBlbHNlOgogICAgICAgICAgICBwcmludCgiTm90IGVub3VnaCBhdmFpbGFibGUgc2VhdHMuIikKCiAgICBkZWYgY2FuY2VsX3RpY2tldChzZWxmLCByZXNlcnZhdGlvbl9pZCk6CiAgICAgICAgY3Vyc29yLmV4ZWN1dGUoIlNFTEVDVCBzZWF0X2NvdW50LCB0cmFpbl9pZCBGUk9NIHJlc2VydmF0aW9ucyBXSEVSRSByZXNlcnZhdGlvbl9pZCA9ID8iLCAocmVzZXJ2YXRpb25faWQsKSkKICAgICAgICByZXNlcnZhdGlvbiA9IGN1cnNvci5mZXRjaG9uZSgpCgogICAgICAgIGlmIHJlc2VydmF0aW9uOgogICAgICAgICAgICBzZWF0X2NvdW50LCB0cmFpbl9pZCA9IHJlc2VydmF0aW9uCiAgICAgICAgICAgIGN1cnNvci5leGVjdXRlKCJVUERBVEUgdHJhaW5zIFNFVCBhdmFpbGFibGVfc2VhdHMgPSBhdmFpbGFibGVfc2VhdHMgKyA/IFdIRVJFIHRyYWluX2lkID0gPyIsIAogICAgICAgICAgICAgICAgICAgICAgICAgICAoc2VhdF9jb3VudCwgdHJhaW5faWQpKQogICAgICAgICAgICBjdXJzb3IuZXhlY3V0ZSgiREVMRVRFIEZST00gcmVzZXJ2YXRpb25zIFdIRVJFIHJlc2VydmF0aW9uX2lkID0gPyIsIChyZXNlcnZhdGlvbl9pZCwpKQogICAgICAgICAgICBjb25uLmNvbW1pdCgpCiAgICAgICAgICAgIHByaW50KCJUaWNrZXQgY2FuY2VsbGVkIHN1Y2Nlc3NmdWxseSEiKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHByaW50KCJSZXNlcnZhdGlvbiBub3QgZm91bmQuIikKCiMgRnVuY3Rpb24gdG8gZGlzcGxheSBhdmFpbGFibGUgdHJhaW5zCmRlZiBkaXNwbGF5X2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fdHJhaW5zKCkKICAgICAgICBlbGlmIGNob2ljZSA9PSAyOgogICAgICAgICAgICB1c2VyX25hbWUgPSBpbnB1dCgiRW50ZXIgeW91ciBuYW1lOiAiKQogICAgICAgICAgICB0cmFpbl9pZCA9IGludChpbnB1dCgiRW50ZXIgdHJhaW4gSUQ6ICIpKQogICAgICAgICAgICBzZWF0X2NvdW50ID0gaW50KGlucHV0KCJFbnRlciBudW1iZXIgb2Ygc2VhdHMgdG8gYm9vazogIikpCiAgICAgICAgICAgIHJlc2VydmF0aW9uID0gUmVzZXJ2YXRpb24odXNlcl9uYW1lLCB0cmFpbl9pZCwgc2VhdF9jb3VudCkKICAgICAgICAgICAgcmVzZXJ2YXRpb24uYm9va190aWNrZXQoKQogICAgICAgIGVsaWYgY2hvaWNlID09IDM6CiAgICAgICAgICAgIHJlc2VydmF0aW9uX2lkID0gaW50KGlucHV0KCJFbnRlciByZXNlcnZhdGlvbiBJRCB0byBjYW5jZWw6ICIpKQogICAgICAgICAgICByZXNlcnZhdGlvbiA9IFJlc2VydmF0aW9uKCIiLCAwLCAwKQogICAgICAgICAgICByZXNlcnZhdGlvbi5jYW5jZWxfdGlja2V0KHJlc2VydmF0aW9uX2lkKQogICAgICAgIGVsaWYgY2hvaWNlID09IDQ6CiAgICAgICAgICAgIHNvdXJjZSA9IGlucHV0KCJFbnRlciBzb3VyY2Ugc3RhdGlvbjogIikKICAgICAgICAgICAgZGVzdGluYXRpb24gPSBpbnB1dCgiRW50ZXIgZGVzdGluYXRpb24gc3RhdGlvbjogIikKICAgICAgICAgICAgc2VhcmNoX3RyYWluc19ieV9zb3VyY2VfZGVzdGluYXRpb24oc291cmNlLCBkZXN0aW5hdGlvbikKICAgICAgICBlbGlmIGNob2ljZSA9PSA1OgogICAgICAgICAgICB2aWV3X3Jlc2VydmF0aW9ucygpCiAgICAgICAgZWxpZiBjaG9pY2UgPT0gNjoKICAgICAgICAgICAgcHJpbnQoIkV4aXRpbmcgc3lzdGVtLiIpCiAg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J1NhbXBhcmFrIEtyYW50aScsJ0tlcmFsYScsJ1JhbmNoaScsMTUwMCwxMywnU3VwZXJmYXN0Jyk7CklOU0VSVCBJTlRPIFRyYWluIFZBTFVFUyAoMTAwNiwgJ0RlbGhpIEludGVyY2l0eScsJ0RlbGhpJywnQmhvcGFsJyw3NTAwLDA5LCdTdXBlcmZhc3QnKTsKSU5TRVJUIElOVE8gVHJhaW4gVkFMVUVTICgxMDA3LCAnUmFqZGhhbmknLCdNdW1iYWknLCdEZWxoaScsMzQwLDEwLCdMb2NhbCcpOwpJTlNFUlQgSU5UTyBUcmFpbiBWQUxVRVMgKDEwMDgsICdBdmFkaCBBc3NhbScsJ0Fzc2FtJywnQWdyYScsMzYwMCwwNywnU3VwZXJmYXN0Jyk7CklOU0VSVCBJTlRPIFRyYWluIFZBTFVFUyAoMTAwOSwgJ1NoYXRhYmRpJywnRGVsaGknLCdDaGFuZGlnYXJoJyw1MDAsMDUsJ1N1cGVyZmFzdCcpOwpJTlNFUlQgSU5UTyBUcmFpbiBWQUxVRVMgKDEwMTAsICdKaGVsdW0nLCdNdW1iYWknLCdKYW1tdScsMzAwMCwxMiwnTG9jYWwnKTsKCg==
CREATE TABLE Train (
TrainNum numeric PRIMARY KEY,
TrainName varchar NOT NULL,
Destination varchar NOT NULL,
Source varchar NOT NULL,
DisTravelled numeric Not NULL,
NumOfCoaches numeric NOT NULL,
Type varchar NOT NULL
);
-- insert some values
INSERT INTO Train VALUES (1001, 'Malwa','Indore','Jammu',2500,1,'Superfast');
INSERT INTO Train VALUES (1002, 'Jammu Mail','Pune','Jammu',3500,12,'Superfast');
INSERT INTO Train VALUES (1003, 'Amritsar Exp','Amritsar','Delhi',900,08,'Local');
INSERT INTO Train VALUES (1004, 'Intercity Exp','Delhi','Bathinda',490,15,'Local');
INSERT INTO Train VALUES (1005, 'Samparak Kranti','Kerala','Ranchi',1500,13,'Superfast');
INSERT INTO Train VALUES (1006, 'Delhi Intercity','Delhi','Bhopal',7500,09,'Superfast');
INSERT INTO Train VALUES (1007, 'Rajdhani','Mumbai','Delhi',340,10,'Local');
INSERT INTO Train VALUES (1008, 'Avadh Assam','Assam','Agra',3600,07,'Superfast');
INSERT INTO Train VALUES (1009, 'Shatabdi','Delhi','Chandigarh',500,05,'Superfast');
INSERT INTO Train VALUES (1010, 'Jhelum','Mumbai','Jammu',3000,12,'Local');
import sqlite3
# Database connection
conn = sqlite3.connect('railway_management.db')
cursor = conn.cursor()
# Create tables for train and reservation data
cursor.execute("""
CREATE TABLE IF NOT EXISTS trains (
train_id INTEGER PRIMARY KEY AUTOINCREMENT,
train_name TEXT,
source TEXT,
destination TEXT,
departure_time TEXT,
available_seats INTEGER
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS reservations (
reservation_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_name TEXT,
train_id INTEGER,
seat_count INTEGER,
status TEXT,
FOREIGN KEY (train_id) REFERENCES trains (train_id)
)
""")
conn.commit()
# Class representing a train
class Train:
def __init__(self, train_name, source, destination, departure_time, available_seats):
self.train_name = train_name
self.source = source
self.destination = destination
self.departure_time = departure_time
self.available_seats = available_seats
def add_train(self):
cursor.execute("""
INSERT INTO trains (train_name, source, destination, departure_time, available_seats)
VALUES (?, ?, ?, ?, ?)
""", (self.train_name, self.source, self.destination, self.departure_time, self.available_seats))
conn.commit()
def show_trains(self):
cursor.execute("SELECT * FROM trains")
for row in cursor.fetchall():
print(f"Train ID: {row[0]}, Name: {row[1]}, Source: {row[2]}, Destination: {row[3]}, Departure: {row[4]}, Available Seats: {row[5]}")
# Class to manage reservations
class Reservation:
def __init__(self, user_name, train_id, seat_count):
self.user_name = user_name
self.train_id = train_id
self.seat_count = seat_count
def book_ticket(self):
cursor.execute("SELECT available_seats FROM trains WHERE train_id = ?", (self.train_id,))
available_seats = cursor.fetchone()[0]
if available_seats >= self.seat_count:
cursor.execute("INSERT INTO reservations (user_name, train_id, seat_count, status) VALUES (?, ?, ?, ?)",
(self.user_name, self.train_id, self.seat_count, 'Booked'))
cursor.execute("UPDATE trains SET available_seats = available_seats - ? WHERE train_id = ?",
(self.seat_count, self.train_id))
conn.commit()
print("Ticket booked successfully!")
else:
print("Not enough available seats.")
def cancel_ticket(self, reservation_id):
cursor.execute("SELECT seat_count, train_id FROM reservations WHERE reservation_id = ?", (reservation_id,))
reservation = cursor.fetchone()
if reservation:
seat_count, train_id = reservation
cursor.execute("UPDATE trains SET available_seats = available_seats + ? WHERE train_id = ?",
(seat_count, train_id))
cursor.execute("DELETE FROM reservations WHERE reservation_id = ?", (reservation_id,))
conn.commit()
print("Ticket cancelled successfully!")
else:
print("Reservation not found.")
# Function to display available trains
def display_available_trains():
print("\nAvailable Trains:")
train.show_trains()
# Function to search for trains
def search_trains_by_source_destination(source, destination):
cursor.execute("SELECT * FROM trains WHERE source = ? AND destination = ?", (source, destination))
rows = cursor.fetchall()
if rows:
for row in rows:
print(f"Train ID: {row[0]}, Name: {row[1]}, Source: {row[2]}, Destination: {row[3]}, Departure: {row[4]}, Available Seats: {row[5]}")
else:
print("No trains found for this route.")
# Function to view reservations
def view_reservations():
cursor.execute("SELECT * FROM reservations")
for row in cursor.fetchall():
print(f"Reservation ID: {row[0]}, User: {row[1]}, Train ID: {row[2]}, Seat Count: {row[3]}, Status: {row[4]}")
# Sample Train Data
train1 = Train("Express 101", "New York", "Boston", "2025-01-10 10:00", 50)
train1.add_train()
train2 = Train("Express 102", "Los Angeles", "Chicago", "2025-01-12 09:00", 30)
train2.add_train()
# Sample Reservation Data
reservation1 = Reservation("John Doe", 1, 2)
reservation1.book_ticket()
reservation2 = Reservation("Jane Smith", 2, 3)
reservation2.book_ticket()
# Menu-driven interface
def main():
while True:
print("\n--- Railway Management System ---")
print("1. View Available Trains")
print("2. Book Ticket")
print("3. Cancel Ticket")
print("4. Search Trains by Source and Destination")
print("5. View All Reservations")
print("6. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
display_available_trains()
elif choice == 2:
user_name = input("Enter your name: ")
train_id = int(input("Enter train ID: "))
seat_count = int(input("Enter number of seats to book: "))
reservation = Reservation(user_name, train_id, seat_count)
reservation.book_ticket()
elif choice == 3:
reservation_id = int(input("Enter reservation ID to cancel: "))
reservation = Reservation("", 0, 0)
reservation.cancel_ticket(reservation_id)
elif choice == 4:
source = input("Enter source station: ")
destination = input("Enter destination station: ")
search_trains_by_source_destination(source, destination)
elif choice == 5:
view_reservations()
elif choice == 6:
print("Exiting system.")
break
else:
print("Invalid choice, please try again.")
if __name__ == "__main__":
main()
# Close the database connection at the end
conn.close()