From d098cd7279450bc67a116b9cdfe43b15925b403b Mon Sep 17 00:00:00 2001 From: Trukhanov Danylo Date: Wed, 25 Dec 2024 23:22:10 +0200 Subject: [PATCH 1/4] version 1.0 --- app/car.py | 17 +++++++++++++++ app/customer.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ app/main.py | 28 ++++++++++++++++++++++-- app/point.py | 18 +++++++++++++++ app/shop.py | 43 ++++++++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 app/car.py create mode 100644 app/customer.py create mode 100644 app/point.py create mode 100644 app/shop.py diff --git a/app/car.py b/app/car.py new file mode 100644 index 00000000..88b7c108 --- /dev/null +++ b/app/car.py @@ -0,0 +1,17 @@ +from __future__ import annotations + + +class Car: + def __init__(self, brand: str, fuel_consumption: float) -> None: + self.brand = brand + self.fuel_consumption = fuel_consumption + + @classmethod + def create_car(cls, car_info: dict[str, str | float | int]) -> Car: + return cls( + brand=car_info["brand"], + fuel_consumption=car_info["fuel_consumption"] + ) + + def calculate_fuel_consumption(self, distance: float) -> float: + return (self.fuel_consumption / 100) * distance diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..27948f9d --- /dev/null +++ b/app/customer.py @@ -0,0 +1,58 @@ +from __future__ import annotations + +from app.car import Car +from app.point import Point +from app.shop import Shop + +class Customer: + def __init__( + self, + name: str, + product_cart: dict[str, int], + location: Point, + money: float, + car: Car + ) -> None: + self.name = name + self.product_cart = product_cart or {} + self.location = location + self.money = money + self.car = car + + @classmethod + def create_customer( + cls, + source: dict[str, str | dict[str, int] | list[int] | int] + ) -> Customer: + return cls( + source["name"], + source["product_cart"], + Point.create_from_location(source["location"]), + source["money"], + Car.create_car(source["car"]) + ) + + def choose_shop(self, shops: list) -> Shop | None: + for shop in shops: + print(f"{self.name}'s trip to the {shop.name} costs {self._calculate_total_expenses(shop)}") + + cheapest_shop = min(shops, key=lambda shop: self._calculate_total_expenses(shop)) + total_expenses = self._calculate_total_expenses(cheapest_shop) + if self.money < total_expenses: + return None + self.money -= total_expenses + return cheapest_shop + + + + def _calculate_total_expenses(self, shop) -> float: + distance = self.location.calculate_distance_to(shop.location) + fuel_expenses = (self.car.calculate_fuel_consumption(distance) * 2) * 2.4 + groceries_expenses = self._calculate_groceries_expenses(shop.products) + + return round(fuel_expenses + groceries_expenses, 2) + + def _calculate_groceries_expenses(self, prices): + return sum( + prices[product_name] * amount for product_name, amount in self.product_cart.items() + ) \ No newline at end of file diff --git a/app/main.py b/app/main.py index 558d7d94..65e1ca7a 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,27 @@ +import json +from typing import Callable + +from app.shop import Shop +from app.customer import Customer + +def get_data(info_key: str, creation_func: Callable) -> list: + with open("app/config.json", "r") as source: + data = json.load(source) + return [creation_func(info) for info in data[info_key]] + def shop_trip(): - # write your code here - pass + customers = get_data("customers", Customer.create_customer) + shops = get_data("shops", Shop.create_shop) + + for client in customers: + print(f"{client.name} has {client.money} dollars") + + client_shop_choice = client.choose_shop(shops) + if client_shop_choice is None: + print(f"{client.name} doesn't have enough money to make a purchase in any shop") + return + print(f"{client.name} rides to {client_shop_choice.name}\n") + client_shop_choice.sell(client) + +shop_trip() + diff --git a/app/point.py b/app/point.py new file mode 100644 index 00000000..27b12cfd --- /dev/null +++ b/app/point.py @@ -0,0 +1,18 @@ +from __future__ import annotations + +import math + + +class Point: + def __init__(self, x: int, y: int) -> None: + self.x = x + self.y = y + + @classmethod + def create_from_location(cls, location: list[int]) -> Point: + return cls(x=location[0], y=location[1]) + + def calculate_distance_to(self, destination: Point) -> float: + return math.sqrt( + math.pow(destination.x - self.x, 2) + math.pow(destination.y - self.y, 2) + ) diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..14f88096 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +from app.point import Point +from datetime import datetime + +class Shop: + def __init__( + self, + name: str, + location: Point, + products: dict[str, float | int] + ) -> None: + self.name = name + self.location = location + self.products = products + + @classmethod + def create_shop( + cls, + shop_info: dict[str, str | list[int] | dict[str, (int | float)]] + ) -> Shop: + return cls( + name=shop_info["name"], + location=Point.create_from_location(shop_info["location"]), + products=shop_info["products"] + ) + + def sell(self, customer): + print(f"Date: 04/01/2021 12:33:41") + + print(f"Thanks, {customer.name}, for your purchase!\nYou have bought:") + for name, amount in customer.product_cart.items(): + product_price = amount * self.products[name] + if product_price.is_integer(): + product_price = int(product_price) + print(f"{amount} {name}s for {product_price} dollars") + total_cost = sum(amount * self.products[name] for name, amount in customer.product_cart.items()) + print(f"Total cost is {round(total_cost, 2)} dollars") + print("See you again!\n") + print(f"{customer.name} rides home") + print(f"{customer.name} now has {customer.money} dollars\n") + + From 286d3bcdf866371a0dfa40d9920ebda664a530a3 Mon Sep 17 00:00:00 2001 From: Trukhanov Danylo Date: Wed, 25 Dec 2024 23:34:06 +0200 Subject: [PATCH 2/4] fixed flake8 --- app/customer.py | 28 ++++++++++++++++++---------- app/main.py | 9 ++++++--- app/point.py | 11 ++++++----- app/shop.py | 18 ++++++++++-------- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/app/customer.py b/app/customer.py index 27948f9d..414b00b5 100644 --- a/app/customer.py +++ b/app/customer.py @@ -4,6 +4,7 @@ from app.point import Point from app.shop import Shop + class Customer: def __init__( self, @@ -34,25 +35,32 @@ def create_customer( def choose_shop(self, shops: list) -> Shop | None: for shop in shops: - print(f"{self.name}'s trip to the {shop.name} costs {self._calculate_total_expenses(shop)}") + print(f"{self.name}'s trip to the {shop.name} " + f"costs {self._calculate_total_expenses(shop)}") - cheapest_shop = min(shops, key=lambda shop: self._calculate_total_expenses(shop)) + cheapest_shop = min( + shops, + key=lambda shop: self._calculate_total_expenses(shop) + ) total_expenses = self._calculate_total_expenses(cheapest_shop) if self.money < total_expenses: return None self.money -= total_expenses return cheapest_shop - - - def _calculate_total_expenses(self, shop) -> float: + def _calculate_total_expenses(self, shop: Shop) -> float: distance = self.location.calculate_distance_to(shop.location) - fuel_expenses = (self.car.calculate_fuel_consumption(distance) * 2) * 2.4 - groceries_expenses = self._calculate_groceries_expenses(shop.products) + fuel_expenses = ( + (self.car.calculate_fuel_consumption(distance) * 2) * 2.4 + ) + groceries_expenses = ( + self._calculate_groceries_expenses(shop.products) + ) return round(fuel_expenses + groceries_expenses, 2) - def _calculate_groceries_expenses(self, prices): + def _calculate_groceries_expenses(self, prices: dict) -> float: return sum( - prices[product_name] * amount for product_name, amount in self.product_cart.items() - ) \ No newline at end of file + prices[product_name] * amount + for product_name, amount in self.product_cart.items() + ) diff --git a/app/main.py b/app/main.py index 65e1ca7a..8bb51d3c 100644 --- a/app/main.py +++ b/app/main.py @@ -4,12 +4,14 @@ from app.shop import Shop from app.customer import Customer + def get_data(info_key: str, creation_func: Callable) -> list: with open("app/config.json", "r") as source: data = json.load(source) return [creation_func(info) for info in data[info_key]] -def shop_trip(): + +def shop_trip() -> None: customers = get_data("customers", Customer.create_customer) shops = get_data("shops", Shop.create_shop) @@ -18,10 +20,11 @@ def shop_trip(): client_shop_choice = client.choose_shop(shops) if client_shop_choice is None: - print(f"{client.name} doesn't have enough money to make a purchase in any shop") + print(f"{client.name} doesn't have enough " + f"money to make a purchase in any shop") return print(f"{client.name} rides to {client_shop_choice.name}\n") client_shop_choice.sell(client) -shop_trip() +shop_trip() diff --git a/app/point.py b/app/point.py index 27b12cfd..41adb327 100644 --- a/app/point.py +++ b/app/point.py @@ -4,15 +4,16 @@ class Point: - def __init__(self, x: int, y: int) -> None: - self.x = x - self.y = y + def __init__(self, cor_x: int, cor_y: int) -> None: + self.x = cor_x + self.y = cor_y @classmethod def create_from_location(cls, location: list[int]) -> Point: - return cls(x=location[0], y=location[1]) + return cls(cor_x=location[0], cor_y=location[1]) def calculate_distance_to(self, destination: Point) -> float: return math.sqrt( - math.pow(destination.x - self.x, 2) + math.pow(destination.y - self.y, 2) + math.pow(destination.x - self.x, 2) + + math.pow(destination.y - self.y, 2) ) diff --git a/app/shop.py b/app/shop.py index 14f88096..f2a2237d 100644 --- a/app/shop.py +++ b/app/shop.py @@ -1,7 +1,8 @@ from __future__ import annotations +from typing import Any from app.point import Point -from datetime import datetime + class Shop: def __init__( @@ -25,19 +26,20 @@ def create_shop( products=shop_info["products"] ) - def sell(self, customer): - print(f"Date: 04/01/2021 12:33:41") - - print(f"Thanks, {customer.name}, for your purchase!\nYou have bought:") + def sell(self, customer: Any) -> None: + print("Date: 04/01/2021 12:33:41") + print(f"Thanks, {customer.name}, for your purchase!" + f"\nYou have bought: ") for name, amount in customer.product_cart.items(): product_price = amount * self.products[name] if product_price.is_integer(): product_price = int(product_price) print(f"{amount} {name}s for {product_price} dollars") - total_cost = sum(amount * self.products[name] for name, amount in customer.product_cart.items()) + total_cost = sum( + amount * self.products[name] + for name, amount in customer.product_cart.items() + ) print(f"Total cost is {round(total_cost, 2)} dollars") print("See you again!\n") print(f"{customer.name} rides home") print(f"{customer.name} now has {customer.money} dollars\n") - - From dbbaceb6e0037632486fc6e511a2f4cb4d63e0f3 Mon Sep 17 00:00:00 2001 From: Trukhanov Danylo Date: Thu, 26 Dec 2024 15:09:12 +0200 Subject: [PATCH 3/4] fixed float int print --- app/main.py | 2 +- app/shop.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 8bb51d3c..9b8f4c47 100644 --- a/app/main.py +++ b/app/main.py @@ -6,7 +6,7 @@ def get_data(info_key: str, creation_func: Callable) -> list: - with open("app/config.json", "r") as source: + with open("config.json", "r") as source: data = json.load(source) return [creation_func(info) for info in data[info_key]] diff --git a/app/shop.py b/app/shop.py index f2a2237d..547328f7 100644 --- a/app/shop.py +++ b/app/shop.py @@ -32,7 +32,7 @@ def sell(self, customer: Any) -> None: f"\nYou have bought: ") for name, amount in customer.product_cart.items(): product_price = amount * self.products[name] - if product_price.is_integer(): + if product_price % int(product_price) == 0: product_price = int(product_price) print(f"{amount} {name}s for {product_price} dollars") total_cost = sum( From cbeb90c4b9487bf2d572a50eb5c323731888853f Mon Sep 17 00:00:00 2001 From: Trukhanov Danylo Date: Thu, 26 Dec 2024 15:22:47 +0200 Subject: [PATCH 4/4] fixed flake8 whitespace conflict --- app/main.py | 2 +- app/shop.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 9b8f4c47..8bb51d3c 100644 --- a/app/main.py +++ b/app/main.py @@ -6,7 +6,7 @@ def get_data(info_key: str, creation_func: Callable) -> list: - with open("config.json", "r") as source: + with open("app/config.json", "r") as source: data = json.load(source) return [creation_func(info) for info in data[info_key]] diff --git a/app/shop.py b/app/shop.py index 547328f7..4b22136d 100644 --- a/app/shop.py +++ b/app/shop.py @@ -28,8 +28,8 @@ def create_shop( def sell(self, customer: Any) -> None: print("Date: 04/01/2021 12:33:41") - print(f"Thanks, {customer.name}, for your purchase!" - f"\nYou have bought: ") + print(f"Thanks, {customer.name}, for your purchase!") + print("You have bought:") for name, amount in customer.product_cart.items(): product_price = amount * self.products[name] if product_price % int(product_price) == 0: