diff --git a/app/car.py b/app/car.py new file mode 100644 index 00000000..9e1aee3f --- /dev/null +++ b/app/car.py @@ -0,0 +1,4 @@ +class Car: + def __init__(self, brand: str, fuel_consumption: float) -> None: + self.brand = brand + self.fuel_consumption = fuel_consumption diff --git a/app/customer.py b/app/customer.py new file mode 100644 index 00000000..ec0722c6 --- /dev/null +++ b/app/customer.py @@ -0,0 +1,45 @@ +import math +from datetime import datetime +from app.car import Car + + +class Customer: + def __init__( + self, name: str, product_cart: dict, + location: list, money: float, car: Car + ) -> None: + self.name = name + self.product_cart = product_cart + self.location = location + self.money = money + self.car = car + + def distance_to(self, shop_location: dict) -> float: + return math.dist(self.location, shop_location) + + def travel_cost(self, distance: float, fuel_price: float) -> float: + return (distance * self.car.fuel_consumption / 100) * fuel_price + + def purchase_cost(self, shop_products: dict) -> float: + total_cost = 0 + for product, quantity in self.product_cart.items(): + total_cost += shop_products[product] * quantity + return total_cost + + def print_receipt(self, shop_name: str, shop_products: dict) -> None: + + datetime_str = datetime( + 2021, 4, 1, 12, 33, 41 + ).strftime("%m/%d/%Y %H:%M:%S") + + print(f"Date: {datetime_str}\n" + f"Thanks, {self.name}, for your purchase!\n" + f"You have bought:") + total_cost = 0 + for product, quantity in self.product_cart.items(): + cost = shop_products.get(product, 0) * quantity + total_cost += cost + formatted_cost = f"{cost: .2f}".rstrip("0").rstrip(".") + print(f"{quantity} {product}s for{formatted_cost} dollars") + print(f"Total cost is {total_cost} dollars\n" + f"See you again!\n") diff --git a/app/main.py b/app/main.py index 558d7d94..6d5c488b 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") as file: + config = json.load(file) + + fuel_price = config["FUEL_PRICE"] + + customers = [ + Customer( + config_customer["name"], + config_customer["product_cart"], + config_customer["location"], + config_customer["money"], + Car(**config_customer["car"]) + ) + for config_customer in config["customers"] + ] + + shops = [Shop(**config_shop) for config_shop in config["shops"]] + for customer in customers: + print(f"{customer.name} has {customer.money} dollars") + trips = [] + + for shop in shops: + distance_to_shop = customer.distance_to(shop.location) + cost_to_shop = customer.travel_cost(distance_to_shop, fuel_price) + purchase_cost = customer.purchase_cost(shop.products) + total_trip_cost = 2 * cost_to_shop + purchase_cost + trips.append((total_trip_cost, shop)) + + print(f"{customer.name}'s trip to the {shop.name} " + f"costs{total_trip_cost: .2f}") + + trips.sort(key=lambda x: x[0]) + cheapest_trip = trips[0] + total_cost, chosen_shop = cheapest_trip + + if total_cost <= customer.money: + print(f"{customer.name} rides to {chosen_shop.name}\n") + customer.print_receipt(chosen_shop.name, chosen_shop.products) + customer.money -= total_cost + print(f"{customer.name} rides home\n" + f"{customer.name} now has{customer.money: .2f} dollars\n") + + 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..673d1be7 --- /dev/null +++ b/app/shop.py @@ -0,0 +1,5 @@ +class Shop: + def __init__(self, name: str, location: list, products: dict) -> None: + self.name = name + self.location = location + self.products = products