diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/car.py b/app/car.py new file mode 100644 index 00000000..60be2627 --- /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 gas_for_distance(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..db2e664c --- /dev/null +++ b/app/customer.py @@ -0,0 +1,30 @@ +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.car = car + self.name = name + self.product_cart = product_cart + self.location = location + self.money = money + self.__home_location = location + + def initial_money(self) -> None: + print(f"{self.name} has {self.money} dollars") + + def price_way( + self, + shop_location: list[int], + fuel_price: float) -> float: + distance = math.dist(self.location, shop_location) + road_price = self.car.gas_for_distance(distance) * 2 * fuel_price + return road_price diff --git a/app/main.py b/app/main.py index 558d7d94..ed7778ef 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,55 @@ -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 file: + config = json.load(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 = {} + for shop in shops: + price_trip = customer.price_way( + shop.location, fuel_price + ) + shop.calculate_product_price(customer) + print(f"{customer.name}'s " + f"trip to the {shop.name} " + f"costs {round(price_trip, 2)}") + price_shop[price_trip] = shop + min_price = min(price_shop) + cheap_shop = price_shop[min_price] + + if customer.money >= min_price: + print(f"{customer.name} rides to {cheap_shop.name}\n") + home = customer.location + customer.location = cheap_shop.location + cheap_shop.print_check(customer) + print(f"{customer.name} rides home") + print(f"{customer.name} now has " + f"{round(customer.money - min_price, 2)}" + f" dollars\n") + customer.location = home + else: + print(f"{customer.name} " + f"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..0b30e54d --- /dev/null +++ b/app/shop.py @@ -0,0 +1,32 @@ +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_check(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" + f" for {int(price) if price == int(price) else price}" + f" dollars" + ) + print(f"Total cost is" + f" {self.calculate_product_price(customer)} dollars") + print("See you again!\n")