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

Refactoring Code #133

Open
wants to merge 5 commits 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.
7 changes: 7 additions & 0 deletions battery/Battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from abc import ABC, abstractmethod


class Battery(ABC):
@abstractmethod
def needs_service():
pass
23 changes: 23 additions & 0 deletions battery/NubbinBattery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from battery.Battery import Battery
from datetime import datetime

class NubbinBattery(Battery):
def __init__(self, last_service_date):
self.last_service_date = last_service_date
self.current_data = datetime.today().date()

def needs_service(self):
service_threshold_date = self.last_service_date.replace(
year=self.last_service_date.year + 4)
if service_threshold_date < datetime.today().date():
return True
else:
return False


if __name__ == "__main__":
today = datetime.today().date()
last_service_date = today.replace(year=today.year - 2)
print(last_service_date)
nb = NubbinBattery(last_service_date)
print(nb.needs_service())
24 changes: 24 additions & 0 deletions battery/SpindlerBattery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from battery.Battery import Battery
from datetime import datetime


class SpindlerBattery(Battery):
def __init__(self, last_service_date):
self.last_service_date = last_service_date
self.current_data = datetime.today().date()

def needs_service(self):
service_threshold_date = self.last_service_date.replace(
year=self.last_service_date.year + 3)
if service_threshold_date < datetime.today().date():
return True
else:
return False


if __name__ == "__main__":
today = datetime.today().date()
last_service_date = today.replace(year=today.year - 5)
print(last_service_date)
nb = SpindlerBattery(last_service_date)
print(nb.needs_service())
File renamed without changes.
18 changes: 14 additions & 4 deletions car.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
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):
pass


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

def needs_service(self):
if self.engine.needs_service() or self.battery.needs_service() or self.tire.needs_service():
return True
else:
return False
45 changes: 45 additions & 0 deletions carFactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from datetime import datetime

from battery.NubbinBattery import NubbinBattery
from battery.SpindlerBattery import SpindlerBattery

from engine.capulet_engine import CapuletEngine
from engine.sternman_engine import SternmanEngine
from engine.willoughby_engine import WilloughbyEngine

from car import Car


class CarFactory():

def create_calliope(self, last_service_date, current_mileage, last_service_mileage):
ce = CapuletEngine(last_service_mileage, current_mileage)
sb = SpindlerBattery(last_service_date)
self.car = Car(ce, sb)

def create_glissade(self, last_service_date, current_mileage, last_service_mileage):
we = WilloughbyEngine(last_service_mileage, current_mileage)
sb = SpindlerBattery(last_service_date)
self.car = Car(we, sb)

def create_palindrome(self, last_service_date, warning_light_is_on):
se = SternmanEngine(warning_light_is_on)
sb = SpindlerBattery(last_service_date)
self.car = Car(se, sb)

def create_rorschach(self, last_service_date, current_mileage, last_service_mileage):
we = WilloughbyEngine(last_service_mileage, current_mileage)
nb = NubbinBattery(last_service_date)
self.car = Car(we, nb)

def create_thovex(self, last_service_date, current_mileage, last_service_mileage):
we = CapuletEngine(last_service_mileage, current_mileage)
nb = NubbinBattery(last_service_date)
self.car = Car(we, nb)

if __name__ == "__main__":

today = datetime.today().date()
cf = CarFactory()
cf.create_calliope(today,30001,0)
print(cf.car.needs_service())
7 changes: 7 additions & 0 deletions engine/Engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from abc import ABC, abstractmethod


class Engine(ABC):
@abstractmethod
def needs_service():
pass
18 changes: 10 additions & 8 deletions engine/capulet_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from abc import ABC
from engine.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):
def needs_service(self):
return self.current_mileage - self.last_service_mileage > 30000


if __name__ == "__main__":
ce = CapuletEngine(0, 30001)
print(ce.needs_service())
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.

15 changes: 8 additions & 7 deletions engine/sternman_engine.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from abc import ABC
from engine.Engine import Engine

from car import Car


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

def engine_should_be_serviced(self):
def needs_service(self):
if self.warning_light_is_on:
return True
else:
return False

if __name__ == "__main__":
se = SternmanEngine(True)
print(se.needs_service())
18 changes: 10 additions & 8 deletions engine/willoughby_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from abc import ABC
from engine.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):
def needs_service(self):
return self.current_mileage - self.last_service_mileage > 60000


if __name__ == "__main__":
ce = WilloughbyEngine(0, 30001)
print(ce.needs_service())
Loading