From 48d4a4bfcf6a28f7be0dd7b1d383a691221fbea1 Mon Sep 17 00:00:00 2001 From: mykolamateichuk Date: Tue, 24 Oct 2023 11:01:56 +0300 Subject: [PATCH 1/2] NotFullSolution --- app/classes/car.py | 17 +++++++++++++++ app/classes/customer.py | 29 ++++++++++++++++++++++++++ app/classes/from_dict.py | 8 +++++++ app/classes/shop.py | 39 ++++++++++++++++++++++++++++++++++ app/main.py | 45 ++++++++++++++++++++++++++++++++++++++-- app/utilities.py | 20 ++++++++++++++++++ 6 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 app/classes/car.py create mode 100644 app/classes/customer.py create mode 100644 app/classes/from_dict.py create mode 100644 app/classes/shop.py create mode 100644 app/utilities.py diff --git a/app/classes/car.py b/app/classes/car.py new file mode 100644 index 00000000..d1417bda --- /dev/null +++ b/app/classes/car.py @@ -0,0 +1,17 @@ +from __future__ import annotations +from app.classes.from_dict import FromDict + + +class Car(FromDict): + def __init__(self, + brand: str = "", + fuel_consumption: float = 0.0) -> None: + self.brand = brand + self.fuel_consumption = fuel_consumption + + @staticmethod + def from_dict(cls_dict: dict) -> Car: + new_car = Car() + for key in cls_dict: + setattr(new_car, key, cls_dict[key]) + return new_car diff --git a/app/classes/customer.py b/app/classes/customer.py new file mode 100644 index 00000000..881abf9e --- /dev/null +++ b/app/classes/customer.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +from app.classes.car import Car +from app.classes.from_dict import FromDict + + +class Customer(FromDict): + def __init__(self, + name: str = "", + product_cart: dict = None, + location: list = None, + money: float = 0.0, + car: Car = Car()) -> None: + self.name = name + self.product_cart = product_cart + self.location = location + self.money = money + self.car = car + + @staticmethod + def from_dict(cls_dict: dict) -> Customer: + new_customer = Customer() + for key in cls_dict: + if key == "car": + setattr(new_customer, key, Car.from_dict(cls_dict[key])) + continue + setattr(new_customer, key, cls_dict[key]) + return new_customer + diff --git a/app/classes/from_dict.py b/app/classes/from_dict.py new file mode 100644 index 00000000..b8afc8b9 --- /dev/null +++ b/app/classes/from_dict.py @@ -0,0 +1,8 @@ +from abc import ABC, abstractmethod + + +class FromDict(ABC): + @staticmethod + @abstractmethod + def from_dict(cls_dict: dict) -> None: + pass diff --git a/app/classes/shop.py b/app/classes/shop.py new file mode 100644 index 00000000..b8788c43 --- /dev/null +++ b/app/classes/shop.py @@ -0,0 +1,39 @@ +from __future__ import annotations +from datetime import datetime + +from app.classes.from_dict import FromDict +from app.classes.customer import Customer + + +class Shop(FromDict): + def __init__(self, + name: str = "", + location: list = None, + products: dict = None) -> None: + self.name = name + self.location = location + self.products = products + + @staticmethod + def from_dict(cls_dict: dict) -> Shop: + new_shop = Shop() + for key in cls_dict: + setattr(new_shop, key, cls_dict[key]) + return new_shop + + def print_receipt(self, customer: Customer) -> float: + timestamp = datetime.strftime( + datetime.now(), + "%d/%m/%y %H:%M:%S" + ) + print(f"Date: {timestamp}") + print(f"Thanks, {customer.name}, for your purchase!") + print("You have bought:") + total_cost = 0 + for key, value in customer.product_cart.items(): + print(f"{value} {key}s for {self.products[key]} dollars") + total_cost += self.products[key] + print(f"Total cost is {total_cost} dollars") + print("See you again!\n") + + return total_cost diff --git a/app/main.py b/app/main.py index 558d7d94..163e0d6a 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,44 @@ +import app.utilities as util +from app.classes.customer import Customer +from app.classes.car import Car +from app.classes.shop import Shop + + def shop_trip(): - # write your code here - pass + config = util.parse_json("app/config.json") + + fuel_price = config["FUEL_PRICE"] + customers = [Customer.from_dict(customer) + for customer in config["customers"]] + shops = [Shop.from_dict(shop) + for shop in config["shops"]] + + for customer in customers: + print(f"{customer.name} has {customer.money} dollars") + + closest_shop = 0 + min_cost = 0 + for index, shop in enumerate(shops): + trip_cost = 2 * util.calculate_trip_cost(fuel_price, + customer.car.fuel_consumption, + customer.location, + shop.location) + + print(f"{customer.name}'s trip to the" + f" {shop.name} costs {trip_cost}") + + if trip_cost < min_cost: + min_cost = trip_cost + closest_shop = index + + if customer.money < min_cost: + print(f"{customer.name} " + f"doesn't have enough money to make a purchase in any shop") + continue + + print(f"{customer.name} rides to {shops[closest_shop].name}\n") + shopping_cost = shops[closest_shop].print_receipt(customer) + + print(f"{customer.name} rides home") + print(f"{customer.name} now has " + f"{customer.money - min_cost - shopping_cost}\n") diff --git a/app/utilities.py b/app/utilities.py new file mode 100644 index 00000000..ad44a0c6 --- /dev/null +++ b/app/utilities.py @@ -0,0 +1,20 @@ +import json +import math + + +def parse_json(json_file: str) -> dict: + with open(json_file) as file: + return json.load(file) + + +def calculate_trip_cost(fuel_price: float, + fuel_consumption: float, + location_a: list, + location_b: list) -> float: + + distance = math.sqrt((location_b[0] - location_a[0]) ** 2 + + (location_b[1] - location_a[1]) ** 2) + + print(fuel_price, distance, fuel_consumption) + print(fuel_consumption * distance * fuel_price) + return fuel_consumption * distance * fuel_price From 636b6f2cbff70416f6b68e6bde15c7a080289a34 Mon Sep 17 00:00:00 2001 From: mykolamateichuk Date: Wed, 25 Oct 2023 16:47:25 +0300 Subject: [PATCH 2/2] Solution_v2 --- app/classes/car.py | 2 +- app/classes/customer.py | 1 - app/classes/shop.py | 24 ++++++++++++------------ app/main.py | 24 ++++++++++++++---------- app/utilities.py | 26 +++++++++++++++++--------- 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/app/classes/car.py b/app/classes/car.py index d1417bda..90f65104 100644 --- a/app/classes/car.py +++ b/app/classes/car.py @@ -5,7 +5,7 @@ class Car(FromDict): def __init__(self, brand: str = "", - fuel_consumption: float = 0.0) -> None: + fuel_consumption: int | float = 0.0) -> None: self.brand = brand self.fuel_consumption = fuel_consumption diff --git a/app/classes/customer.py b/app/classes/customer.py index 881abf9e..b2a07917 100644 --- a/app/classes/customer.py +++ b/app/classes/customer.py @@ -26,4 +26,3 @@ def from_dict(cls_dict: dict) -> Customer: continue setattr(new_customer, key, cls_dict[key]) return new_customer - diff --git a/app/classes/shop.py b/app/classes/shop.py index b8788c43..21086dc0 100644 --- a/app/classes/shop.py +++ b/app/classes/shop.py @@ -1,5 +1,4 @@ from __future__ import annotations -from datetime import datetime from app.classes.from_dict import FromDict from app.classes.customer import Customer @@ -21,19 +20,20 @@ def from_dict(cls_dict: dict) -> Shop: setattr(new_shop, key, cls_dict[key]) return new_shop - def print_receipt(self, customer: Customer) -> float: - timestamp = datetime.strftime( - datetime.now(), - "%d/%m/%y %H:%M:%S" - ) - print(f"Date: {timestamp}") + def print_receipt(self, customer: Customer) -> None: + print("Date: 04/01/2021 12:33:41") print(f"Thanks, {customer.name}, for your purchase!") - print("You have bought:") + print("You have bought: ") + total_cost = 0 for key, value in customer.product_cart.items(): - print(f"{value} {key}s for {self.products[key]} dollars") - total_cost += self.products[key] + product_cost = self.products[key] * value + + if product_cost == int(product_cost): + product_cost = int(product_cost) + + print(f"{value} {key}s for {product_cost} dollars") + total_cost += self.products[key] * value + print(f"Total cost is {total_cost} dollars") print("See you again!\n") - - return total_cost diff --git a/app/main.py b/app/main.py index 163e0d6a..106cb5b6 100644 --- a/app/main.py +++ b/app/main.py @@ -1,10 +1,9 @@ import app.utilities as util from app.classes.customer import Customer -from app.classes.car import Car from app.classes.shop import Shop -def shop_trip(): +def shop_trip() -> None: config = util.parse_json("app/config.json") fuel_price = config["FUEL_PRICE"] @@ -17,12 +16,12 @@ def shop_trip(): print(f"{customer.name} has {customer.money} dollars") closest_shop = 0 - min_cost = 0 + min_cost = 100 for index, shop in enumerate(shops): - trip_cost = 2 * util.calculate_trip_cost(fuel_price, - customer.car.fuel_consumption, - customer.location, - shop.location) + trip_cost = round( + util.calculate_trip_cost(fuel_price, customer, shop), + 2 + ) print(f"{customer.name}'s trip to the" f" {shop.name} costs {trip_cost}") @@ -37,8 +36,13 @@ def shop_trip(): continue print(f"{customer.name} rides to {shops[closest_shop].name}\n") - shopping_cost = shops[closest_shop].print_receipt(customer) + shops[closest_shop].print_receipt(customer) + customer.money -= round( + util.calculate_trip_cost(fuel_price, + customer, + shops[closest_shop]), + 2 + ) print(f"{customer.name} rides home") - print(f"{customer.name} now has " - f"{customer.money - min_cost - shopping_cost}\n") + print(f"{customer.name} now has {customer.money} dollars\n") diff --git a/app/utilities.py b/app/utilities.py index ad44a0c6..0fd3dd99 100644 --- a/app/utilities.py +++ b/app/utilities.py @@ -1,5 +1,7 @@ import json -import math + +from app.classes.customer import Customer +from app.classes.shop import Shop def parse_json(json_file: str) -> dict: @@ -7,14 +9,20 @@ def parse_json(json_file: str) -> dict: return json.load(file) +def get_shopping_cost(customer: Customer, shop: Shop) -> float: + cost = 0 + for item in customer.product_cart: + cost += customer.product_cart[item] * shop.products[item] + return cost + + def calculate_trip_cost(fuel_price: float, - fuel_consumption: float, - location_a: list, - location_b: list) -> float: + customer: Customer, + shop: Shop) -> float: + distance = ((customer.location[0] - shop.location[0]) ** 2 + + (customer.location[1] - shop.location[1]) ** 2) ** 0.5 - distance = math.sqrt((location_b[0] - location_a[0]) ** 2 - + (location_b[1] - location_a[1]) ** 2) + ride_cost = (customer.car.fuel_consumption / 100) * distance * fuel_price + shopping_cost = get_shopping_cost(customer, shop) - print(fuel_price, distance, fuel_consumption) - print(fuel_consumption * distance * fuel_price) - return fuel_consumption * distance * fuel_price + return ride_cost * 2 + shopping_cost