-
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 #661
base: master
Are you sure you want to change the base?
solution #661
Changes from 1 commit
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,7 @@ | ||
from dataclasses import dataclass | ||
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. There is a syntax error in the import statement. It should be |
||
|
||
|
||
@dataclass | ||
class Car: | ||
brand: str | ||
fuel_consumption: float |
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] | ||
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 |
||
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]: | ||
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 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) | ||
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. After the customer makes a purchase, their location should be updated to the shop's location as per the task requirements. Consider adding |
||
|
||
print(f"{self.name} rides home\n" | ||
f"{self.name} now has " | ||
f"{self.money - selected_shop_expenses} dollars\n") |
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) |
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 |
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.
There is a syntax error here. The import statement should be
from dataclasses import dataclass
without the slash (/
).