0% found this document useful (0 votes)
17 views33 pages

RAILWAY RESERVATION SYSTEM CS Project

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views33 pages

RAILWAY RESERVATION SYSTEM CS Project

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Tab 1

PROJECT
2024-2025

Topic:
RAILWAY RESERVATION SYSTEM

Submitted By:
Harrish. T
XII-B
12215
TABLE OF CONTENTS

S.NO SUB-TOPIC
1 Introduction
2 Requirements
3 Source Code
4 Output
5 SQL
6 Bibliography
INTRODUCTION
The Railway Reservation System project is a practical
application designed to simulate and streamline the process of
booking railway tickets, managing train schedules, and
enhancing user experience. This system is developed as part of
the Computer Science curriculum for Class 12, adhering to
CBSE guidelines. It demonstrates the application of Python
programming and database management concepts using
MySQL.

The system serves two primary users: administrators and


passengers. Administrators have tools to manage train details,
including adding, updating, and canceling train schedules.
Passengers, on the other hand, can register, search for trains,
book tickets, and manage their reservations through an intuitive
user interface.
Objectives:
1.​Efficiency in Operations:​

○​ Automate the management of train schedules and


bookings to save time and reduce human errors.
○​ Provide a robust mechanism for passengers to search
for trains based on origin and destination.
2.​Administrative Empowerment:​

○​ Enable administrators to manage train details,


including seat availability, fares, and journey
information.
○​ Facilitate flexibility through train cancellation and
schedule updates.
3.​Enhancing Customer Experience:​

○​ Allow passengers to register and log in to the system


securely.
○​ Simplify ticket booking and cancellation with clear
and user-friendly options.
Significance:
The project highlights the importance of a reliable and efficient
railway reservation system in today's fast-paced world. It
addresses real-world challenges in transportation, ensuring:

●​ Accuracy in data storage and retrieval.


●​ Scalability for managing a growing number of
passengers and trains.
●​ Security to protect sensitive user information.

By combining Python programming with MySQL database


integration, this project showcases how computational tools
can solve practical problems in the real world, making it an
ideal learning experience for Class 12 Computer Science
students.
REQUIREMENTS

SPECIFICATIONS PRE-REQUISITES
Processor - Intel i5 (or Tabulate and MySQL
above) connector Module

256 GB (or above) Memory IDLE python

6 GB RAM (or above) MySQL


SOURCE CODE
import random
import mysql.connector as con
from tabulate import tabulate

# Create database tables


def create_tables():
db = get_connection()
cursor = db.cursor()

# SQL for creating tables


table_user = """
CREATE TABLE IF NOT EXISTS user (
user_id VARCHAR(20) PRIMARY KEY,
passenger_id INT,
name VARCHAR(50),
phone VARCHAR(15),
email_id VARCHAR(50),
password VARCHAR(20)
);
"""
table_train_schedule = """
CREATE TABLE IF NOT EXISTS train_schedule (
train_no INT PRIMARY KEY,
train_name VARCHAR(50),
origin VARCHAR(50),
destination VARCHAR(50),
distance INT,
time VARCHAR(10),
seats_ac INT,
seats_sl INT,
seats_gen INT,
fare_ac INT,
fare_sl INT,
fare_gen INT,
available_days VARCHAR(50)
);
"""
table_booked_tickets = """
CREATE TABLE IF NOT EXISTS booked_tickets (
ticket_id INT AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(20),
train_no INT,
passenger_name VARCHAR(50),
age INT,
gender VARCHAR(10),
class_type VARCHAR(10),
fare INT,
status VARCHAR(20),
FOREIGN KEY (user_id) REFERENCES user(user_id),
FOREIGN KEY (train_no) REFERENCES
train_schedule(train_no)
);
"""

# Execute table creation queries


cursor.execute(table_user)
cursor.execute(table_train_schedule)
cursor.execute(table_booked_tickets)

db.commit()
print("Tables created successfully!")
db.close()

# Establish database connection


def get_connection():
return con.connect(
host="localhost",
user="root",
password="harrish2007",
database="train_reservation"
)

# User Registration
def new_user():
db = get_connection()
cursor = db.cursor()

print("\n--- New User Registration ---")


user_id = input("Enter User ID: ")
name = input("Enter Name: ")
phone = input("Enter Phone Number: ")
email_id = input("Enter Email ID: ")
password = input("Enter Password: ")
passenger_id = random.randint(1000, 9999)
query = """
INSERT INTO user (user_id, passenger_id, name,
phone, email_id, password)
VALUES (%s, %s, %s, %s, %s, %s)
"""
values = (user_id, passenger_id, name, phone, email_id,
password)
cursor.execute(query, values)

db.commit()
print(f"User {user_id} registered successfully!")
db.close()

# Forgot User ID
def forgot_user_id():
db = get_connection()
cursor = db.cursor()

print("\n--- Forgot User ID ---")


email_id = input("Enter Registered Email ID: ")

query = "SELECT user_id FROM user WHERE email_id =


%s"
cursor.execute(query, (email_id,))
result = cursor.fetchone()

if result:
print(f"Your User ID is: {result[0]}")
else:
print("Email not found!")
db.close()

# Old User Login


def old_user():
db = get_connection()
cursor = db.cursor()

print("\n--- User Login ---")


user_id = input("Enter User ID: ")
password = input("Enter Password: ")

query = "SELECT * FROM user WHERE user_id = %s


AND password = %s"
cursor.execute(query, (user_id, password))
result = cursor.fetchone()

if result:
print("Login Successful!")
passenger_panel(user_id)
else:
print("Invalid User ID or Password!")
forgot_user_id()
db.close()

# Admin Panel
# Admin Panel
def admin_panel():
while True:
print("\n--- Admin Panel ---")
print("1. Add Train")
print("2. Update Train Details")
print("3. Cancel Train")
print("4. Logout")

choice = int(input("Enter your choice: "))

if choice == 1:
add_train()
elif choice == 2:
update_train_details()
elif choice == 3:
cancel_train()
elif choice == 4:
break
else:
print("Invalid Choice!")

# Add Train
def add_train():
db = get_connection()
cursor = db.cursor()

print("\n--- Add Train ---")


train_no = int(input("Enter Train Number: "))
train_name = input("Enter Train Name: ")
origin = input("Enter Origin: ")
destination = input("Enter Destination: ")
distance = int(input("Enter Journey Distance (in km):
"))
time = input("Enter Journey Time (e.g., 5h30m): ")
seats_ac = int(input("Enter Seats in AC: "))
seats_sl = int(input("Enter Seats in SL: "))
seats_gen = int(input("Enter Seats in GEN: "))
fare_ac = int(input("Enter Fare for AC: "))
fare_sl = int(input("Enter Fare for SL: "))
fare_gen = int(input("Enter Fare for GEN: "))
available_days = input("Enter Available Days (e.g., Mon,
Wed): ")

query = """
INSERT INTO train_schedule (train_no, train_name,
origin, destination, distance, time,
seats_ac, seats_sl, seats_gen, fare_ac,
fare_sl, fare_gen, available_days)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s)
"""
values = (train_no, train_name, origin, destination,
distance, time, seats_ac,
seats_sl, seats_gen, fare_ac, fare_sl, fare_gen,
available_days)
cursor.execute(query, values)

db.commit()
print("Train added successfully!")
db.close()

# Update Train Details


def update_train_details():
db = get_connection()
cursor = db.cursor()
print("\n--- Update Train Details ---")
train_no = int(input("Enter Train Number to Update: "))

print("What would you like to update?")


print("1. Train Name")
print("2. Origin")
print("3. Destination")
print("4. Distance")
print("5. Journey Time")
print("6. Seats in AC")
print("7. Seats in SL")
print("8. Seats in GEN")
print("9. Fare for AC")
print("10. Fare for SL")
print("11. Fare for GEN")
print("12. Available Days")

choice = int(input("Enter your choice: "))

if choice == 1:
new_value = input("Enter New Train Name: ")
query = "UPDATE train_schedule SET train_name =
%s WHERE train_no = %s"
elif choice == 2:
new_value = input("Enter New Origin: ")
query = "UPDATE train_schedule SET origin = %s
WHERE train_no = %s"
elif choice == 3:
new_value = input("Enter New Destination: ")
query = "UPDATE train_schedule SET destination =
%s WHERE train_no = %s"
elif choice == 4:
new_value = int(input("Enter New Distance (in km):
"))
query = "UPDATE train_schedule SET distance = %s
WHERE train_no = %s"
elif choice == 5:
new_value = input("Enter New Journey Time (e.g.,
5h30m): ")
query = "UPDATE train_schedule SET time = %s
WHERE train_no = %s"
elif choice == 6:
new_value = int(input("Enter New Seats in AC: "))
query = "UPDATE train_schedule SET seats_ac = %s
WHERE train_no = %s"
elif choice == 7:
new_value = int(input("Enter New Seats in SL: "))
query = "UPDATE train_schedule SET seats_sl = %s
WHERE train_no = %s"
elif choice == 8:
new_value = int(input("Enter New Seats in GEN: "))
query = "UPDATE train_schedule SET seats_gen = %s
WHERE train_no = %s"
elif choice == 9:
new_value = int(input("Enter New Fare for AC: "))
query = "UPDATE train_schedule SET fare_ac = %s
WHERE train_no = %s"
elif choice == 10:
new_value = int(input("Enter New Fare for SL: "))
query = "UPDATE train_schedule SET fare_sl = %s
WHERE train_no = %s"
elif choice == 11:
new_value = int(input("Enter New Fare for GEN: "))
query = "UPDATE train_schedule SET fare_gen = %s
WHERE train_no = %s"
elif choice == 12:
new_value = input("Enter New Available Days (e.g.,
Mon, Wed): ")
query = "UPDATE train_schedule SET available_days
= %s WHERE train_no = %s"
else:
print("Invalid choice!")
db.close()
return

# Execute the query


cursor.execute(query, (new_value, train_no))
db.commit()
print("Train details updated successfully!")
db.close()
# Cancel Train
def cancel_train():
db = get_connection()
cursor = db.cursor()

print("\n--- Cancel Train ---")


train_no = int(input("Enter Train Number to Cancel: "))

# Check if the train exists


query_check = "SELECT * FROM train_schedule
WHERE train_no = %s"
cursor.execute(query_check, (train_no,))
train = cursor.fetchone()

if train:
# Proceed to cancel the train
query_delete = "DELETE FROM train_schedule
WHERE train_no = %s"
cursor.execute(query_delete, (train_no,))
db.commit()
print(f"Train Number {train_no} has been canceled
successfully!")
else:
print(f"Train Number {train_no} does not exist.")

db.close()

# Passenger Panel
def passenger_panel(user_id):
while True:
print("\n--- Passenger Panel ---")
print("1. Search Train")
print("2. Book Ticket")
print("3. Cancel Ticket")
print("4. Logout")

choice = int(input("Enter your choice: "))

if choice == 1:
search_train()
elif choice == 2:
book_ticket(user_id)
elif choice == 3:
cancel_ticket(user_id)
elif choice == 4:
break
else:
print("Invalid Choice!")

# Search Train
def search_train():
db = get_connection()
cursor = db.cursor()

print("\n--- Search Train ---")


origin = input("Enter Origin: ")
destination = input("Enter Destination: ")
query = "SELECT * FROM train_schedule WHERE origin
= %s AND destination = %s"
cursor.execute(query, (origin, destination))
trains = cursor.fetchall()

if trains:
print(tabulate(trains, headers=["Train No", "Train
Name", "Origin", "Destination", "Distance",
"Time", "Seats AC", "Seats SL", "Seats
GEN", "Fare AC",
"Fare SL", "Fare GEN", "Available
Days"]))
else:
print("No trains found!")
db.close()

# Book Ticket
def book_ticket(user_id):
db = get_connection()
cursor = db.cursor()

print("\n--- Book Ticket ---")

# Display all available trains


query_trains = "SELECT train_no, train_name, origin,
destination FROM train_schedule"
cursor.execute(query_trains)
trains = cursor.fetchall()
if trains:
print(tabulate(trains, headers=["Train No", "Train
Name", "Origin", "Destination"], tablefmt="fancy_grid"))
train_no = int(input("Enter Train Number for
Booking: "))

# Check if the selected train exists


query_check = "SELECT * FROM train_schedule
WHERE train_no = %s"
cursor.execute(query_check, (train_no,))
train = cursor.fetchone()

if train:
print("1. AC Class")
print("2. Sleeper Class")
print("3. General Class")
class_choice = int(input("Enter Class Choice (1-AC,
2-Sleeper, 3-General): "))

# Select class type and check seat availability


if class_choice == 1:
class_type = "AC"
seat_column = "seats_ac"
fare_column = "fare_ac"
elif class_choice == 2:
class_type = "Sleeper"
seat_column = "seats_sl"
fare_column = "fare_sl"
elif class_choice == 3:
class_type = "General"
seat_column = "seats_gen"
fare_column = "fare_gen"
else:
print("Invalid Class Choice!")
return

# Fetch seat availability and fare


query_seats = f"SELECT {seat_column},
{fare_column} FROM train_schedule WHERE train_no =
%s"
cursor.execute(query_seats, (train_no,))
seat_data = cursor.fetchone()

available_seats, fare = seat_data

if available_seats > 0:
num_tickets = int(input(f"Enter Number of
Tickets to Book (Max {available_seats}): "))
if num_tickets > available_seats:
print(f"Only {available_seats} seats are
available.")
return

# Book tickets
for _ in range(num_tickets):
passenger_name = input("Enter Passenger
Name: ")
age = int(input("Enter Passenger Age: "))
gender = input("Enter Passenger Gender
(M/F/O): ")

# Generate ticket ID and insert into


booked_tickets table
ticket_id = random.randint(100000, 999999)
query_ticket = """
INSERT INTO booked_tickets (user_id,
train_no, passenger_name, age, gender, class_type, fare,
status)
VALUES (%s, %s, %s, %s, %s, %s, %s, 'Confirmed')
"""
cursor.execute(query_ticket, (user_id, train_no,
passenger_name, age, gender, class_type, fare))

# Update seat availability in train_schedule


updated_seats = available_seats - num_tickets
query_update_seats = f"UPDATE train_schedule
SET {seat_column} = %s WHERE train_no = %s"
cursor.execute(query_update_seats,
(updated_seats, train_no))

db.commit()
print(f"Tickets booked successfully! Total Fare:
{fare * num_tickets}")
else:
print(f"No seats available in {class_type} class.")
else:
print("Invalid Train Number!")
else:
print("No trains available for booking.")

db.close()

# Cancel Ticket
def cancel_ticket(user_id):
db = get_connection()
cursor = db.cursor()

print("\n--- Cancel Ticket ---")

# Display user's booked tickets


query_tickets = "SELECT ticket_id, train_no,
passenger_name, class_type, fare FROM booked_tickets
WHERE user_id = %s"
cursor.execute(query_tickets, (user_id,))
tickets = cursor.fetchall()

if tickets:
print(tabulate(tickets, headers=["Ticket ID", "Train
No", "Passenger Name", "Class Type", "Fare"],
tablefmt="fancy_grid"))
ticket_id = int(input("Enter Ticket ID to Cancel: "))

# Fetch details of the selected ticket


query_check = "SELECT train_no, class_type FROM
booked_tickets WHERE ticket_id = %s AND user_id = %s"
cursor.execute(query_check, (ticket_id, user_id))
ticket = cursor.fetchone()

if ticket:
train_no, class_type = ticket

# Determine the seat column based on class type


if class_type.lower() == "ac":
seat_column = "seats_ac"
elif class_type.lower() == "sleeper":
seat_column = "seats_sl"
elif class_type.lower() == "general":
seat_column = "seats_gen"
else:
print("Invalid Class Type!")
db.close()
return

# Cancel ticket and update seat availability


query_cancel = "DELETE FROM booked_tickets
WHERE ticket_id = %s"
cursor.execute(query_cancel, (ticket_id,))

query_update_seats = f"UPDATE train_schedule


SET {seat_column} = {seat_column} + 1 WHERE train_no =
%s"
cursor.execute(query_update_seats, (train_no,))

db.commit()
print("Ticket canceled successfully!")
else:
print("Invalid Ticket ID!")
else:
print("No tickets booked under this User ID.")

db.close()

# Main Menu
def main_menu():
while True:
print("\n--- Main Menu ---")
print("1. Admin Login")
print("2. User Login")
print("3. Register New User")
print("4. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:
admin_panel()
elif choice == 2:
old_user()
elif choice == 3:
new_user()
elif choice == 4:
break
else:
print("Invalid Choice!")

# Entry Point
if __name__ == "__main__":
create_tables()
main_menu()
OUTPUT
SQL
BIBLIOGRAPHY
The following sources were used for the
appropriate information required to
complete the project:

●​CLASS 12th Computer Science Book (SUMITA ARORA)


●​PYTHON https://www.python.org/
●​MySQL https://www.mysql.com/

You might also like