-
Notifications
You must be signed in to change notification settings - Fork 697
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
created shop trip func #589
Changes from 4 commits
9dbe062
57e3959
aa711be
99ec0e9
a19ce69
8eee755
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,22 @@ | ||
import math | ||
|
||
|
||
class Car: | ||
def __init__( | ||
self, | ||
brand: str, | ||
fuel_consumption: int | ||
) -> None: | ||
self.brand = brand | ||
self.fuel_consumption = fuel_consumption | ||
|
||
@staticmethod | ||
def calculate_distance( | ||
location_start: list[int], | ||
location_end: list[int] | ||
) -> float: | ||
return math.sqrt((location_end[0] - location_start[0]) ** 2 | ||
+ (location_end[1] - location_start[1]) ** 2) | ||
|
||
def calculate_trip_cost(self, distance: float, fuel_price: float) -> float: | ||
return distance / 100 * self.fuel_consumption * fuel_price |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,4 +73,4 @@ | |
} | ||
} | ||
] | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from app.cars import Car | ||
|
||
|
||
class Customer: | ||
def __init__( | ||
self, | ||
name: str, | ||
product_cart: dict, | ||
location: list[int], | ||
money: float, | ||
car: Car | ||
) -> None: | ||
self.name = name | ||
self.product_cart = product_cart | ||
self.location = location | ||
self.money = money | ||
self.car = car |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,75 @@ | ||
def shop_trip(): | ||
# write your code here | ||
pass | ||
import json | ||
|
||
from app.shops import Shop | ||
from app.cars import Car | ||
from app.customers import Customer | ||
|
||
|
||
def shop_trip() -> None: | ||
fuel_price = 0.0 | ||
list_of_customers = [] | ||
list_of_shops = [] | ||
|
||
with open("app/config.json", "r") as file_info: | ||
all_info = json.load(file_info) | ||
|
||
Comment on lines
+12
to
+13
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. Do not overload a context manage, perform only load operation within it. |
||
fuel_price = all_info["FUEL_PRICE"] | ||
|
||
for customer in all_info["customers"]: | ||
customer_car = Car(customer["car"]["brand"], | ||
customer["car"]["fuel_consumption"]) | ||
|
||
list_of_customers.append( | ||
Customer( | ||
customer["name"], | ||
customer["product_cart"], | ||
customer["location"], | ||
customer["money"], | ||
customer_car | ||
) | ||
) | ||
|
||
for shop in all_info["shops"]: | ||
list_of_shops.append( | ||
Shop( | ||
shop["name"], | ||
shop["location"], | ||
shop["products"] | ||
) | ||
) | ||
|
||
costs_for_trips = {} | ||
|
||
for customer in list_of_customers: | ||
print(f"{customer.name} has {customer.money} dollars") | ||
for shop in list_of_shops: | ||
cost_for_trip = round( | ||
2 * customer.car.calculate_trip_cost( | ||
customer.car.calculate_distance( | ||
customer.location, | ||
shop.location | ||
), | ||
fuel_price | ||
) | ||
+ shop.calculate_cost_for_products(customer.product_cart) | ||
, 2) | ||
print(f"{customer.name}'s trip to the {shop.name} costs " | ||
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. In case there are no arguments on the same line with an opening parenthesis, your closing parenthesis should not have any arguments on the same line as well. Please, resolve this issue |
||
f"{cost_for_trip}") | ||
costs_for_trips[shop] = cost_for_trip | ||
|
||
if min(costs_for_trips.values()) > customer.money: | ||
print(f"{customer.name} " | ||
f"doesn't have enough money to make a purchase in any shop") | ||
else: | ||
chippest_shop = list(costs_for_trips.keys())[ | ||
list(costs_for_trips.values()).index( | ||
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. Please find another, simple way to find |
||
min(costs_for_trips.values()))] | ||
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. Leave no arguments for closing parenthesis as it is don for an opening one |
||
print(f"{customer.name} rides to {chippest_shop.name}") | ||
customer.location = shop.location | ||
chippest_shop.give_purchase_receipt(customer) | ||
customer.money -= min(costs_for_trips.values()) | ||
print(f"\n{customer.name} rides home\n{customer.name} " | ||
f"now has {customer.money} dollars\n") | ||
|
||
|
||
shop_trip() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import datetime | ||
|
||
from app.customers import Customer | ||
|
||
|
||
class Shop: | ||
def __init__( | ||
self, | ||
name: str, | ||
location: list[int], | ||
products: dict | ||
) -> None: | ||
self.name = name | ||
self.location = location | ||
self.products = products | ||
|
||
def calculate_cost_for_products( | ||
self, | ||
product_cart: dict | ||
) -> float: | ||
total_sum = 0 | ||
for product in product_cart: | ||
total_sum += product_cart[product] * self.products[product] | ||
|
||
return total_sum | ||
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. You could use |
||
|
||
@staticmethod | ||
def format_price(price: float) -> int | float: | ||
if isinstance(price, int): | ||
return price | ||
return int(price) if price.is_integer() else float(price) | ||
|
||
def give_purchase_receipt( | ||
self, | ||
client: Customer | ||
) -> None: | ||
print(f"\nDate: " | ||
f'{datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")}') | ||
print(f"Thanks, {client.name}, for your purchase!\nYou have bought:") | ||
for product in client.product_cart: | ||
current_product = (client.product_cart[product] | ||
* self.products[product]) | ||
print(f"{client.product_cart[product]} {product}s " | ||
f"for " | ||
f"{self.format_price(current_product)} " | ||
f"dollars") | ||
print(f"Total cost is " | ||
f"{self.calculate_cost_for_products(client.product_cart)} " | ||
f"dollars\nSee you again!") |
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.
Add a blank line at the end of the file here