Python Code Example: Booking System for Conference Rooms

A conference room booking system allows users to reserve rooms, check availability, and cancel bookings. This is a great example of Object-Oriented Programming (OOP), as it involves:

Attributes: Room names, booking status, and reservation details.
Methods: Booking a room, checking availability, canceling a reservation, and displaying booked rooms.
Encapsulation: Ensuring only valid bookings and cancellations are processed.

By structuring the system as a class, we organize data and behavior efficiently, making it scalable and maintainable.


Code Example

class ConferenceRoomBooking:
    """Class representing a conference room booking system."""

    def __init__(self):
        """Initialize available rooms and their booking status."""
        self.rooms = {
            "Room A": None,
            "Room B": None,
            "Room C": None
        }

    def book_room(self, room_name, person_name):
        """Book a room if it's available."""
        if room_name in self.rooms:
            if self.rooms[room_name] is None:
                self.rooms[room_name] = person_name
                print(f"✅ {room_name} successfully booked for {person_name}.")
            else:
                print(f"❌ {room_name} is already booked by {self.rooms[room_name]}.")
        else:
            print("❌ Invalid room name.")

    def cancel_booking(self, room_name, person_name):
        """Cancel a booking if the person matches the reservation."""
        if room_name in self.rooms:
            if self.rooms[room_name] == person_name:
                self.rooms[room_name] = None
                print(f"✅ Booking for {room_name} has been canceled by {person_name}.")
            else:
                print(f"❌ Cannot cancel. Either the room is not booked or booked by someone else.")
        else:
            print("❌ Invalid room name.")

    def check_availability(self):
        """Check available rooms."""
        available_rooms = [room for room, booked_by in self.rooms.items() if booked_by is None]
        if available_rooms:
            print("Available Rooms:", ", ".join(available_rooms))
        else:
            print("❌ No rooms available.")

    def display_bookings(self):
        """Display all booked rooms."""
        print("📅 Current Bookings:")
        for room, booked_by in self.rooms.items():
            if booked_by:
                print(f"{room}: Booked by {booked_by}")
            else:
                print(f"{room}: Available")

# Creating a ConferenceRoomBooking object
booking_system = ConferenceRoomBooking()

# Booking rooms
booking_system.book_room("Room A", "Alice")
booking_system.book_room("Room B", "Bob")
booking_system.book_room("Room A", "Charlie")  # Should fail

# Checking availability
booking_system.check_availability()

# Displaying bookings
booking_system.display_bookings()

# Canceling a booking
booking_system.cancel_booking("Room A", "Alice")

# Checking availability after cancellation
booking_system.check_availability()

Expected Output

✅ Room A successfully booked for Alice.
✅ Room B successfully booked for Bob.
❌ Room A is already booked by Alice.
Available Rooms: Room C
📅 Current Bookings:
Room A: Booked by Alice
Room B: Booked by Bob
Room C: Available
✅ Booking for Room A has been canceled by Alice.
Available Rooms: Room A, Room C

Code Explanation

  1. Defining the ConferenceRoomBooking class
    • Stores room availability in a dictionary (self.rooms).
    • Initially, all rooms are available (None).
  2. Creating book_room() method
    • Checks if the room exists and is available.
    • Assigns the room to the booking person’s name if free.
  3. Creating cancel_booking() method
    • Ensures that only the person who booked can cancel.
  4. Creating check_availability() method
    • Displays rooms that are still available.
  5. Creating display_bookings() method
    • Shows the current reservation status for all rooms.
  6. Booking and canceling rooms
    • Alice and Bob book Room A and B.
    • Charlie tries to book an already occupied room (fails).
    • Checking, displaying, and canceling bookings dynamically.

Why Use OOP for a Booking System?

Encapsulation: Prevents unauthorized booking/canceling.
Code Reusability: Easily extend with features (e.g., time slots).
Scalability: Expand to more rooms or user authentication.