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

code refactored #132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
File renamed without changes.
8 changes: 8 additions & 0 deletions battery/base_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from abc import ABC, abstractmethod
from car import Serviceable


class Battery(Serviceable, ABC):
@abstractmethod
def needs_service(self) -> bool:
pass
10 changes: 10 additions & 0 deletions battery/nubbin_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .base_battery import Battery


class NubbinBattery(Battery):
def __init__(self, last_service_date, current_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 >= 1460
10 changes: 10 additions & 0 deletions battery/spindler_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .base_battery import Battery


class SpindlerBattery(Battery):
def __init__(self, last_service_date, current_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 # 2 years
16 changes: 11 additions & 5 deletions car.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from abc import ABC, abstractmethod


class Car(ABC):
def __init__(self, last_service_date):
self.last_service_date = last_service_date

class Serviceable(ABC):
@abstractmethod
def needs_service(self):
def needs_service(self) -> bool:
pass


class Car(Serviceable):
def __init__(self, engine, battery):
self.engine = engine
self.battery = battery

def needs_service(self) -> bool:
return self.engine.needs_service() or self.battery.needs_service()
46 changes: 46 additions & 0 deletions car_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from car import Car
from battery.nubbin_battery import NubbinBattery
from battery.spindler_battery import SpindlerBattery
from engine.capulet_engine import CapuletEngine
from engine.sternman_engine import SternmanEngine
from engine.willoughby_engine import WilloughbyEngine


class CarFactory:
@staticmethod
def create_calliope(
current_date, last_service_date, current_mileage, last_service_mileage
):
engine = CapuletEngine(last_service_mileage, current_mileage)
battery = SpindlerBattery(last_service_date, current_date)
return Car(engine, battery)

@staticmethod
def create_glissade(
current_date, last_service_date, current_mileage, last_service_mileage
):
engine = WilloughbyEngine(last_service_mileage, current_mileage)
battery = SpindlerBattery(last_service_date, current_date)
return Car(engine, battery)

@staticmethod
def create_palindrome(current_date, last_service_date, warning_light_on):
engine = SternmanEngine(warning_light_on)
battery = SpindlerBattery(last_service_date, current_date)
return Car(engine, battery)

@staticmethod
def create_rorschach(
current_date, last_service_date, current_mileage, last_service_mileage
):
engine = WilloughbyEngine(last_service_mileage, current_mileage)
battery = NubbinBattery(last_service_date, current_date)
return Car(engine, battery)

@staticmethod
def create_thovex(
current_date, last_service_date, current_mileage, last_service_mileage
):
engine = CapuletEngine(last_service_mileage, current_mileage)
battery = NubbinBattery(last_service_date, current_date)
return Car(engine, battery)
9 changes: 9 additions & 0 deletions engine/base_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from abc import ABC, abstractmethod

from car import Serviceable


class Engine(Serviceable, ABC):
@abstractmethod
def needs_service(self) -> bool:
pass
15 changes: 6 additions & 9 deletions engine/capulet_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from abc import ABC
from .base_engine import Engine

from car import Car


class CapuletEngine(Car, ABC):
def __init__(self, last_service_date, current_mileage, last_service_mileage):
super().__init__(last_service_date)
self.current_mileage = current_mileage
class CapuletEngine(Engine):
def __init__(self, last_service_mileage, current_mileage):
self.last_service_mileage = last_service_mileage
self.current_mileage = current_mileage

def engine_should_be_serviced(self):
return self.current_mileage - self.last_service_mileage > 30000
def needs_service(self) -> bool:
return self.current_mileage - self.last_service_mileage >= 30000
12 changes: 0 additions & 12 deletions engine/model/calliope.py

This file was deleted.

12 changes: 0 additions & 12 deletions engine/model/glissade.py

This file was deleted.

12 changes: 0 additions & 12 deletions engine/model/palindrome.py

This file was deleted.

12 changes: 0 additions & 12 deletions engine/model/rorschach.py

This file was deleted.

12 changes: 0 additions & 12 deletions engine/model/thovex.py

This file was deleted.

18 changes: 6 additions & 12 deletions engine/sternman_engine.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from abc import ABC
from .base_engine import Engine

from car import Car

class SternmanEngine(Engine):
def __init__(self, warning_light_on):
self.warning_light_on = warning_light_on

class SternmanEngine(Car, ABC):
def __init__(self, last_service_date, warning_light_is_on):
super().__init__(last_service_date)
self.warning_light_is_on = warning_light_is_on

def engine_should_be_serviced(self):
if self.warning_light_is_on:
return True
else:
return False
def needs_service(self) -> bool:
return self.warning_light_on
15 changes: 6 additions & 9 deletions engine/willoughby_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from abc import ABC
from .base_engine import Engine

from car import Car


class WilloughbyEngine(Car, ABC):
def __init__(self, last_service_date, current_mileage, last_service_mileage):
super().__init__(last_service_date)
self.current_mileage = current_mileage
class WilloughbyEngine(Engine):
def __init__(self, last_service_mileage, current_mileage):
self.last_service_mileage = last_service_mileage
self.current_mileage = current_mileage

def engine_should_be_serviced(self):
return self.current_mileage - self.last_service_mileage > 60000
def needs_service(self) -> bool:
return self.current_mileage - self.last_service_mileage >= 60000