-
Notifications
You must be signed in to change notification settings - Fork 701
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
base: master
Are you sure you want to change the base?
solution #650
Changes from 1 commit
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,4 @@ | ||
class Car: | ||
def __init__(self, brand: str, fuel_consumption: float) -> None: | ||
self.brand = brand | ||
self.fuel_consumption = fuel_consumption |
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], | ||
fuel_pice: float | ||
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. There is a typo in the parameter name |
||
) -> 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) |
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( | ||
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. There is a typo in the parameter name |
||
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" | ||
) |
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 |
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.
Ensure that the
location
parameter is correctly typed as a list of integers, i.e.,list[int]
, to match the task requirements.