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

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
Empty file added app/__init__.py
Empty file.
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 gas_for_distance(self, distance: float) -> float:
return self.fuel_consumption * distance / 100
30 changes: 30 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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.car = car
self.name = name
self.product_cart = product_cart
self.location = location
self.money = money
self.__home_location = location

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

def price_way(
self,
shop_location: list[int],
fuel_price: float) -> float:
distance = math.dist(self.location, shop_location)
road_price = self.car.gas_for_distance(distance) * 2 * fuel_price
return road_price
58 changes: 55 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
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 file:
config = json.load(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 = {}
for shop in shops:
price_trip = customer.price_way(
shop.location, fuel_price
) + shop.calculate_product_price(customer)
print(f"{customer.name}'s "
f"trip to the {shop.name} "
f"costs {round(price_trip, 2)}")
price_shop[price_trip] = shop
min_price = min(price_shop)
cheap_shop = price_shop[min_price]

if customer.money >= min_price:
print(f"{customer.name} rides to {cheap_shop.name}\n")
home = customer.location
customer.location = cheap_shop.location
cheap_shop.print_check(customer)
print(f"{customer.name} rides home")
print(f"{customer.name} now has "
f"{round(customer.money - min_price, 2)}"
f" dollars\n")
customer.location = home
else:
print(f"{customer.name} "
f"doesn't have enough money"
f" to make a purchase in any shop")
32 changes: 32 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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_check(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"
f" for {int(price) if price == int(price) else price}"
f" dollars"
)
print(f"Total cost is"
f" {self.calculate_product_price(customer)} dollars")
print("See you again!\n")
Loading