Untitled
unknown
plain_text
2 years ago
4.4 kB
9
Indexable
Never
# -*- coding: utf-8 -*- """ Created on Wed May 10 12:40:37 2023 @author: Busra """ class Passenger: airportTax = 0.03 fareLimit = 1000 fuelSurcharge = 150 def __init__( self, pname, psurname, seatNo, fare ): self.__passengerName = pname self.__passengerSurname = psurname self.setSeatNumber( seatNo ) self.__fare = fare def getPassengerName( self ): return self.__passengerName def getPassengerSurname( self ): return self.__passengerSurname def getSeatNumber( self ): return self.__seatNumber def getFare( self ): return self.__fare def __repr__( self ): rep = f'{self.__passengerSurname}, {self.__passengerName[0]}. ' rep += f'({self.__seatNumber}) Fare: {self.calculate_fare():.2f}TL\n' return rep class Passengers(object): def __init__(self,passengerName, passengerSurname, fare, seatNumber): self.__passengerName = passengerName self.__passengerSurname = passengerSurname self.__seatnumber = seatnumber self.__fare = fare def __repr__(self): return f"{self.__passengerName}, {self.__passengerSurname}, {self.__seatnumber}, {self.__fare}" class FirstClassPassenger(Passenger): def __init__(self, pname, psurname, seatNo, fare, mealPreference): super().__init__(pname, psurname, seatNo, fare) self.__mealPreference = mealPreference def getMealPreference(self): return self.__mealPreference def setMealPreference(self, mealPreference): self.__mealPreference = mealPreference def __repr__(self): return super().__repr__() + f'Meal Preference: {self.__mealPreference}\n' def calculate_fare(self): if self.getFare() > Passenger.fareLimit: return self.getFare() * (1 + Passenger.airportTax) else: return self.getFare() class EconomyPassenger(Passenger): def __init__(self, pname, psurname, seatNo, fare): super().__init__(pname, psurname, seatNo, fare) def calculate_fare(self): return self.getFare() + Passenger.fuelSurcharge class Passengers: def __eq__(self, other): return self.__seatNumber == other.__seatNumber and self.__passengerSurname == other.__passengerSurname def __lt__(self, other): return self.__seatNumber < other.__seatNumber def setSeatNumber(self, seatNo): if len(seatNo) != 3: self.__seatNumber = "unassigned" elif isinstance(self, FirstClassPassenger): row = int(seatNo[:-1]) seat_char = seatNo[-1].upper() if 1 <= row <= 10 and seat_char.isalpha(): self.__seatNumber = f"{row:02}{seat_char}" else: self.__seatNumber = "priority assignment" elif isinstance(self, EconomyPassenger): row = int(seatNo[:-1]) seat_char = seatNo[-1].upper() if row > 10 and seat_char.isalpha(): self.__seatNumber = f"{row:02}{seat_char}" else: self.__seatNumber = "unassigned" else: self.__seatNumber = "unassigned" def main(): passengers = [] with open("passengers.txt", "r") as file: for line in file: data = line.strip().split(", ") if len(data) not in [4, 5]: print(f"Invalid line: {line}") continue if len(data) == 5: p = FirstClassPassenger(data[0], data[1], data[2], float(data[3]), data[4]) else: p = EconomyPassenger(data[0], data[1], data[2], float(data[3])) if p not in passengers: passengers.append(p) else: print(f"duplicate - passenger {p.getPassengerName()} {p.getPassengerSurname()} {p.getSeatNumber()} not added...") print("Original list of Passengers:") for p in passengers: print(p) avg_fare = sum([p.calculate_fare() for p in passengers]) / len(passengers) print(f"Average calculated fare: {avg_fare:.2f}") passengers.sort() print("Assigned Seat Numbers:", " ".join([p.getSeatNumber() for p in passengers if p.getSeatNumber() not in ["unassigned", "priority assignment"]])) if __name__ == "__main__": main()