-
Notifications
You must be signed in to change notification settings - Fork 701
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 #649
base: master
Are you sure you want to change the base?
Solution #649
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Car: | ||
def __init__(self, brand: str, fuel_consumption: float) -> None: | ||
self.brand = brand | ||
self.fuel_consumption = fuel_consumption | ||
|
||
def calculate_fuel_cost(self, distance: float, full_price: float) -> float: | ||
return (self.fuel_consumption / 100) * distance * full_price |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from app.car import Car | ||
from decimal import Decimal | ||
import math | ||
|
||
|
||
class Customer(): | ||
def __init__( | ||
self, | ||
name: str, | ||
product_cart: dict[str, int], | ||
location: list[int], | ||
money: Decimal, | ||
car: Car | ||
) -> None: | ||
self.name = name | ||
self.product_cart = product_cart | ||
self.location = location | ||
self.money = money | ||
self.car = car | ||
|
||
def distance(self, shop_location: list) -> float: | ||
return math.sqrt( | ||
(self.location[0] - shop_location[0]) ** 2 | ||
+ (self.location[1] - shop_location[1]) ** 2) | ||
|
||
def ride_home(self, spend_money: float) -> None: | ||
print(f"{self.name} rides home") | ||
print(f"{self.name} now has {self.money - spend_money} dollars\n") | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,61 @@ | ||
def shop_trip(): | ||
# write your code here | ||
pass | ||
import json | ||
import os | ||
from app.customer import Customer | ||
from app.shop import Shop | ||
from app.car import Car | ||
|
||
|
||
def shop_trip() -> None: | ||
config_path = os.path.join(os.path.dirname(__file__), "config.json") | ||
with open(config_path, "r") as file: | ||
data = json.load(file) | ||
fuel_price, customers_, shops_ = data.values() | ||
|
||
customers = [] | ||
for custom in customers_: | ||
car = Car(**custom["car"]) | ||
customer = Customer( | ||
name=custom["name"], | ||
product_cart=custom["product_cart"], | ||
location=custom["location"], | ||
money=custom["money"], | ||
car=car | ||
) | ||
customers.append(customer) | ||
|
||
shops = [] | ||
for some_shop in shops_: | ||
shop = Shop( | ||
name=some_shop["name"], | ||
location=some_shop["location"], | ||
products=some_shop["products"] | ||
) | ||
shops.append(shop) | ||
|
||
for customer in customers: | ||
customer_money = customer.money | ||
print(f"{customer.name} has {customer_money} dollars") | ||
trip_cost_dict = {} | ||
for shop in shops: | ||
total = 0 | ||
dist_for_shop = customer.distance(shop.location) | ||
fuel_for_shop = customer.car.calculate_fuel_cost( | ||
dist_for_shop, | ||
fuel_price | ||
) | ||
products_cost = shop.products_cost(customer) | ||
total += round((products_cost + fuel_for_shop * 2), 2) | ||
trip_cost_dict[total] = shop | ||
print(f"{customer.name}'s trip to the {shop.name} costs {total}") | ||
if customer.money < min(trip_cost_dict): | ||
print(f"{customer.name} doesn't have enough" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition |
||
f" money to make a purchase in any shop") | ||
else: | ||
total = min(trip_cost_dict) | ||
current_shop = trip_cost_dict[min(trip_cost_dict)] | ||
print(f"{customer.name} rides to {current_shop.name}\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for selecting the shop with the minimum cost is correct, but it would be more efficient to store the result of |
||
current_shop.print_check(customer) | ||
customer.ride_home(total) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
|
||
shop_trip() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from app.customer import Customer | ||
from datetime import datetime | ||
|
||
|
||
class Shop: | ||
def __init__( | ||
self, | ||
name: str, | ||
location: list[int], | ||
products: dict[str, float] | ||
) -> None: | ||
self.name = name | ||
self.location = location | ||
self.products = products | ||
|
||
def products_cost(self, customer: Customer) -> float: | ||
product_cart = customer.product_cart | ||
product_cost = 0 | ||
for product, qty in product_cart.items(): | ||
product_cost += qty * self.products.get(product) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The expression |
||
return product_cost | ||
|
||
def print_check(self, customer: Customer) -> None: | ||
current_datatime = datetime(2021, 1, 4, 12, 33, 41) | ||
current_datatime = current_datatime.strftime("%d/%m/%Y %H:%M:%S") | ||
print(f"Date: {current_datatime}") | ||
print(f"Thanks, {customer.name}, for your purchase!") | ||
print("You have bought:") | ||
milk = customer.product_cart.get("milk") | ||
print(f"{milk} " | ||
f"milks for " | ||
f"{milk * self.products.get('milk')}" | ||
f" dollars") | ||
bread = customer.product_cart.get("bread") | ||
print(f"{bread} " | ||
f"breads for " | ||
f"{round(bread * self.products.get('bread'))}" | ||
f" dollars") | ||
butter = customer.product_cart.get("butter") | ||
print(f"{butter}" | ||
f" butters for " | ||
f"{butter * self.products.get('butter')}" | ||
f" dollars") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
print(f"Total cost is {self.products_cost(customer)} dollars") | ||
print("See you again!\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
ride_home
method, themoney
attribute is not updated after spending money. You should updateself.money
by subtractingspend_money
from it to reflect the current financial state of the customer.