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 Refactor #135

Open
wants to merge 3 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.
13 changes: 13 additions & 0 deletions battery/battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" Base Battery class """

from servicable import Servicable

class Battery(Servicable):
def __init__(self, service_criteria):
self.service_criteria = service_criteria

def needs_service(self, years):
return years >= self.service_criteria
9 changes: 9 additions & 0 deletions battery/nubbin_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" nubbin battery module"""
from .battery import Battery

class NubbinBattery(Battery):
def __init__(self):
super().__init__(service_criteria=4)
9 changes: 9 additions & 0 deletions battery/spindler_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" spindler battery module"""
from .battery import Battery

class SpindlerBattery(Battery):
def __init__(self):
super().__init__(service_criteria=3)
17 changes: 10 additions & 7 deletions car.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from abc import ABC, abstractmethod
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from servicable import Servicable

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

@abstractmethod
def needs_service(self):
pass
class Car(Servicable):
def __init__(self, engine, battery, tire):
self.engine = engine
self.battery = battery

def needs_service(self, mileage, years) -> bool:
return self.engine.needs_service(mileage) or self.battery.needs_service(years)
51 changes: 51 additions & 0 deletions car_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from car import Car
from engine.capulet_engine import CapuletEngine
from engine.sternman_engine import SternmanEngine
from engine.willoughby_engine import WilloughbyEngine
from battery.nubbin_battery import NubbinBattery
from battery.spindler_battery import SpindlerBattery
from tires.carrigan import Carrigan
from tires.octoprime import OctoPrime

class CarFactory:
def create_calliope(tire):
return Calliope(
engine=CapuletEngine(),
battery=SpindlerBattery(),
tire=tire
)

@staticmethod
def create_glissade(tire):
return Glissade(
engine=SternmanEngine(),
battery=SpindlerBattery(),
tire=tire
)

@staticmethod
def create_palindrome(tire):
return Palindrome(
engine=SternmanEngine(),
battery=SpindlerBattery(),
tire=tire
)

@staticmethod
def create_rorschach(tire):
return Rorschach(
engine=CapuletEngine(),
battery=NubbinBattery(),
tire=tire
)

@staticmethod
def create_thovex(tire):
return Thovex(
engine=CapuletEngine(),
battery=NubbinBattery(),
tire=tire
)
16 changes: 4 additions & 12 deletions engine/capulet_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
from abc import ABC
from .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
self.last_service_mileage = last_service_mileage

def engine_should_be_serviced(self):
return self.current_mileage - self.last_service_mileage > 30000
class CapuletEngine(Engine):
def __init__(self):
super().__init__(service_criteria=30000)
13 changes: 13 additions & 0 deletions engine/engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Base engine class."""

from servicable import Servicable

class Engine(Servicable):
def __init__(self, service_criteria):
self.service_criteria = service_criteria

def needs_service(self, mileage):
return mileage >= self.service_criteria
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: 4 additions & 14 deletions engine/sternman_engine.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
from abc import ABC
from .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)
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
class SternmanEngine(Engine):
def __init__(self):
super().__init__(service_criteria=float('inf'))
16 changes: 4 additions & 12 deletions engine/willoughby_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
from abc import ABC
from .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
self.last_service_mileage = last_service_mileage

def engine_should_be_serviced(self):
return self.current_mileage - self.last_service_mileage > 60000
class WilloughbyEngine(Engine):
def __init__(self):
super().__init__(service_criteria=60000)
27 changes: 27 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# main.py
from car.calliope import Calliope
from car.glissade import Glissade
from car.palindrome import Palindrome
from car.rorschach import Rorschach
from car.thovex import Thovex

def main():
# Example usage
calliope_car = Calliope()
glissade_car = Glissade()
palindrome_car = Palindrome()
rorschach_car = Rorschach()
thovex_car = Thovex()

# Check if cars need service
for car in [calliope_car, glissade_car, palindrome_car, rorschach_car, thovex_car]:
if car.needs_service(mileage=2000, years=2):
print(f"The {car.__class__.__name__} car needs service.")
else:
print(f"The {car.__class__.__name__} car is in good condition.")

if __name__ == "__main__":
main()
9 changes: 9 additions & 0 deletions servicable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" Servicable interface """


class Servicable:
def needs_service(self) -> bool:
pass
Empty file added test/test_battery/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions test/test_battery/test_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import unittest
from battery.nubbin_battery import NubbinBattery
from battery.spindler_battery import SpindlerBattery

class TestBattery(unittest.TestCase):
def test_nubbin_battery_needs_service(self):
nubbin_battery = NubbinBattery()
self.assertFalse(nubbin_battery.needs_service(2))
self.assertTrue(nubbin_battery.needs_service(5))

def test_spindler_battery_needs_service(self):
spindler_battery = SpindlerBattery()
self.assertFalse(spindler_battery.needs_service(1))
self.assertTrue(spindler_battery.needs_service(3))

if __name__ == '__main__':
unittest.main()
Loading