Bus Reservation
Bus Reservation
def __init__(self):
self.buses = [] # List of all buses
self.bookings = [] # List of all bookings
def add_bus(self):
"""Add a new bus to the system."""
print("\n--- Add Bus ---")
bus_id = input("Enter Bus ID: ")
bus_name = input("Enter Bus Name: ")
total_seats = int(input("Enter Total Seats: "))
self.buses.append({
"Bus ID": bus_id,
"Bus Name": bus_name,
"Total Seats": total_seats,
"Available Seats": total_seats
})
print("Bus added successfully.")
def view_buses(self):
"""View all available buses."""
if not self.buses:
print("\nNo buses available.")
return
print("\n--- Available Buses ---")
for bus in self.buses:
print(f"Bus ID: {bus['Bus ID']}, Name: {bus['Bus Name']}, "
f"Total Seats: {bus['Total Seats']}, Available Seats:
{bus['Available Seats']}")
def book_seat(self):
"""Book a seat on a bus."""
print("\n--- Book Seat ---")
if not self.buses:
print("No buses available for booking.")
return
bus_id = input("Enter Bus ID to Book: ")
for bus in self.buses:
if bus["Bus ID"] == bus_id:
if bus["Available Seats"] <= 0:
print("Sorry, no seats are available on this bus.")
return
customer_name = input("Enter Customer Name: ")
seat_count = int(input("Enter Number of Seats to Book: "))
if seat_count > bus["Available Seats"]:
print(f"Sorry, only {bus['Available Seats']} seats are
available.")
return
bus["Available Seats"] -= seat_count
self.bookings.append({
"Bus ID": bus_id,
"Customer Name": customer_name,
"Seats Booked": seat_count
})
print(f"{seat_count} seats booked successfully for
{customer_name}.")
return
print("Bus ID not found.")
def view_bookings(self):
"""View all current bookings."""
if not self.bookings:
print("\nNo bookings found.")
return
print("\n--- Current Bookings ---")
for booking in self.bookings:
print(f"Bus ID: {booking['Bus ID']}, Customer: {booking['Customer
Name']}, "
f"Seats Booked: {booking['Seats Booked']}")
def main_menu(self):
"""Display the main menu and handle user choices."""
while True:
print("\n--- Online Bus Reservation System ---")
print("1. Add Bus")
print("2. View Buses")
print("3. Book Seat")
print("4. View Bookings")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == '1':
self.add_bus()
elif choice == '2':
self.view_buses()
elif choice == '3':
self.book_seat()
elif choice == '4':
self.view_bookings()
elif choice == '5':
print("Exiting the system. Goodbye!")
break
else:
print("Invalid choice. Please try again.")