-
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-py-shop-trip #406
Open
Nadiia-developer
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
Nadiia-developer:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from typing import Dict, Any | ||
|
||
|
||
class Car: | ||
FUEL_PRICE = 2.4 | ||
|
||
def __init__(self, data: Dict[str, Any]) -> None: | ||
self.brand = data["brand"] | ||
self.fuel_consumption = data["fuel_consumption"] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import math | ||
from typing import Dict, Any, List | ||
|
||
from app.car.car import Car | ||
from app.shop.shop import Shop | ||
|
||
|
||
class Customer: | ||
|
||
def __init__(self, data: Dict[str, Any]) -> None: | ||
self.name = data["name"] | ||
self.product_cart = data["product_cart"] | ||
self.location = data["location"] | ||
self.money = data["money"] | ||
self.car = Car(data["car"]) | ||
|
||
def calculate_trip_cost(self, shop: Shop) -> float: | ||
distance_to_shop = self.calculate_distance(shop.location) | ||
fuel_consumption_per_km = self.car.fuel_consumption / 100 | ||
fuel_cost_to_shop = ( | ||
distance_to_shop * fuel_consumption_per_km | ||
) * Car.FUEL_PRICE | ||
product_cost = sum( | ||
[ | ||
self.product_cart[product] * shop.products[product] | ||
for product in self.product_cart | ||
] | ||
) | ||
total_trip_cost = fuel_cost_to_shop + product_cost | ||
return round(total_trip_cost, 2) | ||
|
||
def has_enough_money(self, trip_cost: float) -> bool: | ||
return self.money >= trip_cost | ||
|
||
def travel_to_shop(self, shop: Shop) -> None: | ||
self.location = shop.location | ||
print(f"{self.name} rides to {shop.name}\n") | ||
|
||
def travel_home(self, trip_cost: float) -> None: | ||
print(f"{self.name} rides home\n") | ||
remaining_money = self.money - trip_cost | ||
print(f"{self.name} now has {round(remaining_money, 2)} dollars\n") | ||
|
||
def calculate_distance(self, shop_location: List[int]) -> float: | ||
x_location = self.location[0] - shop_location[0] | ||
y_location = self.location[1] - shop_location[1] | ||
distance = math.sqrt(x_location**2 + y_location**2) | ||
return distance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,43 @@ | ||
def shop_trip(): | ||
# write your code here | ||
pass | ||
import json | ||
|
||
from app.customer.customer import Customer | ||
from app.shop.shop import Shop | ||
|
||
|
||
def shop_trip() -> None: | ||
with open("app/config.json", "r") as config_file: | ||
config_data = json.load(config_file) | ||
for customer_data in config_data["customers"]: | ||
customer = Customer(customer_data) | ||
print(f"{customer.name} has {customer.money} dollars\n") | ||
cheapest_shop = None | ||
cheapest_cost = float("inf") | ||
|
||
for shop_data in config_data["shops"]: | ||
shop = Shop(shop_data) | ||
trip_cost = customer.calculate_trip_cost(shop) | ||
if trip_cost < cheapest_cost \ | ||
and customer.has_enough_money(trip_cost): | ||
cheapest_cost = trip_cost | ||
cheapest_shop = shop | ||
|
||
if cheapest_shop: | ||
print( | ||
f"{customer.name}'s " | ||
f"trip to the {cheapest_shop.name} " | ||
f"costs {cheapest_cost:.2f}\n" | ||
) | ||
customer.travel_to_shop(cheapest_shop) | ||
cheapest_shop.sell_products(customer) | ||
customer.travel_home(trip_cost) | ||
print(f"{customer.name} rides to {cheapest_shop.name}\n") | ||
else: | ||
print( | ||
f"{customer.name} " | ||
f"doesn't have enough money " | ||
f"to make a purchase in any shop\n" | ||
) | ||
|
||
|
||
if __name__ == "main": | ||
shop_trip() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import datetime | ||
from typing import Dict, Any | ||
|
||
from app.customer.customer import Customer | ||
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. You cannot import |
||
|
||
|
||
class Shop: | ||
def __init__(self, data: Dict[str, Any]) -> None: | ||
self.name = data["name"] | ||
self.location = data["location"] | ||
self.products = data["products"] | ||
|
||
def sell_products(self, customer: Customer) -> None: | ||
print( | ||
f"\nDate: " | ||
f"{datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')}\n" | ||
) | ||
print(f"Thanks, {customer.name}, for your purchase!\n") | ||
total_cost = 0 | ||
for product, quantity in customer.product_cart.items(): | ||
product_cost = quantity * self.products[product] | ||
print( | ||
f"You have bought: {quantity} {product}s " | ||
f"for {product_cost} dollars\n" | ||
) | ||
total_cost += product_cost | ||
print(f"Total cost is {total_cost} dollars\n") | ||
print("See you again!\n") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do not overload the context manager, place logic not related to the file management outside of its scope.