From f8c86e43b7e7dec13ce02623081408c0b9189f65 Mon Sep 17 00:00:00 2001 From: Liliya Date: Thu, 18 Apr 2024 13:48:49 +0300 Subject: [PATCH] Solution --- app/car.py | 23 +++++++++++++++ app/customer.py | 25 ++++++++++++++++ app/main.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++-- app/shop.py | 35 +++++++++++++++++++++++ 4 files changed, 156 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..25e648c2 --- /dev/null +++ b/app/car.py @@ -0,0 +1,23 @@ +from math import sqrt + + +class Car: + def __init__( + self, + brand: str, + fuel_consumption: float + ) -> None: + self.brand = brand + self.fuel_consumption = fuel_consumption + + def liters_needed( + self, + customer_location: list, + shop_location: list + ) -> float: + distance_to_shop = (sqrt( + (shop_location[0] - customer_location[0]) ** 2 + + (shop_location[1] - customer_location[1]) ** 2) + * 2) + liters_needed = self.fuel_consumption / 100 * distance_to_shop + return liters_needed diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..33b92da3 --- /dev/null +++ b/app/customer.py @@ -0,0 +1,25 @@ +class Customer: + def __init__( + self, + name: str, + products: dict, + location: list, + money: int + ) -> None: + self.name = name + self.products = products + self.location = location + self.money = money + + def money_amount(self) -> str: + return f"{self.name} has {self.money} dollars\n" + + def cost_of_products_in_shop(self, shop: dict) -> float: + total_for_shop = 0 + for product in self.products: + cost_of_product = self.products[product] * shop[product] + total_for_shop += cost_of_product + return total_for_shop + + def change_location(self, shop_location: list) -> None: + self.location = shop_location diff --git a/app/main.py b/app/main.py index 558d7d94..155d24f5 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,73 @@ -def shop_trip(): - # write your code here - pass +import os +import json + +from app.customer import Customer +from app.shop import Shop +from app.car import Car + + +def shop_trip() -> None: + + with open(os.getcwd() + "/app/config.json", "r") as file: + info = json.load(file) + + buyers = info["customers"] + stores = info["shops"] + fuel_price = info["FUEL_PRICE"] + out = """""" + for buyer in buyers: + customer = Customer( + buyer["name"], + buyer["product_cart"], + buyer["location"], + buyer["money"] + ) + car = Car( + brand=buyer["car"]["brand"], + fuel_consumption=buyer["car"]["fuel_consumption"] + ) + out += customer.money_amount() + cost_in_all_shops = {} + for store in stores: + shop = Shop( + store["name"], + store["location"], + store["products"] + ) + cost_in_one_shop = ( + customer.cost_of_products_in_shop(shop.shop_products) + ) + cost_of_fuel = ( + car.liters_needed(customer.location, shop.shop_location) + * fuel_price + ) + sum_shop_and_fuel_cost = round(cost_in_one_shop + cost_of_fuel, 2) + out += (f"{customer.name}'s trip to the {shop.shop_name} " + f"costs {sum_shop_and_fuel_cost}\n") + cost_in_all_shops[shop.shop_name] = sum_shop_and_fuel_cost + min_cost_key = min( + cost_in_all_shops, key=lambda k: cost_in_all_shops[k] + ) + if customer.money > cost_in_all_shops[min_cost_key]: + out += f"{customer.name} rides to {min_cost_key}\n\n" + index_of_the_right_shop = 0 + for index, store in enumerate(stores): + if store["name"] == min_cost_key: + index_of_the_right_shop = index + + min_bill_shop = Shop( + shop_name=stores[index_of_the_right_shop]["name"], + shop_location=stores[index_of_the_right_shop]["location"], + shop_products=stores[index_of_the_right_shop]["products"] + ) + out += min_bill_shop.bill(customer.name, customer.products) + customer.change_location(min_bill_shop.shop_location) + amount_left = customer.money - cost_in_all_shops[min_cost_key] + out += "\n" + out += f"{customer.name} rides home\n" + out += f"{customer.name} now has {amount_left} dollars\n\n" + + else: + out += (f"{customer.name} doesn't have enough money " + f"to make a purchase in any shop") + print(out) diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..1bd758af --- /dev/null +++ b/app/shop.py @@ -0,0 +1,35 @@ +import datetime + + +class Shop: + def __init__( + self, + shop_name: str, + shop_location: list, + shop_products: dict + ) -> None: + self.shop_name = shop_name + self.shop_location = shop_location + self.shop_products = shop_products + + def bill(self, customer_name: str, products_needed: dict) -> str: + out = """""" + out += (f"Date: " + f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}" + f"\n") + out += f"Thanks, {customer_name}, for your purchase!\n" + out += "You have bought:\n" + total_amount = 0 + for product in self.shop_products: + total_for_product = ( + products_needed[product] + * self.shop_products[product] + ) + total_amount += total_for_product + if str(total_for_product)[-2:] == ".0": + total_for_product = int(total_for_product) + out += (f"{products_needed[product]} {product}s " + f"for {total_for_product} dollars\n") + out += f"Total cost is {total_amount} dollars\n" + out += "See you again!\n" + return out