Skip to content

Commit

Permalink
Merge pull request #60 from ShyamKrishna33/bug-fix
Browse files Browse the repository at this point in the history
Fix saveTransactions and saveAccounts method to save all objects
  • Loading branch information
ShyamKrishna33 committed Apr 3, 2024
2 parents ce2e2b2 + 85813f2 commit 32af614
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/java/budgetbuddy/storage/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public void saveAccounts(ArrayList<Account> accounts) {
}
FileWriter fw = new FileWriter(ACCOUNTS_FILE_PATH, false);
for (Account account : accounts) {
fw.write(account.getAccountNumber() + " ," + account.getName() + " ," + account.getBalance() + "\n");
String stringToWrite = account.getAccountNumber() + " ," + account.getName() + " ,"
+ account.getBalance() + "\n";
writeToFile(stringToWrite, ACCOUNTS_FILE_PATH);
}
fw.close();
} catch (IOException e) {
Expand All @@ -48,18 +50,19 @@ public void saveTransactions(ArrayList<Transaction> transactionArrayList) throws
File f = new File(TRANSACTIONS_FILE_PATH);

assert f.exists() : "File does not exist";
FileWriter fw = new FileWriter(TRANSACTIONS_FILE_PATH, false);

for (Transaction transaction : transactionArrayList) {
if (transaction == null) {
break;
}
String stringToWrite = getStringToWrite(transaction);
writeToFile(stringToWrite);
writeToFile(stringToWrite, TRANSACTIONS_FILE_PATH);
}
}

private static void writeToFile(String stringToWrite) throws IOException {
FileWriter fw = new FileWriter(TRANSACTIONS_FILE_PATH, false);
private static void writeToFile(String stringToWrite, String filePath) throws IOException {
FileWriter fw = new FileWriter(filePath, true);
fw.write(stringToWrite);
fw.close();
}
Expand Down

0 comments on commit 32af614

Please sign in to comment.