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

added solution of task #1612

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

Conversation

kostya-savchenko
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 the WorkWithFile class! 😊 Your implementation is close to meeting the task requirements, but there are a few critical issues that need to be addressed before we can approve the pull request:

  1. Exception Handling: The code currently does not handle potential exceptions when parsing integers from the CSV file. This could lead to runtime errors if the file contains invalid data. Please add error handling for Integer.parseInt(lineSplit[1].trim()) to manage potential NumberFormatException.

  2. Method Length and Responsibilities: The getStatistic method is handling multiple responsibilities, making it longer than recommended. Consider breaking it down into smaller methods, each with a single responsibility, to improve readability and maintainability.

  3. Use of Constants: Strings like "supply" and "buy" are used directly in the code. Declaring these as constants will enhance code readability and maintainability.

  4. Method Naming: The current method name getStatistic might not fully convey its operations. Consider renaming it to better reflect its functionality.

  5. Access Modifiers: If getStatistic is only used within the same class, consider using a more restrictive access modifier to adhere to encapsulation principles.

Once these issues are addressed, your code will be in great shape! Keep up the great work, and feel free to reach out if you have any questions or need further assistance. 🚀

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

String line;
while ((line = bufferedReader.readLine()) != null) {
String[] lineSplit = line.split(",");
int value = Integer.parseInt(lineSplit[1].trim());

Choose a reason for hiding this comment

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

Consider adding error handling for Integer.parseInt(lineSplit[1].trim()) to manage potential NumberFormatException if the CSV file contains non-numeric values.

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.

Review Summary

Great job on improving the WorkWithFile class! 🎉 You've successfully implemented exception handling for NumberFormatException and used try-with-resources for file operations, which are excellent practices. However, there are a few areas that could use some refinement:

  1. Method Length and Responsibilities: The getStatistic method is currently handling multiple responsibilities, such as reading from a file, processing data, and writing to a file. Breaking it down into smaller methods, each with a single responsibility, would enhance readability and maintainability.

  2. Use of Constants: The strings "supply" and "buy" are used directly in the code. Declaring these as constants would improve code readability and maintainability.

  3. Access Modifiers: If the getStatistic method is only used within the same class, consider using a more restrictive access modifier to adhere to encapsulation principles.

These improvements will not only make your code cleaner but also more robust and easier to maintain. Keep up the good work, and continue refining your skills! Remember, addressing these points will help you avoid similar issues in the future and enhance your learning experience. 🚀

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

Comment on lines 10 to 47
public void getStatistic(String fromFileName, String toFileName) {
// Variables to store the total supply and buy amounts
int supply = 0;
int buy = 0;
int result;
// Step 1: Read the CSV file
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fromFileName))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] lineSplit = line.split(",");
try {
int value = Integer.parseInt(lineSplit[1].trim());
String lineEach = lineSplit[0].trim();
if (lineEach.equals("supply")) {
supply += value;
} else if (lineEach.equals("buy")) {
buy += value;
}
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid number format", e);
}
}
result = supply - buy;
} catch (IOException e) {
throw new RuntimeException("Error in reading CSV file", e);
}

StringBuilder builderReport = new StringBuilder();
builderReport.append("supply,").append(supply).append(System.lineSeparator());
builderReport.append("buy,").append(buy).append(System.lineSeparator());
builderReport.append("result,").append(result).append(System.lineSeparator());

try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(toFileName))) {
bufferedWriter.write(builderReport.toString());
} catch (IOException e) {
throw new RuntimeException("Error in writing file", e);
}
}

Choose a reason for hiding this comment

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

The getStatistic method is still handling multiple responsibilities, such as reading from a file, processing data, and writing to a file. Consider breaking it down into smaller methods, each with a single responsibility, to improve readability and maintainability.

Comment on lines +23 to +25
if (lineEach.equals("supply")) {
supply += value;
} else if (lineEach.equals("buy")) {

Choose a reason for hiding this comment

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

The strings "supply" and "buy" are used directly in the code. Consider declaring these as constants to enhance code readability and maintainability.

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.

2 participants