diff --git a/battery/__init__.py b/battery/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/battery/battery.py b/battery/battery.py new file mode 100644 index 000000000..19509ef52 --- /dev/null +++ b/battery/battery.py @@ -0,0 +1,4 @@ +from abc import ABC +class Battery(ABC): + def needs_service(self): + pass \ No newline at end of file diff --git a/battery/nubbin_battery.py b/battery/nubbin_battery.py new file mode 100644 index 000000000..f09e8c1ee --- /dev/null +++ b/battery/nubbin_battery.py @@ -0,0 +1,15 @@ +from battery.battery import Battery +from test.util import add_years_to_date + + +class NubbinBattery(Battery): + def __init__(self, current_date, last_service_date): + self.current_date = current_date + self.last_service_date = last_service_date + + def needs_service(self): + date_which_battery_should_be_serviced_by = add_years_to_date(self.last_service_date, 4) + if date_which_battery_should_be_serviced_by < self.current_date: + return True + else: + return False \ No newline at end of file diff --git a/battery/spindler_battery.py b/battery/spindler_battery.py new file mode 100644 index 000000000..39e60655e --- /dev/null +++ b/battery/spindler_battery.py @@ -0,0 +1,15 @@ +from battery.battery import Battery +from test.util import add_years_to_date + + +class SpindlerBattery(Battery): + def __init__(self, current_date, last_service_date): + self.current_date = current_date + self.last_service_date = last_service_date + + def needs_service(self): + date_which_battery_should_be_serviced_by = add_years_to_date(self.last_service_date, 3) + if date_which_battery_should_be_serviced_by < self.current_date: + return True + else: + return False \ No newline at end of file diff --git a/car.py b/car.py deleted file mode 100644 index f7b980a1b..000000000 --- a/car.py +++ /dev/null @@ -1,10 +0,0 @@ -from abc import ABC, abstractmethod - - -class Car(ABC): - def __init__(self, last_service_date): - self.last_service_date = last_service_date - - @abstractmethod - def needs_service(self): - pass diff --git a/engine/capulet_engine.py b/engine/capulet_engine.py index 69a2f3319..2fc043d17 100644 --- a/engine/capulet_engine.py +++ b/engine/capulet_engine.py @@ -1,13 +1,10 @@ -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) +class CapuletEngine(Engine): + def __init__(self, current_mileage, last_service_mileage): 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 + def needs_service(self): + return self.current_mileage - self.last_service_mileage > 30000 \ No newline at end of file diff --git a/engine/engine.py b/engine/engine.py new file mode 100644 index 000000000..f16673d09 --- /dev/null +++ b/engine/engine.py @@ -0,0 +1,5 @@ +from abc import ABC + +class Engine(ABC): + def needs_service(self): + pass \ No newline at end of file diff --git a/engine/sternman_engine.py b/engine/sternman_engine.py index 72d8b5ab3..e002c9274 100644 --- a/engine/sternman_engine.py +++ b/engine/sternman_engine.py @@ -1,15 +1,12 @@ -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 + return False \ No newline at end of file diff --git a/engine/willoughby_engine.py b/engine/willoughby_engine.py index e5e0dc581..adfe901ae 100644 --- a/engine/willoughby_engine.py +++ b/engine/willoughby_engine.py @@ -1,13 +1,10 @@ -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) +class WilloughbyEngine(Engine): + def __init__(self, current_mileage, last_service_mileage): 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 + def needs_service(self): + return self.current_mileage - self.last_service_mileage > 60000 \ No newline at end of file diff --git a/test/car.py b/test/car.py new file mode 100644 index 000000000..05cccb4da --- /dev/null +++ b/test/car.py @@ -0,0 +1,10 @@ +from serviceable import Serviceable + + +class Car(Serviceable): + def __init__(self, engine, battery): + self.engine = engine + self.battery = battery + + def needs_service(self): + return self.engine.needs_service() or self.battery.needs_service() \ No newline at end of file diff --git a/test/car_factory.py b/test/car_factory.py new file mode 100644 index 000000000..2b452319a --- /dev/null +++ b/test/car_factory.py @@ -0,0 +1,38 @@ +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 +from test.car import Car + + +class CarFactory: + def create_calliope(current_date, last_service_date, current_mileage, last_service_mileage): + engine = CapuletEngine(current_mileage, last_service_mileage) + battery = SpindlerBattery(current_date, last_service_date) + car = Car(engine, battery) + return car + + def create_palindrome(current_date, last_service_date, current_mileage, last_service_mileage): + engine = SternmanEngine(current_mileage, last_service_mileage) + battery = SpindlerBattery(current_date, last_service_date) + car = Car(engine, battery) + return car + + def create_glissade(current_date, last_service_date, current_mileage, last_service_mileage): + engine = WilloughbyEngine(current_mileage, last_service_mileage) + battery = SpindlerBattery(current_date, last_service_date) + car = Car(engine, battery) + return car + + def create_rorschach(current_date, last_service_date, current_mileage, last_service_mileage): + engine = WilloughbyEngine(current_mileage, last_service_mileage) + battery = NubbinBattery(current_date, last_service_date) + car = Car(engine, battery) + return car + + def create_thovex(current_date, last_service_date, current_mileage, last_service_mileage): + engine = CapuletEngine(current_mileage, last_service_mileage) + battery = NubbinBattery(current_date, last_service_date) + car = Car(engine, battery) + return car \ No newline at end of file diff --git a/test/serviceable.py b/test/serviceable.py new file mode 100644 index 000000000..0ba86de2e --- /dev/null +++ b/test/serviceable.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class Serviceable(ABC): + @abstractmethod + def needs_service(self): + pass \ No newline at end of file diff --git a/test/test_batteries.py b/test/test_batteries.py new file mode 100644 index 000000000..00fb08167 --- /dev/null +++ b/test/test_batteries.py @@ -0,0 +1,31 @@ +import unittest +from datetime import date + +from battery.nubbin_battery import NubbinBattery +from battery.spindler_battery import SpindlerBattery + +class TestNubbinBattery(unittest.TestCase): + def test_needs_service(self): + current_date = date.fromisoformat("2023-11-04") + last_service_date = date.fromisoformat("2018-11-04") + battery = NubbinBattery(current_date, last_service_date) + self.assertTrue(battery.needs_service()) + + def test_does_not_needs_service(self): + current_date = date.fromisoformat("2023-11-04") + last_service_date = date.fromisoformat("2022-11-04") + battery = NubbinBattery(current_date, last_service_date) + self.assertFalse(battery.needs_service()) + +class TestSpindlerBattery(unittest.TestCase): + def test_needs_service(self): + current_date = date.fromisoformat("2023-11-04") + last_service_date = date.fromisoformat("2015-11-04") + battery = SpindlerBattery(current_date, last_service_date) + self.assertTrue(battery.needs_service()) + + def test_does_not_needs_service(self): + current_date = date.fromisoformat("2023-11-04") + last_service_date = date.fromisoformat("2022-11-04") + battery = SpindlerBattery(current_date, last_service_date) + self.assertFalse(battery.needs_service()) \ No newline at end of file diff --git a/test/test_car.py b/test/test_car.py deleted file mode 100644 index f5994670d..000000000 --- a/test/test_car.py +++ /dev/null @@ -1,188 +0,0 @@ -import unittest -from datetime import datetime - -from engine.model.calliope import Calliope -from engine.model.glissade import Glissade -from engine.model.palindrome import Palindrome -from engine.model.rorschach import Rorschach -from engine.model.thovex import Thovex - - -class TestCalliope(unittest.TestCase): - def test_battery_should_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 3) - current_mileage = 0 - last_service_mileage = 0 - - car = Calliope(last_service_date, current_mileage, last_service_mileage) - self.assertTrue(car.needs_service()) - - def test_battery_should_not_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 1) - current_mileage = 0 - last_service_mileage = 0 - - car = Calliope(last_service_date, current_mileage, last_service_mileage) - self.assertFalse(car.needs_service()) - - def test_engine_should_be_serviced(self): - last_service_date = datetime.today().date() - current_mileage = 30001 - last_service_mileage = 0 - - car = Calliope(last_service_date, current_mileage, last_service_mileage) - self.assertTrue(car.needs_service()) - - def test_engine_should_not_be_serviced(self): - last_service_date = datetime.today().date() - current_mileage = 30000 - last_service_mileage = 0 - - car = Calliope(last_service_date, current_mileage, last_service_mileage) - self.assertFalse(car.needs_service()) - - -class TestGlissade(unittest.TestCase): - def test_battery_should_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 3) - current_mileage = 0 - last_service_mileage = 0 - - car = Glissade(last_service_date, current_mileage, last_service_mileage) - self.assertTrue(car.needs_service()) - - def test_battery_should_not_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 1) - current_mileage = 0 - last_service_mileage = 0 - - car = Glissade(last_service_date, current_mileage, last_service_mileage) - self.assertFalse(car.needs_service()) - - def test_engine_should_be_serviced(self): - last_service_date = datetime.today().date() - current_mileage = 60001 - last_service_mileage = 0 - - car = Glissade(last_service_date, current_mileage, last_service_mileage) - self.assertTrue(car.needs_service()) - - def test_engine_should_not_be_serviced(self): - last_service_date = datetime.today().date() - current_mileage = 60000 - last_service_mileage = 0 - - car = Glissade(last_service_date, current_mileage, last_service_mileage) - self.assertFalse(car.needs_service()) - - -class TestPalindrome(unittest.TestCase): - def test_battery_should_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 5) - warning_light_is_on = False - - car = Palindrome(last_service_date, warning_light_is_on) - self.assertTrue(car.needs_service()) - - def test_battery_should_not_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 3) - warning_light_is_on = False - - car = Palindrome(last_service_date, warning_light_is_on) - self.assertFalse(car.needs_service()) - - def test_engine_should_be_serviced(self): - last_service_date = datetime.today().date() - warning_light_is_on = True - - car = Palindrome(last_service_date, warning_light_is_on) - self.assertTrue(car.needs_service()) - - def test_engine_should_not_be_serviced(self): - last_service_date = datetime.today().date() - warning_light_is_on = False - - car = Palindrome(last_service_date, warning_light_is_on) - self.assertFalse(car.needs_service()) - - -class TestRorschach(unittest.TestCase): - def test_battery_should_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 5) - current_mileage = 0 - last_service_mileage = 0 - - car = Rorschach(last_service_date, current_mileage, last_service_mileage) - self.assertTrue(car.needs_service()) - - def test_battery_should_not_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 3) - current_mileage = 0 - last_service_mileage = 0 - - car = Rorschach(last_service_date, current_mileage, last_service_mileage) - self.assertFalse(car.needs_service()) - - def test_engine_should_be_serviced(self): - last_service_date = datetime.today().date() - current_mileage = 60001 - last_service_mileage = 0 - - car = Rorschach(last_service_date, current_mileage, last_service_mileage) - self.assertTrue(car.needs_service()) - - def test_engine_should_not_be_serviced(self): - last_service_date = datetime.today().date() - current_mileage = 60000 - last_service_mileage = 0 - - car = Rorschach(last_service_date, current_mileage, last_service_mileage) - self.assertFalse(car.needs_service()) - - -class TestThovex(unittest.TestCase): - def test_battery_should_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 5) - current_mileage = 0 - last_service_mileage = 0 - - car = Thovex(last_service_date, current_mileage, last_service_mileage) - self.assertTrue(car.needs_service()) - - def test_battery_should_not_be_serviced(self): - today = datetime.today().date() - last_service_date = today.replace(year=today.year - 3) - current_mileage = 0 - last_service_mileage = 0 - - car = Thovex(last_service_date, current_mileage, last_service_mileage) - self.assertFalse(car.needs_service()) - - def test_engine_should_be_serviced(self): - last_service_date = datetime.today().date() - current_mileage = 30001 - last_service_mileage = 0 - - car = Thovex(last_service_date, current_mileage, last_service_mileage) - self.assertTrue(car.needs_service()) - - def test_engine_should_not_be_serviced(self): - last_service_date = datetime.today().date() - current_mileage = 30000 - last_service_mileage = 0 - - car = Thovex(last_service_date, current_mileage, last_service_mileage) - self.assertFalse(car.needs_service()) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/test_engines.py b/test/test_engines.py new file mode 100644 index 000000000..9fcdf8d2d --- /dev/null +++ b/test/test_engines.py @@ -0,0 +1,45 @@ +import unittest + +from engine.capulet_engine import CapuletEngine +from engine.willoughby_engine import WilloughbyEngine +from engine.sternman_engine import SternmanEngine + + +class TestCapuletEngine(unittest.TestCase): + def test_needs_service(self): + current_mileage = 30001 + last_service_mileage = 0 + engine = CapuletEngine(current_mileage, last_service_mileage) + self.assertTrue(engine.needs_service()) + + def test_does_not_needs_service(self): + current_mileage = 30000 + last_service_mileage = 0 + engine = CapuletEngine(current_mileage, last_service_mileage) + self.assertFalse(engine.needs_service()) + + +class TestWilloughbyEngine(unittest.TestCase): + def test_needs_service(self): + current_mileage = 60001 + last_service_mileage = 0 + engine = WilloughbyEngine(current_mileage, last_service_mileage) + self.assertTrue(engine.needs_service()) + + def test_does_not_needs_service(self): + current_mileage = 60000 + last_service_mileage = 0 + engine = WilloughbyEngine(current_mileage, last_service_mileage) + self.assertFalse(engine.needs_service()) + + +class TestSternmanEngine(unittest.TestCase): + def test_needs_service(self): + warning_light_is_on = True + engine = SternmanEngine(warning_light_is_on) + self.assertTrue(engine.needs_service()) + + def test_does_not_needs_service(self): + warning_light_is_on = False + engine = SternmanEngine(warning_light_is_on) + self.assertFalse(engine.needs_service()) diff --git a/test/test_tires.py b/test/test_tires.py new file mode 100644 index 000000000..e9011fe4a --- /dev/null +++ b/test/test_tires.py @@ -0,0 +1,26 @@ +import unittest + +from tires.carrigan_tires import CarriganTires +from tires.octoprime_tires import OctoprimeTires + +class TestCarriganTires(unittest.TestCase): + def test_needs_service_true(self): + tire_wear = [0.1, 0.3, 0.2, 0.9] + tires = CarriganTires(tire_wear) + self.assertTrue(tires.needs_service()) + + def test_needs_service_false(self): + tire_wear = [0.1, 0.2, 0.4, 0.2] + tires = CarriganTires(tire_wear) + self.assertFalse(tires.needs_service()) + +class TestOctoprimeTires(unittest.TestCase): + def test_needs_service_true(self): + tire_wear = [0.8, 0.8, 0.8, 0.7] + tires = OctoprimeTires(tire_wear) + self.assertTrue(tires.needs_service()) + + def test_needs_service_false(self): + tire_wear = [0.1, 0.2, 0.4, 0.2] + tires = OctoprimeTires(tire_wear) + self.assertFalse(tires.needs_service()) \ No newline at end of file diff --git a/test/util.py b/test/util.py new file mode 100644 index 000000000..1f5616d7f --- /dev/null +++ b/test/util.py @@ -0,0 +1,3 @@ +def add_years_to_date(original_date, years_to_add): + result = original_date.replace(year=original_date.year + years_to_add) + return result \ No newline at end of file diff --git a/tires/__init__.py b/tires/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tires/carrigan_tires.py b/tires/carrigan_tires.py new file mode 100644 index 000000000..bb8100cf0 --- /dev/null +++ b/tires/carrigan_tires.py @@ -0,0 +1,11 @@ +from tires.tires import Tires + +class CarriganTires(Tires): + def __init__(self, tire_wear): + self.tire_wear = tire_wear + + def needs_service(self): + for tire in self.tire_wear: + if tire >= 0.9: + return True + return False \ No newline at end of file diff --git a/tires/octoprime_tires.py b/tires/octoprime_tires.py new file mode 100644 index 000000000..bc75852e7 --- /dev/null +++ b/tires/octoprime_tires.py @@ -0,0 +1,8 @@ +from tires.tires import Tires + +class OctoprimeTires(Tires): + def __init__(self, tire_wear): + self.tire_wear = tire_wear + + def needs_service(self): + return sum(self.tire_wear) >= 3.0 \ No newline at end of file diff --git a/tires/tires.py b/tires/tires.py new file mode 100644 index 000000000..35bbbeab3 --- /dev/null +++ b/tires/tires.py @@ -0,0 +1,5 @@ +from abc import ABC + +class Tires(ABC): + def needs_service(self): + pass \ No newline at end of file