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 #675

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Solution #675

wants to merge 3 commits into from

Conversation

VovaGld
Copy link

@VovaGld VovaGld commented Dec 25, 2024

No description provided.

Copy link

@4ikkamony 4ikkamony left a comment

Choose a reason for hiding this comment

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

Nice, I especially approve of the use of factory methods.
IMHO, main.py does too much work, most of the calculations could be moved to the classes. Consider it)

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Thank you for your hard work on this project! While your code meets many of the task requirements, there are several critical issues related to error handling that need to be addressed before it can be approved. These issues could lead to runtime errors if the expected data is not present, which impacts the core functionality of your application. Specifically, the code assumes the presence of certain keys in dictionaries and JSON data without checking for their existence, which could result in KeyError exceptions. Adding error handling for these cases will make your code more robust and reliable. Once these changes are made, your solution will be much stronger. Keep up the great work, and I look forward to seeing the improvements! 😊

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review button in the Reviewers tab.

app/car.py Outdated
Comment on lines 11 to 12
def create_car(cls, car_info: dict) -> Car:
return cls(car_info["brand"], car_info["fuel_consumption"])

Choose a reason for hiding this comment

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

The create_car method assumes that the dictionary car_info will always contain the keys 'brand' and 'fuel_consumption'. It would be safer to add error handling to manage cases where these keys might be missing, such as using car_info.get('brand') with a default value or raising an informative exception.

app/customer.py Outdated
Comment on lines 17 to 24
def create_customer(cls, customer_info: dict) -> Customer:
return cls(
customer_info["name"],
customer_info["product_cart"],
customer_info["location"],
customer_info["money"],
Car.create_car(customer_info["car"]),
)

Choose a reason for hiding this comment

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

The create_customer method assumes that the dictionary customer_info will always contain the keys 'name', 'product_cart', 'location', 'money', and 'car'. It would be safer to add error handling to manage cases where these keys might be missing, such as using customer_info.get('name') with a default value or raising an informative exception.

app/main.py Outdated
Comment on lines 11 to 12
data_from_file = json.load(file)
fuel_price = data_from_file["FUEL_PRICE"]

Choose a reason for hiding this comment

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

The code assumes that the JSON file contains the key 'FUEL_PRICE'. It would be safer to add error handling to manage cases where this key might be missing, such as using data_from_file.get('FUEL_PRICE') with a default value or raising an informative exception.

app/main.py Outdated
Comment on lines 14 to 17
customers_list = [
Customer.create_customer(customer)
for customer in data_from_file["customers"]
]

Choose a reason for hiding this comment

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

The code assumes that the JSON file contains a list under the key 'customers'. Consider adding error handling to manage cases where this key might be missing or the data is not in the expected format.

app/main.py Outdated
Comment on lines 19 to 21
shops_list = [
Shop.create_shop(shop) for shop in data_from_file["shops"]
]

Choose a reason for hiding this comment

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

The code assumes that the JSON file contains a list under the key 'shops'. Consider adding error handling to manage cases where this key might be missing or the data is not in the expected format.

Comment on lines +52 to +54
price = quantity * min_cost_shop[0].products[product]
formate_price = f"{int(price)}" \
if price == int(price) else f"{price}"

Choose a reason for hiding this comment

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

The calculation of formate_price could be simplified by using Python's string formatting capabilities, such as f'{price:.2f}' to ensure two decimal places, which would handle both integer and float cases more elegantly.

app/shop.py Outdated
Comment on lines 13 to 18
def create_shop(cls, shop_info: dict) -> Shop:
return cls(
shop_info["name"],
shop_info["location"],
shop_info["products"]
)

Choose a reason for hiding this comment

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

The create_shop method assumes that the dictionary shop_info will always contain the keys 'name', 'location', and 'products'. It would be safer to add error handling to manage cases where these keys might be missing, such as using shop_info.get('name') with a default value or raising an informative exception.

Comment on lines +34 to +36
for product in self._products:
products_total_cost += (self._products[product]
* customer.products[product])

Choose a reason for hiding this comment

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

The calculate_total_product_cost method assumes that the customer always wants to buy products available in the shop. Consider adding a check to ensure that the product exists in the customer's product list before attempting to access it, to avoid potential KeyError exceptions.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

Something went wrong while processing the review.
[CODE: 3]

If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

Something went wrong while processing the review.
[CODE: 3]

If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

Something went wrong while processing the review.
[CODE: 3]

If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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


Great job on implementing the code structure and functionality as per the requirements! 🎉 While the code meets most of the task requirements, there are a few areas that need improvement to enhance robustness and clarity:

  1. Error Handling: The error messages for missing keys in the JSON data and other parts of the code are not very informative. Providing more context, such as specifying which key is missing, will make debugging easier.

  2. Runtime Errors: In the calculate_total_product_cost method, ensure that products in the customer's cart exist in the shop's inventory to avoid KeyError. Similarly, check the lengths of home_location and shop_location lists in the calculate_cost_one_way method to prevent IndexError.

  3. Type Consistency: Align the type hint for the money property in the Customer class with the constructor to avoid potential type inconsistencies.

  4. Logical Conditions: Consider using <= instead of < when checking if a customer can afford a trip to ensure they can proceed even if their money is exactly equal to the cost.

These improvements will enhance the code's robustness and user-friendliness. Keep up the good work, and make sure to address these issues in future iterations to avoid similar problems. Your effort and dedication to learning are commendable! 🚀

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

Comment on lines +13 to +15
return cls(car_info["brand"], car_info["fuel_consumption"])
except KeyError as e:
print(e, "not exist")

Choose a reason for hiding this comment

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

The error handling in the create_car method catches KeyError, but the error message 'not exist' is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.

Comment on lines +18 to +22
self, fuel_price: float, home_location: list, shop_location: list
) -> float:
distance = sqrt(
(shop_location[0] - home_location[0]) ** 2
+ (shop_location[1] - home_location[1]) ** 2

Choose a reason for hiding this comment

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

The calculate_cost_one_way method assumes that home_location and shop_location are lists with at least two elements. If these lists are shorter, an IndexError will occur. Consider adding checks to ensure these lists have the expected length before accessing their elements.

Comment on lines +26 to +27
except KeyError as e:
print(e, "not exist")

Choose a reason for hiding this comment

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

Similar to the Car class, the error handling in the create_customer method catches KeyError, but the error message 'not exist' is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.

Comment on lines +46 to +50
def money(self) -> int | float:
return self._money

@money.setter
def money(self, money: int | float) -> None:

Choose a reason for hiding this comment

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

The type hint for the money property is int | float, but the constructor only accepts int. Consider aligning the type hint in the constructor with the property to avoid potential type inconsistencies.

Comment on lines +16 to +17
except KeyError as e:
print(e, "not exist")

Choose a reason for hiding this comment

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

The error message 'not exist' for missing keys in the JSON data is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.

Comment on lines +24 to +25
except KeyError as e:
print(e, "not exist")

Choose a reason for hiding this comment

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

Similar to the previous comment, the error message 'not exist' for missing keys in the JSON data is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.

Comment on lines +32 to +33
except KeyError as e:
print(e, "not exist")

Choose a reason for hiding this comment

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

Again, the error message 'not exist' for missing keys in the JSON data is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.

Comment on lines +58 to +77
if min_cost_shop[1] < customer.money:
print(f"{customer.name} rides to {min_cost_shop[0].name}\n")
print("Date: 04/01/2021 12:33:41")
print(f"Thanks, {customer.name}, for your purchase!")
print("You have bought:")
for product, quantity in customer.products.items():
price = quantity * min_cost_shop[0].products[product]
formate_price = f"{int(price)}" \
if price == int(price) else f"{price}"
print(f"{quantity} {product + 's'} "
f"for {formate_price} dollars")
print(
f"Total cost is "
f"{min_cost_shop[0].calculate_total_product_cost(customer)} "
f"dollars"
)
print("See you again!\n")
print(f"{customer.name} rides home")
customer.money -= min_cost_shop[1]
print(f"{customer.name} now has {customer.money} dollars\n")

Choose a reason for hiding this comment

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

The code assumes that the customer can afford the trip if min_cost_shop[1] < customer.money. However, this does not account for the case where the customer's money is exactly equal to the cost. Consider using <= to ensure the customer can afford the trip even if their money is exactly equal to the cost.

Comment on lines +20 to +21
except KeyError as e:
print(e, "not exist")

Choose a reason for hiding this comment

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

The error handling in the create_shop method catches KeyError, but the error message 'not exist' is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.

Comment on lines +37 to +39
for product in self._products:
products_total_cost += (self._products[product]
* customer.products[product])

Choose a reason for hiding this comment

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

The calculate_total_product_cost method assumes that all products in the customer's cart are available in the shop. If a product is not available, this will raise a KeyError. Consider adding a check to ensure the product exists in the shop's inventory before attempting to access its price.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants