-3

If I have an ArrayList of bank account objects, how can I add and remove things from the object specified, such as checking and savings accounts? My bank account objects will include: private String firstName; private String lastName; private String username; private String password; private int idNum; private double checkingBal; and private double savingsBal. Is there a simple way to do this without having to create a new bank account object for that member that wants to add or remove an account?

I've looked online for a while now but I haven't found anything applicable to this kind of scenario. I'm only required to create a simple bank app that has a login page, member registration page if they don't have an account, and a member menu that pops up once logged in that allows the user to deposit, withdraw, add an account, delete an account, view an account, and logout. I'm open to other ideas for handling accounts if there are any you'd like to suggest.

  • refer to https://stackoverflow.com/questions/19616972/how-to-add-an-object-to-an-arraylist-in-java and https://stackoverflow.com/questions/10714233/remove-item-from-arraylist – Amrit Jun 27 '17 at 03:21
  • @amrit unfortunately those links didn't help. The first link talks about adding a new object. I want to add on to an object that already exists by adding an account rather than creating a new object. The second link talks about removing an item at a specific index. That would be similar to removing an entire object in my case but I want to remove a specific account from the object at a particular index if prompted by the user. – Growing My Roots Jun 27 '17 at 14:47
  • @Abhishek please elaborate – Growing My Roots Jun 27 '17 at 14:48
  • Lets take example about one bank which having struts frame work... Most of the banks uses middleware which is use to identify particular user is active or suspend... So if you have to call api which provides you info about user account, first need to hit middleware api to check user is active or not and then go to bank api. Such implementations vary from bank to bank... – Abhishek Jun 27 '17 at 14:58
  • So if you want to do any operation related to bank... You have to take care about its framework... Api... timeouts... And each time you have to update info to server side... – Abhishek Jun 27 '17 at 15:04
  • Ok that makes sense, but that's beyond my knowledge at the moment. I'm only required to create a simple bank app that has a login page, member registration page if they don't have an account, and a member menu that pops up once logged in that allows the user to deposit, withdraw, add an account, delete an account, view an account, and logout. – Growing My Roots Jun 27 '17 at 15:08

1 Answers1

1

I think the answer you are looking for is inheritance. In Java you can create a parent Class (i.e BankAccount) and then CheckingAccount and SavingsAccount can inherit it.

BankAccount (Parent)

public class BankAccount {
    public int accountNumber;

    public BankAccount(int accountNumber) {
        this.accountNumber = accountNumber;
    }

}

CheckingAccount (Child)

public class CheckingAccount extends BankAccount {
    public int amt;

    public CheckingAccount(int accountNumber, int amt) {
        super(accountNumber);
        this.amt = amt;
    }

}

Saving Account (Child)

public class SavingsAccount extends BankAccount {
    public int amt;
    public float interest;    

    public BankAccount(int accountNumber, int amt, float interest) {
        super(accountNumber);
        this.amt = amt;
        this.interest = interest;
    }

}

Then you can make an ArrayList of BankAccount and add both SavingsAccount and CheckingAccount:

ArrayList<BankAccount> accounts = new ArrayList<>();
accounts.add(new CheckingAccount(1234, 100));
accounts.add(new SavingsAccount(1234, 400, .01));
Chris
  • 2,435
  • 6
  • 26
  • 49