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 #581

Open
wants to merge 10 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
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 spent_liters_of_fuel(self, distance: float) -> float:
return self.fuel_consumption * distance / 100
29 changes: 29 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import math

from app.car import Car


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

def initial_money(self) -> None:
print(f"{self.name} has {self.money} dollars")

def calculate_way_price(self, shop_location: list[int],
fuel_price: float) -> float:
distance = math.dist(self.location, shop_location)
road_price = self.car.spent_liters_of_fuel(distance) * 2 * fuel_price
return road_price
54 changes: 51 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
def shop_trip():
# write your code here
pass
import json

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


def shop_trip() -> None:
with open("app/config.json", "r") as config_file:
config = json.load(config_file)
fuel_price = config["FUEL_PRICE"]
customers = [
Customer(
customer["name"],
customer["product_cart"],
customer["location"],
customer["money"],
Car(**customer["car"])
)
for customer in config["customers"]
]
shops = [
Shop(**shop)
for shop in config["shops"]
]

for customer in customers:
customer.initial_money()
price_shop_list = {}
for shop in shops:
price_for_trip = customer.calculate_way_price(
shop.location, fuel_price
) + shop.calculate_product_price(customer)
print(f"{customer.name}'s trip to the {shop.name} "
f"costs {round(price_for_trip, 2)}")
price_shop_list[price_for_trip] = shop
min_price = min(price_shop_list)
cheapest_shop = price_shop_list[min_price]

if customer.money >= min_price:
print(f"{customer.name} rides to "
f"{cheapest_shop.name}\n")
home_location = customer.location
customer.location = cheapest_shop.location
cheapest_shop.print_receipt(customer)
print(f"{customer.name} rides home\n{customer.name} now has "
f"{round(customer.money - min_price, 2)} dollars\n")
customer.location = home_location
else:
print(f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop")
36 changes: 36 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import datetime

from app.customer import Customer


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

def calculate_product_price(self, customer: Customer) -> float:
full_price = 0
for product, amount in customer.product_cart.items():
full_price += self.products[product] * amount
return full_price

def print_receipt(self, customer: Customer) -> None:
now = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print(f"Date: {now}\n"
f"Thanks, {customer.name}, for your purchase!\n"
"You have bought:")
for product, amount in customer.product_cart.items():
price = self.products[product] * amount
print(
f"{amount} {product}s for "
f"{int(price) if price == int(price) else price} dollars"
)
print(f"Total cost is "
f"{self.calculate_product_price(customer)} dollars\n"
f"See you again!\n")
Loading