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

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
4 changes: 4 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Car:
def __init__(self, brand: str, fuel: float) -> None:
self.brand = brand
self.fuel = fuel
17 changes: 17 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
52 changes: 49 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
def shop_trip():
# write your code here
pass
from app.transfer import transfer
from math import hypot


def shop_trip() -> None:
fuel_price, list_customers, list_shops = transfer()
for customer in list_customers:
shop_list = []
print(f"{customer.name} has {customer.money} dollars")
for shop in list_shops:
car_fuel_1km = customer.car.fuel / 100
loc1 = customer.location
loc2 = shop.location
dist = hypot(loc2[0] - loc1[0], loc2[1] - loc1[1])
cost_travel = round(dist * car_fuel_1km * fuel_price * 2, 2)
cost_products = sum(customer.product_cart[key] * shop.products[key]
for key in shop.products)
costs = cost_products + cost_travel
print(f"{customer.name}'s trip to the {shop.name} costs {costs}")
shop_list.append((shop, costs))
customer_choice = min(shop_list, key=lambda x: x[1])
if customer.money < customer_choice[1]:
print(customer.name,
"doesn't have enough money to make a purchase in any shop")
break
print(f"{customer.name} rides to {customer_choice[0].name}")
print()
print("Date: 04/01/2021 12:33:41")
print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought:")
shop_choice = customer_choice[0].products
costs = 0
for key in customer.product_cart:
costs += (customer.product_cart[key] * shop_choice[key])
total = customer.product_cart[key] * shop_choice[key]
if total == int(total):
total = int(total)
print(f"{customer.product_cart[key]} {key}s for {total} dollars")
print(f"Total cost is {costs} dollars")
print("See you again!")
print()
print(f"{customer.name} rides home")
new_balance = customer.money - customer_choice[1]
print(f"""{customer.name} now has {new_balance} dollars""")
print()


if __name__ == "__main__":
shop_trip()
9 changes: 9 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Shop:
def __init__(
self, name: str,
location: list[int],
products: dict
) -> None:
self.name = name
self.location = location
self.products = products
24 changes: 24 additions & 0 deletions app/transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json
from app.customer import Customer
from app.car import Car
from app.shop import Shop


def transfer() -> list:
with open("app/config.json") as file:
config = json.load(file)
fuel_price = config["FUEL_PRICE"]
list_customers = []
list_shops = []
for customer, shop in zip(config["customers"], config["shops"]):
list_customers.append(Customer(
customer["name"], customer["product_cart"],
customer["location"], customer["money"],
Car(customer["car"]["brand"], customer["car"]["fuel_consumption"])
))
list_shops.append(Shop(
shop["name"],
shop["location"],
shop["products"]
))
return [fuel_price, list_customers, list_shops]
Loading