-
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
Implemented method getStatistic #1619
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
} |
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.
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.