Hi I just finished a VERY Basic airline reservation system. Wanted some feedback please let me know what you all think, if I am obfuscating information or passing parameters where I shouldn't or if things don't make sense
Airline Class: handles booking and refunds
Passenger: Has a seat assignment, and a balance to pay for ticket
Seat:
Firstclass and Coach
Plane:
a dictionary object holding seats and passengers get passed as values to those seats
here is my code:
class Airline:
def __init__(self, name=None):
self._name = name
self._booked = []
def get_name(self):
return self._name
def set_name(self, name):
self._name = name
def book(self, passenger, plane, cls=None):
while cls not in ['first class', 'coach']:
cls = input("Please pick a seat: First class or Coach ").lower()
if cls not in ['first class', 'coach']:
print("Please select either from 'first class' or 'coach'")
pass
if cls == 'first class':
first_class = ([(number, seat) for number, seat in enumerate(plane.capacity)][0:10])
choice = None
while choice not in range(10):
try:
choice = int(input(f"Please select a number between 0 and 9 for your seats: "))
except ValueError:
print("Please select a valid number between 0 and 9")
if choice in self._booked:
print(f"That seat is taken please choose another seat\n"
f"These seats are booked: {self._booked}")
choice = None
for seat in first_class:
if seat[0] == choice:
plane.capacity[seat[1]] = passenger
passenger._balance = passenger._balance - seat[1].price
self._booked.append(seat[0])
passenger._assignment = seat[1].tier + f" seat {seat[0]}"
else:
coach = ([(number, seat) for number, seat in enumerate(plane.capacity)][10:50])
choice = None
while choice not in range(10, 50):
try:
choice = int(input(f"Please select a number between 10 and 50 for your seats: "))
except ValueError:
print("Please select a valid number between 10 and 50")
if choice in self._booked:
print(f"That seat is taken please choose another seat\n"
f"These seats are booked: {self._booked}")
choice = None
for seat in coach:
if seat[0] == choice:
plane.capacity[seat[1]] = passenger
passenger._balance = passenger._balance - seat[1].price
self._booked.append(seat[0])
passenger._assignment = seat[1].tier + f" seat {seat[0]}"
def refund(self, passenger, plane):
for i, (seat, person) in enumerate(plane.capacity.items()):
if person == passenger:
plane.capacity[seat] = None
passenger._balance = passenger._balance + seat.price
passenger._assignment = None
self._booked.remove(i)
class Passenger:
def __init__(self, balance=1000, assignment=None):
self._balance = balance
self._assignment = assignment
def get_balance(self):
return self._balance
def get_assignment(self):
return self._assignment
class Seat:
def __init__(self):
pass
class FirstClass(Seat):
def __init__(self):
super().__init__()
self.tier = 'First Class'
self.price = 500
class Coach(Seat):
def __init__(self):
super().__init__()
self.tier = 'Coach'
self.price = 100
class Plane:
def __init__(self):
self.capacity = {}
temp_capacity = [] # Create a temporary list to append seats into ( this will be the seats in the airplane)
for i in range(10): # first 10 seats are first class
temp_capacity.append(FirstClass())
for i in range(10, 50): # last 40 seats are coach class
temp_capacity.append(Coach())
for seat in temp_capacity:
self.capacity[seat] = None # Each seat has no value(person) assigned
def view_plane(self):
for i, k in self.capacity.items():
print(f"{i} : {k}")
def get_available_seats(self):
count = 0
for value in self.capacity.values():
if value is None:
count += 1
return count
Running this below: (Will output how I envisioned the plane to be built and how seats are assigned to passengers, etc.)
plane = Plane()
p = Passenger()
p2 = Passenger()
p3 = Passenger()
airline = Airline()
plane.view_plane()
airline.book(p, plane)
airline.book(p2, plane)
print(airline._booked)
print(f"passenger 1 balance: {p.get_balance()}\n"
f"passenger 1 assignment: {p.get_assignment()}\n"
f"passenger 2 balance: {p2.get_balance()}\n"
f"passenger 2 assignment: {p2.get_assignment()}\n"
f"Number of seats available: {plane.get_available_seats()}\n"
f"Number of seats booked: {len(airline._booked)}")
plane.view_plane()
airline.book(p3, plane)
plane.view_plane()
print("--------------")
print(airline._booked)
print(f"passenger 1 balance: {p.get_balance()}\n"
f"passenger 1 assignment: {p.get_assignment()}\n"
f"passenger 2 balance: {p2.get_balance()}\n"
f"passenger 2 assignment: {p2.get_assignment()}\n"
f"passenger 3 balance: {p3.get_balance()}\n"
f"passenger 3 assignment: {p3.get_assignment()}\n"
f"Number of seats available: {plane.get_available_seats()}\n"
f"Number of seats booked: {len(airline._booked)}")
print("----------------")
airline.refund(p2, plane)
print(airline._booked)
print(f"passenger 1 balance: {p.get_balance()}\n"
f"passenger 1 assignment: {p.get_assignment()}\n"
f"passenger 2 balance: {p2.get_balance()}\n"
f"passenger 2 assignment: {p2.get_assignment()}\n"
f"passenger 3 balance: {p3.get_balance()}\n"
f"passenger 3 assignment: {p3.get_assignment()}\n"
f"Number of seats available: {plane.get_available_seats()}\n"
f"Number of seats booked: {len(airline._booked)}")
Please let me know better ways to do this or better interfaces etc. I will be slowly adding more complexity but my main goal was having the airline be able to book and place passengers in the plane and refund them from their seat while updating the planes capacity during these changes.
Will be adding multiple planes per airline which will undoubtedly change the structure of some of the classes but for right now let me know if what I have allows for the basic booking and refunding operations to work efficiently and if the classes are setup properly or what improvements can be made.
I feel you should have moved the First class and Coach booking outside of book method, as this method is doing lot of work, and difficult to understand as code is lengthy.
Seems code is getting repeated in First class and coach booking, instead of that you can think to pass the argument to selected class method and then perform booking by this way repeated code will be removed from the method.
Same for the method where yours viewing the information of of booking, there is so much repetition of the code.
Whenever you are writing code make sure repetition does not happen there, this is consider as a bad smell in a code.