Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewChugreev committed Oct 27, 2024
1 parent 27ad07e commit 94b9fed
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Car:
def __init__(self, brand: str, fuel_consumption: float) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

def calculate_fuel_cost(self, distance: float, full_price: float) -> float:
return (self.fuel_consumption / 100) * distance * full_price
28 changes: 28 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from app.car import Car
from decimal import Decimal
import math


class Customer():
def __init__(
self,
name: str,
product_cart: dict[str, int],
location: list[int],
money: Decimal,
car: Car
) -> None:
self.name = name
self.product_cart = product_cart
self.location = location
self.money = money
self.car = car

def distance(self, shop_location: list) -> float:
return math.sqrt(
(self.location[0] - shop_location[0]) ** 2
+ (self.location[1] - shop_location[1]) ** 2)

def ride_home(self, spend_money: float) -> None:
print(f"{self.name} rides home")
print(f"{self.name} now has {self.money - spend_money} dollars\n")
64 changes: 61 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
def shop_trip():
# write your code here
pass
import json
import os
from app.customer import Customer
from app.shop import Shop
from app.car import Car


def shop_trip() -> None:
config_path = os.path.join(os.path.dirname(__file__), "config.json")
with open(config_path, "r") as file:
data = json.load(file)
fuel_price, customers_, shops_ = data.values()

customers = []
for custom in customers_:
car = Car(**custom["car"])
customer = Customer(
name=custom["name"],
product_cart=custom["product_cart"],
location=custom["location"],
money=custom["money"],
car=car
)
customers.append(customer)

shops = []
for some_shop in shops_:
shop = Shop(
name=some_shop["name"],
location=some_shop["location"],
products=some_shop["products"]
)
shops.append(shop)

for customer in customers:
customer_money = customer.money
print(f"{customer.name} has {customer_money} dollars")
trip_cost_dict = {}
for shop in shops:
total = 0
dist_for_shop = customer.distance(shop.location)
fuel_for_shop = customer.car.calculate_fuel_cost(
dist_for_shop,
fuel_price
)
products_cost = shop.products_cost(customer)
total += round((products_cost + fuel_for_shop * 2), 2)
trip_cost_dict[total] = shop
print(f"{customer.name}'s trip to the {shop.name} costs {total}")
if customer.money < min(trip_cost_dict):
print(f"{customer.name} doesn't have enough"
f" money to make a purchase in any shop")
else:
total = min(trip_cost_dict)
current_shop = trip_cost_dict[min(trip_cost_dict)]
print(f"{customer.name} rides to {current_shop.name}\n")
current_shop.print_check(customer)
customer.ride_home(total)


shop_trip()
45 changes: 45 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from app.customer import Customer
from datetime import datetime


class Shop:
def __init__(
self,
name: str,
location: list[int],
products: dict[str, float]
) -> None:
self.name = name
self.location = location
self.products = products

def products_cost(self, customer: Customer) -> float:
product_cart = customer.product_cart
product_cost = 0
for product, qty in product_cart.items():
product_cost += qty * self.products.get(product)
return product_cost

def print_check(self, customer: Customer) -> None:
current_datatime = datetime(2021, 1, 4, 12, 33, 41)
current_datatime = current_datatime.strftime("%d/%m/%Y %H:%M:%S")
print(f"Date: {current_datatime}")
print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought:")
milk = customer.product_cart.get("milk")
print(f"{milk} "
f"milks for "
f"{milk * self.products.get('milk')}"
f" dollars")
bread = customer.product_cart.get("bread")
print(f"{bread} "
f"breads for "
f"{round(bread * self.products.get('bread'))}"
f" dollars")
butter = customer.product_cart.get("butter")
print(f"{butter}"
f" butters for "
f"{butter * self.products.get('butter')}"
f" dollars")
print(f"Total cost is {self.products_cost(customer)} dollars")
print("See you again!\n")

0 comments on commit 94b9fed

Please sign in to comment.