0

I thought some concepts are clear, but apparently that's not exactly true. I'm sure it's something very simple and asking you to answer rather than blame me or mark as duplicate etc, since I searched other similar questions, but they are not exactly as mine.

So I have this simple class Bank that creates an array of objects BankAccount by giving it the size. The Bank class has method isFull to check if the array is full, which, however I cannot call from a class MainApp where I just create instances to test my methods.

Bank

public class Bank {

    // array of BANKACCOUNT objects
    private BankAccount[] accountList;      // will hold all the accounts

    private int totalAccounts;              // to hold the total number of accounts

    public Bank(int sizeIn) {
        totalAccounts = sizeIn;
        accountList = new BankAccount[totalAccounts];
    }

    // check if the list is full
    public boolean isFull() {
        if(accountList.length == totalAccounts) {
           return true;
        }
        return false;
    }

    // add an item to the array
    public boolean add(BankAccount accountIn) {
        boolean added = false;
        if(isFull()){
            System.out.println("The account list is full");
            added = false;
        } else {
            accountList[totalAccounts] = accountIn;
            totalAccounts++;
            added = true;
        }
        return added;
    }
    // other methods...

BankAccount

public class BankAccount {
    private String nameOfHolder;
    private String accNumber;
    private double balance;
    private static double interestRate; 

    public BankAccount(String INPname, double INPbalance){
        accNumber = "NL35FAKE" + Randomize.RandomNumGen(); //created a random number for every bank account in a separate class
        nameOfHolder = INPname;
        balance = INPbalance;
    }
    // other methods...

Main program

public class MainApp {
    Bank[] bankList = new Bank[3];
    BankAccount acc1 = new BankAccount("Stacey", 7500);
    BankAccount acc2 = new BankAccount("Maria", 15000);
    bankList[0].add(acc1);
    bankList[1].add(acc2);
    bankList.isFull();   // THIS DOES NOT WORK. 

I do not see the isFull() method unless if I call it this way:
bankList[0].isFull() which makes no sense as I want to check the link of accounts.

Thanks in advance. :)

16
  • and where is add mehod in Bank class ? because it's not like this that you add something to an array Commented Aug 6, 2017 at 14:05
  • 1
    Why do you think you can call the methods of Bank one Bank[]? Bank[] is a different type which doesn't inherit anything of Bank. Commented Aug 6, 2017 at 14:05
  • Do you understand that the isFull() method acts on a Bank object and not an array of such objects? Call Bank#isFull() on each element in your array of banks, not on the array itself. Commented Aug 6, 2017 at 14:05
  • 1
    Your bankList is an array. So how you have to get access to the object first. And more bank[0].add(acc1); and bank[1].add(acc2); should raise error also. Commented Aug 6, 2017 at 14:06
  • 1
    @Tom: yes, you're right. He/she needs a deeper understanding of both reference arrays and creation of instances. Commented Aug 6, 2017 at 14:09

1 Answer 1

2

you are doing it wrong,

you are creating array of Bank, you only need one, and then you can add BankAccounts into that bank. and check isFull()

public class MainApp {
    public static void main(String args[]){
        Bank bank = new Bank(3);// a bank that will hold up to 3 accounts
        BankAccount acc1 = new BankAccount("Stacey", 7500);
        BankAccount acc2 = new BankAccount("Maria", 15000);
        bank.add(acc1);
        bank.add(acc2);
        bank.isFull();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

ah the bank and bankList is something i forgot to change. I only edited it here on SO for readability. But it's "bank" everywhere. So its not that
thanks, it worked. :) Always nice to see someone who just answers a question, no matter how dumb it is, rather than showing off. Either way, thanks to all of you who helped! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.