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

implemented getStatistic() method #1626

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/core/basesyntax/WorkWithFile.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
package core.basesyntax;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class WorkWithFile {
public void getStatistic(String fromFileName, String toFileName) {
int supply = 0;
int buy = 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());

if ("supply".equalsIgnoreCase(operation)) {
supply += 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" are hardcoded. Consider declaring them as constants to enhance readability and maintainability.

buy += amount;
}
}
} catch (IOException e) {
throw new RuntimeException(e);

Choose a reason for hiding this comment

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

Using RuntimeException for exception handling is not ideal. Consider using a more specific exception type or a logging framework for better error handling.

}
writeToFile(supply, buy, toFileName);
}

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 long and handles multiple responsibilities. Consider breaking it down into smaller methods, each responsible for a single task, to improve readability and maintainability.


public void writeToFile(int supply, int buy, String toFileName) {
int result = supply - buy;
try (BufferedWriter writer = new BufferedWriter(new FileWriter(toFileName))) {
writer.write("supply," + supply + "\n");
writer.write("buy," + buy + "\n");
writer.write("result," + result + "\n");

Choose a reason for hiding this comment

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

The string "result" is hardcoded. Consider declaring it as a constant to enhance readability and maintainability.

} catch (IOException e) {
throw new RuntimeException(e);
}
}
}