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

Develop #673

Open
wants to merge 3 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
14 changes: 14 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import math


class Car:
def __init__(self, brand: str, fuel_consumption: float) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

def fuel_cost(self, distance: float, fuel_price: float) -> float:
return (distance / 100) * fuel_price * self.fuel_consumption

@staticmethod
def calculate_distance(home: list, shop: list) -> float:
return math.sqrt((shop[0] - home[0]) ** 2 + (shop[1] - home[1]) ** 2)
Comment on lines +13 to +14

Choose a reason for hiding this comment

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

The calculate_distance method assumes that both home and shop lists have exactly two elements (representing x and y coordinates). It would be safer to validate the length of these lists before performing calculations to avoid potential index errors.

53 changes: 53 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from typing import Any
from app.shop import Shop
from app.car import Car


class Customer:
def __init__(self,
client_name: str,
client_products: dict,
location: list,
money: float,
car: dict) -> None:
self.client_name = client_name
self.client_products = client_products
self.location = location
self.money = money
self.car = Car(car["brand"], car["fuel_consumption"])

def total_cost(self, shop: Shop, fuel_price: float) -> float:
distance = self.car.calculate_distance(self.location, shop.location)
fuel_cost = self.car.fuel_cost(distance * 2, fuel_price)
product_cost = shop.products_cost(self.client_products)

Choose a reason for hiding this comment

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

The products_cost method in the Shop class returns -1 if a product is not available. You should handle this case in the total_cost method to avoid incorrect calculations.

return fuel_cost + product_cost

def right_store(self, shops: list, fuel_price: float) -> Any:
best_cost = float("inf")
best_shop = None
for shop in shops:
total_cost = self.total_cost(shop, fuel_price)
print(f"{self.client_name}'s trip to the "
f"{shop.name} costs {round(total_cost, 2)}")
if 0 < total_cost < best_cost:
best_cost = total_cost
best_shop = shop
if self.money > best_cost:

Choose a reason for hiding this comment

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

The condition if self.money > best_cost should be if self.money >= best_cost to ensure the customer can afford the trip even if they have exactly the amount needed.

print(f"{self.client_name} rides to {best_shop.name}\n")
return best_shop, best_cost

def trip_or_stay_home(self, shop: Shop, fuel_price: float) -> None:
total_cost = self.total_cost(shop, fuel_price)
product_cost = shop.products_cost(self.client_products)
if self.money > total_cost:
self.money -= total_cost
self.location = shop.location
shop.purchase_receipt(self.client_name,
self.client_products,
product_cost)
print(f"\n{self.client_name} rides home")
print(f"{self.client_name} now has"
f" {round(self.money, 2)} dollars\n")
else:
print(f"{self.client_name} doesn't have enough"
f" money to make a purchase in any shop")
33 changes: 30 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
def shop_trip():
# write your code here
pass
import os
import json
from app.customer import Customer
from app.shop import Shop


def shop_trip() -> None:
config_path = os.path.join(os.getcwd(), "app", "config.json")
with open(config_path) as file:
info = json.load(file)

fuel_price = info["FUEL_PRICE"]
customers = [
Customer(customer["name"],
customer["product_cart"],
customer["location"],
customer["money"],
customer["car"])
for customer in info["customers"]
]
shops = [
Shop(shop["name"],
shop["location"],
shop["products"])
for shop in info["shops"]]

for customer in customers:
print(f"{customer.client_name} has {customer.money} dollars")
best_shop, best_cost = customer.right_store(shops, fuel_price)
customer.trip_or_stay_home(best_shop, fuel_price)
29 changes: 29 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Shop:
def __init__(self, name: str, location: list, products: dict) -> None:
self.name = name
self. location = location
self.products = products

def products_cost(self, client_products: dict) -> int:
total = 0
for product_name, amount in client_products.items():
if product_name in self.products:
total += self.products[product_name] * amount
else:
return -1

Choose a reason for hiding this comment

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

Returning -1 when a product is not available is a valid approach, but ensure that the calling code handles this case to avoid incorrect calculations or logic errors.

return total

def purchase_receipt(self,
client_name: str,
client_products: dict,
total_cost: float) -> None:
print("Date: 04/01/2021 12:33:41")
print(f"Thanks, {client_name}, for your purchase!")
print("You have bought:")
for product_name, amount in client_products.items():
value = round(self.products[product_name] * amount, 2)
final_value = int(value) if isinstance(value, float) and value.is_integer() else value
print(f"{amount} {product_name}s for"
f" {round(final_value, 2)} dollars")
print(f"Total cost is {round(total_cost, 2)} dollars")
print("See you again!")
Loading