Railway System Project
Railway System Project
2024-25
PROJECT TOPIC :
1|Page
CERTIFICATE
This is to certify that JATIN JARWAL of
class: XII A of JINVANI BHARTI
SCHOOL has done his project on
RAILWAY RESERVATION SYSTEM under my
supervision. He has taken interest and has
shown at most sincerity in completion of this
project.
I certify this project up to my expectation &
as per guidelines issued by CBSE, NEW DELHI.
PRINCIPAL
2|Page
ACKNOWLEDGMENT
It is with pleasure that I acknowledge my sincere
gratitude to our teacher, MRS. SHALINI
KHETERPAL who taught and undertook the
responsibility of teaching the subject computer
science. I have been greatly benefited from his
classes.
I am especially indebted to our Principal MRS.
PREETI JAIN who has always been a source of
encouragement and support and without whose
inspiration this project would not have been a
successful I would like to place on record
heartfelt thanks to him.
Finally, I would like to express my sincere
appreciation for all the other students in my
batch their friend & the fine time that we all
shared together.
3|Page
HARDWARES AND SOFTWARES
REQUIRED
HARDWARES
1. Desktop / Laptop
SOFTWARES
1. Python (latest version)
2. MySQL
3. Python Connector Module
4|Page
CONTENTS
S.No. Topic Page No.
1 Certificate 2
2 Acknowledgement 3
4 Introduction 6
6 MySQL Database 22
5|Page
7 Outputs 25
8 References 30
INTRODUCTION
The project RAILWAY RESERVATION SYSTEM
Objectives of the Project
Book Tickets
The `Book_Ticket(uid)` function simulates the ticket
booking process, allowing users to select trains,
classes, and passengers for reservation.
Cancel Tickets
The `Cancel_Ticket()` function facilitates the cancellation
of booked tickets, showcasing the system's ability to
handle modifications to user bookings.
210 | P a g
e
import mysql.connector
def connect_to_db():
try:
connection = mysql.connector.connect(
host="localhost",
user="root",
password="7790",
database="railway_system"
)
print("Successfully connected to the database")
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def view_trains(connection):
cursor = connection.cursor()
cursor.execute("SELECT * FROM trains")
result = cursor.fetchall()
print("\nAvailable Trains:")
print("ID\tName\t\tSource\t\tDestination\t AvailableSeats")
print("-"*70)
for row in result:
print(f"{row[0]}\t{row[1]}\t{row[2]}\t\t{row[3]}\t\t\t{row[4]}")
print("-"*70)
def view_reservations(connection):
cursor = connection.cursor()
cursor.execute("SELECT r.reservation_id, t.train_name, r.passenger_name,
r.seats FROM reservations r JOIN trains t ON r.train_id = t.train_id")
result = cursor.fetchall()
print("\nReservations:")
print("Reservation ID\tTrain Name\tPassenger Name\tSeats")
print("-"*65)
for row in result:
print(f"{row[0]}\t\t{row[1]}\t{row[2]}\t\t{row[3]}")
print("-"*65)
def book_ticket(connection):
cursor = connection.cursor()
train_id = int(input("Enter Train ID: "))
seats = int(input("Enter Number of Seats: "))
211 | P a g
e
for k in range(seats):
passenger_name = input("Enter Passenger Name: ")
cursor.execute("SELECT seats FROM trains WHERE train_id=%s", (train_id,))
result = cursor.fetchone()
if result:
available_seats = result[0]
if available_seats >= seats:
new_seats = available_seats - 1
cursor.execute("UPDATE trains SET seats=%s WHERE train_id=%s",
(new_seats, train_id))
cursor.execute("INSERT INTO reservations (train_id, passenger_name,
seats) VALUES (%s, %s, %s)",
(train_id, passenger_name, 1))
connection.commit()
print(f"Booked a seat for {passenger_name} on train {train_id}")
else:
print("Not enough seats available")
else:
print(f"Train ID {train_id} not found in the database.")
def cancel_ticket(connection):
reservation_id = int(input("Enter Reservation ID to cancel: "))
cursor = connection.cursor()
cursor.execute("SELECT train_id, seats FROM reservations WHERE
reservation_id=%s", (reservation_id,))
reservation = cursor.fetchone()
if reservation:
train_id, seats = reservation
cursor.execute("DELETE FROM reservations WHERE reservation_id=%s",
(reservation_id,))
cursor.execute("SELECT seats FROM trains WHERE train_id=%s", (train_id,))
available_seats = cursor.fetchone()[0]
new_seats = available_seats + seats
cursor.execute("UPDATE trains SET seats=%s WHERE train_id=%s",
(new_seats, train_id))
connection.commit()
print(f"Cancelled reservation {reservation_id}, and released {seats} seats")
else:
print("Reservation not found")
def main():
connection = connect_to_db()
if connection:
while True:
print("\nOptions: \n1. View Trains \n2. Book Ticket \n3. Cancel Ticket \n4.
View Reservations \n5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
view_trains(connection)
elif choice == 2:
book_ticket(connection)
212 | P a g
e
elif choice == 3:
cancel_ticket(connection)
elif choice == 4:
view_reservations(connection)
elif choice == 5:
connection.close()
print("Thank you for using the Railway Reservation System!")
break
else:
print("Invalid choice, please try again")
if __name__ == "__main__":
main()
213 | P a g
e
MYSQL
DATABASE:
214 | P a g
e
TRAIN TABLE:
TRAINS LIST:
RESERVATIONS LIST:
215 | P a g
e
OUTPUTS:
216 | P a g
e
TRAIN RESERVATION MAIN PAGE:
217 | P a g
e
CANCEL TICKET:
BOOKING TICKETS :
30 | P a g e
VIEWING RESERVATIONS:
30 | P a g e
REFERENCES:
1. CLASS 11TH& 12TH COMPUTER
SCIENCE BOOK
(SUMITA ARORA)
2. PYTHON
HTTPS://www.PYTHON.ORG/
3. MYSQL
HTTPS://WWW.MYSQL.COM/
31 | P a g e