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

Solution #668

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
53 changes: 53 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import math
from app.shop import Shop


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

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

def cost_of_the_trip(
self,
fuel_prize: float,
customer_location: list[int],
shop_location: list[int]) -> float:
Comment on lines +26 to +27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that customer_location and shop_location are validated as lists of integers. This is important to prevent potential runtime errors if invalid data is passed to the method.

distance = math.sqrt(
(shop_location[0] - customer_location[0]) ** 2
+ (shop_location[1] - customer_location[1]) ** 2)
return round(self.car["fuel_consumption"]
* distance / 100 * fuel_prize, 2)

def does_not_have_enough_money(self) -> None:
print(f"{self.name} doesn't have enough "
f"money to make a purchase in any shop")

def customer_ride_to_shop(self, shop_obj: Shop) -> None:
print(f"{self.name} rides to {shop_obj.name}")

def bought_milk(self, product_milk: float) -> None:
print(
f'{self.product_cart["milk"]} milks '
f'for {self.product_cart["milk"] * product_milk} dollars')
Comment on lines +43 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using double quotes for strings to maintain consistency throughout the code, as recommended by the checklist.


def bought_bread(self, product_bread: float) -> None:
print(
f'{self.product_cart["bread"]} bread '
f'for {self.product_cart["bread"] * product_bread} dollars')
Comment on lines +48 to +49

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using double quotes for strings to maintain consistency throughout the code, as recommended by the checklist.


def bought_butter(self, product_butter: float) -> None:
print(f'{self.product_cart["butter"]} butter '
f'for {self.product_cart["butter"] * product_butter} dollars')
Comment on lines +52 to +53

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using double quotes for strings to maintain consistency throughout the code, as recommended by the checklist.

73 changes: 70 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,70 @@
def shop_trip():
# write your code here
pass
import os
from datetime import datetime
import json

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


def shop_trip() -> None:
config_path = os.path.join(os.path.dirname(__file__), "config.json")
with open(config_path) as config:
config_data = json.load(config)
fuel_prize = config_data["FUEL_PRICE"]

list_customers = [Customer(customers["name"],
customers["product_cart"],
customers["location"],
customers["money"],
customers["car"]
) for customers in config_data["customers"]]

list_shops = [Shop(
shops["name"],
shops["location"],
shops["products"])
for shops in config_data["shops"]]

for customer in list_customers:
customer.customer_has_money()
costs = float("inf")
shop_obj = None
for shop in list_shops:
min_cost = (customer.cost_of_the_trip(
fuel_prize,
customer.location,
shop.location) * 2 + shop.price_products(
customer.product_cart["milk"],
customer.product_cart["bread"],
customer.product_cart["butter"]
))
if min_cost < costs:
costs = min_cost
shop_obj = shop
print(f"{customer.name}'s trip to "
f"the {shop.name} costs {min_cost}")
if customer.money < costs:
Comment on lines +44 to +45

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using double quotes for strings to maintain consistency throughout the code, as recommended by the checklist.

customer.does_not_have_enough_money()

else:
customer.customer_ride_to_shop(shop_obj)
now = datetime.now()
print()
print(now.strftime("Date: %d/%m/%Y %H:%M:%S"))
print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought:")
customer.bought_milk(shop_obj.products["milk"])
customer.bought_bread(shop_obj.products["bread"])
customer.bought_butter(shop_obj.products["butter"])
total_cost = (customer.product_cart["milk"]
* shop_obj.products["milk"]
+ customer.product_cart["bread"]
* shop_obj.products["bread"]
+ customer.product_cart["butter"]
* shop_obj.products["butter"])
print(f"Total cost is {total_cost} dollars")
print("See you again!")
print()
print(f"{customer.name} rides home")
print(f"{customer.name} now has {customer.money - costs} dollars")
print()
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[int], products: dict) -> None:
self.name = name
self.location = location
self.products = products

def price_products(
self,
quantity_milk: int,
quantity_bread: int,
quantity_butter: int
) -> float:
return (self.products["milk"] * quantity_milk
+ self.products["bread"] * quantity_bread
+ self.products["butter"] * quantity_butter)
1 change: 0 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from app.main import shop_trip


def test_shop_trip_output(monkeypatch):
datetime_mock = MagicMock(wrap=datetime.datetime)
datetime_mock.now.return_value = datetime.datetime(2021, 1, 4, 12, 33, 41)
Expand Down
Loading