0% found this document useful (0 votes)
4 views

Train_Ticket_Reservation_System

Train ticket reservation system in python program

Uploaded by

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

Train_Ticket_Reservation_System

Train ticket reservation system in python program

Uploaded by

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

Python Program for Train Ticket Reservation System

This program is a simple implementation of a Train Ticket Reservation system. It allows


users to check seat availability, book tickets, cancel tickets, and display the reservation
status for a specific train.

Python Code

class TrainReservation:
def __init__(self, train_name, total_seats):
self.train_name = train_name
self.total_seats = total_seats
self.reserved_seats = 0

def check_availability(self):
available_seats = self.total_seats - self.reserved_seats
return available_seats

def book_ticket(self, num_tickets):


if num_tickets <= 0:
print("Invalid number of tickets!")
return
available_seats = self.check_availability()
if num_tickets > available_seats:
print(f"Sorry, only {available_seats} seats are available.")
else:
self.reserved_seats += num_tickets
print(f"Successfully booked {num_tickets} tickets.")

def cancel_ticket(self, num_tickets):


if num_tickets <= 0 or num_tickets > self.reserved_seats:
print("Invalid cancellation request!")
return
self.reserved_seats -= num_tickets
print(f"Successfully cancelled {num_tickets} tickets.")

def show_reservation(self):
print(f"Train: {self.train_name}")
print(f"Total Seats: {self.total_seats}")
print(f"Reserved Seats: {self.reserved_seats}")
print(f"Available Seats: {self.check_availability()}")

# Example usage
def menu():
train = TrainReservation("Express Train", 100)

while True:
print("\nMenu:")
print("1. Check Availability")
print("2. Book Ticket")
print("3. Cancel Ticket")
print("4. Show Reservation Status")
print("5. Exit")
choice = int(input("Enter your choice: "))

if choice == 1:
print(f"Available Seats: {train.check_availability()}")
elif choice == 2:
num_tickets = int(input("Enter number of tickets to book: "))
train.book_ticket(num_tickets)
elif choice == 3:
num_tickets = int(input("Enter number of tickets to cancel: "))
train.cancel_ticket(num_tickets)
elif choice == 4:
train.show_reservation()
elif choice == 5:
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")

menu()

You might also like