Skip to content

Commit

Permalink
needs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
VBorovskiy committed Oct 2, 2023
1 parent a3f12af commit b947658
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 61 deletions.
32 changes: 0 additions & 32 deletions app/Classes/__init__.py

This file was deleted.

29 changes: 29 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import os
from app.customer import Customer
from app.shop import Shop


def get_data(file_name: str) -> tuple | None:
current_dir = os.path.dirname(__file__)
file_path = os.path.join(current_dir, file_name)
try:
with open(file_path, "r") as file:
content = json.load(file)
except FileNotFoundError:
print(current_dir)
print("File not found!")
return

if all(key in content for key in ("FUEL_PRICE", "customers", "shops")):
customer_list = []
shop_list = []
fuel_price = content["FUEL_PRICE"]

for client in content["customers"]:
if len(client) != 0:
customer_list.append(Customer(**client))
for store in content["shops"]:
if len(store) != 0:
shop_list.append(Shop(**store))
return customer_list, shop_list, fuel_price,
File renamed without changes.
25 changes: 12 additions & 13 deletions app/Classes/customer.py → app/customer.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
import math
from dataclasses import dataclass
from app.Classes.car import Car
from app.Classes.shop import Shop
from app.car import Car
from app.shop import Shop


@dataclass
class Customer:
name: str
product_cart: dict
location: list
money: int
money: float
car: Car

def __post_init__(self) -> None:
self.car = Car(**self.car)

def calculate_distance(self, shop: Shop) -> float:
def calculate_distance(self, shop: Shop) -> int:
x1, y1 = self.location
return (math.sqrt(
return int(math.sqrt(
(shop.location[0] - x1) ** 2 + (shop.location[1] - y1) ** 2))

def calculate_trip_cost(self, shops: list[Shop],
fuel_price: float) -> tuple:
print(f"{self.name} has {self.money} dollars")
trip_costs = dict()
for shop in shops:
if all(product in shop.products
for product in self.product_cart.keys()):
products_price = sum(amount * shop.products[product]
for product, amount in
self.product_cart.items())
trip_costs[shop] = (fuel_price * self.car.fuel_consumption
* self.calculate_distance(shop)
+ products_price)
for product in self.product_cart):
road_cost = round(fuel_price * self.car.fuel_consumption *
self.calculate_distance(shop) / 100, 2)
trip_costs[shop] = round(road_cost
+ shop.calculate_price(self), 2)
print(f"{self.name}'s trip to the {shop.name}"
f" costs {trip_costs[shop]}")
else:
Expand All @@ -41,8 +40,8 @@ def calculate_trip_cost(self, shops: list[Shop],

def trip_to_cheapest(self, shop: Shop, total_cost: float) -> bool:
if self.money >= total_cost:
self.location = shop.location
self.money -= shop.buy_products(self)
self.location = shop.location
print(f"{self.name} rides home\n"
"=========================================================")
return True
Expand Down
15 changes: 6 additions & 9 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import Classes
from Classes import CUSTOMER_LIST, SHOP_LIST, FUEL_PRICE
from app import get_data


def shop_trip() -> None:
Classes.get_data("config.json")
for customer in CUSTOMER_LIST:
cheapest_shop, cost = customer.calculate_trip_cost(SHOP_LIST,
FUEL_PRICE)
customer_list, shop_list, fuel_price = get_data("config.json")
for customer in customer_list:
cheapest_shop, cost = customer.calculate_trip_cost(shop_list,
fuel_price)
customer.trip_to_cheapest(cheapest_shop, cost)


shop_trip()
shop_trip()
20 changes: 13 additions & 7 deletions app/Classes/shop.py → app/shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ class Shop:
def __hash__(self) -> int:
return hash(self.name)

def buy_products(self, customer: Customer) -> float:
def calculate_price(self, customer: Customer, do_print: bool = False) -> float:
bill = 0
purchase_time = datetime.now().strftime("Date: %m/%d/%Y %H:%M:%S")
print(f"\n{purchase_time}")
print(f"Thanks, {customer.name},"
f" for your purchase!\n You have bought:")
for product in customer.product_cart:
price = self.products[product]
amount = customer.product_cart[product]
cost = price * amount
cost = round(price * amount, 2)
bill += cost
print(f"{amount} {product} for {cost}")
if do_print:
print(f"{amount} {product} for {cost} dollars")
return bill

def buy_products(self, customer: Customer) -> float:

purchase_time = datetime.now().strftime("Date: %m/%d/%Y %H:%M:%S")
print(f"\n{purchase_time}")
print(f"Thanks, {customer.name},"
f" for your purchase!\n You have bought:")
bill = self.calculate_price(customer, do_print=True)
print(f"\nTotal cost is {bill} dollars\n See you again!")
return bill

0 comments on commit b947658

Please sign in to comment.