Design a Booking Desk
Lesson 48 of 48 in Coddy's Python for C Developers course.
Challenge
BeginnerImplement Room(capacity) with independent capacity and bookings (name-to-seat-count dict), reserve(name,seats), and cancel(name). Failed operations raise ValueError without mutation: reserve requires positive int seats, a new name, and enough remaining capacity; cancel requires an existing name. BookingDesk(room) stores the supplied room; handle(command) delegates reserve/cancel, catches ValueError, returns ok/invalid, and never prints. main() reads nonnegative capacity, nonnegative command count, then commands reserve name seats or cancel name. Names are nonempty lowercase ASCII tokens. Reject malformed integers, wrong field counts, unknown or empty commands, duplicate bookings, and insufficient capacity. Print ok/invalid per command, then available:N and sorted active name:seats lines. Canceled names may reserve again; zero capacity admits no reservations. All data stays in memory.
Place Room in room.py, BookingDesk in booking_desk.py, and input/output in main.py inside main(). Keep the supplied imports and entry wrapper. The desk uses the supplied current room rather than constructing a replacement.
The driver replaces desk.room with a probe, like injecting a C callback/context pair, to check delegation and state preservation.
Your code goes in room.py, booking_desk.py, main.py. The locked driver.py is the entry file, supplies execution and checks, and cannot be edited.
Try it yourself
from room import Room
from booking_desk import BookingDesk
from main import main
main()
probe_room = Room(4)
other_room = Room(4)
probe_room.reserve("a", 3)
assert probe_room.capacity == 4 and probe_room.bookings == {"a": 3}
assert other_room.bookings == {}
for name, seats in [("a", 1), ("b", 2), ("c", 0), ("d", -1)]:
try:
probe_room.reserve(name, seats)
except ValueError:
pass
else:
raise AssertionError("Invalid reservation accepted")
assert probe_room.bookings == {"a": 3}
try:
probe_room.cancel("missing")
except ValueError:
pass
else:
raise AssertionError("Unknown cancellation accepted")
probe_room.cancel("a")
assert probe_room.bookings == {}
probe_desk = BookingDesk(other_room)
assert probe_desk.room is other_room
assert probe_desk.handle("reserve x 4") == "ok"
assert other_room.bookings == {"x": 4}
class ProbeRoom:
def __init__(self):
self.calls = []
def reserve(self, name, seats):
self.calls.append(("reserve", name, seats))
if seats == 9:
raise ValueError("probe rejection")
def cancel(self, name):
self.calls.append(("cancel", name))
probe = ProbeRoom()
probe_desk.room = probe
assert probe_desk.handle("reserve y 2") == "ok"
assert probe_desk.handle("cancel y") == "ok"
assert probe_desk.handle("reserve z 9") == "invalid"
assert probe_desk.handle("reserve a bad") == "invalid"
assert probe.calls == [("reserve", "y", 2), ("cancel", "y"), ("reserve", "z", 9)]
All lessons in Python for C Developers
1From C Programs to Python
Run Python Without mainPrinting Without Format CodesNames Versus Typed StorageDivision and Integer LimitsSuites Instead of BracesRecap: A Python Receipt4Iteration and Transformations
Iteration Without PointersIndexes Without Pointer MathSentinels and Loop ControlComprehensions and SortingRecap: Stable Ranking2Values and Truth Tests
Absence Is Not a Null PointerContent Versus Object IdentityTruth Tests and OperandsConditional Value SelectionRecap: Missing or Eligible3Containers and Aliasing
Lists Versus C ArraysTuples and Struct ValuesMappings and Lookup LoopsPointers, Copies, and AliasesRecap: Inventory Collections6Contracts, Errors, and Modules
Text Input and Exact OutputError Codes Versus ExceptionsImports Are Not IncludesAnnotations Versus C TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler