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

initial commit #1618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
supply,188
buy,115
result,73
43 changes: 43 additions & 0 deletions src/main/java/core/basesyntax/WorkWithFile.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
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 supplySum = 0;
int buySum = 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 (operation.equals("supply")) { // Виправлено назву

Choose a reason for hiding this comment

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

The comment '// Виправлено назву' is not necessary for the functionality of the code. Consider removing it if it's not needed for documentation purposes.

supplySum += amount;
} else if (operation.equals("buy")) {
buySum += amount;
}
}
} catch (IOException e) {
throw new RuntimeException("Error reading file", e);
}

int result = supplySum - buySum;

Choose a reason for hiding this comment

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

Ensure that the calculation of result (supplySum - buySum) aligns with the task requirements. If the task specifies a different calculation, adjust accordingly.


try (BufferedWriter writer = new BufferedWriter(new FileWriter(toFileName))) {
writer.write("supply," + supplySum + System.lineSeparator());
writer.write("buy," + buySum + System.lineSeparator());
writer.write("result," + result + System.lineSeparator());
} catch (IOException e) {
throw new RuntimeException("Error writing to file", e);
}
}

public static void main(String[] args) {
WorkWithFile workWithFile = new WorkWithFile();
String inputFile = "apple.csv";
String outputFile = "output.csv";

workWithFile.getStatistic(inputFile, outputFile);
System.out.println("Файл оброблено успішно!");
}
}