-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Hw 1 solution #1233
base: master
Are you sure you want to change the base?
Hw 1 solution #1233
Conversation
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.
Thank you for your hard work on this project! While your code is generally well-structured and adheres to many best practices, there are a few critical issues that need to be addressed before we can approve the pull request:
-
Exception Handling: Ensure that the
OperationException
andFileException
classes are defined in your project. These custom exceptions are crucial for handling operation-related and file-related errors appropriately. -
Null Checks: Add checks to handle cases where a fruit is not present in the
fruitsStorage
to prevent potentialNullPointerException
during supply, return, and purchase operations. -
Operation Handler Map: Ensure that the
operationHandlerMap
includes a check for unregistered operation types to preventNullPointerException
. -
Data Parsing: Add error handling for
Integer.parseInt
to manage potentialNumberFormatException
if the quantity is not a valid integer. -
Data Integrity: Ensure that the input data format always includes a header to avoid missing data when processing reports.
-
Data Encapsulation: Consider returning an unmodifiable view or a copy of the
fruitsStorage
map to prevent unintended external modifications.
Addressing these issues will enhance the robustness and reliability of your code. Keep up the great work, and I'm confident you'll have these resolved in no time! 😊
✨ 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.
public Map<String, Integer> getFruitsStorage() { | ||
return fruitsStorage; |
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.
Returning a direct reference to the fruitsStorage
map can lead to unintended modifications from outside the class. Consider returning an unmodifiable view or a copy of the map to prevent external modifications.
try { | ||
reportList = Files.readAllLines(Path.of(filePath)); | ||
} catch (IOException e) { | ||
throw new FileException("Can't read data from file: " + filePath); |
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 FileException
class is defined in your project. If not, you need to create this custom exception class to handle file-related errors appropriately.
try { | ||
Files.writeString(Path.of(filePath), report); | ||
} catch (IOException e) { | ||
throw new FileException("Can't write data to file: " + filePath); |
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 FileException
class is defined in your project. If not, you need to create this custom exception class to handle file-related errors appropriately.
return type; | ||
} | ||
} | ||
throw new OperationException("Operation not found by code: " + codeOperation); |
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 OperationException
class is defined in your project. If not, you need to create this custom exception class to handle invalid operation codes appropriately.
@Override | ||
public List<ShopOperation> convertToOperation(List<String> reportList) { | ||
List<ShopOperation> shopOperationList = new ArrayList<>(); | ||
for (String line : reportList.subList(LIST_START, reportList.size())) { |
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.
The method skips the first line of the reportList
, assuming it is a header. Ensure that the input data format always includes a header to avoid missing data.
public OperationHandler get(OperationType operationType) { | ||
return operationHandlerMap.get(operationType); |
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.
Consider adding a check to handle cases where operationType
is not present in the operationHandlerMap
. This will prevent potential NullPointerException
if an unregistered operation type is requested.
@Override | ||
public void handle(ShopOperation shopOperation) { | ||
String fruit = shopOperation.getFruit(); | ||
int quantity = Storage.fruitsStorage.get(fruit); |
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.
Consider adding a check to handle cases where the fruit is not present in the fruitsStorage
. This will prevent potential NullPointerException
if a purchase operation is attempted for a fruit not in storage.
throw new OperationException("Operation is impossible, not enough quantity of: " | ||
+ fruit); |
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 OperationException
class is defined in your project. If not, you need to create this custom exception class to handle operation-related errors appropriately.
@Override | ||
public void handle(ShopOperation shopOperation) { | ||
String fruit = shopOperation.getFruit(); | ||
int quantity = Storage.fruitsStorage.get(fruit); |
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.
Consider adding a check to handle cases where the fruit is not present in the fruitsStorage
. This will prevent potential NullPointerException
if a return operation is attempted for a fruit not in storage.
@Override | ||
public void handle(ShopOperation shopOperation) { | ||
String fruit = shopOperation.getFruit(); | ||
int quantity = Storage.fruitsStorage.get(fruit); |
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.
Consider adding a check to handle cases where the fruit is not present in the fruitsStorage
. This will prevent potential NullPointerException
if a supply operation is attempted for a fruit not in storage.
added if block to method get() in StrategyOperationServiceImpl
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.
Your code demonstrates a solid understanding of the task requirements and adheres to many best practices. However, there are a few areas for improvement:
- Dependency Inversion Principle: Consider using an interface for the
operationHandlerMap
to better adhere to this principle. - Exception Handling: Ensure that custom exceptions like
FileException
andOperationException
are correctly defined and imported. Include original exceptions as causes for better context. - Null and Index Checks: Avoid catching
NullPointerException
for control flow. Validate inputs to prevent exceptions likeIndexOutOfBoundsException
andNumberFormatException
. - Encapsulation: Avoid exposing internal storage maps directly. Provide methods to manipulate data safely.
These improvements will enhance the robustness and maintainability of your code. Keep up the good work and continue learning from these insights! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
public Map<String, Integer> getFruitsStorage() { | ||
return fruitsStorage; |
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.
Exposing the internal storage map directly through getFruitsStorage
can lead to unintended modifications from outside the class. Consider providing specific methods for adding, removing, or updating entries in the storage to maintain better control over the data.
@@ -0,0 +1,20 @@ | |||
package core.basesyntax.file; | |||
|
|||
import exception.FileException; |
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 FileException
class is correctly defined and imported. If it is not present in the project, you need to create it to handle file-related exceptions properly.
try { | ||
reportList = Files.readAllLines(Path.of(filePath)); | ||
} catch (IOException e) { | ||
throw new FileException("Can't read data from file: " + filePath); |
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.
Consider including the original exception e
as a cause in the FileException
to provide more context about the error. For example: throw new FileException("Can't read data from file: " + filePath, e);
@@ -0,0 +1,17 @@ | |||
package core.basesyntax.file; | |||
|
|||
import exception.FileException; |
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 FileException
class is correctly defined and imported. If it is not present in the project, you need to create it to handle file-related exceptions properly.
try { | ||
Files.writeString(Path.of(filePath), report); | ||
} catch (IOException e) { | ||
throw new FileException("Can't write data to file: " + filePath); |
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.
Consider including the original exception e
as a cause in the FileException
to provide more context about the error. For example: throw new FileException("Can't write data to file: " + filePath, e);
import java.util.Map; | ||
|
||
public class StrategyOperationServiceImpl implements StrategyOperationService { | ||
private Map<OperationType, OperationHandler> operationHandlerMap; |
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.
Consider making operationHandlerMap
final to indicate that it should not be reassigned after initialization. This can help prevent accidental modifications to the map reference.
|
||
@Override | ||
public OperationHandler get(OperationType operationType) { | ||
if (operationHandlerMap.get(operationType) != null) { |
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.
You can simplify the check by using operationHandlerMap.containsKey(operationType)
to verify if the map contains the key before retrieving the handler.
} catch (NullPointerException e) { | ||
throw new OperationException("Operation is not correct: " + shopOperation); | ||
} |
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.
Catching NullPointerException
is generally not a best practice for control flow. Instead, consider checking if the fruit exists in the storage before attempting to retrieve its quantity. This can prevent the exception from occurring in the first place.
} catch (NullPointerException e) { | ||
throw new OperationException("Operation is not correct: " + shopOperation); | ||
} |
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.
Catching NullPointerException
is generally not a best practice for control flow. Instead, consider checking if the fruit exists in the storage before attempting to retrieve its quantity. This can prevent the exception from occurring in the first place.
} catch (NullPointerException e) { | ||
throw new OperationException("Operation is not correct: " + shopOperation); | ||
} |
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.
Catching NullPointerException
is generally not a best practice for control flow. Instead, consider checking if the fruit exists in the storage before attempting to retrieve its quantity. This can prevent the exception from occurring in the first place.
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.
Good job! Let’s improve your solution ;)
public class Storage { | ||
public static final Map<String, Integer> fruitsStorage = new HashMap<>(); | ||
|
||
public Map<String, Integer> getFruitsStorage() { |
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.
You can remove this method because your map is public ;)
try { | ||
reportList = Files.readAllLines(Path.of(filePath)); | ||
} catch (IOException e) { | ||
throw new FileException("Can't read data from file: " + filePath); |
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.
throw new FileException("Can't read data from file: " + filePath); | |
throw new FileException("Can't read data from file: " + filePath, e); |
try { | ||
Files.writeString(Path.of(filePath), report); | ||
} catch (IOException e) { | ||
throw new FileException("Can't write data to file: " + filePath); |
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.
the same
if (operationHandlerMap.get(operationType) != null) { | ||
return operationHandlerMap.get(operationType); | ||
} | ||
throw new OperationException("Operation type is not correct: " + operationType); |
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.
if (operationHandlerMap.get(operationType) != null) { | |
return operationHandlerMap.get(operationType); | |
} | |
throw new OperationException("Operation type is not correct: " + operationType); | |
OperationHandler handler = operationHandlerMap.get(operationType) | |
if (handlerMap != null) { | |
return handler; | |
} | |
throw new OperationException("Operation type is not correct: " + operationType); |
Correct this method and get rid of the redundant try-catch blocks in your handlers
} catch (NullPointerException e) { | ||
throw new OperationException("Operation is not correct: " + shopOperation); | ||
} |
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.
remove try-catch block here
} catch (NullPointerException e) { | ||
throw new OperationException("Operation is not correct: " + shopOperation); | ||
} | ||
|
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.
} catch (NullPointerException e) { | ||
throw new OperationException("Operation is not correct: " + shopOperation); | ||
} |
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.
the same
} catch (NullPointerException e) { | ||
throw new OperationException("Operation is not correct: " + shopOperation); | ||
} |
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.
the same
…if the key is in the Storage to prevent NPE. Is this check necessary or unnecessary?
No description provided.