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 all 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.
9 changes: 9 additions & 0 deletions app/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"]
72 changes: 72 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import datetime
import math
from typing import Dict, Any, List

from app.car import Car


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
]
Comment on lines +24 to +27

Choose a reason for hiding this comment

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

Do not use list comprehension inside of sum, use generator expression instead.

)
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


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")
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 import Customer
from app.customer import Shop


def shop_trip() -> None:
with open("app/config.json", "r") as config_file:
config_data = json.load(config_file)

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.

for customer_data in config_data["customers"]:
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()