From 94b9fedef54f79817e10e8b273b5b82328100d05 Mon Sep 17 00:00:00 2001 From: Andrew Chugreev Date: Sun, 27 Oct 2024 13:52:05 +0200 Subject: [PATCH] Solution --- app/car.py | 7 ++++++ app/customer.py | 28 ++++++++++++++++++++++ app/main.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++--- app/shop.py | 45 ++++++++++++++++++++++++++++++++++ 4 files changed, 141 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..49daf580 --- /dev/null +++ b/app/car.py @@ -0,0 +1,7 @@ +class Car: + def __init__(self, brand: str, fuel_consumption: float) -> None: + self.brand = brand + self.fuel_consumption = fuel_consumption + + def calculate_fuel_cost(self, distance: float, full_price: float) -> float: + return (self.fuel_consumption / 100) * distance * full_price diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..ad77de61 --- /dev/null +++ b/app/customer.py @@ -0,0 +1,28 @@ +from app.car import Car +from decimal import Decimal +import math + + +class Customer(): + def __init__( + self, + name: str, + product_cart: dict[str, int], + location: list[int], + money: Decimal, + car: Car + ) -> None: + self.name = name + self.product_cart = product_cart + self.location = location + self.money = money + self.car = car + + def distance(self, shop_location: list) -> float: + return math.sqrt( + (self.location[0] - shop_location[0]) ** 2 + + (self.location[1] - shop_location[1]) ** 2) + + def ride_home(self, spend_money: float) -> None: + print(f"{self.name} rides home") + print(f"{self.name} now has {self.money - spend_money} dollars\n") diff --git a/app/main.py b/app/main.py index 558d7d94..c986d27e 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,61 @@ -def shop_trip(): - # write your code here - pass +import json +import os +from app.customer import Customer +from app.shop import Shop +from app.car import Car + + +def shop_trip() -> None: + config_path = os.path.join(os.path.dirname(__file__), "config.json") + with open(config_path, "r") as file: + data = json.load(file) + fuel_price, customers_, shops_ = data.values() + + customers = [] + for custom in customers_: + car = Car(**custom["car"]) + customer = Customer( + name=custom["name"], + product_cart=custom["product_cart"], + location=custom["location"], + money=custom["money"], + car=car + ) + customers.append(customer) + + shops = [] + for some_shop in shops_: + shop = Shop( + name=some_shop["name"], + location=some_shop["location"], + products=some_shop["products"] + ) + shops.append(shop) + + for customer in customers: + customer_money = customer.money + print(f"{customer.name} has {customer_money} dollars") + trip_cost_dict = {} + for shop in shops: + total = 0 + dist_for_shop = customer.distance(shop.location) + fuel_for_shop = customer.car.calculate_fuel_cost( + dist_for_shop, + fuel_price + ) + products_cost = shop.products_cost(customer) + total += round((products_cost + fuel_for_shop * 2), 2) + trip_cost_dict[total] = shop + print(f"{customer.name}'s trip to the {shop.name} costs {total}") + if customer.money < min(trip_cost_dict): + print(f"{customer.name} doesn't have enough" + f" money to make a purchase in any shop") + else: + total = min(trip_cost_dict) + current_shop = trip_cost_dict[min(trip_cost_dict)] + print(f"{customer.name} rides to {current_shop.name}\n") + current_shop.print_check(customer) + customer.ride_home(total) + + +shop_trip() diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..478745b4 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,45 @@ +from app.customer import Customer +from datetime import datetime + + +class Shop: + def __init__( + self, + name: str, + location: list[int], + products: dict[str, float] + ) -> None: + self.name = name + self.location = location + self.products = products + + def products_cost(self, customer: Customer) -> float: + product_cart = customer.product_cart + product_cost = 0 + for product, qty in product_cart.items(): + product_cost += qty * self.products.get(product) + return product_cost + + def print_check(self, customer: Customer) -> None: + current_datatime = datetime(2021, 1, 4, 12, 33, 41) + current_datatime = current_datatime.strftime("%d/%m/%Y %H:%M:%S") + print(f"Date: {current_datatime}") + print(f"Thanks, {customer.name}, for your purchase!") + print("You have bought:") + milk = customer.product_cart.get("milk") + print(f"{milk} " + f"milks for " + f"{milk * self.products.get('milk')}" + f" dollars") + bread = customer.product_cart.get("bread") + print(f"{bread} " + f"breads for " + f"{round(bread * self.products.get('bread'))}" + f" dollars") + butter = customer.product_cart.get("butter") + print(f"{butter}" + f" butters for " + f"{butter * self.products.get('butter')}" + f" dollars") + print(f"Total cost is {self.products_cost(customer)} dollars") + print("See you again!\n")