From ff0a0971ebefb6819ea03d781d75a443aaf10508 Mon Sep 17 00:00:00 2001 From: Nadiia Date: Tue, 10 Oct 2023 19:24:15 +0300 Subject: [PATCH 1/4] Solution-py-shop-trip --- app/car.py | 4 ++++ app/customer.py | 39 +++++++++++++++++++++++++++++++++++++++ app/main.py | 37 +++++++++++++++++++++++++++++++++++-- app/shop.py | 19 +++++++++++++++++++ 4 files changed, 97 insertions(+), 2 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..48b66a18 --- /dev/null +++ b/app/car.py @@ -0,0 +1,4 @@ +class Car: + def __init__(self, data): + self.brand = data["brand"] + self.fuel_consumption = data["fuel_consumption"] \ No newline at end of file diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..060c001c --- /dev/null +++ b/app/customer.py @@ -0,0 +1,39 @@ +import math +from app.car import Car + + +class Customer: + FUEL_PRICE = 2.4 + + def __init__(self, data): + self.name = data["name"] + self.product_cart = data["product_cart"] + self.location = data["location"] + self.money = data["money"] + self.car = Car(data["car"]) + + def calculate_trip_cost(self, shop): + distance_to_shop = self.calculate_distance(shop.location) + fuel_consumption_per_km = self.car.fuel_consumption / 100 + fuel_cost_to_shop = (distance_to_shop * fuel_consumption_per_km) * self.FUEL_PRICE + product_cost = sum([self.product_cart[product] * shop.products[product] for product in self.product_cart]) + total_trip_cost = fuel_cost_to_shop + product_cost + return round(total_trip_cost, 2) + + def has_enough_money(self, trip_cost): + return self.money >= trip_cost + + def travel_to_shop(self, shop): + self.location = shop.location + print(f"{self.name} rides to {shop.name}\n") + + def travel_home(self, trip_cost): + print(f"{self.name} rides home\n") + remaining_money = self.money - trip_cost + print(f"{self.name} now has {round(remaining_money, 2)} dollars\n") + + def calculate_distance(self, shop_location): + x_location = self.location[0] - shop_location[0] + y_location = self.location[1] - shop_location[1] + distance = math.sqrt(x_location ** 2 + y_location ** 2) + return distance diff --git a/app/main.py b/app/main.py index 558d7d94..7d713837 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,36 @@ +import json +from app.customer import Customer +from app.shop import Shop + + def shop_trip(): - # write your code here - pass + with open("app/config.json", "r") as config_file: + config_data = json.load(config_file) + for customer_data in config_data["customers"]: + customer = Customer(customer_data) + print(f"{customer.name} has {customer.money} dollars\n") + cheapest_shop = None + cheapest_cost = float('inf') + + for shop_data in config_data["shops"]: + shop = Shop(shop_data) + trip_cost = customer.calculate_trip_cost(shop) + if trip_cost < cheapest_cost and customer.has_enough_money(trip_cost): + cheapest_cost = trip_cost + cheapest_shop = shop + + if cheapest_shop: + print(f"{customer.name}'s trip to the {cheapest_shop.name} costs {cheapest_cost:.2f}\n") + customer.travel_to_shop(cheapest_shop) + cheapest_shop.sell_products(customer) + customer.travel_home(trip_cost) + print(f"{customer.name} rides to {cheapest_shop.name}\n") + else: + print(f"{customer.name} doesn't have enough money to make a purchase in any shop\n") + + +if __name__ == "main": + shop_trip() + + + diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..31991994 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,19 @@ +import datetime + + +class Shop: + def __init__(self, data): + self.name = data["name"] + self.location = data["location"] + self.products = data["products"] + + def sell_products(self, customer): + print(f"\nDate: {datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n") + print(f"Thanks, {customer.name}, for your purchase!\n") + total_cost = 0 + for product, quantity in customer.product_cart.items(): + product_cost = quantity * self.products[product] + print(f"You have bought: {quantity} {product}s for {product_cost} dollars\n") + total_cost += product_cost + print(f"Total cost is {total_cost} dollars\n") + print("See you again!\n") \ No newline at end of file From 7a05deae592b2d1371ceddec2dc9b3460f1646a9 Mon Sep 17 00:00:00 2001 From: Nadiia Date: Wed, 11 Oct 2023 17:38:09 +0300 Subject: [PATCH 2/4] Solution-two-py-shop-trip --- app/car.py | 9 +++++++-- app/customer.py | 29 +++++++++++++++++++---------- app/main.py | 23 +++++++++++++++-------- app/shop.py | 17 ++++++++++++----- 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/app/car.py b/app/car.py index 48b66a18..90305382 100644 --- a/app/car.py +++ b/app/car.py @@ -1,4 +1,9 @@ +from typing import Dict, Any + + class Car: - def __init__(self, data): + FUEL_PRICE = 2.4 + + def __init__(self, data: Dict[str, Any]) -> None: self.brand = data["brand"] - self.fuel_consumption = data["fuel_consumption"] \ No newline at end of file + self.fuel_consumption = data["fuel_consumption"] diff --git a/app/customer.py b/app/customer.py index 060c001c..795a7b45 100644 --- a/app/customer.py +++ b/app/customer.py @@ -1,39 +1,48 @@ import math +from typing import Dict, Any, List + from app.car import Car +from app.shop import Shop class Customer: - FUEL_PRICE = 2.4 - def __init__(self, data): + def __init__(self, data: Dict[str, Any]) -> None: self.name = data["name"] self.product_cart = data["product_cart"] self.location = data["location"] self.money = data["money"] self.car = Car(data["car"]) - def calculate_trip_cost(self, shop): + def calculate_trip_cost(self, shop: Shop) -> float: distance_to_shop = self.calculate_distance(shop.location) fuel_consumption_per_km = self.car.fuel_consumption / 100 - fuel_cost_to_shop = (distance_to_shop * fuel_consumption_per_km) * self.FUEL_PRICE - product_cost = sum([self.product_cart[product] * shop.products[product] for product in self.product_cart]) + fuel_cost_to_shop = ( + distance_to_shop * fuel_consumption_per_km + ) * Car.FUEL_PRICE + product_cost = sum( + [ + self.product_cart[product] * shop.products[product] + for product in self.product_cart + ] + ) total_trip_cost = fuel_cost_to_shop + product_cost return round(total_trip_cost, 2) - def has_enough_money(self, trip_cost): + def has_enough_money(self, trip_cost: float) -> bool: return self.money >= trip_cost - def travel_to_shop(self, shop): + def travel_to_shop(self, shop: Shop) -> None: self.location = shop.location print(f"{self.name} rides to {shop.name}\n") - def travel_home(self, trip_cost): + def travel_home(self, trip_cost: float) -> None: print(f"{self.name} rides home\n") remaining_money = self.money - trip_cost print(f"{self.name} now has {round(remaining_money, 2)} dollars\n") - def calculate_distance(self, shop_location): + def calculate_distance(self, shop_location: List[int]) -> float: x_location = self.location[0] - shop_location[0] y_location = self.location[1] - shop_location[1] - distance = math.sqrt(x_location ** 2 + y_location ** 2) + distance = math.sqrt(x_location**2 + y_location**2) return distance diff --git a/app/main.py b/app/main.py index 7d713837..2d80ab3a 100644 --- a/app/main.py +++ b/app/main.py @@ -1,36 +1,43 @@ import json + from app.customer import Customer from app.shop import Shop -def shop_trip(): +def shop_trip() -> None: with open("app/config.json", "r") as config_file: config_data = json.load(config_file) for customer_data in config_data["customers"]: customer = Customer(customer_data) print(f"{customer.name} has {customer.money} dollars\n") cheapest_shop = None - cheapest_cost = float('inf') + cheapest_cost = float("inf") for shop_data in config_data["shops"]: shop = Shop(shop_data) trip_cost = customer.calculate_trip_cost(shop) - if trip_cost < cheapest_cost and customer.has_enough_money(trip_cost): + if trip_cost < cheapest_cost \ + and customer.has_enough_money(trip_cost): cheapest_cost = trip_cost cheapest_shop = shop if cheapest_shop: - print(f"{customer.name}'s trip to the {cheapest_shop.name} costs {cheapest_cost:.2f}\n") + print( + f"{customer.name}'s " + f"trip to the {cheapest_shop.name} " + f"costs {cheapest_cost:.2f}\n" + ) customer.travel_to_shop(cheapest_shop) cheapest_shop.sell_products(customer) customer.travel_home(trip_cost) print(f"{customer.name} rides to {cheapest_shop.name}\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 " + f"to make a purchase in any shop\n" + ) if __name__ == "main": shop_trip() - - - diff --git a/app/shop.py b/app/shop.py index 31991994..0fb2f605 100644 --- a/app/shop.py +++ b/app/shop.py @@ -1,19 +1,26 @@ import datetime +from typing import Dict, Any class Shop: - def __init__(self, data): + def __init__(self, data: Dict[str, Any]) -> None: self.name = data["name"] self.location = data["location"] self.products = data["products"] - def sell_products(self, customer): - print(f"\nDate: {datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n") + def sell_products(self, customer: dict[str, Any]) -> None: + print( + f"\nDate: " + f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n" + ) print(f"Thanks, {customer.name}, for your purchase!\n") total_cost = 0 for product, quantity in customer.product_cart.items(): product_cost = quantity * self.products[product] - print(f"You have bought: {quantity} {product}s for {product_cost} dollars\n") + print( + f"You have bought: {quantity} {product}s " + f"for {product_cost} dollars\n" + ) total_cost += product_cost print(f"Total cost is {total_cost} dollars\n") - print("See you again!\n") \ No newline at end of file + print("See you again!\n") From 2618c85abdf4caa7c074d6f9499acff01e23a733 Mon Sep 17 00:00:00 2001 From: Nadiia Date: Wed, 11 Oct 2023 18:08:11 +0300 Subject: [PATCH 3/4] Solution-three-py-shop-trip --- app/__init__.py | 0 app/car/__init__.py | 0 app/{ => car}/car.py | 0 app/customer/__init__.py | 0 app/{ => customer}/customer.py | 4 ++-- app/main.py | 4 ++-- app/shop/__init__.py | 0 app/{ => shop}/shop.py | 4 +++- 8 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 app/__init__.py create mode 100644 app/car/__init__.py rename app/{ => car}/car.py (100%) create mode 100644 app/customer/__init__.py rename app/{ => customer}/customer.py (93%) create mode 100644 app/shop/__init__.py rename app/{ => shop}/shop.py (86%) diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/car/__init__.py b/app/car/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/car.py b/app/car/car.py similarity index 100% rename from app/car.py rename to app/car/car.py diff --git a/app/customer/__init__.py b/app/customer/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/customer.py b/app/customer/customer.py similarity index 93% rename from app/customer.py rename to app/customer/customer.py index 795a7b45..e8cd2046 100644 --- a/app/customer.py +++ b/app/customer/customer.py @@ -1,8 +1,8 @@ import math from typing import Dict, Any, List -from app.car import Car -from app.shop import Shop +from app.car.car import Car +from app.shop.shop import Shop class Customer: diff --git a/app/main.py b/app/main.py index 2d80ab3a..6fb3a7e6 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,7 @@ import json -from app.customer import Customer -from app.shop import Shop +from app.customer.customer import Customer +from app.shop.shop import Shop def shop_trip() -> None: diff --git a/app/shop/__init__.py b/app/shop/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/shop.py b/app/shop/shop.py similarity index 86% rename from app/shop.py rename to app/shop/shop.py index 0fb2f605..00cc9f29 100644 --- a/app/shop.py +++ b/app/shop/shop.py @@ -1,6 +1,8 @@ import datetime from typing import Dict, Any +from app.customer.customer import Customer + class Shop: def __init__(self, data: Dict[str, Any]) -> None: @@ -8,7 +10,7 @@ def __init__(self, data: Dict[str, Any]) -> None: self.location = data["location"] self.products = data["products"] - def sell_products(self, customer: dict[str, Any]) -> None: + def sell_products(self, customer: Customer) -> None: print( f"\nDate: " f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n" From e9b8c25343eea49e095d582eceb72408de48efb2 Mon Sep 17 00:00:00 2001 From: Nadiia Date: Thu, 12 Oct 2023 17:18:42 +0300 Subject: [PATCH 4/4] Solution-four-py-shop-trip --- app/{car => }/car.py | 0 app/car/__init__.py | 0 app/{customer => }/customer.py | 32 ++++++++++++++++++++++++++++---- app/customer/__init__.py | 0 app/main.py | 4 ++-- app/shop/__init__.py | 0 app/shop/shop.py | 28 ---------------------------- 7 files changed, 30 insertions(+), 34 deletions(-) rename app/{car => }/car.py (100%) delete mode 100644 app/car/__init__.py rename app/{customer => }/customer.py (58%) delete mode 100644 app/customer/__init__.py delete mode 100644 app/shop/__init__.py delete mode 100644 app/shop/shop.py diff --git a/app/car/car.py b/app/car.py similarity index 100% rename from app/car/car.py rename to app/car.py diff --git a/app/car/__init__.py b/app/car/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/app/customer/customer.py b/app/customer.py similarity index 58% rename from app/customer/customer.py rename to app/customer.py index e8cd2046..4eca4bc6 100644 --- a/app/customer/customer.py +++ b/app/customer.py @@ -1,8 +1,8 @@ +import datetime import math from typing import Dict, Any, List -from app.car.car import Car -from app.shop.shop import Shop +from app.car import Car class Customer: @@ -14,7 +14,7 @@ def __init__(self, data: Dict[str, Any]) -> None: self.money = data["money"] self.car = Car(data["car"]) - def calculate_trip_cost(self, shop: Shop) -> float: + def calculate_trip_cost(self, shop: "Shop") -> float: distance_to_shop = self.calculate_distance(shop.location) fuel_consumption_per_km = self.car.fuel_consumption / 100 fuel_cost_to_shop = ( @@ -32,7 +32,7 @@ def calculate_trip_cost(self, shop: Shop) -> float: def has_enough_money(self, trip_cost: float) -> bool: return self.money >= trip_cost - def travel_to_shop(self, shop: Shop) -> None: + def travel_to_shop(self, shop: "Shop") -> None: self.location = shop.location print(f"{self.name} rides to {shop.name}\n") @@ -46,3 +46,27 @@ def calculate_distance(self, shop_location: List[int]) -> float: y_location = self.location[1] - shop_location[1] distance = math.sqrt(x_location**2 + y_location**2) return distance + + +class Shop: + def __init__(self, data: Dict[str, Any]) -> None: + self.name = data["name"] + self.location = data["location"] + self.products = data["products"] + + def sell_products(self, customer: Customer) -> None: + print( + f"\nDate: " + f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n" + ) + print(f"Thanks, {customer.name}, for your purchase!\n") + total_cost = 0 + for product, quantity in customer.product_cart.items(): + product_cost = quantity * self.products[product] + print( + f"You have bought: {quantity} {product}s " + f"for {product_cost} dollars\n" + ) + total_cost += product_cost + print(f"Total cost is {total_cost} dollars\n") + print("See you again!\n") diff --git a/app/customer/__init__.py b/app/customer/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/app/main.py b/app/main.py index 6fb3a7e6..b10b04e4 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,7 @@ import json -from app.customer.customer import Customer -from app.shop.shop import Shop +from app.customer import Customer +from app.customer import Shop def shop_trip() -> None: diff --git a/app/shop/__init__.py b/app/shop/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/app/shop/shop.py b/app/shop/shop.py deleted file mode 100644 index 00cc9f29..00000000 --- a/app/shop/shop.py +++ /dev/null @@ -1,28 +0,0 @@ -import datetime -from typing import Dict, Any - -from app.customer.customer import Customer - - -class Shop: - def __init__(self, data: Dict[str, Any]) -> None: - self.name = data["name"] - self.location = data["location"] - self.products = data["products"] - - def sell_products(self, customer: Customer) -> None: - print( - f"\nDate: " - f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n" - ) - print(f"Thanks, {customer.name}, for your purchase!\n") - total_cost = 0 - for product, quantity in customer.product_cart.items(): - product_cost = quantity * self.products[product] - print( - f"You have bought: {quantity} {product}s " - f"for {product_cost} dollars\n" - ) - total_cost += product_cost - print(f"Total cost is {total_cost} dollars\n") - print("See you again!\n")