──────────────────────────────────────

29. Festival Room Booking · Give a Booking a Room

Connect two objects and use a room’s capacity in a booking decision.

Lesson files · lessons/composition/cooperating-objects

Before you start
- Create objects with named attributes.
- Use self to read values for the current object.

By the end
- Use the assigned room’s capacity in Booking.fits().
- Predict the result when a group fills its room exactly and when it exceeds capacity.

Where you can use it
Store the room on each booking so the capacity check uses the room that was actually assigned.

A class can keep an instance of another class in an attribute. A Booking has a Room, so its constructor receives a Room instance and stores it as self.room. This design is called composition. The room owns its capacity. The booking owns the number of people. self.room.capacity reads the capacity from the room attached to this booking.

Predict
If the room holds eight and the group has eight, how many seats are spare?
- 0
- 8

Worked example (example.py)

class Room:
    def __init__(self, name, capacity):
        self.name = name
        self.capacity = capacity


class Booking:
    def __init__(self, room, people):
        self.room = room
        self.people = people

    def spare_seats(self):
        return self.room.capacity - self.people


studio = Room("Studio", 8)
print(Booking(studio, 5).spare_seats())
print(Booking(studio, 8).spare_seats())


Example result
3
0

Complete Booking.fits() so it returns True when the group fits the assigned room. A group that exactly fills the room fits. Test the supplied group size with both the Studio and the Hall.

Starter program (starter.py)

class Room:
    def __init__(self, name, capacity):
        self.name = name
        self.capacity = capacity


class Booking:
    def __init__(self, room, people):
        self.room = room
        self.people = people

    def fits(self):
        return False


people = int(input())
rooms = [Room("Studio", 8), Room("Hall", 20)]
for room in rooms:
    booking = Booking(room, people)
    print(f"{room.name}: {booking.fits()}")


Starter input
8

Hint
Compare self.people with self.room.capacity.

Case 1. Try the sample
Input
8
Expected output
Studio: True
Hall: True

Case 2. Only the larger room fits
Input
9
Expected output
Studio: False
Hall: True

Case 3. Neither room fits
Input
21
Expected output
Studio: False
Hall: False

Stretch
Add a third room and predict the result for the same group.

Reflect
Which object should you update if the Studio gets an additional chair?

Check your prediction
0 The booking subtracts its group size from the room’s capacity.
