From 5c98ec88fe9aaa74656b224ddc6ecf5eddd13005 Mon Sep 17 00:00:00 2001 From: Vlad Kleshko Date: Mon, 31 Jul 2023 10:17:19 +0300 Subject: [PATCH 1/5] for checking --- app/car.py | 4 ++++ app/customer.py | 52 +++++++++++++++++++++++++++++++++++++++++ app/main.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++--- app/shop.py | 13 +++++++++++ 4 files changed, 127 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..c5e690b8 --- /dev/null +++ b/app/car.py @@ -0,0 +1,4 @@ +class Car: + def __init__(self, brand: str, fuel_consumption: float): + self.brand = brand + self.fuel_consumption = fuel_consumption diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..10db9786 --- /dev/null +++ b/app/customer.py @@ -0,0 +1,52 @@ +import datetime + +from math import sqrt +from typing import List + +from app.car import Car +from app.shop import Shop + + +class Customer: + def __init__( + self, + name: str, + product_cart: dict, + customer_location: List[int], + money: int, + car: Car + ) -> None: + self.name = name + self.product_cart = product_cart + self.customer_location = customer_location + self.money = money + self.car = car + + def initial_money(self) -> None: + print(f"{self.name} has {self.money} dollars") + + def distance(self, shop_location: list, fuel_consumption: float, fuel_price: float) -> float: + x1, y1 = self.customer_location + x2, y2 = shop_location + distance_to_shop = sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) + trip_to_shop = distance_to_shop / 100 * fuel_consumption * fuel_price * 2 + + return round(trip_to_shop, 2) + + def time_for_shopping(self, shop: Shop) -> None: + total_cost = 0.0 + date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S") + print(f"Date: {date}") + print(f"Thanks, {self.name}, for your purchase!") + print("You have bought:") + + product_price_list = [value for value in shop.products.values()] + product_list = iter(product_price_list) + + for key, value in self.product_cart.items(): + cost = value * next(product_list) + total_cost += cost + print(f"{value} {key}s for {cost} dollars") + + print(f"Total cost is {total_cost} dollars") + print("See you again!") diff --git a/app/main.py b/app/main.py index 558d7d94..e78a8201 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,58 @@ -def shop_trip(): - # write your code here - pass +import json + +from app.shop import Shop +from app.car import Car +from app.customer import Customer + + +def shop_trip() -> None: + with open("app/config.json", "r") as data_file: + data = json.load(data_file) + + fuel_price = data["FUEL_PRICE"] + customers_data = data["customers"] + shops_data = data["shops"] + + for customer_data in customers_data: + customer_car = Car(customer_data["car"]["brand"], customer_data["car"]["fuel_consumption"]) + customer = Customer(customer_data["name"], customer_data["product_cart"], customer_data["location"], + customer_data["money"], customer_car) + + min_total_cost = float('inf') + best_shop = None + + customer.initial_money() + + for shop_data in shops_data: + shop_name = shop_data["name"] + shop_location = shop_data["location"] + products = shop_data["products"] + + shop = Shop(shop_name, shop_location, products) + + total_cost = (customer.distance(shop.shop_location, customer_car.fuel_consumption, fuel_price)) + + product_price_list = [value for value in products.values()] + product_list = iter(product_price_list) + for key, value in customer.product_cart.items(): + cost = value * next(product_list) + total_cost += cost + + print(f"{customer.name}'s trip to the {shop_name} costs {round(total_cost, 2)}") + + if total_cost < min_total_cost and total_cost <= customer.money: + min_total_cost = total_cost + best_shop = shop + + if best_shop is not None: + print(f"{customer.name} rides to {best_shop.shop_name}\n") + + customer.time_for_shopping(best_shop) + customer.money -= min_total_cost + print(f"{customer.name} now has {round(customer.money, 2)} dollars\n") + + else: + print(f"{customer.name} doesn't have enough money to make a purchase in any shop\n") + + +shop_trip() diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..8fa58d95 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,13 @@ +from typing import List + + +class Shop: + def __init__( + self, + shop_name: str, + shop_location: List[int], + products: dict + ) -> None: + self.shop_name = shop_name + self.shop_location = shop_location + self.products = products From c62e2e256eceec27beb32f1cb925624d5d63bf0a Mon Sep 17 00:00:00 2001 From: Vlad Kleshko Date: Mon, 31 Jul 2023 10:51:09 +0300 Subject: [PATCH 2/5] solution --- app/car.py | 2 +- app/customer.py | 15 ++++++++++----- app/main.py | 36 +++++++++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/car.py b/app/car.py index c5e690b8..9e1aee3f 100644 --- a/app/car.py +++ b/app/car.py @@ -1,4 +1,4 @@ class Car: - def __init__(self, brand: str, fuel_consumption: float): + def __init__(self, brand: str, fuel_consumption: float) -> None: self.brand = brand self.fuel_consumption = fuel_consumption diff --git a/app/customer.py b/app/customer.py index 10db9786..0d9600e6 100644 --- a/app/customer.py +++ b/app/customer.py @@ -25,11 +25,16 @@ def __init__( def initial_money(self) -> None: print(f"{self.name} has {self.money} dollars") - def distance(self, shop_location: list, fuel_consumption: float, fuel_price: float) -> float: + def distance( + self, + shop_location: list, + fuel_consumption: float, + fuel_price: float + ) -> float: x1, y1 = self.customer_location x2, y2 = shop_location - distance_to_shop = sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) - trip_to_shop = distance_to_shop / 100 * fuel_consumption * fuel_price * 2 + dist = sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) + trip_to_shop = dist / 100 * fuel_consumption * fuel_price * 2 return round(trip_to_shop, 2) @@ -38,7 +43,7 @@ def time_for_shopping(self, shop: Shop) -> None: date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S") print(f"Date: {date}") print(f"Thanks, {self.name}, for your purchase!") - print("You have bought:") + print("You have bought: ") product_price_list = [value for value in shop.products.values()] product_list = iter(product_price_list) @@ -49,4 +54,4 @@ def time_for_shopping(self, shop: Shop) -> None: print(f"{value} {key}s for {cost} dollars") print(f"Total cost is {total_cost} dollars") - print("See you again!") + print("See you again!\n") diff --git a/app/main.py b/app/main.py index e78a8201..47a91631 100644 --- a/app/main.py +++ b/app/main.py @@ -14,11 +14,19 @@ def shop_trip() -> None: shops_data = data["shops"] for customer_data in customers_data: - customer_car = Car(customer_data["car"]["brand"], customer_data["car"]["fuel_consumption"]) - customer = Customer(customer_data["name"], customer_data["product_cart"], customer_data["location"], - customer_data["money"], customer_car) - - min_total_cost = float('inf') + customer_car = Car( + customer_data["car"]["brand"], + customer_data["car"]["fuel_consumption"] + ) + customer = Customer( + customer_data["name"], + customer_data["product_cart"], + customer_data["location"], + customer_data["money"], + customer_car + ) + + min_total_cost = float("inf") best_shop = None customer.initial_money() @@ -30,7 +38,13 @@ def shop_trip() -> None: shop = Shop(shop_name, shop_location, products) - total_cost = (customer.distance(shop.shop_location, customer_car.fuel_consumption, fuel_price)) + total_cost = ( + customer.distance( + shop.shop_location, + customer_car.fuel_consumption, + fuel_price + ) + ) product_price_list = [value for value in products.values()] product_list = iter(product_price_list) @@ -38,7 +52,8 @@ def shop_trip() -> None: cost = value * next(product_list) total_cost += cost - print(f"{customer.name}'s trip to the {shop_name} costs {round(total_cost, 2)}") + print(f"{customer.name}'s trip to the " + f"{shop_name} costs {round(total_cost, 2)}") if total_cost < min_total_cost and total_cost <= customer.money: min_total_cost = total_cost @@ -49,10 +64,13 @@ def shop_trip() -> None: customer.time_for_shopping(best_shop) customer.money -= min_total_cost - print(f"{customer.name} now has {round(customer.money, 2)} dollars\n") + print(f"{customer.name} rides home") + print(f"{customer.name} " + f"now has {round(customer.money, 2)} dollars\n") else: - print(f"{customer.name} doesn't have enough money to make a purchase in any shop\n") + print(f"{customer.name} " + f"doesn't have enough money to make a purchase in any shop") shop_trip() From a065447c5d0ae0c1a1d157552781e70db3e44dfa Mon Sep 17 00:00:00 2001 From: Vlad Kleshko Date: Mon, 31 Jul 2023 20:27:49 +0300 Subject: [PATCH 3/5] i fixed all mistakes --- app/customer.py | 9 +++------ app/main.py | 27 ++++++++++++--------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/app/customer.py b/app/customer.py index 0d9600e6..c5ab0dac 100644 --- a/app/customer.py +++ b/app/customer.py @@ -45,13 +45,10 @@ def time_for_shopping(self, shop: Shop) -> None: print(f"Thanks, {self.name}, for your purchase!") print("You have bought: ") - product_price_list = [value for value in shop.products.values()] - product_list = iter(product_price_list) - - for key, value in self.product_cart.items(): - cost = value * next(product_list) + for product_name, price in self.product_cart.items(): + cost = price * shop.products[product_name] total_cost += cost - print(f"{value} {key}s for {cost} dollars") + print(f"{price} {product_name}s for {cost} dollars") print(f"Total cost is {total_cost} dollars") print("See you again!\n") diff --git a/app/main.py b/app/main.py index 47a91631..76356295 100644 --- a/app/main.py +++ b/app/main.py @@ -13,6 +13,15 @@ def shop_trip() -> None: customers_data = data["customers"] shops_data = data["shops"] + shops = {} + for shop_data in shops_data: + shop_name = shop_data["name"] + shop_location = shop_data["location"] + products = shop_data["products"] + + shop = Shop(shop_name, shop_location, products) + shops[shop_name] = shop + for customer_data in customers_data: customer_car = Car( customer_data["car"]["brand"], @@ -30,14 +39,7 @@ def shop_trip() -> None: best_shop = None customer.initial_money() - - for shop_data in shops_data: - shop_name = shop_data["name"] - shop_location = shop_data["location"] - products = shop_data["products"] - - shop = Shop(shop_name, shop_location, products) - + for shop_name, shop in shops.items(): total_cost = ( customer.distance( shop.shop_location, @@ -46,10 +48,8 @@ def shop_trip() -> None: ) ) - product_price_list = [value for value in products.values()] - product_list = iter(product_price_list) - for key, value in customer.product_cart.items(): - cost = value * next(product_list) + for product_name, price in customer.product_cart.items(): + cost = price * shop.products[product_name] total_cost += cost print(f"{customer.name}'s trip to the " @@ -71,6 +71,3 @@ def shop_trip() -> None: else: print(f"{customer.name} " f"doesn't have enough money to make a purchase in any shop") - - -shop_trip() From 2dc80642a5ac40ba012a35eb004611a9258eacf8 Mon Sep 17 00:00:00 2001 From: Vlad Kleshko Date: Tue, 1 Aug 2023 21:14:18 +0300 Subject: [PATCH 4/5] i fixed my mistakes --- app/main.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/main.py b/app/main.py index 76356295..3c2cb72c 100644 --- a/app/main.py +++ b/app/main.py @@ -13,14 +13,14 @@ def shop_trip() -> None: customers_data = data["customers"] shops_data = data["shops"] - shops = {} + shops = [] for shop_data in shops_data: shop_name = shop_data["name"] shop_location = shop_data["location"] products = shop_data["products"] shop = Shop(shop_name, shop_location, products) - shops[shop_name] = shop + shops.append(shop) for customer_data in customers_data: customer_car = Car( @@ -39,7 +39,7 @@ def shop_trip() -> None: best_shop = None customer.initial_money() - for shop_name, shop in shops.items(): + for shop in shops: total_cost = ( customer.distance( shop.shop_location, @@ -48,12 +48,13 @@ def shop_trip() -> None: ) ) - for product_name, price in customer.product_cart.items(): - cost = price * shop.products[product_name] - total_cost += cost + total_cost += sum( + price * shop.products[product_name] + for product_name, price in customer.product_cart.items() + ) print(f"{customer.name}'s trip to the " - f"{shop_name} costs {round(total_cost, 2)}") + f"{shop.shop_name} costs {round(total_cost, 2)}") if total_cost < min_total_cost and total_cost <= customer.money: min_total_cost = total_cost From bdbef3a6051082512b36d78f7bac4d6ede0a1be8 Mon Sep 17 00:00:00 2001 From: Vlad Kleshko Date: Tue, 1 Aug 2023 21:50:33 +0300 Subject: [PATCH 5/5] i use sum function --- app/customer.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/customer.py b/app/customer.py index c5ab0dac..207485aa 100644 --- a/app/customer.py +++ b/app/customer.py @@ -46,9 +46,13 @@ def time_for_shopping(self, shop: Shop) -> None: print("You have bought: ") for product_name, price in self.product_cart.items(): - cost = price * shop.products[product_name] - total_cost += cost - print(f"{price} {product_name}s for {cost} dollars") + print(f"{price} {product_name}s for " + f"{price * shop.products[product_name]} dollars") + total_cost = sum( + price * shop.products[product_name] + for product_name, price in self.product_cart.items() + ) print(f"Total cost is {total_cost} dollars") + print("See you again!\n")