From e669153e57f4bbe1d858682424121acc62d43b70 Mon Sep 17 00:00:00 2001 From: Alexander Izmashkin Date: Mon, 9 Oct 2023 15:16:47 +0300 Subject: [PATCH 1/3] Solution/Alex --- app/car.py | 14 ++++++++++++ app/customer.py | 21 ++++++++++++++++++ app/main.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++--- app/shop.py | 41 ++++++++++++++++++++++++++++++++++ 4 files changed, 131 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..8fe27d52 --- /dev/null +++ b/app/car.py @@ -0,0 +1,14 @@ +from dataclasses import dataclass + + +@dataclass +class Car: + brand: str + fuel_consumption: int | float + + def fuel_cost( + self, + distance: int | float, + fuel_price: int | float + ) -> float: + return (distance / 100) * self.fuel_consumption * fuel_price diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..d245b087 --- /dev/null +++ b/app/customer.py @@ -0,0 +1,21 @@ +from typing import List +from dataclasses import dataclass +from app.car import Car + + +@dataclass +class Customer: + name: str + product_cart: dict + location: List[int] + money: int | float + car: Car + + def money_start(self) -> None: + print(f"{self.name} has {self.money} dollars") + + def ride_to_home(self) -> None: + print(f"{self.name} rides home") + + def money_end(self) -> None: + print(f"{self.name} now has {self.money} dollars\n") diff --git a/app/main.py b/app/main.py index 558d7d94..916703a6 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,55 @@ -def shop_trip(): - # write your code here - pass +import json +import os + +from app.car import Car +from app.customer import Customer +from app.shop import Shop + + +def shop_trip() -> None: + current_directory = os.getcwd() + configuration_file = "app/config.json" + file_path = os.path.join(current_directory, configuration_file) + with open(file_path, "r") as file: + data = json.load(file) + fuel_price = data["FUEL_PRICE"] + customers = data["customers"] + customer_list = [ + Customer(customer["name"], + customer["product_cart"], + customer["location"], + customer["money"], + Car(customer["car"]["brand"], + customer["car"]["fuel_consumption"], + )) for customer in customers + ] + + shops = data["shops"] + shop_list = [ + Shop(shop["name"], + shop["location"], + shop["products"] + ) for shop in shops + ] + + for customer in customer_list: + customer.money_start() + shops_cost = {} + for shop in shop_list: + products_cost = shop.products_cost(customer) + fuel_cost = customer.car.fuel_cost(shop.distance(customer), + fuel_price) + trip_cost = round(products_cost + fuel_cost * 2, 2) + print(f"{customer.name}'s trip to the " + f"{shop.name} costs {trip_cost}") + shops_cost.update({trip_cost: shop}) + min_shop = shops_cost[min(shops_cost)] + if customer.money >= min(shops_cost): + min_shop.ride_to_shop(customer) + min_shop.print_receipt(customer) + customer.ride_to_home() + customer.money -= min(shops_cost) + customer.money_end() + else: + print(f"{customer.name} doesn't have enough " + f"money to make a purchase in any shop") diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..df2d6b66 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,41 @@ +import math +import datetime + +from dataclasses import dataclass +from typing import List +from app.customer import Customer + + +def format_num(num: int | float) -> int | float: + float_num = float(round(num, 2)) + return int(float_num) if float_num.is_integer() else float_num + + +@dataclass +class Shop: + name: str + location: List[int] + products: dict + + def distance(self, customer: Customer) -> float: + return math.dist(self.location, customer.location) + + def ride_to_shop(self, customer: Customer) -> None: + print(f"{customer.name} rides to {self.name}\n") + + def products_cost(self, customer: Customer) -> int | float: + return sum(self.products[prod] * customer.product_cart[prod] + for prod in self.products.keys()) + + def print_receipt(self, customer: Customer) -> None: + total = 0 + date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S") + print(f"Date: {date}") + print(f"Thanks, {customer.name}, for your purchase!") + print("You have bought: ") + for prod, quantity in customer.product_cart.items(): + price = quantity * self.products[prod] + total += price + print(f"{quantity} {prod}s for {format_num(price)} dollars") + print(f"Total cost is {total} dollars\n" + f"See you again!\n") From f35eb414ed26f2b65f92c298bb1de485d7d27a81 Mon Sep 17 00:00:00 2001 From: Alexander Izmashkin Date: Mon, 9 Oct 2023 17:36:02 +0300 Subject: [PATCH 2/3] Solution/Alex/2 --- app/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 916703a6..0e17dc70 100644 --- a/app/main.py +++ b/app/main.py @@ -14,7 +14,7 @@ def shop_trip() -> None: data = json.load(file) fuel_price = data["FUEL_PRICE"] customers = data["customers"] - customer_list = [ + customers_instances = [ Customer(customer["name"], customer["product_cart"], customer["location"], @@ -25,17 +25,17 @@ def shop_trip() -> None: ] shops = data["shops"] - shop_list = [ + shop_instances = [ Shop(shop["name"], shop["location"], shop["products"] ) for shop in shops ] - for customer in customer_list: + for customer in customers_instances: customer.money_start() shops_cost = {} - for shop in shop_list: + for shop in shop_instances: products_cost = shop.products_cost(customer) fuel_cost = customer.car.fuel_cost(shop.distance(customer), fuel_price) From 091ccb4d44fa93f4f3fe1a95127560c3be02e7f8 Mon Sep 17 00:00:00 2001 From: Alexander Izmashkin Date: Tue, 10 Oct 2023 13:41:25 +0300 Subject: [PATCH 3/3] Solution/Alex/3 --- app/shop.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/shop.py b/app/shop.py index df2d6b66..b768f8bd 100644 --- a/app/shop.py +++ b/app/shop.py @@ -24,8 +24,8 @@ def ride_to_shop(self, customer: Customer) -> None: print(f"{customer.name} rides to {self.name}\n") def products_cost(self, customer: Customer) -> int | float: - return sum(self.products[prod] * customer.product_cart[prod] - for prod in self.products.keys()) + return sum(price * customer.product_cart[prod] + for prod, price in self.products.items()) def print_receipt(self, customer: Customer) -> None: total = 0