Your task is to implement a flight booking system with the following capabilities:
- Create Flights with capacities and destinations.
- Register Passengers with unique identifiers.
- Allow Bookings linking passengers to flights.
- Manage the system via a BookingSystem class.
- Handle errors and edge cases, like full flights or duplicate bookings.
You will write code to pass the provided unittest tests, giving hands-on experience with Python OOP.
- Represents a single flight.
- Key attributes:
flight_number,origin,destination,capacity,booked_passengers. - Key methods:
add_booking(passenger)β Adds a passenger; raisesFlightFullErrorif full.available_seats(property) β Returns seats left.__eq__β Compares flights byflight_number.__str__β Human-readable summary including flight number and seats left.
- Concepts learned: Encapsulation, dunder methods, exception handling, list management.
- Represents a traveler.
- Key attributes:
name,passport_number,bookings. - Key methods:
add_booking(flight)β Adds a flight to passengerβs bookings.cancel_booking(flight)β Removes a flight from bookings.total_bookings(property) β Returns number of bookings.__eq__β Compares passengers bypassport_number.__repr__β Debug-friendly string with name and passport.
- Concepts learned: Properties, dunder methods, composition.
- Connects a
Passengerto aFlight. - Key attributes:
passenger,flight,status. - Key methods:
confirm()β Links passenger and flight; sets status toConfirmed.cancel()β Unlinks passenger and flight; sets status toCancelled.__str__β Displays passenger, flight, and status.
- Concepts learned: Composition, state management, dunder methods.
- Manages all flights, passengers, and bookings.
- Key methods:
add_flight(...)β Adds a flight.register_passenger(...)β Registers a new passenger.make_booking(flight_number, passport_number)β Creates and confirms a booking.cancel_booking(flight_number, passport_number)β Cancels a booking.get_passenger_manifest(flight_number)β Returns list of passengers on a flight.- Iteration and
len()support over active bookings.
- Concepts learned: Aggregation, iteration, len(), system-level composition, exception handling.
- Contains custom exceptions like
FlightFullError. - Helps students learn how to define and use custom exceptions.
All functionality is verified via unit tests:
test_flight.pyβ Tests flight creation, bookings, dunder methods, and capacity.test_passenger.pyβ Tests passenger creation, booking management, and equality.test_booking.pyβ Tests booking confirmation, cancellation, and linkage.test_booking_system.pyβ Tests system-level operations, iteration, len, and manifests.
Purpose: Students write code to pass these tests, learning TDD and OOP in action.
- Classes and Objects β Creating
Flight,Passenger,Booking,BookingSystem. - Composition/Aggregation β Linking passengers to flights via bookings.
- Dunder Methods β
__eq__,__str__,__repr__. - Properties β
Passenger.total_bookings,Flight.available_seats. - Exception Handling β Custom exceptions like
FlightFullError. - Iteration and len() β Over active bookings in
BookingSystem. - TDD Workflow β Implement code to pass tests.
To start
python -m unittest discover -s tests
x0 O/