diff --git a/src/main/java/seedu/bankwithus/AccountList.java b/src/main/java/seedu/bankwithus/AccountList.java index fd9e3e6e01..4bdeeb9387 100644 --- a/src/main/java/seedu/bankwithus/AccountList.java +++ b/src/main/java/seedu/bankwithus/AccountList.java @@ -1,14 +1,32 @@ package seedu.bankwithus; +import java.util.ArrayList; import java.util.Scanner; public class AccountList { - + protected ArrayList 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(); + } + } } diff --git a/src/main/java/seedu/bankwithus/Ui.java b/src/main/java/seedu/bankwithus/Ui.java index 229648fa68..4803c07865 100644 --- a/src/main/java/seedu/bankwithus/Ui.java +++ b/src/main/java/seedu/bankwithus/Ui.java @@ -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."); + } } diff --git a/src/test/java/seedu/bankwithus/AccountListTest.java b/src/test/java/seedu/bankwithus/AccountListTest.java new file mode 100644 index 0000000000..03fbda095c --- /dev/null +++ b/src/test/java/seedu/bankwithus/AccountListTest.java @@ -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)); + } +}