Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Classes for Car Service System #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 88 additions & 6 deletions car.py
Original file line number Diff line number Diff line change
@@ -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))