diff --git a/app/car.py b/app/car.py new file mode 100644 index 00000000..171271b4 --- /dev/null +++ b/app/car.py @@ -0,0 +1,7 @@ +class Car: + def __init__(self, brand: str, fuel_consumption: float) -> None: + self.brand = brand + self.fuel_consumption = fuel_consumption + + def spent_liters_of_fuel(self, distance: float) -> float: + return self.fuel_consumption * distance / 100 diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..9f1f8f7c --- /dev/null +++ b/app/customer.py @@ -0,0 +1,29 @@ +import math + +from app.car import Car + + +class Customer: + def __init__( + self, + name: str, + product_cart: dict, + location: list[int], + money: int, + car: Car + ) -> None: + self.name = name + self.product_cart = product_cart + self.location = location + self.money = money + self.car = car + self.__home_location = location + + def initial_money(self) -> None: + print(f"{self.name} has {self.money} dollars") + + def calculate_way_price(self, shop_location: list[int], + fuel_price: float) -> float: + distance = math.dist(self.location, shop_location) + road_price = self.car.spent_liters_of_fuel(distance) * 2 * fuel_price + return road_price diff --git a/app/main.py b/app/main.py index 558d7d94..a6865ddd 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,51 @@ -def shop_trip(): - # write your code here - pass +import json + +from app.car import Car +from app.customer import Customer +from app.shop import Shop + + +def shop_trip() -> None: + with open("app/config.json", "r") as config_file: + config = json.load(config_file) + fuel_price = config["FUEL_PRICE"] + customers = [ + Customer( + customer["name"], + customer["product_cart"], + customer["location"], + customer["money"], + Car(**customer["car"]) + ) + for customer in config["customers"] + ] + shops = [ + Shop(**shop) + for shop in config["shops"] + ] + + for customer in customers: + customer.initial_money() + price_shop_list = {} + for shop in shops: + price_for_trip = customer.calculate_way_price( + shop.location, fuel_price + ) + shop.calculate_product_price(customer) + print(f"{customer.name}'s trip to the {shop.name} " + f"costs {round(price_for_trip, 2)}") + price_shop_list[price_for_trip] = shop + min_price = min(price_shop_list) + cheapest_shop = price_shop_list[min_price] + + if customer.money >= min_price: + print(f"{customer.name} rides to " + f"{cheapest_shop.name}\n") + home_location = customer.location + customer.location = cheapest_shop.location + cheapest_shop.print_receipt(customer) + print(f"{customer.name} rides home\n{customer.name} now has " + f"{round(customer.money - min_price, 2)} dollars\n") + customer.location = home_location + else: + print(f"{customer.name} doesn't have enough money " + f"to make a purchase in any shop") diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..97326460 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,36 @@ +import datetime + +from app.customer import Customer + + +class Shop: + def __init__( + self, + name: str, + location: list[int], + products: dict + ) -> None: + self.name = name + self.location = location + self.products = products + + def calculate_product_price(self, customer: Customer) -> float: + full_price = 0 + for product, amount in customer.product_cart.items(): + full_price += self.products[product] * amount + return full_price + + def print_receipt(self, customer: Customer) -> None: + now = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S") + print(f"Date: {now}\n" + f"Thanks, {customer.name}, for your purchase!\n" + "You have bought:") + for product, amount in customer.product_cart.items(): + price = self.products[product] * amount + print( + f"{amount} {product}s for " + f"{int(price) if price == int(price) else price} dollars" + ) + print(f"Total cost is " + f"{self.calculate_product_price(customer)} dollars\n" + f"See you again!\n")