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

"sol" #601

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

"sol" #601

Show file tree
Hide file tree
Changes from 6 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_consumption: float) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption
45 changes: 45 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import math
from datetime import datetime
from app.car import Car


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

def distance_to(self, shop_location: dict) -> float:
return math.dist(self.location, shop_location)

def travel_cost(self, distance: float, fuel_price: float) -> float:
return (distance * self.car.fuel_consumption / 100) * fuel_price

def purchase_cost(self, shop_products: dict) -> float:
total_cost = 0
for product, quantity in self.product_cart.items():
total_cost += shop_products[product] * quantity
return total_cost

def print_receipt(self, shop_name: str, shop_products: dict) -> None:

datetime_str = datetime(
2021, 4, 1, 12, 33, 41
).strftime("%m/%d/%Y %H:%M:%S")

print(f"Date: {datetime_str}\n"
f"Thanks, {self.name}, for your purchase!\n"
f"You have bought:")
total_cost = 0
for product, quantity in self.product_cart.items():
cost = shop_products.get(product, 0) * quantity
total_cost += cost
formatted_cost = f"{cost: .2f}".rstrip("0").rstrip(".")
print(f"{quantity} {product}s for{formatted_cost} dollars")
print(f"Total cost is {total_cost} dollars\n"
f"See you again!\n")
57 changes: 54 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
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") as file:
config = json.load(file)

fuel_price = config["FUEL_PRICE"]

for config_customer in config["customers"]:
customers = [
Customer(
config_customer["name"],
config_customer["product_cart"],
config_customer["location"],
config_customer["money"],
Car(**config_customer["car"])
)
for config_customer in config["customers"]
]

arsenmarkotskyi marked this conversation as resolved.
Show resolved Hide resolved
shops = [Shop(**config_shop) for config_shop in config["shops"]]
for customer in customers:
print(f"{customer.name} has {customer.money} dollars")
trips = []

for shop in shops:
distance_to_shop = customer.distance_to(shop.location)
cost_to_shop = customer.travel_cost(distance_to_shop, fuel_price)
purchase_cost = customer.purchase_cost(shop.products)
total_trip_cost = 2 * cost_to_shop + purchase_cost
trips.append((total_trip_cost, shop))

print(f"{customer.name}'s trip to the {shop.name} "
f"costs{total_trip_cost: .2f}")

trips.sort(key=lambda x: x[0])
cheapest_trip = trips[0]
total_cost, chosen_shop = cheapest_trip

if total_cost <= customer.money:
print(f"{customer.name} rides to {chosen_shop.name}\n")
customer.location = chosen_shop.location
customer.print_receipt(chosen_shop.name, chosen_shop.products)
customer.location = [0, 0]
customer.money -= total_cost
arsenmarkotskyi marked this conversation as resolved.
Show resolved Hide resolved
print(f"{customer.name} rides home\n"
f"{customer.name} now has{customer.money: .2f} dollars\n")

print(f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop")
5 changes: 5 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Shop:
def __init__(self, name: str, location: list, products: dict) -> None:
self.name = name
self.location = location
self.products = products
Loading