Skip to content

Commit

Permalink
'Solution'
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii-Haponiuk committed Jun 12, 2024
1 parent 27ad07e commit a6f82a1
Show file tree
Hide file tree
Showing 4 changed files with 116 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 @@
from dataclasses import dataclass


@dataclass
class Car:
brand: str
fuel_consumption: float
60 changes: 60 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from app.car import Car
from app.shop import Shop
import datetime


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

def __str__(self) -> str:
return (f"Customer(name={self.name}, "
f"product_cart={self.product_cart}, "
f"location={self.location}, "
f"money={self.money}, car={self.car})")

def calculate_distance(self, other_location: list) -> float:
return ((self.location[0] - other_location[0]) ** 2
+ (self.location[1] - other_location[1]) ** 2) ** 0.5

def cost_of_products(self, price: dict) -> float:
return sum(self.product_cart[product] * price[product]
for product in self.product_cart)

def calculate_trip_expenses(
self,
shop: Shop,
fuel_price: float | int
) -> float:
return round(self.calculate_distance(shop.location)
* self.car.fuel_consumption * fuel_price * 2 / 100
+ self.cost_of_products(shop.products), 2)

def shopping(self, shop: Shop) -> None:
formatted_date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print()
print(f"Date: {formatted_date}")
print(f"Thanks, {self.name}, for your purchase!")
print("You have bought:")
spending_on_products = 0
for product in self.product_cart:
purchase = self.product_cart[product] * shop.products[product]
str_purchase = int(purchase) \
if purchase == int(purchase) \
else purchase
spending_on_products += purchase
print(f"{self.product_cart[product]} {product}s for "
f"{str_purchase} dollars")
print(f"Total cost is {spending_on_products} dollars")
print("See you again!\n")
44 changes: 41 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
def shop_trip():
# write your code here
pass
import json
from app.customer import Customer
from app.shop import Shop


def shop_trip() -> None:
with open("app/config.json", "r") as file:
data = json.load(file)
fuel_price, customers, shops = data.values()
people = [Customer(*customer.values()) for customer in customers]
citi_shops = [Shop(*shop.values()) for shop in shops]

for person in people:
print(f"{person.name} has {person.money} dollars")
total_costs = []
for shop in citi_shops:
total_costs.append(
(shop, person.calculate_trip_expenses(
shop, fuel_price)
)
)
print(f"{person.name}'s trip to the {total_costs[-1][0].name}"
f" costs {total_costs[-1][1]}")
cheapest_shop, spending = min(total_costs, key=lambda x: x[1])

if person.money < spending:
print(f"{person.name} doesn't have enough money"
f" to make a purchase in any shop")
continue

print(f"{person.name} rides to {cheapest_shop.name}")
person.shopping(cheapest_shop)
person.money = person.money - spending

print(f"{person.name} rides home")
print(f"{person.name} now has {person.money} dollars")
print()


if __name__ == "__main__":
shop_trip()
8 changes: 8 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class Shop:
name: str
location: list
products: dict

0 comments on commit a6f82a1

Please sign in to comment.