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 method getStatistic #1619

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
47 changes: 47 additions & 0 deletions src/main/java/core/basesyntax/WorkWithFile.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
package core.basesyntax;

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

public class WorkWithFile {
public void getStatistic(String fromFileName, String toFileName) {
File file = new File(fromFileName);

if (!file.exists()) {
System.out.println("the file does not exist.");
return;
Comment on lines +15 to +16

Choose a reason for hiding this comment

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

Currently, the code only prints a message when the file does not exist and then returns. Consider throwing an exception or logging a more detailed message to inform the caller about the missing file, as per the task requirements.

}

int supplyTotal = 0;
int buyTotal = 0;

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;

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

if (operation.equals("supply")) {
supplyTotal += amount;
} else if (operation.equals("buy")) {
buyTotal += amount;
}
}
} catch (IOException e) {
System.out.println("Error while reading file: " + e.getMessage());
return;

Choose a reason for hiding this comment

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

Returning immediately on an IOException without any notification or logging might make it difficult to debug issues. Consider logging the exception or rethrowing it to provide more context to the caller.

Choose a reason for hiding this comment

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

Returning from the method after catching an IOException without further action might not be the best approach. Consider re-throwing the exception or logging it with more context to aid in debugging.

}

int result = supplyTotal - buyTotal;

try (BufferedWriter writer = new BufferedWriter(new FileWriter(toFileName))) {
writer.write("supply," + supplyTotal);
writer.newLine();
writer.write("buy," + buyTotal);
writer.newLine();
writer.write("result," + result);
} catch (IOException e) {
System.out.println("Error while writing file: " + e.getMessage());
return;

Choose a reason for hiding this comment

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

Similar to the previous catch block, consider logging the IOException or rethrowing it to provide more context and aid in debugging.

Choose a reason for hiding this comment

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

Similar to the previous catch block, returning after catching an IOException without further action is not ideal. Consider re-throwing the exception or logging it with more context.

}
}
}