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' #647

Open
wants to merge 1 commit 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
7 changes: 7 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Car:
def __init__(self, brand: str, fuel_consumption: float) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

def calculate_fuel_cost(self, distance: float, fuel_price: float) -> float:
return fuel_price * ((distance / 100) * self.fuel_consumption)
46 changes: 46 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from math import sqrt
from app.car import Car
from app.shop import Shop


class Customer:
def __init__(self,
name: str,
product_cart: dict,
location: list[int],
money: float,
car: dict) -> None:
self.name = name
self.product_cart = product_cart
self.location = location
self.money = money
self.car = Car(car["brand"], car["fuel_consumption"])

def distance_to(self, shop_location: list[int]) -> float:
return sqrt(
(self.location[0] - shop_location[0]) ** 2
+ (self.location[1] - shop_location[1]) ** 2
)

def calculate_total_trip_cost(self,
shop: Shop,
fuel_price: float) -> float:
distance = self.distance_to(shop.location)
fuel_cost_to_road = (self.car.calculate_fuel_cost(distance, fuel_price)
* 2)
product_cost = shop.calculate_product_cost(self.product_cart)
return fuel_cost_to_road + product_cost

def buy_products(self, shop: Shop, fuel_price: float) -> None:
total_trip_cost = self.calculate_total_trip_cost(shop, fuel_price)
if self.money >= total_trip_cost:
print(f"{self.name} rides to {shop.name}")
self.location = shop.location
shop.print_receipt(self.name, self.product_cart)
self.money -= total_trip_cost
print(f"{self.name} rides home")
self.location = [0, 0]
print(f"{self.name} now has {round(self.money, 2)} dollars\n")
else:
print(f"{self.name} doesn't have enough money "
f"to make a purchase in any shop")
33 changes: 30 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
def shop_trip():
# write your code here
pass
import json
from os import path
from app.customer import Customer
from app.shop import Shop


def shop_trip() -> None:
config_path = path.join(path.dirname(__file__), "config.json")
with open(config_path, "r") as file:
config = json.load(file)

fuel_price = config["FUEL_PRICE"]
customers = [Customer(**customer) for customer in config["customers"]]
shops = [Shop(**shop) for shop in config["shops"]]

for customer in customers:
print(f"{customer.name} has {customer.money} dollars")
best_shop = None
cheapest_cost = float("inf")

for shop in shops:
trip_cost = customer.calculate_total_trip_cost(shop, fuel_price)
print(f"{customer.name}'s trip to the "
f"{shop.name} costs {round(trip_cost, 2)}")
if trip_cost < cheapest_cost:
cheapest_cost = trip_cost
best_shop = shop

if best_shop:
customer.buy_products(best_shop, fuel_price)
39 changes: 39 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import datetime


class Shop:
def __init__(self, name: str, location: list[int], products: dict) -> None:
self.name = name
self.location = location
self.products = products

def calculate_single_product_cost(
self,
product: str,
amount: int
) -> float:
return self.products.get(product) * amount

def calculate_product_cost(self, product_cart: dict) -> float:
total_cost = 0

for product, amount in product_cart.items():
total_cost += self.calculate_single_product_cost(product, amount)
return total_cost

def print_receipt(self, customer_name: str, product_cart: dict) -> None:
print(
f'\nDate: {datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")}'
)
print(f"Thanks, {customer_name}, for your purchase!")
print("You have bought:")

for product, amount in product_cart.items():
cost = self.calculate_single_product_cost(product, amount)
formatted_cost = (f"{round(cost, 1)}" if cost % 1 != 0
else f"{int(cost)}")
print(f"{amount} {product}s for {formatted_cost} dollars")

total_cost = self.calculate_product_cost(product_cart)
print(f"Total cost is {round(total_cost, 1)} dollars")
print("See you again!\n")
Loading