0% found this document useful (0 votes)
9 views7 pages

Document

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)
9 views7 pages

Document

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/ 7

import pickle

import os

# -----------------------------
# Railway Ticket Reservation System
# Class 12 Project (Python + File Handling)
# -----------------------------

FILENAME = "railway.dat"

class Ticket:
def __init__(self, pnr, name, age, gender, train_no, train_name,
source, destination):
self.pnr = pnr
self.name = name
self.age = age
self.gender = gender
self.train_no = train_no
self.train_name = train_name
self.source = source
self.destination = destination
def display(self):
print("="*40)
print(f"PNR Number : {self.pnr}")
print(f"Passenger Name : {self.name}")
print(f"Age / Gender : {self.age} / {self.gender}")
print(f"Train No/Name : {self.train_no} - {self.train_name}")
print(f"From {self.source} ---> To {self.destination}")
print("="*40)

# -----------------------------
# Utility Functions
# -----------------------------

def add_ticket():
pnr = input("Enter PNR Number: ")
name = input("Enter Passenger Name: ")
age = int(input("Enter Age: "))
gender = input("Enter Gender (M/F): ")
train_no = input("Enter Train Number: ")
train_name = input("Enter Train Name: ")
source = input("Enter Source Station: ")
destination = input("Enter Destination Station: ")

ticket = Ticket(pnr, name, age, gender, train_no, train_name, source,


destination)

with open(FILENAME, "ab") as f:


pickle.dump(ticket, f)

print("\n Ticket booked successfully!\n")

def display_all_tickets():
if not os.path.exists(FILENAME):
print("No records found!")
return

with open(FILENAME, "rb") as f:


print("\n--- All Booked Tickets ---\n")
while True:
try:
ticket = pickle.load(f)
ticket.display()
except EOFError:
break

def search_ticket():
if not os.path.exists(FILENAME):
print("No records found!")
return

pnr = input("Enter PNR to search: ")


found = False
with open(FILENAME, "rb") as f:
while True:
try:
ticket = pickle.load(f)
if ticket.pnr == pnr:
print("\nTicket Found:")
ticket.display()
found = True
break
except EOFError:
break
if not found:
print(" Ticket not found!")

def delete_ticket():
if not os.path.exists(FILENAME):
print("No records found!")
return

pnr = input("Enter PNR to cancel: ")


tickets = []
found = False

with open(FILENAME, "rb") as f:


while True:
try:
ticket = pickle.load(f)
if ticket.pnr != pnr:
tickets.append(ticket)
else:
found = True
except EOFError:
break

if found:
with open(FILENAME, "wb") as f:
for t in tickets:
pickle.dump(t, f)

print(" Ticket cancelled successfully!")


else:

print(" Ticket not found!")

# -----------------------------
# Main Menu
# -----------------------------

def main():
while True:
print("\n=== Railway Ticket Reservation System ===")
print("1. Book Ticket")
print("2. Display All Tickets")
print("3. Search Ticket by PNR")
print("4. Cancel Ticket")
print("5. Exit")

choice = input("Enter choice (1-5): ")

if choice == '1':
add_ticket()
elif choice == '2':
display_all_tickets()
elif choice == '3':
search_ticket()
elif choice == '4':
delete_ticket()
elif choice == '5':
print("Thank you for using Railway Reservation System!")
break
else:
print(" Invalid choice! Try again.")

if __name__ == "__main__":
main()

You might also like