-
Notifications
You must be signed in to change notification settings - Fork 701
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
base: master
Are you sure you want to change the base?
solution #653
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"] | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for missing keys in the |
||
return { | ||
"fuel_price": fuel_price, | ||
"shops": shops, | ||
"customers": customers | ||
} |
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable name |
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
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") |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for missing keys in the |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
cost = ((distance * 2 * customer.car_consumption / 100 * fuel_price) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
+ customer.milk * shop.price_milk | ||
+ customer.butter * shop.price_butter | ||
+ customer.bread * shop.price_bread) | ||
return round(cost, 2) |
There was a problem hiding this comment.
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. Usingdict.get()
with default values can preventKeyError
exceptions if any expected key is missing.