generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
DaniilSitailo
wants to merge
5
commits into
mate-academy:master
Choose a base branch
from
DaniilSitailo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb50973
reed all data from an input CSV file, created a report, and wrote it …
DaniilSitailo 04fcadc
reed all data from an input CSV file, created a report, and wrote it …
DaniilSitailo 7bc783e
reed all data from an input CSV file, created a report, and wrote it …
DaniilSitailo 365e8bc
all fix
DaniilSitailo ec931d3
all fix
DaniilSitailo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(","); | ||
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]); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using
StringBuilder
instead ofString
for concatenating strings in loops to optimize memory usage, as suggested in the checklist.