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

implemented "Shop trip" behaviour #600

Open
wants to merge 3 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
15 changes: 15 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Car:
def __init__(self, brand: str, fuel_consumption: float) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

def __repr__(self) -> str:
return (f"Car(brand={self.brand}, "
f"fuel_consumption={self.fuel_consumption})")

def calculate_fuel_costs(
self, distance_to_shop_and_home: float,
fuel_price: float
) -> float:
return (distance_to_shop_and_home * (self.fuel_consumption / 100)
* fuel_price)
82 changes: 82 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import datetime

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


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

def __repr__(self) -> str:
return (f"Customer(name={self.name}, products={self.product_cart}, "
f"location={self.location}, "
f"money={self.money}, car={self.car})")

def buy_products(self, current_shop: Shop) -> float:
total_cost = 0
for key, value in self.product_cart.items():
cost = current_shop.products[key] * value
cost = round(cost) if ".0" in str(cost) else cost
print(f"{value} {key}s for {cost} dollars")
total_cost += cost

return total_cost

def get_all_trip_costs(self, shops: list[Shop], fuel_price: float) -> dict:
result_costs = {}
print(f"{self.name} has {self.money} dollars")
for shop in shops:
trip_cost = self.calculate_trip_cost(shop, fuel_price)
print(f"{self.name}'s trip to the {shop.name} costs {trip_cost}")
result_costs[shop] = trip_cost

return result_costs

def calculate_trip_cost(self, shop: Shop, fuel_price: float) -> float:
distance_to_shop_and_home = Location.calculate_distance(
shop_location=shop.location,
customer_location=self.location
)

fuel_cost_to_shop_and_home = self.car.calculate_fuel_costs(
distance_to_shop_and_home=distance_to_shop_and_home,
fuel_price=fuel_price
)

product_cost_total = shop.total_cost_of_product_in_cart(
product_cart=self.product_cart
)

# multiply means fuel costs to home and shop
total_trip_cost = (product_cost_total
+ (fuel_cost_to_shop_and_home * 2))

return round(total_trip_cost, 2)

def process_purchasing(self, current_shop: Shop, min_cost: float) -> None:
print(f"{self.name} rides to {current_shop.name}")
print()
today = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print(f"Date: {today}")
print(f"Thanks, {self.name}, for your purchase!")
print("You have bought:")

total_cost = self.buy_products(current_shop)

print(f"Total cost is {total_cost} dollars")
print("See you again!")
print()

print(f"{self.name} rides home")
print(f"{self.name} now has {self.money - min_cost} dollars")
print()
11 changes: 11 additions & 0 deletions app/location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from math import sqrt


class Location:
@staticmethod
def calculate_distance(
shop_location: list[int],
customer_location: list[int]
) -> float:
return sqrt((shop_location[0] - customer_location[0]) ** 2
+ (shop_location[1] - customer_location[1]) ** 2)
77 changes: 74 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
def shop_trip():
# write your code here
pass
from json import load
from os import path

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


def get_data_from_json() -> dict:
file_path = path.abspath("config.json")
with open(file_path) as file:
return load(file)


def init_customer(customer: dict) -> Customer:
return Customer(
name=customer["name"],
product_cart=customer["product_cart"],
location=customer["location"],
money=customer["money"],
car=get_instance_of_car(customer["car"]),
)


def get_instance_of_car(car: dict) -> Car:
return Car(brand=car["brand"], fuel_consumption=car["fuel_consumption"])


def init_shops(data: list) -> list[Shop]:
return [
Shop(
name=shop["name"],
location=shop["location"],
products=shop["products"],
)

for shop in data
]


def get_min_cost_and_current_shop(trip_costs: dict) -> list:
min_cost = float("inf")
current_shop = ""
for key, value in trip_costs.items():
if min_cost > value:
min_cost = value
current_shop = key

return [min_cost, current_shop]


def shop_trip() -> None:
data = get_data_from_json()
fuel_price = data["FUEL_PRICE"]
shops = init_shops(data["shops"])
for customer in data["customers"]:
customer = init_customer(customer)
trip_costs = customer.get_all_trip_costs(shops, fuel_price)

min_cost_and_current_shop_list = (
get_min_cost_and_current_shop(trip_costs))

min_cost = min_cost_and_current_shop_list[0]
current_shop = min_cost_and_current_shop_list[1]

if customer.money < min_cost:
print(f"{customer.name} doesn't have enough"
f" money to make a purchase in any shop")
continue

customer.process_purchasing(
current_shop=current_shop,
min_cost=min_cost
)
15 changes: 15 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Shop:
def __init__(self, name: str, location: list, products: dict) -> None:
self.name = name
self.location = location
self.products = products

def __repr__(self) -> str:
return (f"Shop(name={self.name}, location={self.location}, "
f"product={self.products})")

def total_cost_of_product_in_cart(self, product_cart: dict) -> float:
return sum(
self.products[key] * value
for key, value in product_cart.items()
)
File renamed without changes.
Loading