From b460c8612c560f6c9eaca7dd50d314e2a89cdb20 Mon Sep 17 00:00:00 2001 From: Vadym Piddubnyi Date: Mon, 23 Dec 2024 00:20:04 -0600 Subject: [PATCH 1/2] Solution --- app/car.py | 12 ++++++++++++ app/customer.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ app/main.py | 26 +++++++++++++++++++++++--- app/shop.py | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 app/car.py create mode 100644 app/customer.py create mode 100644 app/shop.py diff --git a/app/car.py b/app/car.py new file mode 100644 index 00000000..e0df2b9f --- /dev/null +++ b/app/car.py @@ -0,0 +1,12 @@ +from dataclasses import dataclass +from math import sqrt + + +@dataclass +class Car: + brand: str + fuel_consumption: float + + def total_consumption(self, start: list, end: list) -> float: + distance = sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) + return round(2 * distance * self.fuel_consumption / 100, 2) diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..da5b6572 --- /dev/null +++ b/app/customer.py @@ -0,0 +1,49 @@ +from app.car import Car +from app.shop import Shop + + +class Customer: + def __init__( + self, + name: str, + product_cart: dict, + location: list, + money: int, + car: dict + ) -> None: + self.name = name + self.product_cart = product_cart + self.location = location + self.money = money + self.car = Car(*car.values()) + print(f"{self.name} has {self.money} dollars") + + def trip_cost( + self, + shop: Shop, + fuel_price: float + ) -> float: + products_cost = shop.products_cost(self.product_cart).values() + fuel_cons = self.car.total_consumption(self.location, shop.location) + return round(sum(products_cost) + fuel_cons * fuel_price, 2) + + def cheapest_trip( + self, + shops: list[Shop], + fuel_price: float + ) -> (Shop, float): + trips_cost = [] + for shop in shops: + trip_cost = self.trip_cost(shop, fuel_price) + print(f"{self.name}'s trip to the {shop.name} costs {trip_cost}") + trips_cost.append(trip_cost) + cheapest_trip_index = trips_cost.index(min(trips_cost)) + return shops[cheapest_trip_index], trips_cost[cheapest_trip_index] + + def return_home( + self, + trip_cost: float, + ) -> None: + print(f"{self.name} rides home") + print(f"{self.name} now has " + f"{round(self.money - trip_cost, 2)} dollars\n") diff --git a/app/main.py b/app/main.py index 558d7d94..3f7987d8 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,23 @@ -def shop_trip(): - # write your code here - pass +from json import load +from app.customer import Customer +from app.shop import Shop + + +def shop_trip() -> None: + with open("config.json") as config: + data = load(config) + + fuel_price = data["FUEL_PRICE"] + + for customer in data["customers"]: + current_customer = Customer(*customer.values()) + shops = [Shop(*shop.values()) for shop in data["shops"]] + shop, cheapest_trip = current_customer.cheapest_trip(shops, fuel_price) + + if cheapest_trip > current_customer.money: + print(f"{current_customer.name} " + f"doesn't have enough money to make a purchase in any shop") + else: + print(f"{current_customer.name} rides to {shop.name}\n") + shop.shopping(current_customer.name, current_customer.product_cart) + current_customer.return_home(cheapest_trip) diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..1d62c94e --- /dev/null +++ b/app/shop.py @@ -0,0 +1,33 @@ +from __future__ import annotations +from dataclasses import dataclass +from datetime import datetime + + +@dataclass +class Shop: + name: str + location: list + products: dict + + def products_cost(self, cart: dict) -> dict: + return {product: cart[product] * self.products[product] for product in cart} + + def shopping(self, customer_name: str, cart: dict) -> None: + print("Date: ", datetime.now().strftime("%d/%m/%Y %H:%M:%S")) + print(f"Thanks, {customer_name}, for your purchase!") + print("You have bought:") + + products_cost = self.products_cost(cart) + for product in products_cost: + print(f"{cart[product]} {product}s for " + f"{self.round_price(products_cost[product])} dollars") + + print(f"Total cost is {sum(self.products_cost(cart).values())}" + f" dollars") + print("See you again!\n") + + @staticmethod + def round_price(price: float) -> int | float: + if int(price) == price: + return int(price) + return price From 661f11cdf438636ba7d6ac658c5fc905a8ea3166 Mon Sep 17 00:00:00 2001 From: Vadym Piddubnyi Date: Mon, 23 Dec 2024 00:22:16 -0600 Subject: [PATCH 2/2] Solution --- app/shop.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/shop.py b/app/shop.py index 1d62c94e..e9bee7b7 100644 --- a/app/shop.py +++ b/app/shop.py @@ -10,7 +10,10 @@ class Shop: products: dict def products_cost(self, cart: dict) -> dict: - return {product: cart[product] * self.products[product] for product in cart} + return { + product: cart[product] * self.products[product] + for product in cart + } def shopping(self, customer_name: str, cart: dict) -> None: print("Date: ", datetime.now().strftime("%d/%m/%Y %H:%M:%S"))