diff --git a/app/car.py b/app/car.py new file mode 100644 index 00000000..8fe27d52 --- /dev/null +++ b/app/car.py @@ -0,0 +1,14 @@ +from dataclasses import dataclass + + +@dataclass +class Car: + brand: str + fuel_consumption: int | float + + def fuel_cost( + self, + distance: int | float, + fuel_price: int | float + ) -> float: + return (distance / 100) * self.fuel_consumption * fuel_price diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..d245b087 --- /dev/null +++ b/app/customer.py @@ -0,0 +1,21 @@ +from typing import List +from dataclasses import dataclass +from app.car import Car + + +@dataclass +class Customer: + name: str + product_cart: dict + location: List[int] + money: int | float + car: Car + + def money_start(self) -> None: + print(f"{self.name} has {self.money} dollars") + + def ride_to_home(self) -> None: + print(f"{self.name} rides home") + + def money_end(self) -> None: + print(f"{self.name} now has {self.money} dollars\n") diff --git a/app/main.py b/app/main.py index 558d7d94..0e17dc70 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,55 @@ -def shop_trip(): - # write your code here - pass +import json +import os + +from app.car import Car +from app.customer import Customer +from app.shop import Shop + + +def shop_trip() -> None: + current_directory = os.getcwd() + configuration_file = "app/config.json" + file_path = os.path.join(current_directory, configuration_file) + with open(file_path, "r") as file: + data = json.load(file) + fuel_price = data["FUEL_PRICE"] + customers = data["customers"] + customers_instances = [ + Customer(customer["name"], + customer["product_cart"], + customer["location"], + customer["money"], + Car(customer["car"]["brand"], + customer["car"]["fuel_consumption"], + )) for customer in customers + ] + + shops = data["shops"] + shop_instances = [ + Shop(shop["name"], + shop["location"], + shop["products"] + ) for shop in shops + ] + + for customer in customers_instances: + customer.money_start() + shops_cost = {} + for shop in shop_instances: + products_cost = shop.products_cost(customer) + fuel_cost = customer.car.fuel_cost(shop.distance(customer), + fuel_price) + trip_cost = round(products_cost + fuel_cost * 2, 2) + print(f"{customer.name}'s trip to the " + f"{shop.name} costs {trip_cost}") + shops_cost.update({trip_cost: shop}) + min_shop = shops_cost[min(shops_cost)] + if customer.money >= min(shops_cost): + min_shop.ride_to_shop(customer) + min_shop.print_receipt(customer) + customer.ride_to_home() + customer.money -= min(shops_cost) + customer.money_end() + else: + print(f"{customer.name} doesn't have enough " + f"money to make a purchase in any shop") diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..b768f8bd --- /dev/null +++ b/app/shop.py @@ -0,0 +1,41 @@ +import math +import datetime + +from dataclasses import dataclass +from typing import List +from app.customer import Customer + + +def format_num(num: int | float) -> int | float: + float_num = float(round(num, 2)) + return int(float_num) if float_num.is_integer() else float_num + + +@dataclass +class Shop: + name: str + location: List[int] + products: dict + + def distance(self, customer: Customer) -> float: + return math.dist(self.location, customer.location) + + def ride_to_shop(self, customer: Customer) -> None: + print(f"{customer.name} rides to {self.name}\n") + + def products_cost(self, customer: Customer) -> int | float: + return sum(price * customer.product_cart[prod] + for prod, price in self.products.items()) + + def print_receipt(self, customer: Customer) -> None: + total = 0 + date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S") + print(f"Date: {date}") + print(f"Thanks, {customer.name}, for your purchase!") + print("You have bought: ") + for prod, quantity in customer.product_cart.items(): + price = quantity * self.products[prod] + total += price + print(f"{quantity} {prod}s for {format_num(price)} dollars") + print(f"Total cost is {total} dollars\n" + f"See you again!\n")