Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2223S2#26 from xiaoge26/master
Browse files Browse the repository at this point in the history
Add create account method
  • Loading branch information
tyuyang authored Mar 12, 2023
2 parents 37da2c0 + e290bf2 commit 9d5da07
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/seedu/bankwithus/AccountList.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
package seedu.bankwithus;

import java.util.ArrayList;
import java.util.Scanner;

public class AccountList {

protected ArrayList<Account> accounts;
private Ui ui;
public AccountList() {

}

public AccountList(Scanner scanner) {

}

/**
* Creates a new account and adds it to the AccountList.
*
* @param name Name of the new account to be added
* @param balanceString Balance of the new account to be added in String type
* @throws NumberFormatException If balanceString cannot be parsed into a float number
*/
public void addAccount(String name, String balanceString) throws NumberFormatException {
try {
float balance = Float.parseFloat(balanceString);
Account newAccount = new Account(name, balance);
accounts.add(newAccount);
} catch (NumberFormatException e) {
ui.showNumberFormatError();
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/bankwithus/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public void showFileCreated() {
public void showIOError() {
System.out.println("Something's really wrong! Exiting program now.");
}

public void showNumberFormatError() {
System.out.println("The input is not a number! Please try again.");
}
}
16 changes: 16 additions & 0 deletions src/test/java/seedu/bankwithus/AccountListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package seedu.bankwithus;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;

class AccountListTest {

@Test
void addAccount_invalidBalance_expectException() throws NullPointerException {
String name = "Bob";
String balanceString = "abc";
AccountList accountList = new AccountList();
assertThrows(NullPointerException.class,
() -> accountList.addAccount(name, balanceString));
}
}

0 comments on commit 9d5da07

Please sign in to comment.