Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Liliya committed Apr 18, 2024
1 parent 27ad07e commit f8c86e4
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 3 deletions.
23 changes: 23 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from math import sqrt


class Car:
def __init__(
self,
brand: str,
fuel_consumption: float
) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

def liters_needed(
self,
customer_location: list,
shop_location: list
) -> float:
distance_to_shop = (sqrt(
(shop_location[0] - customer_location[0]) ** 2
+ (shop_location[1] - customer_location[1]) ** 2)
* 2)
liters_needed = self.fuel_consumption / 100 * distance_to_shop
return liters_needed
25 changes: 25 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Customer:
def __init__(
self,
name: str,
products: dict,
location: list,
money: int
) -> None:
self.name = name
self.products = products
self.location = location
self.money = money

def money_amount(self) -> str:
return f"{self.name} has {self.money} dollars\n"

def cost_of_products_in_shop(self, shop: dict) -> float:
total_for_shop = 0
for product in self.products:
cost_of_product = self.products[product] * shop[product]
total_for_shop += cost_of_product
return total_for_shop

def change_location(self, shop_location: list) -> None:
self.location = shop_location
76 changes: 73 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,73 @@
def shop_trip():
# write your code here
pass
import os
import json

from app.customer import Customer
from app.shop import Shop
from app.car import Car


def shop_trip() -> None:

with open(os.getcwd() + "/app/config.json", "r") as file:
info = json.load(file)

buyers = info["customers"]
stores = info["shops"]
fuel_price = info["FUEL_PRICE"]
out = """"""
for buyer in buyers:
customer = Customer(
buyer["name"],
buyer["product_cart"],
buyer["location"],
buyer["money"]
)
car = Car(
brand=buyer["car"]["brand"],
fuel_consumption=buyer["car"]["fuel_consumption"]
)
out += customer.money_amount()
cost_in_all_shops = {}
for store in stores:
shop = Shop(
store["name"],
store["location"],
store["products"]
)
cost_in_one_shop = (
customer.cost_of_products_in_shop(shop.shop_products)
)
cost_of_fuel = (
car.liters_needed(customer.location, shop.shop_location)
* fuel_price
)
sum_shop_and_fuel_cost = round(cost_in_one_shop + cost_of_fuel, 2)
out += (f"{customer.name}'s trip to the {shop.shop_name} "
f"costs {sum_shop_and_fuel_cost}\n")
cost_in_all_shops[shop.shop_name] = sum_shop_and_fuel_cost
min_cost_key = min(
cost_in_all_shops, key=lambda k: cost_in_all_shops[k]
)
if customer.money > cost_in_all_shops[min_cost_key]:
out += f"{customer.name} rides to {min_cost_key}\n\n"
index_of_the_right_shop = 0
for index, store in enumerate(stores):
if store["name"] == min_cost_key:
index_of_the_right_shop = index

min_bill_shop = Shop(
shop_name=stores[index_of_the_right_shop]["name"],
shop_location=stores[index_of_the_right_shop]["location"],
shop_products=stores[index_of_the_right_shop]["products"]
)
out += min_bill_shop.bill(customer.name, customer.products)
customer.change_location(min_bill_shop.shop_location)
amount_left = customer.money - cost_in_all_shops[min_cost_key]
out += "\n"
out += f"{customer.name} rides home\n"
out += f"{customer.name} now has {amount_left} dollars\n\n"

else:
out += (f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop")
print(out)
35 changes: 35 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import datetime


class Shop:
def __init__(
self,
shop_name: str,
shop_location: list,
shop_products: dict
) -> None:
self.shop_name = shop_name
self.shop_location = shop_location
self.shop_products = shop_products

def bill(self, customer_name: str, products_needed: dict) -> str:
out = """"""
out += (f"Date: "
f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}"
f"\n")
out += f"Thanks, {customer_name}, for your purchase!\n"
out += "You have bought:\n"
total_amount = 0
for product in self.shop_products:
total_for_product = (
products_needed[product]
* self.shop_products[product]
)
total_amount += total_for_product
if str(total_for_product)[-2:] == ".0":
total_for_product = int(total_for_product)
out += (f"{products_needed[product]} {product}s "
f"for {total_for_product} dollars\n")
out += f"Total cost is {total_amount} dollars\n"
out += "See you again!\n"
return out

0 comments on commit f8c86e4

Please sign in to comment.