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

Open
wants to merge 4 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
37 changes: 37 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from app.shop import Shop


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

def trip_price(self, fuel_price: int | float, shop: Shop) -> int | float:
distance = (
(
(shop.location[0] - self.location[0]) ** 2
+ (shop.location[1] - self.location[1]) ** 2
) ** 0.5
)
fuel_cost = fuel_price * (
self.car["fuel_consumption"] / 100
) * distance * 2

products_price = sum(
[
shop.products[product] * amount
for product, amount in self.products_cart.items()
]
)

return fuel_cost + products_price
67 changes: 64 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
def shop_trip():
# write your code here
pass
import json
from app.customer import Customer
from app.shop import Shop


def shop_trip() -> None:
with open("config.json", "r") as config_file:
config_info = json.load(config_file)

fuel_price = config_info["FUEL_PRICE"]

customers = []
for customer in config_info["customers"]:
customers.append(
Customer(
customer["name"],
customer["product_cart"],
customer["location"],
customer["money"],
customer["car"]
)
)

shops = []
for shop in config_info["shops"]:
shops.append(
Shop(
shop["name"],
shop["location"],
shop["products"]
)
)

for customer in customers:
print(f"{customer.name} has {customer.money} dollars")

best_shop = [shops[0], customer.trip_price(fuel_price, shops[0])]
for shop in shops:
trip_price = customer.trip_price(fuel_price, shop)
if trip_price < best_shop[1]:
best_shop = [shop, trip_price]

print(
f"{customer.name}'s trip to the {shop.name} "
f"costs {round(customer.trip_price(fuel_price, shop), 2)}"
)

if customer.money < customer.trip_price(fuel_price, best_shop[0]):
print(
f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop"
)
break

initial_coordinates = customer.location
print(f"{customer.name} rides to {best_shop[0].name}")
customer.location = shop.location

print(best_shop[0].receipt(customer))

customer.location = initial_coordinates
customer.money -= customer.trip_price(fuel_price, best_shop[0])
print(f"{customer.name} rides home\n"
f"{customer.name} now has {round(customer.money, 2)} dollars\n")
35 changes: 35 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 receipt(self, customer: object) -> str:
date = datetime.datetime.now()
receipt = (f"\nDate: {date.strftime('%d/%m/%Y %H:%M:%S')}"
f"\nThanks, {customer.name}, for your purchase!"
f"\nYou have bought: \n")

total_cost = 0
for product in customer.products_cart:
price_of_products = (
customer.products_cart[product]
* self.products[product]
)
total_cost += price_of_products

receipt += (f"{customer.products_cart[product]} {product}s for "
f"{price_of_products} dollars\n")

receipt += (f"Total cost is {total_cost} dollars"
f"\nSee you again!\n")

return receipt
76 changes: 76 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"FUEL_PRICE": 2.4,
"customers": [
{
"name": "Bob",
"product_cart": {
"milk": 4,
"bread": 2,
"butter": 5
},
"location": [12, -2],
"money": 55,
"car": {
"brand": "Suzuki",
"fuel_consumption": 9.9
}
},
{
"name": "Alex",
"product_cart": {
"milk": 2,
"bread": 2,
"butter": 2
},
"location": [1, -2],
"money": 41,
"car": {
"brand": "BMW",
"fuel_consumption": 9.1
}
},
{
"name": "Monica",
"product_cart": {
"milk": 3,
"bread": 3,
"butter": 1
},
"location": [11, -2],
"money": 12,
"car": {
"brand": "Audi",
"fuel_consumption": 7.6
}
}
],
"shops": [
{
"name": "Outskirts Shop",
"location": [10, -5],
"products": {
"milk": 3,
"bread": 1,
"butter": 2.5
}
},
{
"name": "Shop '24/7'",
"location": [4, 3],
"products": {
"milk": 2,
"bread": 1.5,
"butter": 3.2
}
},
{
"name": "Central Shop",
"location": [0, 0],
"products": {
"milk": 3,
"bread": 2,
"butter": 3.5
}
}
]
}
Loading