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

solved task #593

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
18 changes: 18 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from dataclasses import dataclass

from app.customer import Customer
from app.shop import Shop


@dataclass
class Car:
brand: str
fuel_consumption: int | float

def litres_per_trip(
self,
shop: Shop,
customer: Customer
) -> int | float:
distance = shop.calculate_trip_distance(customer)
return (self.fuel_consumption * distance) / 100
13 changes: 13 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import annotations
from dataclasses import dataclass


@dataclass
class Customer:
name: str
product_cart: dict
location: list
money: int | float

def __str__(self) -> str:
return f"{self.name} has {self.money} dollars"
53 changes: 50 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
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 config_file:
data = json.load(config_file)
customers = data["customers"]
shops = data["shops"]

for person in customers:
customer = Customer(
person["name"],
person["product_cart"],
person["location"],
person["money"],
)
car = Car(person["car"]["brand"], person["car"]["fuel_consumption"])
print(customer)

prices = {}
for shop in shops:
shop_inst = Shop(
shop["name"],
shop["location"],
shop["products"]
)
fuel_cost = (data["FUEL_PRICE"]
* car.litres_per_trip(shop_inst, customer))
total = round(fuel_cost * 2 + shop_inst.shopping_cost(customer), 2)
prices[total] = shop_inst
print(f"{customer.name}'s trip to the {shop_inst.name} "
f"costs {total}")

if customer.money >= min(prices):
cheapest_shop = prices[min(prices)]
print(f"{customer.name} rides to {cheapest_shop.name}")
customer.location = cheapest_shop.location
customer.money -= min(prices)
cheapest_shop.issue_receipt(customer)
print(
f"\n{customer.name} rides home"
f"\n{customer.name} now has {customer.money} dollars\n"
)
else:
print(f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop")
42 changes: 42 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import datetime
from dataclasses import dataclass
from math import sqrt

from app.customer import Customer


@dataclass
class Shop:
name: str
location: list
products: dict

def calculate_trip_distance(self, customer: Customer) -> int | float:
shop_pos_x = self.location[0]
shop_pos_y = self.location[1]
customer_pos_x = customer.location[0]
customer_pos_y = customer.location[1]
return sqrt(
(shop_pos_x - customer_pos_x) ** 2
+ (shop_pos_y - customer_pos_y) ** 2
)

def shopping_cost(self, customer: Customer) -> int | float:
total_price = 0
for product, amount in customer.product_cart.items():
total_price += self.products.get(product) * amount
return total_price

def issue_receipt(self, customer: Customer) -> None:
date = datetime.datetime.now()
print(f'\nDate: {date.strftime("%d/%m/%Y %H:%M:%S")}\n'
f"Thanks, {customer.name}, for your purchase!\n"
f"You have bought:")
for product, count in customer.product_cart.items():
cost = self.products[product] * count
print(f"{count} {product}s for "
f"{int(cost) if float(cost) == int(cost) else cost} dollars")
print(
f"Total cost is {self.shopping_cost(customer)} dollars\n"
f"See you again!"
)
Loading