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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dataclasses import dataclass

Choose a reason for hiding this comment

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

There is a syntax error here. The import statement should be from dataclasses import dataclass without the slash (/).

Choose a reason for hiding this comment

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

There is a syntax error in the import statement. It should be from dataclasses import dataclass without the slash.



@dataclass
class Car:
brand: str
fuel_consumption: float
74 changes: 74 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import math
from dataclasses import dataclass
from datetime import datetime
from typing import List

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


@dataclass
class Customer:
name: str
product_cart: dict
location: List[int]
money: int
car: Car

def calculate_products_cost(self, shop: Shop,
print_info: bool = False) -> float:
product_coast = sum([shop.products[key] * self.product_cart[key]

Choose a reason for hiding this comment

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

The variable product_coast seems to be a typo. It should be product_cost to reflect its purpose of storing the total cost of products.

for key in self.product_cart])
if print_info:
date = datetime(2021, 1, 4, 12, 33, 41)
date_str = date.strftime("%d/%m/%Y %H:%M:%S")
print(f"Date: {date_str}\n"
f"Thanks, {self.name}, for your purchase!\n"
"You have bought:")
for key, value in self.product_cart.items():
price = shop.products[key] * value
if price % 1 == 0:
price = int(price)
print(f"{value} {key}s for {price} dollars")
print(f"Total cost is {product_coast} dollars\n"
"See you again!\n")

return product_coast

def calculate_travel_expenses(self, shop: Shop,
fuel_price: float) -> float:
distance = math.dist(self.location, shop.location)
return (distance * 2 * self.car.fuel_consumption / 100) * fuel_price

def calculate_shops_trip(self, shops: List[Shop],
fuel_price: float) -> dict:
shops_dict = {}
for shop in shops:
products = self.calculate_products_cost(shop)
trip = self.calculate_travel_expenses(shop, fuel_price)
shops_dict[shop.name] = round(products + trip, 2)
return shops_dict

def print_info(self, shops: List[Shop], fuel_price: float) -> None:
print(f"{self.name} has {self.money} dollars")
shops_trip = self.calculate_shops_trip(shops, fuel_price)
for key, value in shops_trip.items():
print(f"{self.name}'s trip to the {key} costs {value}")

selected_shop_name = min(shops_trip, key=shops_trip.get)
selected_shop_expenses = shops_trip[selected_shop_name]
if self.money >= shops_trip[selected_shop_name]:

Choose a reason for hiding this comment

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

Ensure that when the customer decides to make a purchase, their location is updated to the shop's location as required by the task description.

print(f"{self.name} rides to {selected_shop_name}\n")
else:
print(f"{self.name} doesn't have enough money "
f"to make a purchase in any shop\n")
return
selected_shop_instance = None
for shop in shops:
if shop.name == selected_shop_name:
selected_shop_instance = shop
self.calculate_products_cost(selected_shop_instance, print_info=True)

Choose a reason for hiding this comment

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

After the customer makes a purchase, their location should be updated to the shop's location as per the task requirements. Consider adding self.location = selected_shop_instance.location after the purchase.


print(f"{self.name} rides home\n"
f"{self.name} now has "
f"{self.money - selected_shop_expenses} dollars\n")
36 changes: 33 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
def shop_trip():
# write your code here
pass
import json

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


def shop_trip() -> None:
with open("app/config.json", "r") as file:
data = json.load(file)
fuel_price = data["FUEL_PRICE"]
customers = data["customers"]
shops = data["shops"]
customers_instance = []
for customer in customers:
car_instance = Car(brand=customer["car"]["brand"],
fuel_consumption=customer["car"]
["fuel_consumption"])
customer_instance = Customer(name=customer["name"],
product_cart=customer["product_cart"],
location=customer["location"],
money=customer["money"],
car=car_instance)
customers_instance.append(customer_instance)
shops_instance = []
for shop in shops:
shop_instance = Shop(name=shop["name"],
location=shop["location"],
products=shop["products"])
shops_instance.append(shop_instance)

for customer in customers_instance:
customer.print_info(shops_instance, fuel_price)
8 changes: 8 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class Shop:
name: str
location: list
products: dict
Loading