Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #599

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from dataclasses import dataclass


@dataclass
class Car:
brand: str
fuel_consumption: float

def calculate_trip_cost(self, distance: float, fuel_price: float) -> float:
fuel_needed = (distance / 100) * self.fuel_consumption
return round(fuel_needed * fuel_price, 2)
6 changes: 6 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import json


def load_config(filename: str) -> dict:
with open(f"app/{filename}", "r") as file:
return json.load(file)
41 changes: 41 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from dataclasses import dataclass
from typing import List, Dict
from app.car import Car
from app.shop import Shop


@dataclass
class Customer:
name: str
product_cart: Dict[str, int]
home_location: List[float]
current_location: List[float]
money: float
car: Car

def calculate_trip_cost(self, shop: Shop, fuel_price: float) -> float:
distance = shop.calculate_distance(self.current_location)
return self.car.calculate_trip_cost(distance * 2, fuel_price)

def calculate_products_price(self, shop: Shop) -> float | int:
return sum(shop.get_product_price(product)
* quantity for product, quantity
in self.product_cart.items())

def get_full_price(self, shop: Shop, fuel_price: float) -> float:
trip_cost = self.calculate_trip_cost(shop, fuel_price)
products_cost = self.calculate_products_price(shop)
return trip_cost + products_cost

def can_afford_trip(self, shop: Shop, fuel_price: float) -> bool:
return self.money >= self.get_full_price(shop, fuel_price)

def make_purchase(self, shop: Shop, fuel_price: float) -> bool:
if self.can_afford_trip(shop, fuel_price):
self.money -= self.get_full_price(shop, fuel_price)
self.current_location = shop.location
return True
return False

def go_back(self) -> None:
self.current_location = self.home_location
68 changes: 65 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
def shop_trip():
# write your code here
pass
from app.config import load_config
from app.car import Car
from app.shop import Shop
from app.customer import Customer
from datetime import datetime


def shop_trip() -> None:
config = load_config("config.json")
fuel_price = config["FUEL_PRICE"]
shops = [Shop(**shop) for shop
in config["shops"]]
customers = [Customer(customer["name"],
customer["product_cart"],
customer["location"],
customer["location"],
customer["money"],
Car(customer["car"]["brand"],
customer["car"]["fuel_consumption"]))
for customer in config["customers"]]
for customer in customers:
min_price = float("inf")
best_shop = ""
print(f"{customer.name} has {customer.money} dollars")
for shop in shops:
price = customer.get_full_price(shop, fuel_price)
print(f"{customer.name}'s trip to the {shop.name} costs {price}")

if price < min_price:
min_price = price
best_shop = shop

if not customer.can_afford_trip(best_shop, fuel_price):
print(f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop")
continue

print(f"{customer.name} rides to {best_shop.name}\n")
customer.make_purchase(best_shop, fuel_price)
date = (datetime(
2021, 4, 1, 12, 33, 41
).strftime("%m/%d/%Y %H:%M:%S"))
print(
f"Date: {date}\n"
f"Thanks, {customer.name}, for your purchase!\n"
f"You have bought:"
)

total_price = 0
for product_name, product_count in customer.product_cart.items():
product_account_price = (
product_count * best_shop.get_product_price(product_name)
)
total_price += product_account_price
print(f"{product_count} {product_name}s "
f"for{product_account_price: .2f}".rstrip("0").rstrip(".")
+ " dollars")

print(f"Total cost is {total_price} dollars\nSee you again!\n")
customer.go_back()
print(f"{customer.name} rides home\n"
f"{customer.name} now has {customer.money} dollars\n")


shop_trip()
16 changes: 16 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from dataclasses import dataclass
from typing import List, Dict
from math import dist


@dataclass
class Shop:
name: str
location: List[float]
products: Dict[str, float]

def calculate_distance(self, customer_location: List[float]) -> float:
return dist(self.location, customer_location)

def get_product_price(self, product: str) -> float | int:
return self.products.get(product)
Loading