Menu
CoddyTech

Design a Booking Desk

Lesson 48 of 48 in Coddy's Python for Java Developers course.

challenge icon

Challenge

Beginner

Implement 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. Java interface-based delegation becomes Python behavioral delegation: the locked driver replaces desk.room with a probe and checks calls and state preservation. All data stays in memory.

Editable files: room.py, booking_desk.py, main.py. Keep the provided imports and the required API names. The locked driver.py supplies execution and checks; do not edit it.

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.

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 Java Developers

Practice on your own: Online Python compiler