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

reed all data from an input CSV file, created a report, and wrote it … #1623

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

Conversation

DaniilSitailo
Copy link

…to newFile

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 task! Your implementation of the getStatistic method in WorkWithFile.java is mostly correct and meets many of the task requirements. However, there are a few critical issues that need to be addressed before we can approve the pull request:

  1. Method Length and Responsibility: The getStatistic method is currently handling multiple responsibilities, which makes it quite lengthy and harder to maintain. It's important to refactor this method into smaller, more focused methods like readData, calculateStatistics, and writeReport. This will improve the readability and maintainability of your code.

  2. Exception Handling: The current approach to exception handling could be improved. Instead of just printing error messages, it's recommended to re-throw a RuntimeException with a meaningful message. This will provide more context when an error occurs and help with debugging.

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

  4. String Concatenation: While your code does not currently use string concatenation inside loops, it's a good practice to use StringBuilder for concatenating strings, even outside of loops, to optimize memory usage.

Please address these issues to ensure your code meets the core requirements and maintains a high standard of quality. Once these changes are made, we can proceed with approving your pull request. Keep up the great work, and don't hesitate to reach out if you have any questions! 😊

✨ 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 11 to 47
public void getStatistic(String fromFileName, String toFileName) {
int totalSupply = 0;
int totalBuy = 0;

try (BufferedReader reader = new BufferedReader(new FileReader(fromFileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
String operation = parts[0].trim();
int amount = Integer.parseInt(parts[1].trim());

// Calculating total supply and buy
if ("supply".equalsIgnoreCase(operation)) {
totalSupply += amount;
} else if ("buy".equalsIgnoreCase(operation)) {
totalBuy += amount;
}

}

} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
} catch (NumberFormatException e) {
System.err.println("Error parsing the amount: " + e.getMessage());
}

int result = totalSupply - totalBuy;

try (BufferedWriter writer = new BufferedWriter(new FileWriter(toFileName))) {
writer.write("supply," + totalSupply + System.lineSeparator());
writer.write("buy," + totalBuy + System.lineSeparator());
writer.write("result," + result + System.lineSeparator());
} catch (IOException e) {
System.err.println("Error writing to the file: " + e.getMessage());
}

}

Choose a reason for hiding this comment

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

The getStatistic method is too large and performs multiple tasks. According to the checklist, methods should be short and responsible for one task. Consider dividing this method into smaller methods, such as readData, calculateStatistics, and writeReport.

try (BufferedReader reader = new BufferedReader(new FileReader(fromFileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");

Choose a reason for hiding this comment

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

Consider using StringBuilder instead of String for concatenating strings in loops to optimize memory usage, as suggested in the checklist.

Comment on lines 23 to 25
if ("supply".equalsIgnoreCase(operation)) {
totalSupply += amount;
} else if ("buy".equalsIgnoreCase(operation)) {

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" should be declared as constants to improve code readability and maintainability, as recommended in the checklist.

Comment on lines 31 to 34
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
} catch (NumberFormatException e) {
System.err.println("Error parsing the amount: " + e.getMessage());

Choose a reason for hiding this comment

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

The exception handling could be improved. Instead of just printing the error message, re-throw a RuntimeException with a meaningful message, as suggested in the checklist.

Comment on lines 43 to 44
} catch (IOException e) {
System.err.println("Error writing to the file: " + e.getMessage());

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, improve exception handling by re-throwing a RuntimeException with a meaningful message.

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

Congratulations on your implementation of the WorkWithFile class! 🎉 Your refactoring of the getStatistic method into smaller, focused methods (readData, writeReport, and buildReport) is a great step towards improving code readability and maintainability. The use of constants for "supply" and "buy" enhances clarity, and your implementation of exception handling by re-throwing a RuntimeException with meaningful messages is commendable.

You've effectively used StringBuilder for efficient string concatenation and ensured proper resource management with try-with-resources for file handling. These are all excellent practices that align with the task requirements.

While your code meets the task requirements, remember to review the comments provided to further refine your code. Addressing any issues mentioned will help you avoid similar problems in the future and enhance your coding skills. Keep up the great work and continue learning from your experiences! 🚀

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

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