diff --git a/app/car.py b/app/car.py new file mode 100644 index 00000000..6dd018cc --- /dev/null +++ b/app/car.py @@ -0,0 +1,11 @@ +from dataclasses import dataclass + + +@dataclass +class Car: + brand: str + fuel_consumption: float + + def calculate_trip_cost(self, distance: float, fuel_price: float) -> float: + fuel_needed = (distance / 100) * self.fuel_consumption + return round(fuel_needed * fuel_price, 2) diff --git a/app/config.py b/app/config.py new file mode 100644 index 00000000..7ba38528 --- /dev/null +++ b/app/config.py @@ -0,0 +1,6 @@ +import json + + +def load_config(filename: str) -> dict: + with open(f"app/{filename}", "r") as file: + return json.load(file) diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..30336a5d --- /dev/null +++ b/app/customer.py @@ -0,0 +1,41 @@ +from dataclasses import dataclass +from typing import List, Dict +from app.car import Car +from app.shop import Shop + + +@dataclass +class Customer: + name: str + product_cart: Dict[str, int] + home_location: List[float] + current_location: List[float] + money: float + car: Car + + def calculate_trip_cost(self, shop: Shop, fuel_price: float) -> float: + distance = shop.calculate_distance(self.current_location) + return self.car.calculate_trip_cost(distance * 2, fuel_price) + + def calculate_products_price(self, shop: Shop) -> float | int: + return sum(shop.get_product_price(product) + * quantity for product, quantity + in self.product_cart.items()) + + def get_full_price(self, shop: Shop, fuel_price: float) -> float: + trip_cost = self.calculate_trip_cost(shop, fuel_price) + products_cost = self.calculate_products_price(shop) + return trip_cost + products_cost + + def can_afford_trip(self, shop: Shop, fuel_price: float) -> bool: + return self.money >= self.get_full_price(shop, fuel_price) + + def make_purchase(self, shop: Shop, fuel_price: float) -> bool: + if self.can_afford_trip(shop, fuel_price): + self.money -= self.get_full_price(shop, fuel_price) + self.current_location = shop.location + return True + return False + + def go_back(self) -> None: + self.current_location = self.home_location diff --git a/app/main.py b/app/main.py index 558d7d94..7ded2219 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,65 @@ -def shop_trip(): - # write your code here - pass +from app.config import load_config +from app.car import Car +from app.shop import Shop +from app.customer import Customer +from datetime import datetime + + +def shop_trip() -> None: + config = load_config("config.json") + fuel_price = config["FUEL_PRICE"] + shops = [Shop(**shop) for shop + in config["shops"]] + customers = [Customer(customer["name"], + customer["product_cart"], + customer["location"], + customer["location"], + customer["money"], + Car(customer["car"]["brand"], + customer["car"]["fuel_consumption"])) + for customer in config["customers"]] + for customer in customers: + min_price = float("inf") + best_shop = "" + print(f"{customer.name} has {customer.money} dollars") + for shop in shops: + price = customer.get_full_price(shop, fuel_price) + print(f"{customer.name}'s trip to the {shop.name} costs {price}") + + if price < min_price: + min_price = price + best_shop = shop + + if not customer.can_afford_trip(best_shop, fuel_price): + print(f"{customer.name} doesn't have enough money " + f"to make a purchase in any shop") + continue + + print(f"{customer.name} rides to {best_shop.name}\n") + customer.make_purchase(best_shop, fuel_price) + date = (datetime( + 2021, 4, 1, 12, 33, 41 + ).strftime("%m/%d/%Y %H:%M:%S")) + print( + f"Date: {date}\n" + f"Thanks, {customer.name}, for your purchase!\n" + f"You have bought:" + ) + + total_price = 0 + for product_name, product_count in customer.product_cart.items(): + product_account_price = ( + product_count * best_shop.get_product_price(product_name) + ) + total_price += product_account_price + print(f"{product_count} {product_name}s " + f"for{product_account_price: .2f}".rstrip("0").rstrip(".") + + " dollars") + + print(f"Total cost is {total_price} dollars\nSee you again!\n") + customer.go_back() + print(f"{customer.name} rides home\n" + f"{customer.name} now has {customer.money} dollars\n") + + +shop_trip() diff --git a/app/shop.py b/app/shop.py new file mode 100644 index 00000000..9b96c3c8 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,16 @@ +from dataclasses import dataclass +from typing import List, Dict +from math import dist + + +@dataclass +class Shop: + name: str + location: List[float] + products: Dict[str, float] + + def calculate_distance(self, customer_location: List[float]) -> float: + return dist(self.location, customer_location) + + def get_product_price(self, product: str) -> float | int: + return self.products.get(product)