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

Open
wants to merge 2 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
12 changes: 12 additions & 0 deletions app/car.py
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)
Comment on lines +11 to +12

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.

49 changes: 49 additions & 0 deletions app/customer.py
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()

Choose a reason for hiding this comment

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

The products_cost method returns a dictionary, but you are calling .values() on it, which returns a view object. Ensure that this is intended, as you later sum these values. If products_cost is expected to return a total cost, consider modifying the method to return a single float value instead of a dictionary.

fuel_cons = self.car.total_consumption(self.location, shop.location)

Choose a reason for hiding this comment

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

The total_consumption method in the Car class calculates the round trip fuel consumption. Ensure that this aligns with your requirement to calculate the cost for a round trip, as per the task description.

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]

Choose a reason for hiding this comment

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

Ensure that the cheapest_trip method returns both the shop and the cost correctly. The tuple return type (Shop, float) seems appropriate, but verify that the logic correctly identifies the cheapest trip.


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")
26 changes: 23 additions & 3 deletions app/main.py
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"]]

Choose a reason for hiding this comment

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

Ensure that the Customer class constructor is correctly handling the unpacked values from the customer dictionary. The order of values should match the parameters expected by the Customer class constructor.

shop, cheapest_trip = current_customer.cheapest_trip(shops, fuel_price)

Choose a reason for hiding this comment

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

Similarly, ensure that the Shop class constructor is correctly handling the unpacked values from the shop dictionary. The order of values should match the parameters expected by the Shop class constructor.


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)

Choose a reason for hiding this comment

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

The shopping method in the Shop class should correctly handle the purchase process. Ensure that the method is implemented to print the purchase receipt and update any necessary state.

36 changes: 36 additions & 0 deletions app/shop.py
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 {

Choose a reason for hiding this comment

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

The products_cost method returns a dictionary of product costs. Ensure that this aligns with how you intend to use these costs in other parts of the application, as seen in customer.py.

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

Choose a reason for hiding this comment

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

Consider storing the result of self.products_cost(cart).values() in a variable to avoid recalculating it multiple times. This will improve readability and efficiency.


print(f"Total cost is {sum(self.products_cost(cart).values())}"
f" dollars")
print("See you again!\n")

Choose a reason for hiding this comment

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

The round_price method is designed to return an integer if the price is a whole number. Ensure that this behavior is intended, as it might affect how prices are displayed in the shopping receipt.


@staticmethod
def round_price(price: float) -> int | float:
if int(price) == price:
return int(price)
return price
Loading