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

Develop #341

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

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


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

def get_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 = self.get_product_price(shop)
return round(fuel_cost + products_price, 2)

def get_product_price(self, shop: Shop) -> float:
return sum(
[
shop.products[product] * amount
for product, amount in self.product_cart.items()
]
)

def print_the_purchase_receipt(self, cheapest_shop: Shop) -> None:
print(f"\nDate: "
f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}")
print(f"Thanks, {self.name}, for your purchase!")
print("You have bought: ")
for product, amount in cheapest_shop.products.items():
count = self.product_cart.get(product, 0)
if count > 0:
print(f"{count} {product}s for {count * amount} dollars")
66 changes: 63 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
def shop_trip():
# write your code here
pass
from app.car import Car
from app.customer import Customer
from app.shop import Shop
import json


def load_config() -> tuple[float, list[Customer], list[Shop]]:
with open("app/config.json") as file:
config = json.load(file)

fuel_price = config["FUEL_PRICE"]

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

shops = [
Shop(name=shop["name"],
shop_location=shop["location"],
products=shop["products"])
for shop in config["shops"]
]

return fuel_price, customers, shops


def print_purchase_receipt(customer: Customer, shop: Shop) -> None:
customer.print_the_purchase_receipt(shop)
print(f"Total cost is {customer.get_product_price(shop)} dollars")
print("See you again!\n")


def shop_trip() -> None:
fuel_price, customers, shops = load_config()

for customer in customers:
print(f"{customer.name} has {customer.money} dollars")
cheapest_cost = None
cheapest_shop = None
for shop in shops:
trip_cost = customer.get_trip_price(fuel_price, shop)
print(
f"{customer.name}'s trip to the {shop.name} costs {trip_cost}")
if cheapest_cost is None or trip_cost < cheapest_cost:
cheapest_cost = trip_cost
cheapest_shop = shop
if customer.money >= cheapest_cost:
print(f"{customer.name} rides to {cheapest_shop.name}")
customer.location = cheapest_shop.location
customer.money -= cheapest_cost

print_purchase_receipt(customer, cheapest_shop)

print(f"{customer.name} rides home")
print(f"{customer.name} now has {customer.money} dollars\n")
else:
print(f"{customer.name} doesn't have "
f"enough money to make a purchase in any shop")
13 changes: 13 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


class Shop:

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