Write a Python class AirlineBooking
that allows booking, canceling, and checking availability of flights. The class should have methods to book a flight, cancel a booking, and display available seats.
Example 1:
Input: book_seat(5), available_seats()
Output: 45
Example 2:
Input: book_seat(5), cancel_seat(3), available_seats()
Output: 48
Maintain a variable for total seats and a variable for booked seats. Provide methods to book, cancel, and check available seats.
class AirlineBooking: def __init__(self, total_seats=50): self.total_seats = total_seats self.booked_seats = 0 def book_seat(self, seats): self.booked_seats += seats def cancel_seat(self, seats): self.booked_seats -= seats def available_seats(self): return self.total_seats - self.booked_seats # Example usage booking = AirlineBooking() booking.book_seat(5) print(booking.available_seats()) # Output: 45 booking.cancel_seat(3) print(booking.available_seats()) # Output: 48
Unlock AI & Data Science treasures. Log in!