-
Notifications
You must be signed in to change notification settings - Fork 725
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 #672
base: master
Are you sure you want to change the base?
solution #672
Changes from all commits
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,4 @@ | ||
class Car: | ||
def __init__(self, brand: str, fuel_consumption: float) -> None: | ||
self.brand = brand | ||
self.fuel_consumption = fuel_consumption |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from app.car import Car | ||
|
||
|
||
class Customer: | ||
def __init__( | ||
self, | ||
name: str, | ||
product_cart: dict, | ||
location: tuple, | ||
money: float, | ||
car: "Car" | ||
) -> None: | ||
self.name = name | ||
self.product_cart = product_cart | ||
self.location = location | ||
self.money = money | ||
self.car = car | ||
|
||
def move_to(self, location: tuple) -> None: | ||
self.location = location |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,73 @@ | ||
def shop_trip(): | ||
# write your code here | ||
pass | ||
import json | ||
from app.shop import Shop | ||
from app.customer import Customer | ||
from app.car import Car | ||
|
||
|
||
def shop_trip() -> None: | ||
with open("app/config.json") as file: | ||
config = json.load(file) | ||
|
||
fuel_price = config["FUEL_PRICE"] | ||
customers = [] | ||
shops = [] | ||
|
||
for shop_data in config["shops"]: | ||
shops.append(Shop(**shop_data)) | ||
|
||
for customer_data in config["customers"]: | ||
car = Car(**customer_data.pop("car")) | ||
customers.append(Customer(**customer_data, car=car)) | ||
|
||
for customer in customers: | ||
print(f"{customer.name} has {customer.money} dollars") | ||
best_shop = None | ||
best_cost = float("inf") | ||
|
||
for shop in shops: | ||
trip_cost, distance = shop.calculate_trip_cost( | ||
customer.location, | ||
fuel_price, | ||
customer.car.fuel_consumption | ||
) | ||
|
||
product_cost, _ = shop.purchase( | ||
customer.name, customer.product_cart | ||
) | ||
total_cost = trip_cost + product_cost | ||
|
||
print(f"{customer.name}'s trip to " | ||
f"the {shop.name} costs {round(total_cost, 2)}") | ||
if total_cost < best_cost and total_cost <= customer.money: | ||
best_cost = total_cost | ||
best_shop = shop | ||
|
||
if best_shop: | ||
trip_cost, _ = best_shop.calculate_trip_cost( | ||
customer.location, | ||
fuel_price, | ||
customer.car.fuel_consumption | ||
) | ||
customer.money -= best_cost | ||
customer.move_to(best_shop.location) | ||
|
||
print(f"{customer.name} rides to {best_shop.name}\n") | ||
product_cost, receipt = best_shop.purchase( | ||
customer.name, customer.product_cart | ||
) | ||
print(receipt) | ||
|
||
trip_home_cost, _ = best_shop.calculate_trip_cost( | ||
customer.location, | ||
fuel_price, | ||
customer.car.fuel_consumption | ||
) | ||
original_location = customer.location | ||
customer.money -= trip_home_cost | ||
customer.move_to(original_location) | ||
|
||
Comment on lines
+65
to
+67
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 customer's location is not correctly updated after the trip home. You should store the original location before moving to the shop and use it to update the customer's location after the trip home. |
||
print(f"{customer.name} rides home") | ||
print(f"{customer.name} now has {customer.money} dollars\n") | ||
else: | ||
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 customer's remaining money is rounded to two decimal places when printed. This will help maintain consistency in the output format. |
||
print(f"{customer.name} doesn't have enough money" | ||
f" to make a purchase in any shop") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from math import sqrt | ||
from datetime import datetime | ||
|
||
|
||
class Shop: | ||
def __init__(self, name: str, location: tuple, products: dict) -> None: | ||
self.name = name | ||
self.location = location | ||
self.products = products | ||
|
||
def calculate_trip_cost( | ||
self, | ||
customer_location: tuple, | ||
fuel_price: float, | ||
fuel_consumption: float | ||
) -> tuple: | ||
distance = sqrt((self.location[0] - customer_location[0]) ** 2 | ||
+ (self.location[1] - customer_location[1]) ** 2) | ||
return round( | ||
distance * 2 * fuel_consumption / 100 * fuel_price, 2 | ||
), distance | ||
|
||
def purchase(self, customer_name: str, product_cart: dict) -> tuple: | ||
total_cost = 0 | ||
specific_time = datetime.now() # Get current time | ||
receipt = [ | ||
f"Date: {specific_time.strftime("%m/%d/%Y %H:%M:%S")}", | ||
f"Thanks, {customer_name}, for your purchase!", | ||
"You have bought:" | ||
] | ||
|
||
for product, quantity in product_cart.items(): | ||
if product in self.products: | ||
cost = self.products[product] * quantity | ||
formatted_cost = int(cost) \ | ||
if cost.is_integer() \ | ||
else round(cost, 1) | ||
Comment on lines
+35
to
+37
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 rounding logic for |
||
receipt.append(f"{quantity} {product}s " | ||
f"for {formatted_cost} dollars") | ||
total_cost += cost | ||
|
||
total_cost = int(total_cost) if total_cost.is_integer() \ | ||
else round(total_cost, 1) | ||
Comment on lines
+42
to
+43
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 rounding logic for |
||
receipt.append(f"Total cost is {total_cost} 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. Ensure that the total cost is rounded to two decimal places as required by the task description. |
||
receipt.append("See you again!\n") | ||
|
||
return total_cost, "\n".join(receipt) |
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.
Ensure that the total cost is rounded to two decimal places when printed. This is important for maintaining consistency in the output format.