Skip to content

Commit

Permalink
Handle duplicate account number while reading account file
Browse files Browse the repository at this point in the history
  • Loading branch information
vibes-863 committed Apr 13, 2024
1 parent 5da6d9c commit 4b32adf
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/main/java/budgetbuddy/storage/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static void createDataFolderIfNotExists() throws IOException {
}
}

public ArrayList<Account> readAccountFile() throws IOException, FileCorruptedException {
public ArrayList<Account> readAccountFile(ArrayList<Integer> existingAccountNumbers) throws IOException, FileCorruptedException {
File f = new File(ACCOUNTS_FILE_PATH);
Scanner s = new Scanner(f);

Expand Down Expand Up @@ -164,7 +164,12 @@ public ArrayList<Account> readAccountFile() throws IOException, FileCorruptedExc
throw new FileCorruptedException("Invalid account name");
}

if (existingAccountNumbers.contains(accountNumber)) {
throw new FileCorruptedException("Duplicate account number");
}

accounts.add(new Account(accountNumber, accountInfo[1], balance));
existingAccountNumbers.add(accountNumber);
}
return accounts;
}
Expand Down Expand Up @@ -204,20 +209,17 @@ public AccountManager loadAccounts() {
}
return createNewAccountManager();
}
ArrayList<Integer> existingAccountNumbers = new ArrayList<>();
ArrayList<Account> accounts = null;
try {
accounts = readAccountFile();
accounts = readAccountFile(existingAccountNumbers);
} catch (FileCorruptedException e) {
UserInterface.printFileCorruptedError();
return createNewAccountManager();
}
if (accounts.isEmpty()) {
return createNewAccountManager();
}
ArrayList<Integer> existingAccountNumbers = new ArrayList<>();
for (Account account : accounts) {
existingAccountNumbers.add(account.getAccountNumber());
}
return new AccountManager(accounts, existingAccountNumbers);
} catch (IOException e) {
UserInterface.printFileCorruptedError();
Expand Down

0 comments on commit 4b32adf

Please sign in to comment.