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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Car:
def __init__(self, brand: str, fuel_consumption: float) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption
41 changes: 41 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from app.car import Car
import math


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

def cost_trip(
self,
location: list[int, int],

Choose a reason for hiding this comment

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

Ensure that the location parameter is correctly typed as a list of integers, i.e., list[int], to match the task requirements.

fuel_pice: float

Choose a reason for hiding this comment

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

There is a typo in the parameter name fuel_pice. It should be fuel_price to match the task description and maintain consistency.

) -> float:
distance = math.sqrt(
(self.location[0] - location[0]) ** 2
+ (self.location[1] - location[1]) ** 2
)
cost = round(
(distance / 100 * self.car.fuel_consumption * fuel_pice * 2), 2
)
return cost

def cost_product(
self,
products: dict
) -> float:
cost = 0
for product in products:
cost += self.products[product] * products[product]
return round(cost, 2)
94 changes: 91 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,91 @@
def shop_trip():
# write your code here
pass
from app.car import Car
from app.customer import Customer
from app.shop import Shop
from datetime import datetime
import json


def shop_trip() -> None:
config = {}
with open("app/config.json", "r") as file:
config = json.load(file)
fuel_price = config["FUEL_PRICE"]
customers = config["customers"]
shops = config["shops"]

for customer in customers:
car = Car(
brand=customer["car"]["brand"],
fuel_consumption=customer["car"]["fuel_consumption"]
)
customer_instance = Customer(
name=customer["name"],
location=customer["location"],
products=customer["product_cart"],
money=customer["money"],
car=car
)
print(
f"{customer_instance.name} has "
f"{customer_instance.money} dollars"
)
min_cost = customer_instance.money
cheapest_shop = Shop(
name=shops[0]["name"],
location=shops[0]["location"],
products=shops[0]["products"]
)
for shop in shops:
shop_instance = Shop(
name=shop["name"],
location=shop["location"],
products=shop["products"]
)
cost = customer_instance.cost_trip(
location=shop_instance.location,
fuel_pice=fuel_price
) + customer_instance.cost_product(

Choose a reason for hiding this comment

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

There is a typo in the parameter name fuel_pice. It should be fuel_price to match the task description and maintain consistency.

products=shop_instance.products
)
print(
f"{customer_instance.name}'s trip "
f"to the {shop_instance.name} costs {cost}"
)
if min_cost > cost:
min_cost = cost
cheapest_shop = shop_instance
if min_cost < customer_instance.money:
print(f"{customer_instance.name} rides to {cheapest_shop.name}")
print()
current_datetime = datetime(2021, 1, 4, 12, 33, 41)
current_datetime = current_datetime.strftime("%d/%m/%Y %H:%M:%S")
print(f"Date: {current_datetime}")
print(f"Thanks, {customer_instance.name}, for your purchase!")
print("You have bought:")
cost_products = 0
for product in customer_instance.products:
cost_any_product = (
customer_instance.products[product]
* cheapest_shop.products[product]
)
if (cost_any_product * 10) % 10 == 0:
cost_any_product = int(cost_any_product)
print(
f"{customer_instance.products[product]} {product}s "
f"for {cost_any_product} dollars"
)
cost_products += cost_any_product
print(f"Total cost is {cost_products} dollars")
print("See you again!")
print()
print(f"{customer_instance.name} rides home")
print(
f"{customer_instance.name} now has "
f"{customer_instance.money - min_cost} dollars"
)
print()
else:
print(
f"{customer_instance.name} doesn't have"
f" enough money to make a purchase in any shop"
)
10 changes: 10 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Shop:
def __init__(
self,
name: str,
location: list[int, int],
products: dict
) -> None:
self.name = name
self.location = location
self.products = products
Loading