-
Notifications
You must be signed in to change notification settings - Fork 741
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 #669
base: master
Are you sure you want to change the base?
Solution #669
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,12 @@ | ||
from dataclasses import dataclass | ||
from math import sqrt | ||
|
||
|
||
@dataclass | ||
class Car: | ||
brand: str | ||
fuel_consumption: float | ||
|
||
def total_consumption(self, start: list, end: list) -> float: | ||
distance = sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) | ||
return round(2 * distance * self.fuel_consumption / 100, 2) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from app.car import Car | ||
from app.shop import Shop | ||
|
||
|
||
class Customer: | ||
def __init__( | ||
self, | ||
name: str, | ||
product_cart: dict, | ||
location: list, | ||
money: int, | ||
car: dict | ||
) -> None: | ||
self.name = name | ||
self.product_cart = product_cart | ||
self.location = location | ||
self.money = money | ||
self.car = Car(*car.values()) | ||
print(f"{self.name} has {self.money} dollars") | ||
|
||
def trip_cost( | ||
self, | ||
shop: Shop, | ||
fuel_price: float | ||
) -> float: | ||
products_cost = shop.products_cost(self.product_cart).values() | ||
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 |
||
fuel_cons = self.car.total_consumption(self.location, shop.location) | ||
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 |
||
return round(sum(products_cost) + fuel_cons * fuel_price, 2) | ||
|
||
def cheapest_trip( | ||
self, | ||
shops: list[Shop], | ||
fuel_price: float | ||
) -> (Shop, float): | ||
trips_cost = [] | ||
for shop in shops: | ||
trip_cost = self.trip_cost(shop, fuel_price) | ||
print(f"{self.name}'s trip to the {shop.name} costs {trip_cost}") | ||
trips_cost.append(trip_cost) | ||
cheapest_trip_index = trips_cost.index(min(trips_cost)) | ||
return shops[cheapest_trip_index], trips_cost[cheapest_trip_index] | ||
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 the |
||
|
||
def return_home( | ||
self, | ||
trip_cost: float, | ||
) -> None: | ||
print(f"{self.name} rides home") | ||
print(f"{self.name} now has " | ||
f"{round(self.money - trip_cost, 2)} dollars\n") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
def shop_trip(): | ||
# write your code here | ||
pass | ||
from json import load | ||
from app.customer import Customer | ||
from app.shop import Shop | ||
|
||
|
||
def shop_trip() -> None: | ||
with open("config.json") as config: | ||
data = load(config) | ||
|
||
fuel_price = data["FUEL_PRICE"] | ||
|
||
for customer in data["customers"]: | ||
current_customer = Customer(*customer.values()) | ||
shops = [Shop(*shop.values()) for shop in data["shops"]] | ||
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 the |
||
shop, cheapest_trip = current_customer.cheapest_trip(shops, 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. Similarly, ensure that the |
||
|
||
if cheapest_trip > current_customer.money: | ||
print(f"{current_customer.name} " | ||
f"doesn't have enough money to make a purchase in any shop") | ||
else: | ||
print(f"{current_customer.name} rides to {shop.name}\n") | ||
shop.shopping(current_customer.name, current_customer.product_cart) | ||
current_customer.return_home(cheapest_trip) | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
|
||
|
||
@dataclass | ||
class Shop: | ||
name: str | ||
location: list | ||
products: dict | ||
|
||
def products_cost(self, cart: dict) -> dict: | ||
return { | ||
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 |
||
product: cart[product] * self.products[product] | ||
for product in cart | ||
} | ||
|
||
def shopping(self, customer_name: str, cart: dict) -> None: | ||
print("Date: ", datetime.now().strftime("%d/%m/%Y %H:%M:%S")) | ||
print(f"Thanks, {customer_name}, for your purchase!") | ||
print("You have bought:") | ||
|
||
products_cost = self.products_cost(cart) | ||
for product in products_cost: | ||
print(f"{cart[product]} {product}s for " | ||
f"{self.round_price(products_cost[product])} dollars") | ||
Comment on lines
+25
to
+26
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 storing the result of |
||
|
||
print(f"Total cost is {sum(self.products_cost(cart).values())}" | ||
f" dollars") | ||
print("See you again!\n") | ||
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 |
||
|
||
@staticmethod | ||
def round_price(price: float) -> int | float: | ||
if int(price) == price: | ||
return int(price) | ||
return price |
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.
The formula for calculating the total fuel consumption seems to assume a round trip (multiplying the distance by 2). If this is intentional, it's fine. Otherwise, if you only need the one-way consumption, you should remove the multiplication by 2.