0

This is the ArrayList class, I am trying to get the index of the Account object by using only an Account Number. My Account Object consists of ((object)Customer, (double) Balance) and my Customer Object consists of ((object) Name, (String) actNum, (object) Address).

import java.util.ArrayList;

public class Database {

    //Instance Veriables
    private ArrayList<Account> list;  
    private Account account;
    private int index;
    private Boolean found;


    public Database()
    {
        list = new ArrayList<Account>();
    }

    public void addAccount(Account a)
    {
        list.add(a);
    }

    public void deleteAccount(int i)
    {
        list.remove(i);
    }

    public void searchAccount(String actNum)
    {

        this.found = list.contains(actNum);
        this.index = list.indexOf(actNum);
        this.account = list.get(0);
    }

    public int getIndex()
    {
        return index;
    }

    public Account getAccount()
    {
        return account;
    }

    public Boolean isInList()
    {
        return found;
    }

}

1 Answer 1

2
private int indexForAccountWithNum(String searchActNum) {
    for (int i = 0; i < list.size(); i++)
        if (list.get(i).getCustomer().getAccountNum() == searchActNum)
            return i;
    return -1;
}

Or in Java 8

IntStream.range(0, list.size())
    .filter(i -> list.get(i).getCustomer().getAccountNum() == searchActNum)
    .findFirst().orElse(-1);
Sign up to request clarification or add additional context in comments.

2 Comments

The thing is is that the accountNum isnt even in the Account class, it's in another sub-class called Customer. So it's like to classes deep and it keeps getting errors
I've added a getCustomer call. If you are getting errors your'll need to post the errors and any details of code triggering them.

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.