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

The solution of the task #597

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
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
inline-quotes = "
ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818
ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818, E231
max-line-length = 79
max-complexity = 18
select = B,C,E,F,W,T4,B9,ANN,Q0,N8,VNE
Expand Down
14 changes: 14 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from dataclasses import dataclass


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

def fuel_price(
self,
distance: float | int,
fuel_price: float | int
) -> float | int:
return distance * self.fuel_consumption / 100 * fuel_price
12 changes: 12 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from dataclasses import dataclass

from app.car import Car


@dataclass
class Customer:
name: str
product_cart: dict
location: list[int]
money: int
car: Car
45 changes: 42 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
def shop_trip():
# write your code here
pass
import json
import math

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


def shop_trip() -> None:
with open("app/config.json", "r") as f:
data = json.load(f)
fuel_price = data["FUEL_PRICE"]
customers = data["customers"]
shops = [Shop(**shop) for shop in data["shops"]]
for people in customers:
customer = Customer(
people["name"],
people["product_cart"],
people["location"],
people["money"],
Car(**people["car"])
)
print(f"{customer.name} has {customer.money} dollars")
prices = {}
for shop in shops:
cost = round(shop.trip_cost(
math.dist(customer.location,shop.location),
fuel_price,
customer
), 2)
prices[cost] = shop
print(f"{customer.name}'s trip to the {shop.name} costs {cost}")
cost_of_ride = min([price for price in prices])
if cost_of_ride <= customer.money:
print(f"{customer.name} rides to {prices[cost_of_ride].name}\n")
prices[cost_of_ride].print_receipt(customer)
print(f"{customer.name} rides home")
customer.money -= cost_of_ride
print(f"{customer.name} now has {customer.money} dollars\n")
continue
print(f"{customer.name} doesn't have enough "
f"money to make a purchase in any shop")
37 changes: 37 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from dataclasses import dataclass

from app.customer import Customer


@dataclass
class Shop:
name: str
location: list[int]
products: dict

def trip_cost(
self,
distance: float | int,
fuel_price: float | int,
customer: Customer
) -> float | int:
price = 0
for product in customer.product_cart:
price += (self.products.get(product)
* customer.product_cart[product])
return customer.car.fuel_price(distance, fuel_price) * 2 + price

def print_receipt(self, customer: Customer) -> None:
customer.location = self.location
print(f"Date: 04/01/2021 12:33:41\n"
f"Thanks, {customer.name}, for your purchase!\n"
f"You have bought:")
cost = 0
for product, quantity in customer.product_cart.items():
price = self.products[product] * quantity
if int(price) == price:
price = int(price)
cost += price
print(f"{quantity} {product}s for {price} dollars")
print(f"Total cost is {cost} dollars\n"
f"See you again!\n")
Loading