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

Develop #414

Open
wants to merge 2 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
17 changes: 17 additions & 0 deletions app/classes/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import annotations
from app.classes.from_dict import FromDict


class Car(FromDict):
def __init__(self,
brand: str = "",
fuel_consumption: int | float = 0.0) -> None:
Comment on lines +7 to +8

Choose a reason for hiding this comment

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

Do you really want to accept creating cars with "" brand and 0.0 fuel_consumption?
Skip adding default values here, fix everywhere

self.brand = brand
self.fuel_consumption = fuel_consumption

@staticmethod
def from_dict(cls_dict: dict) -> Car:
new_car = Car()
for key in cls_dict:
setattr(new_car, key, cls_dict[key])
return new_car
Comment on lines +13 to +17

Choose a reason for hiding this comment

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

To create instances you can just use ** to unpack and list comprehension. It will look much more readable, and you won't need to create FromDict class. Also think about method and parameter names to increase readability
And this method better to be classmethod, not static

Choose a reason for hiding this comment

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

Same everywhere

28 changes: 28 additions & 0 deletions app/classes/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import annotations

from app.classes.car import Car
from app.classes.from_dict import FromDict


class Customer(FromDict):
def __init__(self,
name: str = "",
product_cart: dict = None,
location: list = None,
money: float = 0.0,
car: Car = Car()) -> None:
self.name = name
self.product_cart = product_cart
self.location = location
self.money = money
self.car = car

@staticmethod
def from_dict(cls_dict: dict) -> Customer:
new_customer = Customer()
for key in cls_dict:
if key == "car":
setattr(new_customer, key, Car.from_dict(cls_dict[key]))
continue
setattr(new_customer, key, cls_dict[key])
return new_customer
8 changes: 8 additions & 0 deletions app/classes/from_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from abc import ABC, abstractmethod


class FromDict(ABC):
@staticmethod
@abstractmethod
def from_dict(cls_dict: dict) -> None:
pass
39 changes: 39 additions & 0 deletions app/classes/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

from app.classes.from_dict import FromDict
from app.classes.customer import Customer


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

@staticmethod
def from_dict(cls_dict: dict) -> Shop:
new_shop = Shop()
for key in cls_dict:
setattr(new_shop, key, cls_dict[key])
return new_shop

def print_receipt(self, customer: Customer) -> None:
print("Date: 04/01/2021 12:33:41")
print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought: ")

total_cost = 0
for key, value in customer.product_cart.items():
product_cost = self.products[key] * value

if product_cost == int(product_cost):
product_cost = int(product_cost)

print(f"{value} {key}s for {product_cost} dollars")
total_cost += self.products[key] * value

print(f"Total cost is {total_cost} dollars")
print("See you again!\n")
51 changes: 48 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
def shop_trip():
# write your code here
pass
import app.utilities as util
from app.classes.customer import Customer
from app.classes.shop import Shop


def shop_trip() -> None:
config = util.parse_json("app/config.json")

fuel_price = config["FUEL_PRICE"]
customers = [Customer.from_dict(customer)
for customer in config["customers"]]
shops = [Shop.from_dict(shop)
for shop in config["shops"]]
Comment on lines +10 to +13

Choose a reason for hiding this comment

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

You can just use unpacking, that I mentioned in previoius comments


for customer in customers:
print(f"{customer.name} has {customer.money} dollars")

closest_shop = 0
min_cost = 100
for index, shop in enumerate(shops):
trip_cost = round(
util.calculate_trip_cost(fuel_price, customer, shop),
2
)

print(f"{customer.name}'s trip to the"
f" {shop.name} costs {trip_cost}")

if trip_cost < min_cost:
min_cost = trip_cost
closest_shop = index

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

print(f"{customer.name} rides to {shops[closest_shop].name}\n")
shops[closest_shop].print_receipt(customer)
customer.money -= round(
util.calculate_trip_cost(fuel_price,
customer,
shops[closest_shop]),
2

Choose a reason for hiding this comment

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

this 2 looks really lonely here (

)

print(f"{customer.name} rides home")
print(f"{customer.name} now has {customer.money} dollars\n")
Comment on lines +6 to +48

Choose a reason for hiding this comment

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

Consider split into methods

28 changes: 28 additions & 0 deletions app/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json

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


def parse_json(json_file: str) -> dict:
with open(json_file) as file:
return json.load(file)


def get_shopping_cost(customer: Customer, shop: Shop) -> float:
cost = 0
for item in customer.product_cart:
cost += customer.product_cart[item] * shop.products[item]
return cost


def calculate_trip_cost(fuel_price: float,
customer: Customer,
shop: Shop) -> float:
distance = ((customer.location[0] - shop.location[0]) ** 2
+ (customer.location[1] - shop.location[1]) ** 2) ** 0.5

ride_cost = (customer.car.fuel_consumption / 100) * distance * fuel_price
shopping_cost = get_shopping_cost(customer, shop)

return ride_cost * 2 + shopping_cost