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

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
7 changes: 7 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Car:
def __init__(self, brand: str, fuel_consumption: float) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

def calculate_fuel_cost(self, distance: float, full_price: float) -> float:
return (self.fuel_consumption / 100) * distance * full_price
30 changes: 30 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from app.car import Car
from decimal import Decimal
import math


class Customer():
def __init__(
self,
name: str,
product_cart: dict[str, int],
location: list[int],
money: Decimal,
car: Car
) -> None:
self.name = name
self.product_cart = product_cart
self.location = location
self.money = money
self.car = car

def distance(self, shop_location: list) -> float:
return math.sqrt(
(self.location[0] - shop_location[0]) ** 2
+ (self.location[1] - shop_location[1]) ** 2)

def ride_home(self, spend_money: float) -> None:
print(f"{self.name} rides home")
money_after_shop = self.money - spend_money
self.money = money_after_shop
print(f"{self.name} now has {money_after_shop} dollars\n")
65 changes: 62 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
def shop_trip():
# write your code here
pass
import json
import os
from app.customer import Customer
from app.shop import Shop
from app.car import Car


def shop_trip() -> None:
config_path = os.path.join(os.path.dirname(__file__), "config.json")
with open(config_path, "r") as file:
data = json.load(file)
fuel_price, customers_, shops_ = data.values()

customers = []
for custom in customers_:
car = Car(**custom["car"])
customer = Customer(
name=custom["name"],
product_cart=custom["product_cart"],
location=custom["location"],
money=custom["money"],
car=car
)
customers.append(customer)

shops = []
for some_shop in shops_:
shop = Shop(
name=some_shop["name"],
location=some_shop["location"],
products=some_shop["products"]
)
shops.append(shop)

for customer in customers:
customer_money = customer.money
print(f"{customer.name} has {customer_money} dollars")
trip_cost_dict = {}
for shop in shops:
total = 0
dist_for_shop = customer.distance(shop.location)
fuel_for_shop = customer.car.calculate_fuel_cost(
dist_for_shop,
fuel_price
)
products_cost = shop.products_cost(customer)
total += round((products_cost + fuel_for_shop * 2), 2)
trip_cost_dict[total] = shop
print(f"{customer.name}'s trip to the {shop.name} costs {total}")
if trip_cost_dict:
if customer.money < min(trip_cost_dict):
print(f"{customer.name} doesn't have enough"
f" money to make a purchase in any shop")
else:
total = min(trip_cost_dict)
current_shop = trip_cost_dict[min(trip_cost_dict)]
print(f"{customer.name} rides to {current_shop.name}\n")
current_shop.print_check(customer)
customer.ride_home(total)


shop_trip()
35 changes: 35 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from app.customer import Customer
from datetime import datetime


class Shop:
def __init__(
self,
name: str,
location: list[int],
products: dict[str, float]
) -> None:
self.name = name
self.location = location
self.products = products

def products_cost(self, customer: Customer) -> float:
product_cart = customer.product_cart
product_cost = 0
for product, qty in product_cart.items():
product_cost += qty * self.products.get(product)

Choose a reason for hiding this comment

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

The expression self.products.get(product) may return None if the product is not found in the shop's inventory. This could lead to a TypeError when multiplying None by qty. Consider providing a default value (e.g., 0) to avoid this issue.

return product_cost

def print_check(self, customer: Customer) -> None:
current_datatime = datetime(2021, 1, 4, 12, 33, 41)
current_datatime = current_datatime.strftime("%d/%m/%Y %H:%M:%S")
print(f"Date: {current_datatime}")
print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought:")
for product, quantity in customer.product_cart.items():
if product in self.products:
cost = self.products[product] * quantity
cost_str = f"{cost:.1f}" if cost % 1 else f"{int(cost)}"
print(f"{quantity} {product}s for {cost_str} dollars")
print(f"Total cost is {self.products_cost(customer)} dollars")
print("See you again!\n")
Loading