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

Hw 1 solution #1233

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

Conversation

ovodolazov
Copy link

No description provided.

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 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:

  1. Exception Handling: Ensure that the OperationException and FileException classes are defined in your project. These custom exceptions are crucial for handling operation-related and file-related errors appropriately.

  2. Null Checks: Add checks to handle cases where a fruit is not present in the fruitsStorage to prevent potential NullPointerException during supply, return, and purchase operations.

  3. Operation Handler Map: Ensure that the operationHandlerMap includes a check for unregistered operation types to prevent NullPointerException.

  4. Data Parsing: Add error handling for Integer.parseInt to manage potential NumberFormatException if the quantity is not a valid integer.

  5. Data Integrity: Ensure that the input data format always includes a header to avoid missing data when processing reports.

  6. 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.

Comment on lines 9 to 10
public Map<String, Integer> getFruitsStorage() {
return fruitsStorage;

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);

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);

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);

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())) {

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.

Comment on lines 16 to 17
public OperationHandler get(OperationType operationType) {
return operationHandlerMap.get(operationType);

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);

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.

Comment on lines 14 to 15
throw new OperationException("Operation is impossible, not enough quantity of: "
+ fruit);

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);

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);

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
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.


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 and OperationException 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 like IndexOutOfBoundsException and NumberFormatException.
  • 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! ✨

Comment on lines 9 to 10
public Map<String, Integer> getFruitsStorage() {
return fruitsStorage;

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;

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);

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;

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);

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;

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) {

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.

Comment on lines 19 to 21
} catch (NullPointerException e) {
throw new OperationException("Operation is not correct: " + shopOperation);
}

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.

Comment on lines 14 to 16
} catch (NullPointerException e) {
throw new OperationException("Operation is not correct: " + shopOperation);
}

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.

Comment on lines 14 to 16
} catch (NullPointerException e) {
throw new OperationException("Operation is not correct: " + shopOperation);
}

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.

Copy link

@liliia-ponomarenko liliia-ponomarenko left a 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() {

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);

Choose a reason for hiding this comment

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

Suggested change
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);

Choose a reason for hiding this comment

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

the same

Comment on lines 18 to 21
if (operationHandlerMap.get(operationType) != null) {
return operationHandlerMap.get(operationType);
}
throw new OperationException("Operation type is not correct: " + operationType);

Choose a reason for hiding this comment

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

Suggested change
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

Comment on lines 19 to 21
} catch (NullPointerException e) {
throw new OperationException("Operation is not correct: " + shopOperation);
}

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);
}

Choose a reason for hiding this comment

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

Suggested change

Comment on lines 14 to 16
} catch (NullPointerException e) {
throw new OperationException("Operation is not correct: " + shopOperation);
}

Choose a reason for hiding this comment

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

the same

Comment on lines 14 to 16
} catch (NullPointerException e) {
throw new OperationException("Operation is not correct: " + shopOperation);
}

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?
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.

4 participants