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 1 commit
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
30 changes: 30 additions & 0 deletions src/main/java/core/basesyntax/WorkWithFile.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
package core.basesyntax;

import java.io.*;

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.

}

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) {
e.printStackTrace();

Choose a reason for hiding this comment

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

Instead of using e.printStackTrace(), consider throwing a runtime exception or logging the error using a logging framework. This will provide more control over error handling and is generally more suitable for production environments.

}
}
}
Loading