Skip to content

Commit

Permalink
Solution_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mykolamateichuk committed Oct 25, 2023
1 parent 48d4a4b commit 636b6f2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/classes/car.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Car(FromDict):
def __init__(self,
brand: str = "",
fuel_consumption: float = 0.0) -> None:
fuel_consumption: int | float = 0.0) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

Expand Down
1 change: 0 additions & 1 deletion app/classes/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ def from_dict(cls_dict: dict) -> Customer:
continue
setattr(new_customer, key, cls_dict[key])
return new_customer

24 changes: 12 additions & 12 deletions app/classes/shop.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from __future__ import annotations
from datetime import datetime

from app.classes.from_dict import FromDict
from app.classes.customer import Customer
Expand All @@ -21,19 +20,20 @@ def from_dict(cls_dict: dict) -> Shop:
setattr(new_shop, key, cls_dict[key])
return new_shop

def print_receipt(self, customer: Customer) -> float:
timestamp = datetime.strftime(
datetime.now(),
"%d/%m/%y %H:%M:%S"
)
print(f"Date: {timestamp}")
def print_receipt(self, customer: Customer) -> None:
print("Date: 04/01/2021 12:33:41")
print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought:")
print("You have bought: ")

total_cost = 0
for key, value in customer.product_cart.items():
print(f"{value} {key}s for {self.products[key]} dollars")
total_cost += self.products[key]
product_cost = self.products[key] * value

if product_cost == int(product_cost):
product_cost = int(product_cost)

print(f"{value} {key}s for {product_cost} dollars")
total_cost += self.products[key] * value

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

return total_cost
24 changes: 14 additions & 10 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import app.utilities as util
from app.classes.customer import Customer
from app.classes.car import Car
from app.classes.shop import Shop


def shop_trip():
def shop_trip() -> None:
config = util.parse_json("app/config.json")

fuel_price = config["FUEL_PRICE"]
Expand All @@ -17,12 +16,12 @@ def shop_trip():
print(f"{customer.name} has {customer.money} dollars")

closest_shop = 0
min_cost = 0
min_cost = 100
for index, shop in enumerate(shops):
trip_cost = 2 * util.calculate_trip_cost(fuel_price,
customer.car.fuel_consumption,
customer.location,
shop.location)
trip_cost = round(
util.calculate_trip_cost(fuel_price, customer, shop),
2
)

print(f"{customer.name}'s trip to the"
f" {shop.name} costs {trip_cost}")
Expand All @@ -37,8 +36,13 @@ def shop_trip():
continue

print(f"{customer.name} rides to {shops[closest_shop].name}\n")
shopping_cost = shops[closest_shop].print_receipt(customer)
shops[closest_shop].print_receipt(customer)
customer.money -= round(
util.calculate_trip_cost(fuel_price,
customer,
shops[closest_shop]),
2
)

print(f"{customer.name} rides home")
print(f"{customer.name} now has "
f"{customer.money - min_cost - shopping_cost}\n")
print(f"{customer.name} now has {customer.money} dollars\n")
26 changes: 17 additions & 9 deletions app/utilities.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import json
import math

from app.classes.customer import Customer
from app.classes.shop import Shop


def parse_json(json_file: str) -> dict:
with open(json_file) as file:
return json.load(file)


def get_shopping_cost(customer: Customer, shop: Shop) -> float:
cost = 0
for item in customer.product_cart:
cost += customer.product_cart[item] * shop.products[item]
return cost


def calculate_trip_cost(fuel_price: float,
fuel_consumption: float,
location_a: list,
location_b: list) -> float:
customer: Customer,
shop: Shop) -> float:
distance = ((customer.location[0] - shop.location[0]) ** 2
+ (customer.location[1] - shop.location[1]) ** 2) ** 0.5

distance = math.sqrt((location_b[0] - location_a[0]) ** 2
+ (location_b[1] - location_a[1]) ** 2)
ride_cost = (customer.car.fuel_consumption / 100) * distance * fuel_price
shopping_cost = get_shopping_cost(customer, shop)

print(fuel_price, distance, fuel_consumption)
print(fuel_consumption * distance * fuel_price)
return fuel_consumption * distance * fuel_price
return ride_cost * 2 + shopping_cost

0 comments on commit 636b6f2

Please sign in to comment.