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/Alex #405

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions app/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from dataclasses import dataclass


@dataclass
class Car:
brand: str
fuel_consumption: int | float

def fuel_cost(
self,
distance: int | float,
fuel_price: int | float
) -> float:
return (distance / 100) * self.fuel_consumption * fuel_price
21 changes: 21 additions & 0 deletions app/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List
from dataclasses import dataclass
from app.car import Car


@dataclass
class Customer:
name: str
product_cart: dict
location: List[int]
money: int | float
car: Car

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

def ride_to_home(self) -> None:
print(f"{self.name} rides home")

def money_end(self) -> None:
print(f"{self.name} now has {self.money} dollars\n")
58 changes: 55 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
def shop_trip():
# write your code here
pass
import json
import os

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


def shop_trip() -> None:
current_directory = os.getcwd()
configuration_file = "app/config.json"
file_path = os.path.join(current_directory, configuration_file)
with open(file_path, "r") as file:
data = json.load(file)
fuel_price = data["FUEL_PRICE"]
customers = data["customers"]
customer_list = [
Customer(customer["name"],

Choose a reason for hiding this comment

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

rename customer_list to customers_instances

Copy link
Author

Choose a reason for hiding this comment

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

fixed

customer["product_cart"],
customer["location"],
customer["money"],
Car(customer["car"]["brand"],
customer["car"]["fuel_consumption"],
)) for customer in customers
]

shops = data["shops"]
shop_list = [
Shop(shop["name"],

Choose a reason for hiding this comment

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

The same rename

Copy link
Author

Choose a reason for hiding this comment

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

fixed

shop["location"],
shop["products"]
) for shop in shops
]

for customer in customer_list:
customer.money_start()
shops_cost = {}
for shop in shop_list:
products_cost = shop.products_cost(customer)
fuel_cost = customer.car.fuel_cost(shop.distance(customer),
fuel_price)
trip_cost = round(products_cost + fuel_cost * 2, 2)
print(f"{customer.name}'s trip to the "
f"{shop.name} costs {trip_cost}")
shops_cost.update({trip_cost: shop})
min_shop = shops_cost[min(shops_cost)]
if customer.money >= min(shops_cost):
min_shop.ride_to_shop(customer)
min_shop.print_receipt(customer)
customer.ride_to_home()
customer.money -= min(shops_cost)
customer.money_end()
else:
print(f"{customer.name} doesn't have enough "
f"money to make a purchase in any shop")
41 changes: 41 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import math
import datetime

from dataclasses import dataclass
from typing import List
from app.customer import Customer


def format_num(num: int | float) -> int | float:
float_num = float(round(num, 2))
return int(float_num) if float_num.is_integer() else float_num


@dataclass
class Shop:
name: str
location: List[int]
products: dict

def distance(self, customer: Customer) -> float:
return math.dist(self.location, customer.location)

def ride_to_shop(self, customer: Customer) -> None:
print(f"{customer.name} rides to {self.name}\n")

def products_cost(self, customer: Customer) -> int | float:
return sum(self.products[prod] * customer.product_cart[prod]
for prod in self.products.keys())

Choose a reason for hiding this comment

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

You can iterate through items instead of usage self.products[prod]

Copy link
Author

Choose a reason for hiding this comment

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

Ok, corrected.


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