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

private static final String SUPPLY = "supply";
private static final String BUY = "buy";

public void getStatistic(String fromFileName, String toFileName) {
int[] statistics = readData(fromFileName);
int totalSupply = statistics[0];
int totalBuy = statistics[1];
int result = totalSupply - totalBuy;
writeReport(toFileName, totalSupply, totalBuy, result);
}

private int[] readData(String fromFileName) {
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(",");

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.

if (parts.length != 2) {
throw new RuntimeException("Invalid line format: " + line);
}

String operation = parts[0].trim();
int amount;
try {
amount = Integer.parseInt(parts[1].trim());
} catch (NumberFormatException e) {
throw new RuntimeException("Error parsing the amount in line: " + line, e);
}

if (SUPPLY.equalsIgnoreCase(operation)) {
totalSupply += amount;
} else if (BUY.equalsIgnoreCase(operation)) {
totalBuy += amount;
} else {
throw new RuntimeException("Unknown operation in line: " + line);
}

}

} catch (IOException e) {
throw new RuntimeException("Error reading the file: " + fromFileName, e);
}

return new int[]{totalSupply, totalBuy};
}

private void writeReport(String toFileName, int totalSupply, int totalBuy, int result) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(toFileName))) {
writer.write(buildReport(totalSupply, totalBuy, result));
} catch (IOException e) {
throw new RuntimeException("Error writing to the file: " + toFileName, e);
}

}

private String buildReport(int totalSupply, int totalBuy, int result) {
StringBuilder report = new StringBuilder();
report.append(SUPPLY).append(",").append(totalSupply).append(System.lineSeparator());
report.append(BUY).append(",").append(totalBuy).append(System.lineSeparator());
report.append("result,").append(result).append(System.lineSeparator());
return report.toString();
}

public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Usage: java WorkWithFile <inputFileName> <outputFileName>");
return;
}

WorkWithFile statistic = new WorkWithFile();
statistic.getStatistic(args[0], args[1]);
}

}