Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlenko committed Oct 31, 2024
1 parent 27ad07e commit 1a61346
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class Car:

brand: str
fuel_consumption: float
46 changes: 46 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from __future__ import annotations
from dataclasses import dataclass
from math import sqrt
from app.car import Car
from app.shop import Shop


@dataclass
class Customer:

name: str
product_cart: dict
location: list[int]
money: int | float
car: Car

def calculate_distance(self, shop: Shop) -> int | float:
x1, y1 = (self.location[0], self.location[1])
x2, y2 = (shop.location[0], shop.location[1])

return sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))

def calculate_travel_cost(
self,
fuel_price: float,
shop: Shop
) -> int | float:

l_per_km = self.car.fuel_consumption / 100
distance = self.calculate_distance(shop)
return round(l_per_km * distance * fuel_price * 2, 2)

def calculate_cart_cost(self, shop: Shop) -> int | float:
cart_cost = 0
for item in self.product_cart:
cart_cost += self.product_cart[item] * shop.products[item]
return cart_cost

def calculate_total_cost(
self,
fuel_price: float,
shop: Shop
) -> int | float:
travel_cost = self.calculate_travel_cost(fuel_price, shop)
cart_cost = self.calculate_cart_cost(shop)
return travel_cost + cart_cost
39 changes: 39 additions & 0 deletions app/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from app.customer import Customer
from app.car import Car
from app.shop import Shop
import json


def process_json_data() -> list:
customers = []
shops = []

with open("app/config.json") as file:
data = json.load(file)

fuel_price = data["FUEL_PRICE"]

for customer in data["customers"]:
customers.append(
Customer(
customer["name"],
customer["product_cart"],
customer["location"],
customer["money"],
Car(
customer["car"]["brand"],
customer["car"]["fuel_consumption"]
)
)
)

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

return [fuel_price, customers, shops]
55 changes: 52 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
def shop_trip():
# write your code here
pass
from __future__ import annotations
from app.data import process_json_data
import datetime


def shop_trip() -> None:
data = process_json_data()

fuel_price = data[0]
customers = data[1]
shops = data[2]

for customer in customers:
print(f"{customer.name} has {customer.money} dollars")

cheapest_shop = [None, 0]
for shop in shops:
total_cost = customer.calculate_total_cost(fuel_price, shop)
if total_cost < cheapest_shop[1] or cheapest_shop[1] == 0:
cheapest_shop[0] = shop
cheapest_shop[1] = total_cost
print(f"{customer.name}'s trip to "
f"the {shop.name} costs {total_cost}")

if customer.money > cheapest_shop[1]:
print(f"{customer.name} rides to {cheapest_shop[0].name}")
print(
"\nDate:",
datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
)
print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought:")

total_products_cost = 0
for key, value in customer.product_cart.items():
cost_per_product = (
customer.product_cart[key] * cheapest_shop[0].products[key]
)
if cost_per_product.is_integer():
cost_per_product = int(cost_per_product)
total_products_cost += cost_per_product
print(f"{value} {key}s for {cost_per_product} dollars")

print(f"Total cost is {total_products_cost} dollars")
print("See you again!\n")

print(f"{customer.name} rides home")
print(f"{customer.name} now has "
f"{customer.money - cheapest_shop[1]} dollars\n")
else:
print(f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop")
9 changes: 9 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass


@dataclass
class Shop:

name: str
location: list
products: dict

0 comments on commit 1a61346

Please sign in to comment.