-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a5a904
commit c068044
Showing
4 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Car: | ||
def __init__(self, brand: str, fuel_consumption: int | float) -> None: | ||
self.brand = brand | ||
self.fuel_consumption = fuel_consumption | ||
|
||
def calculate_fuel_cost(self, first_location: list, second_location: list, fuel_price: float) -> float: | ||
|
||
distance = ((first_location[0] - second_location[0]) ** 2 + | ||
(first_location[1] - second_location[1]) ** 2) ** 0.5 | ||
|
||
return (self.fuel_consumption / 100) * distance * fuel_price * 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from __future__ import annotations | ||
from typing import List | ||
|
||
from app.car import Car | ||
from app.shop import Shop | ||
|
||
|
||
class Customer: | ||
def __init__( | ||
self, name: str, | ||
product_cart: dict, | ||
location: list, | ||
money: int | float, | ||
car: dict | ||
) -> None: | ||
self.name = name | ||
self.product_cart = product_cart | ||
self.location = location | ||
self.money = money | ||
self.car = Car(**car) | ||
self.cheapest_shop = None | ||
self.enough_money = False | ||
self.total_cost = 0 | ||
|
||
def choose_the_best_shop(self, shops_list: List[Shop], fuel_price: float) -> None: | ||
shops = {} | ||
for shop in shops_list: | ||
costs_of_product = shop.calculate_amount_for_products(self.product_cart) | ||
fuel_cost = self.car.calculate_fuel_cost( | ||
self.location, shop.location, fuel_price | ||
) | ||
shops[shop] = round(costs_of_product + fuel_cost, 2) | ||
|
||
self.cheapest_shop = min(shops, key=shops.get) | ||
if self.money >= shops[self.cheapest_shop]: | ||
self.enough_money = True | ||
self.total_cost = round(shops[self.cheapest_shop], 2) | ||
|
||
print(f"{self.name} has {self.money} dollars") | ||
|
||
for shop in shops: | ||
print(f"{self.name}'s trip to the {shop.name} costs {shops[shop]}") | ||
|
||
def ride_to_the_shop(self) -> None: | ||
|
||
if self.enough_money: | ||
print(f"{self.name} rides to {self.cheapest_shop.name}\n") | ||
self.location = self.cheapest_shop.location | ||
else: | ||
print(f"{self.name} doesn't have enough money to make a purchase in any shop") | ||
|
||
def buy_products(self) -> None: | ||
if self.enough_money: | ||
self.cheapest_shop.print_bill( | ||
self.product_cart, self.name | ||
) | ||
|
||
self.money -= self.total_cost | ||
|
||
def ride_to_home(self) -> None: | ||
if self.enough_money: | ||
print( | ||
f"{self.name} rides home\n" | ||
f"{self.name} now has {self.money} dollars\n" | ||
) | ||
|
||
@classmethod | ||
def create_customers(cls, customers: List) -> List[Customer]: | ||
return [cls(**customer) for customer in customers] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
def shop_trip(): | ||
# write your code here | ||
pass | ||
import json | ||
from pathlib import Path | ||
|
||
from app.customer import Customer | ||
from app.shop import Shop | ||
|
||
|
||
def shop_trip() -> None: | ||
file_path = Path(__file__).parent / "config.json" | ||
with open(file_path) as config: | ||
config = json.load(config) | ||
|
||
fuel_price = config.get("FUEL_PRICE") | ||
customers = Customer.create_customers(config.get("customers")) | ||
shops = Shop.create_shops(config.get("shops")) | ||
|
||
for customer in customers: | ||
customer.choose_the_best_shop( | ||
shops_list=shops, | ||
fuel_price=fuel_price | ||
) | ||
customer.ride_to_the_shop() | ||
customer.buy_products() | ||
customer.ride_to_home() | ||
|
||
|
||
if __name__ == "__main__": | ||
shop_trip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
import datetime | ||
from typing import List | ||
|
||
|
||
class Shop: | ||
def __init__( | ||
self, | ||
name: str, | ||
location: list, | ||
products: dict | ||
) -> None: | ||
self.name = name | ||
self.location = location | ||
self.products = products | ||
|
||
def calculate_amount_for_products(self, product_cart: dict) -> int | float: | ||
total_amount = 0 | ||
for product_name, count in product_cart.items(): | ||
total_amount += self.products.get(product_name, 0) * count | ||
return total_amount | ||
|
||
def print_bill(self, product_cart: dict, name: str) -> float: | ||
|
||
print(f"Date: {datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n" | ||
f"Thanks, {name}, for your purchase!\n" | ||
"You have bought: ") | ||
|
||
total_amount = 0 | ||
for product_name, count in product_cart.items(): | ||
amount = self.products.get(product_name, 0) * count | ||
total_amount += amount | ||
print(f"{count} {product_name}s for {round(amount, 2)} dollars") | ||
|
||
print( | ||
f"Total cost is {round(total_amount, 2)} dollars\n" | ||
f"See you again!\n" | ||
) | ||
|
||
@classmethod | ||
def create_shops(cls, shops: List) -> List[Shop]: | ||
return [cls(**shop) for shop in shops] |