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. :)

Pixie
  • 311
  • 2
  • 14
  • and where is `add` mehod in Bank class ? because it's not like this that you add something to an array – azro Aug 06 '17 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`. – Tom Aug 06 '17 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. – Tim Biegeleisen Aug 06 '17 at 14:05
  • `bankList` is the array. You need to call that method on an element within the array via indexing: `bankList[0].isFull()`. If you want to call the method on every element in the array, you should use a `for` loop. – Vince Aug 06 '17 at 14:06
  • That code will call a NPE here `bank[0].add(acc1);` as you never create Bank objects. – Hovercraft Full Of Eels Aug 06 '17 at 14:06
  • 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. – Shafi Aug 06 '17 at 14:06
  • @HovercraftFullOfEels That's true, but not the question here :D. – Tom Aug 06 '17 at 14:06
  • @Tom: But his question is meaningless until he first solves the NPE. Regardless he needs to read a basic tutorial on arrays. – Hovercraft Full Of Eels Aug 06 '17 at 14:07
  • @HovercraftFullOfEels No disrespect, but its not meaningless. NPE plays no role til he fixes this compiler error, which takes priority. – Vince Aug 06 '17 at 14:08
  • i'll edit with the add() method. – Pixie Aug 06 '17 at 14:08
  • @VinceEmigh: yes, you're right – Hovercraft Full Of Eels Aug 06 '17 at 14:08
  • @HovercraftFullOfEels The picture looks more like a "she" ;D. But anyway, OP can't even run the code because she doesn't know why she can't call methods of `Bank` on an object of type `Bank[]`. That's the question/issue. – Tom Aug 06 '17 at 14:09
  • 1
    @Tom: yes, you're right. He/she needs a deeper understanding of both reference arrays and creation of instances. – Hovercraft Full Of Eels Aug 06 '17 at 14:09
  • "// THIS DOES NOT WORK." please explain ["does not work"](http://importblogkit.com/2015/07/does-not-work/). – Pshemo Aug 06 '17 at 14:11
  • @Hovercraft Full Of Eels "she". Thanks, will do! – Pixie Aug 06 '17 at 14:13
  • still learning, so that's why i asked. Trying to learn right :) No need to fight here :) DOES NOT WORK means when I put the dot operator, i see totally different methods, some of which i don't recognize. But none of them is from the Bank class. – Pixie Aug 06 '17 at 14:16

1 Answers1

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();
    }
}
Yazan
  • 6,074
  • 1
  • 19
  • 33
  • 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 – Pixie Aug 06 '17 at 14:21
  • 1
    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! :) – Pixie Aug 06 '17 at 14:29