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 #610

Open
wants to merge 6 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.
Empty file added app/customers/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions app/customers/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Car:
def __init__(self, brand: str, fuel_consumption: float) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

def calculate_fuel_spent(
self,
distance: float,
fuel_price: float
) -> float:
return self.fuel_consumption / 100 * distance * fuel_price
18 changes: 18 additions & 0 deletions app/customers/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from app.customers.car import Car
from app.trips.point import Point


class Customer:
def __init__(
self,
name: str,
product_cart: dict,
location: list,
money: int | float,
car: dict,
) -> None:
self.name = name
self.cart = product_cart
self.location = Point(*location)
self.money = money
self.car = Car(**car)
33 changes: 30 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
def shop_trip():
# write your code here
pass
import json
from pathlib import Path

from app.trips.trip import Trip
from app.customers.customer import Customer
from app.shops.shop import Shop


base_dir = Path(__file__).resolve().parent
file_name = "config.json"


def get_data_from_file() -> tuple:
with open(base_dir / file_name, "r") as file:
file_data = json.load(file)
return (
file_data["FUEL_PRICE"],
file_data["customers"],
file_data["shops"]
)


def shop_trip() -> None:

fuel_price, customers, shops = get_data_from_file()
customers = [Customer(**customer_info) for customer_info in customers]
shops = [Shop(**shop_info) for shop_info in shops]

for customer in customers:
Trip(fuel_price, customer, shops).print_trip_log()
Empty file added app/shops/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions app/shops/cart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

class Cart:
def __init__(self, cart: dict, products: dict) -> None:
self.cart = cart
self.products = products

def calculate_product_cost(self) -> int | float:
total_cost = 0
for product in self.cart:
total_cost += self.cart[product] * self.products[product]
return total_cost

def generate_receipt(self) -> None:
for product in self.cart:
product_cost = self.cart[product] * self.products[product]

# removing .0 from product_cost
product_cost = int(product_cost) \
if product_cost % 1 == 0 else product_cost

print(
f"{self.cart[product]} {product}s for {product_cost} dollars"
)
25 changes: 25 additions & 0 deletions app/shops/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from datetime import datetime

from app.customers.customer import Customer
from app.shops.cart import Cart
from app.trips.point import Point


class Shop:
def __init__(self, name: str, location: list, products: dict) -> None:
self.name = name
self.location = Point(*location)
self.products = products

def print_shop_log(self, spending: dict, customer: Customer) -> None:
# setting date manually since test checks for const str
date = datetime.strptime("04/01/2021 12:33:41", "%d/%m/%Y %H:%M:%S")
print(f"Date: {datetime.strftime(date, '%d/%m/%Y %H:%M:%S')}")

print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought:")
Cart(customer.cart, self.products).generate_receipt()
print(f"Total cost is " f"{spending['products_cost']} dollars")

customer.money -= spending["total"]
print("See you again!\n")
Empty file added app/trips/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions app/trips/point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# flake8: noqa: VNE001
from __future__ import annotations
import math

class Point:
def __init__(self, x: int, y: int) -> None:
self.x = x #noqa: VNE001
self.y = y #noqa: VNE001

def calculate_distance(self, other: Point) -> float:
return math.sqrt((other.x - self.x) ** 2 + (other.y - self.y) ** 2)
66 changes: 66 additions & 0 deletions app/trips/trip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from app.customers.customer import Customer
from app.shops.shop import Shop
from app.trips.point import Point
from app.shops.cart import Cart


class Trip:
def __init__(
self,
fuel_price: float,
customer: Customer,
shops: list[Shop]
) -> None:
self.fuel_price = fuel_price
self.customer = customer
self.shops = shops
self.cost = {}

def print_trip_log(self) -> None:
print(f"{self.customer.name} has {self.customer.money} dollars")

for shop in self.shops:
self.cost[shop] = self.calculate_shop_trip_cost(shop)

print(
f"{self.customer.name}'s trip to the "
f"{shop.name} costs {self.cost[shop]['total']}"
)

closest_shop = self.find_closest_shop()

if self.customer.money > self.cost[closest_shop]["total"]:
print(f"{self.customer.name} rides to {closest_shop.name}\n")

closest_shop.print_shop_log(self.cost[closest_shop], self.customer)

print(f"{self.customer.name} rides home")
print(f"{self.customer.name} now has "
f"{self.customer.money} dollars\n")

else:
print(
f"{self.customer.name} doesn't have enough "
f"money to make a purchase in any shop"
)

def calculate_shop_trip_cost(self, shop: Shop) -> dict:
distance = Point.calculate_distance(
self.customer.location,
shop.location
)
trip_fuel_price = 2 * self.customer.car.calculate_fuel_spent(
distance, self.fuel_price
)

product_cost = Cart(
self.customer.cart,
shop.products
).calculate_product_cost()

total_spending = round(product_cost + trip_fuel_price, 2)

return {"products_cost": product_cost, "total": total_spending}

def find_closest_shop(self) -> dict:
return min(self.cost, key=lambda shop: self.cost[shop]["total"])
Loading