diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..fb1528af --- /dev/null +++ b/app/customer.py @@ -0,0 +1,53 @@ +import math +from app.shop import Shop + + +class Customer: + def __init__( + self, + name: str, + product_cart: dict, + location: list[int], + money: int, + car: dict + ) -> None: + self.name = name + self.product_cart = product_cart + self.location = location + self.money = money + self.car = car + + def customer_has_money(self) -> None: + print(f"{self.name} has {self.money} dollars") + + def cost_of_the_trip( + self, + fuel_prize: float, + customer_location: list[int], + shop_location: list[int]) -> float: + distance = math.sqrt( + (shop_location[0] - customer_location[0]) ** 2 + + (shop_location[1] - customer_location[1]) ** 2) + return round(self.car["fuel_consumption"] + * distance / 100 * fuel_prize, 2) + + def does_not_have_enough_money(self) -> None: + print(f"{self.name} doesn't have enough " + f"money to make a purchase in any shop") + + def customer_ride_to_shop(self, shop_obj: Shop) -> None: + print(f"{self.name} rides to {shop_obj.name}") + + def bought_milk(self, product_milk: float) -> None: + print( + f'{self.product_cart["milk"]} milks ' + f'for {self.product_cart["milk"] * product_milk} dollars') + + def bought_bread(self, product_bread: float) -> None: + print( + f'{self.product_cart["bread"]} bread ' + f'for {self.product_cart["bread"] * product_bread} dollars') + + def bought_butter(self, product_butter: float) -> None: + print(f'{self.product_cart["butter"]} butter ' + f'for {self.product_cart["butter"] * product_butter} dollars') diff --git a/app/main.py b/app/main.py index 558d7d94..cc902f9b 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,70 @@ -def shop_trip(): - # write your code here - pass +import os +from datetime import datetime +import json + +from app.shop import Shop +from app.customer import Customer + + +def shop_trip() -> None: + config_path = os.path.join(os.path.dirname(__file__), "config.json") + with open(config_path) as config: + config_data = json.load(config) + fuel_prize = config_data["FUEL_PRICE"] + + list_customers = [Customer(customers["name"], + customers["product_cart"], + customers["location"], + customers["money"], + customers["car"] + ) for customers in config_data["customers"]] + + list_shops = [Shop( + shops["name"], + shops["location"], + shops["products"]) + for shops in config_data["shops"]] + + for customer in list_customers: + customer.customer_has_money() + costs = float("inf") + shop_obj = None + for shop in list_shops: + min_cost = (customer.cost_of_the_trip( + fuel_prize, + customer.location, + shop.location) * 2 + shop.price_products( + customer.product_cart["milk"], + customer.product_cart["bread"], + customer.product_cart["butter"] + )) + if min_cost < costs: + costs = min_cost + shop_obj = shop + print(f"{customer.name}'s trip to " + f"the {shop.name} costs {min_cost}") + if customer.money < costs: + customer.does_not_have_enough_money() + + else: + customer.customer_ride_to_shop(shop_obj) + now = datetime.now() + print() + print(now.strftime("Date: %d/%m/%Y %H:%M:%S")) + print(f"Thanks, {customer.name}, for your purchase!") + print("You have bought:") + customer.bought_milk(shop_obj.products["milk"]) + customer.bought_bread(shop_obj.products["bread"]) + customer.bought_butter(shop_obj.products["butter"]) + total_cost = (customer.product_cart["milk"] + * shop_obj.products["milk"] + + customer.product_cart["bread"] + * shop_obj.products["bread"] + + customer.product_cart["butter"] + * shop_obj.products["butter"]) + print(f"Total cost is {total_cost} dollars") + print("See you again!") + print() + print(f"{customer.name} rides home") + print(f"{customer.name} now has {customer.money - costs} dollars") + print() diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..c031edc4 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,15 @@ +class Shop: + def __init__(self, name: str, location: list[int], products: dict) -> None: + self.name = name + self.location = location + self.products = products + + def price_products( + self, + quantity_milk: int, + quantity_bread: int, + quantity_butter: int + ) -> float: + return (self.products["milk"] * quantity_milk + + self.products["bread"] * quantity_bread + + self.products["butter"] * quantity_butter) diff --git a/tests/test_main.py b/tests/test_main.py index 4bc4fe41..14c7c7c0 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -6,7 +6,6 @@ from app.main import shop_trip - def test_shop_trip_output(monkeypatch): datetime_mock = MagicMock(wrap=datetime.datetime) datetime_mock.now.return_value = datetime.datetime(2021, 1, 4, 12, 33, 41)