diff --git a/car.py b/car.py index f7b980a1b..1e23cacdc 100644 --- a/car.py +++ b/car.py @@ -1,10 +1,92 @@ -from abc import ABC, abstractmethod +from datetime import date -class Car(ABC): - def __init__(self, last_service_date): +# Serviceable Interface +class Serviceable: + def needs_service(self) -> bool: + raise NotImplementedError + + +# Engine Classes +class Engine(Serviceable): + pass + +class CapuletEngine(Engine): + def __init__(self, last_service_mileage: int, current_mileage: int): + self.last_service_mileage = last_service_mileage + self.current_mileage = current_mileage + + def needs_service(self) -> bool: + return self.current_mileage - self.last_service_mileage > 10000 + + +class SternmanEngine(Engine): + def __init__(self, warning_light_on: bool): + self.warning_light_on = warning_light_on + + def needs_service(self) -> bool: + return self.warning_light_on + + +class WilloughbyEngine(Engine): + def __init__(self, last_service_mileage: int, current_mileage: int): + self.last_service_mileage = last_service_mileage + self.current_mileage = current_mileage + + def needs_service(self) -> bool: + return self.current_mileage - self.last_service_mileage > 5000 + + +# Battery Classes +class Battery(Serviceable): + pass + +class SpindlerBattery(Battery): + def __init__(self, last_service_date: date, current_date: date): self.last_service_date = last_service_date + self.current_date = current_date + + def needs_service(self) -> bool: + return (self.current_date - self.last_service_date).days > 365 + + +class NubbinBattery(Battery): + def __init__(self, last_service_date: date, current_date: date): + self.last_service_date = last_service_date + self.current_date = current_date + + def needs_service(self) -> bool: + return (self.current_date - self.last_service_date).days > 730 + + +# Car Class +class Car(Serviceable): + def __init__(self, engine: Engine, battery: Battery): + self.engine = engine + self.battery = battery + + def needs_service(self) -> bool: + return self.engine.needs_service() or self.battery.needs_service() + + +# CarFactory Class +class CarFactory: + @staticmethod + def create_calliope(current_date: date, last_service_date: date, current_mileage: int, last_service_mileage: int) -> Car: + return Car(CapuletEngine(last_service_mileage, current_mileage), SpindlerBattery(last_service_date, current_date)) + + @staticmethod + def create_glissade(current_date: date, last_service_date: date, current_mileage: int, last_service_mileage: int) -> Car: + return Car(WilloughbyEngine(last_service_mileage, current_mileage), NubbinBattery(last_service_date, current_date)) + + @staticmethod + def create_palindrome(current_date: date, last_service_date: date, warning_light_on: bool) -> Car: + return Car(SternmanEngine(warning_light_on), SpindlerBattery(last_service_date, current_date)) + + @staticmethod + def create_rorschach(current_date: date, last_service_date: date, current_mileage: int, last_service_mileage: int) -> Car: + return Car(CapuletEngine(last_service_mileage, current_mileage), NubbinBattery(last_service_date, current_date)) - @abstractmethod - def needs_service(self): - pass + @staticmethod + def create_thovex(current_date: date, last_service_date: date, current_mileage: int, last_service_mileage: int) -> Car: + return Car(WilloughbyEngine(last_service_mileage, current_mileage), SpindlerBattery(last_service_date, current_date))