• Create class passenger, bus and seat. Each Seat object has three attributes, i.e., seat number, availability flag (1 if the seat is available, 0 otherwise), and an occupant (empty string is the seat is still available, otherwise a Passenger object).
• Each Passenger has a name and can purchase a Seat in a Bus. purchase_seat function needs a seat number (not a Seat object) and the Bus object. If that particular seat is available (as indicated by its availability flag), it will be assigned to that passenger.
• Each object of Bus class is composed of 24 Seat objects. Bus object also has a ticket price per seat attribute and a total booking amount attribute. Use a loop in the constructor to create 24 Seat objects. As seats are purchased by passenger, their ticket price is added to total booking amount attribute. Bus has a display function in which each Seat object of the Bus calls display function of Seat class.
• Create a Bus object, and two passenger objects. Both passengers try to buy seat 15
class Seat:
seat_n = 0
free = 1
occupant = ""
def display(self):
if self.free == 1:
print('Seat number %s is available' % (self.seat_n))
else:
print('Seat number %s is unavailable and occupied by %s' % (self.seat_n, self.occupant))
class Bus:
booking = 0
price = 15
seats = [Seat() for x in range(24)]
for x in range(24):
seats[x].seat_n = x+1
def display(self):
for x in range(24):
self.seats[x].display()
class Passenger:
name = ""
def purchase_seat(self, seat_n, bus):
if bus.seats[seat_n].free == 1:
bus.seats[seat_n].free = 0
bus.seats[seat_n].occupant = self.name
bus.booking += bus.price
print('Seat number %s is now occupied by %s' % (seat_n, self.name))
else:
print('Seat number %s is already occupied by %s' % (seat_n, bus.seats[seat_n].occupant))
bus = Bus()
pas1 = Passenger()
pas1.name = "Bob"
pas2 = Passenger()
pas2.name = "Jim"
pas1.purchase_seat(15,bus)
pas1.purchase_seat(15,bus)
bus.display()
print(bus.booking)
Comments
Leave a comment