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 #653

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
10 changes: 10 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Customer:
def __init__(self, customer: dict) -> None:
self.name = customer["name"]
self.product_cart = customer["product_cart"]
self.milk = self.product_cart["milk"]
self.bread = self.product_cart["bread"]
self.butter = self.product_cart["butter"]
self.location = customer["location"]
self.money = customer["money"]
self.car_consumption = customer["car"]["fuel_consumption"]
Comment on lines +3 to +10

Choose a reason for hiding this comment

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

Consider adding error handling for missing keys in the customer dictionary. Using dict.get() with default values can prevent KeyError exceptions if any expected key is missing.

22 changes: 22 additions & 0 deletions app/file_opening.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import json

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


def file_open(name_of_file: str) -> dict:
current_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(current_dir, name_of_file)
with open(config_path) as f:
person_data = json.load(f)
shops = [Shop(shop) for shop in person_data["shops"]]
customers = [
Customer(customer) for customer in person_data["customers"]
]
fuel_price = person_data["FUEL_PRICE"]
Comment on lines +12 to +17

Choose a reason for hiding this comment

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

Consider adding error handling for missing keys in the person_data dictionary. This can prevent KeyError exceptions if any expected key is missing. Using dict.get() with default values or try-except blocks can be helpful.

return {
"fuel_price": fuel_price,
"shops": shops,
"customers": customers
}
56 changes: 53 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
def shop_trip():
# write your code here
pass
from datetime import datetime

from app.trip_cost import calculate_trip_cost
from app.file_opening import file_open


def shop_trip() -> None:
user_data = file_open("config.json")
fuel_price = user_data["fuel_price"]
customers = user_data["customers"]
shops = user_data["shops"]
for customer in customers:
cheeper_shop = dict()
print(f"{customer.name} has {customer.money} dollars")

Choose a reason for hiding this comment

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

The variable name cheeper_shop seems to be a typo. Consider renaming it to cheaper_shop for clarity and correctness.

for shop in shops:
cost = calculate_trip_cost(customer, shop, fuel_price)
cheeper_shop.update({cost: shop})
print(f"{customer.name}'s trip "
f"to the {shop.name} costs "
f"{cost}")
min_cost = min(list(cheeper_shop.keys()))
if min_cost <= customer.money:

Choose a reason for hiding this comment

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

Ensure that cheeper_shop is not empty before calling min(). If it's empty, min() will raise a ValueError. Consider adding a check to handle this case.

customer.location = cheeper_shop[min_cost].location
print(f"{customer.name} rides to {cheeper_shop[min_cost].name}\n")
else:
print(f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop")
continue
price_all_milks = (cheeper_shop[min_cost].price_milk * customer.milk)
if price_all_milks == int(price_all_milks):
price_all_milks = int(price_all_milks)
price_all_breads = (
cheeper_shop[min_cost].price_bread * customer.bread
)
if price_all_breads == int(price_all_breads):
price_all_breads = int(price_all_breads)
price_all_butters = (
cheeper_shop[min_cost].price_butter * customer.butter
)
if price_all_butters == int(price_all_butters):
price_all_butters = int(price_all_butters)
total = price_all_breads + price_all_milks + price_all_butters
date = datetime(2021, 1, 4, 12, 33, 41).strftime("%d/%m/%Y %H:%M:%S")
print(f"Date: {date}")
print(f"Thanks, {customer.name}, for your purchase!"
"\nYou have bought:"
f"\n{customer.milk} milks for {price_all_milks} dollars"
f"\n{customer.bread} breads for {price_all_breads} dollars"
f"\n{customer.butter} butters for {price_all_butters} dollars"
f"\nTotal cost is {total} dollars"
f"\nSee you again!\n"
f"\n{customer.name} rides home\n"
f"{customer.name} now has {customer.money - min_cost} dollars\n")
8 changes: 8 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Shop:
def __init__(self, shop: dict) -> None:
self.name = shop["name"]
self.location = shop["location"]
self.products = shop["products"]
self.price_milk = self.products["milk"]
self.price_bread = self.products["bread"]
self.price_butter = self.products["butter"]
Comment on lines +3 to +8

Choose a reason for hiding this comment

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

Consider adding error handling for missing keys in the shop dictionary. Using dict.get() with default values can prevent KeyError exceptions if any expected key is missing.

17 changes: 17 additions & 0 deletions app/trip_cost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from app.customer import Customer
from app.shop import Shop


def calculate_trip_cost(
customer: Customer,
shop: Shop,
fuel_price: float
) -> float:
distance = (abs(customer.location[0] - shop.location[0])
** 2 + abs(customer.location[1] - shop.location[1])
** 2) ** 0.5
Comment on lines +10 to +12

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 tuples or lists with at least two elements. If not, accessing these indices will raise an IndexError.

cost = ((distance * 2 * customer.car_consumption / 100 * fuel_price)

Choose a reason for hiding this comment

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

Ensure that customer.car_consumption is a valid number. If it's not set or is invalid, this calculation will raise an error.

+ customer.milk * shop.price_milk
+ customer.butter * shop.price_butter
+ customer.bread * shop.price_bread)
return round(cost, 2)
Loading