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

created shop trip func #589

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
22 changes: 22 additions & 0 deletions app/cars.py
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
2 changes: 1 addition & 1 deletion app/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@
}
}
]
}
}

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

17 changes: 17 additions & 0 deletions app/customers.py
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
78 changes: 75 additions & 3 deletions app/main.py
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

Choose a reason for hiding this comment

The 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 "

Choose a reason for hiding this comment

The 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(

Choose a reason for hiding this comment

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

Please find another, simple way to find chippest_shop
Maybe you can do it during iteration over list_of_shops

min(costs_for_trips.values()))]

Choose a reason for hiding this comment

The 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()
49 changes: 49 additions & 0 deletions app/shops.py
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

Choose a reason for hiding this comment

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

You could use sum and generator expression here


@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!")
Loading