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

for checking #338

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 math import sqrt
from typing import List

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


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

def initial_money(self) -> None:
print(f"{self.name} has {self.money} dollars")

def distance(
self,
shop_location: list,
fuel_consumption: float,
fuel_price: float
) -> float:
x1, y1 = self.customer_location
x2, y2 = shop_location
dist = sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
trip_to_shop = dist / 100 * fuel_consumption * fuel_price * 2

return round(trip_to_shop, 2)

def time_for_shopping(self, shop: Shop) -> None:
total_cost = 0.0
date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print(f"Date: {date}")
print(f"Thanks, {self.name}, for your purchase!")
print("You have bought: ")

for product_name, price in self.product_cart.items():
print(f"{price} {product_name}s for "
f"{price * shop.products[product_name]} dollars")

total_cost = sum(
price * shop.products[product_name]
for product_name, price in self.product_cart.items()
)
print(f"Total cost is {total_cost} dollars")

print("See you again!\n")
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
import json

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


def shop_trip() -> None:
with open("app/config.json", "r") as data_file:
data = json.load(data_file)

fuel_price = data["FUEL_PRICE"]
customers_data = data["customers"]
shops_data = data["shops"]

shops = []
for shop_data in shops_data:
shop_name = shop_data["name"]
shop_location = shop_data["location"]
products = shop_data["products"]

shop = Shop(shop_name, shop_location, products)
shops.append(shop)

for customer_data in customers_data:
customer_car = Car(
customer_data["car"]["brand"],
customer_data["car"]["fuel_consumption"]
)
customer = Customer(
customer_data["name"],
customer_data["product_cart"],
customer_data["location"],
customer_data["money"],
customer_car
)

min_total_cost = float("inf")
best_shop = None

customer.initial_money()
for shop in shops:
total_cost = (
customer.distance(
shop.shop_location,
customer_car.fuel_consumption,
fuel_price
)
)

total_cost += sum(
price * shop.products[product_name]
for product_name, price in customer.product_cart.items()
)

print(f"{customer.name}'s trip to the "
f"{shop.shop_name} costs {round(total_cost, 2)}")

if total_cost < min_total_cost and total_cost <= customer.money:
min_total_cost = total_cost
best_shop = shop

if best_shop is not None:
print(f"{customer.name} rides to {best_shop.shop_name}\n")

customer.time_for_shopping(best_shop)
customer.money -= min_total_cost
print(f"{customer.name} rides home")
print(f"{customer.name} "
f"now has {round(customer.money, 2)} dollars\n")

else:
print(f"{customer.name} "
f"doesn't have 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 @@
from typing import List


class Shop:
def __init__(
self,
shop_name: str,
shop_location: List[int],
products: dict
) -> None:
self.shop_name = shop_name
self.shop_location = shop_location
self.products = products
Loading