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-py-shop-trip #406

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Empty file added app/__init__.py
Empty file.
Empty file added app/car/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions app/car/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Dict, Any


class Car:
FUEL_PRICE = 2.4

def __init__(self, data: Dict[str, Any]) -> None:
self.brand = data["brand"]
self.fuel_consumption = data["fuel_consumption"]
Empty file added app/customer/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions app/customer/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import math
from typing import Dict, Any, List

from app.car.car import Car
from app.shop.shop import Shop


class Customer:

def __init__(self, data: Dict[str, Any]) -> None:
self.name = data["name"]
self.product_cart = data["product_cart"]
self.location = data["location"]
self.money = data["money"]
self.car = Car(data["car"])

def calculate_trip_cost(self, shop: Shop) -> float:
distance_to_shop = self.calculate_distance(shop.location)
fuel_consumption_per_km = self.car.fuel_consumption / 100
fuel_cost_to_shop = (
distance_to_shop * fuel_consumption_per_km
) * Car.FUEL_PRICE
product_cost = sum(
[
self.product_cart[product] * shop.products[product]
for product in self.product_cart
]
)
total_trip_cost = fuel_cost_to_shop + product_cost
return round(total_trip_cost, 2)

def has_enough_money(self, trip_cost: float) -> bool:
return self.money >= trip_cost

def travel_to_shop(self, shop: Shop) -> None:
self.location = shop.location
print(f"{self.name} rides to {shop.name}\n")

def travel_home(self, trip_cost: float) -> None:
print(f"{self.name} rides home\n")
remaining_money = self.money - trip_cost
print(f"{self.name} now has {round(remaining_money, 2)} dollars\n")

def calculate_distance(self, shop_location: List[int]) -> float:
x_location = self.location[0] - shop_location[0]
y_location = self.location[1] - shop_location[1]
distance = math.sqrt(x_location**2 + y_location**2)
return distance
46 changes: 43 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
def shop_trip():
# write your code here
pass
import json

from app.customer.customer import Customer
from app.shop.shop import Shop


def shop_trip() -> None:
with open("app/config.json", "r") as config_file:
config_data = json.load(config_file)
for customer_data in config_data["customers"]:

Choose a reason for hiding this comment

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

Do not overload the context manager, place logic not related to the file management outside of its scope.

customer = Customer(customer_data)
print(f"{customer.name} has {customer.money} dollars\n")
cheapest_shop = None
cheapest_cost = float("inf")

for shop_data in config_data["shops"]:
shop = Shop(shop_data)
trip_cost = customer.calculate_trip_cost(shop)
if trip_cost < cheapest_cost \
and customer.has_enough_money(trip_cost):
cheapest_cost = trip_cost
cheapest_shop = shop

if cheapest_shop:
print(
f"{customer.name}'s "
f"trip to the {cheapest_shop.name} "
f"costs {cheapest_cost:.2f}\n"
)
customer.travel_to_shop(cheapest_shop)
cheapest_shop.sell_products(customer)
customer.travel_home(trip_cost)
print(f"{customer.name} rides to {cheapest_shop.name}\n")
else:
print(
f"{customer.name} "
f"doesn't have enough money "
f"to make a purchase in any shop\n"
)


if __name__ == "main":
shop_trip()
Empty file added app/shop/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions app/shop/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import datetime
from typing import Dict, Any

from app.customer.customer import Customer
Copy link

@Dimosphen1 Dimosphen1 Oct 11, 2023

Choose a reason for hiding this comment

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

You cannot import Customer here because you import Shop inside of the app/customer/customer.py. You could read more about cyclic imports in Python and try to resolve them by reformatting the project structure upon the next pull request.



class Shop:
def __init__(self, data: Dict[str, Any]) -> None:
self.name = data["name"]
self.location = data["location"]
self.products = data["products"]

def sell_products(self, customer: Customer) -> None:
print(
f"\nDate: "
f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n"
)
print(f"Thanks, {customer.name}, for your purchase!\n")
total_cost = 0
for product, quantity in customer.product_cart.items():
product_cost = quantity * self.products[product]
print(
f"You have bought: {quantity} {product}s "
f"for {product_cost} dollars\n"
)
total_cost += product_cost
print(f"Total cost is {total_cost} dollars\n")
print("See you again!\n")
Loading