Q.Every mode of transport utilises a reservation system to ensure its smooth and efficient functioning. If you analyse you would find many things in common. You are required to identify any one mode of transportation and prepare a reservation system for it. For example, let us look at the Railway reservation system we talked about earlier. The complex task of designing a good railway reservation system is seen as designing the different components of the system and then making them work with each other efficiently. Possible sub-systems are shown in Figure 1. Each of them may be modelled using functions.
Write a python code to automate the reservation needs of the identified mode of transport.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Model one transport mode (a bus operator) as co-operating functions — show routes, book (availability check + billing + PNR), cancel (release seats), view booking — sharing a routes dictionary and a bookings dictionary.
The idea. The question is really about system decomposition: a complex reservation system becomes manageable when each sub-system (enquiry, reservation, billing, cancellation) is its own function and they communicate through shared data structures. Here the shared state is routes (per-bus fare and live seat inventory) and bookings (per-PNR passenger record). Booking must atomically check availability, compute the bill and decrement seats; cancellation must put the seats back — the pair of functions keeps the inventory consistent.
Program:
# A simple BUS reservation system, decomposed into co-operating functions
routes = {
'B101': ['Bengaluru', 'Mysuru', 450.0, 40], # [from, to, fare, seats available]
'B202': ['Bengaluru', 'Chennai', 950.0, 30],
'B303': ['Mysuru', 'Mangaluru', 700.0, 35],
}
bookings = {} # PNR -> [name, bus, seats, amount]
next_pnr = 5001
def show_routes():
print("Bus From To Fare Seats left")
for bus in routes:
r = routes[bus]
print(bus, " ", r[0], " ", r[1], " ", r[2], " ", r[3])
def book_ticket():
global next_pnr
bus = input("Bus number: ")
if bus not in routes:
print("No such bus")
return
name = input("Passenger name: ")
seats = int(input("Number of seats: "))
if seats > routes[bus][3]:
print("Only", routes[bus][3], "seats left")
return
amount = seats * routes[bus][2]
routes[bus][3] = routes[bus][3] - seats
bookings[next_pnr] = [name, bus, seats, amount]
print("Booked! PNR:", next_pnr, "| Amount:", amount)
next_pnr = next_pnr + 1
def cancel_ticket():
pnr = int(input("PNR to cancel: "))
if pnr not in bookings:
print("No such PNR")
return
(name, bus, seats, amount) = bookings.pop(pnr)
routes[bus][3] = routes[bus][3] + seats
print("Cancelled PNR", pnr, "-", seats, "seat(s) released on", bus)
def show_booking():
pnr = int(input("PNR: "))
if pnr in bookings:
print("PNR", pnr, ":", bookings[pnr])
else:
print("No such PNR")
while True:
print()
print("1.Routes 2.Book 3.Cancel 4.My booking 5.Exit")
choice = input("Enter choice: ")
if choice == '1':
show_routes()
elif choice == '2':
book_ticket()
elif choice == '3':
cancel_ticket()
elif choice == '4':
show_booking()
elif choice == '5':
print("Thank you for travelling with us!")
break
Sample session:
1.Routes 2.Book 3.Cancel 4.My booking 5.Exit
Enter choice: 1
Bus From To Fare Seats left
B101 Bengaluru Mysuru 450.0 40
B202 Bengaluru Chennai 950.0 30
B303 Mysuru Mangaluru 700.0 35
1.Routes 2.Book 3.Cancel 4.My booking 5.Exit
Enter choice: 2
Bus number: B101
Passenger name: Ravi
Number of seats: 2
Booked! PNR: 5001 | Amount: 900.0
1.Routes 2.Book 3.Cancel 4.My booking 5.Exit
Enter choice: 4
PNR: 5001
PNR 5001 : ['Ravi', 'B101', 2, 900.0] …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.