0

I am creating an ArrayList of Accounts (an object) and the Account constructor is

public Account(String name, int accNum, int balance)
{
    myName = name;
    myAccountNum = accNum;
    myBalance = balance;
}

I want to know how to check the ArrayList to determine if a given accountNumber exists in it, and if it does, return true

private static ArrayList<Account> accounts = new ArrayList<Account>();

My initial thought was this, but I do not think that this works

if(accounts.contains(tempAccNum))
{
    //executes code that I have
}
AJPerez
  • 3,435
  • 10
  • 61
  • 91
Hersh
  • 23
  • 3

3 Answers3

0

For the Java ArrayList, contains performs an object equality comparison. To use .contains, you would need to

  1. Implement the .equals() method for the Account class, and have it check only the this.myAccountNum property against the account number of the input Account.
  2. Create a dummy Account with the desired account number to pass into contains.

A better method would involve evaluating an iterator, and checking the account numbers at each step. Here I assume that myAccountNum is a public property of the Account class.

Iterator<E> it = Accounts.iterator();
while (it.hasNext()) {
    Account acc = it.next();
    if(acc.myAccountNum == tempAccNum)
        return true;
}

For ArrayList specifically, using .get with an index is not too bad:

for(int index = 0; index < Accounts.size(); ++index) {
    if(Accounts.get(index).myAccountNum == tempAccNum)
        return true;

For other List types, using indices can be very poor.
Ways to iterate over a list in Java

tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • when you say myAccountNum what do you mean by this? And I cannot use iterators as this is a school project and my teacher is requiring that we use an arraylist of objects – Hersh Feb 28 '18 at 03:16
  • @Hersh, you are required to state when you are asking homework questions. My answer does use an `ArrayList` -- that is what you said `Accounts` is. And you also gave us `myAccountNumber` in your question - it is a property of the `Account` class. – tehhowch Feb 28 '18 at 13:44
  • I didn't know that thanks for telling me. It's for a lab we're doing and she's told us that we can go here if we need help so I did. In terms of the second part of your statement, someone answered my original question below with the part that i need from your myAccountNum. My friend today also explained to me how it works, but thanks for your help – Hersh Feb 28 '18 at 22:50
0

I will suggest you to make it simple by just implementing an equals() method for account class.

@Override
public boolean equals(Object o){
  if(o==null)return false;
  if(o.getClass()!=this.getClass())return false;
  Account demo = (Account)o;
  if(!demo.myName.equals(this.myName))return false;
  if(demo.myAccountNum != this.myAccountNum)return false ;
  if(demo.myBalance = this.myBalance)return false ;
}

then use contains method

Randhawa
  • 226
  • 1
  • 15
0

First add getter for accNum in Account model

Then try this

public boolean containsAcc(int accno) {
    for(int i=0;i<accounts.size();i++) {
        if(accounts!= null && accounts.get(i).getMyAccountNum()==acno) {
            return true;
        }
    }
    return false;
}

Here getMyAccountNum() is the getter declared in Account model(shown below)

Then check this

if(containsAcc(tempAccNum))
{
//your code
}

Your Account model should be like this

public class Account {
String myName;
int myAccountNum;
int myBalance;
public Account(String name, int accNum, int balance)
{
    this.myName = name;
    this.myAccountNum = accNum;
    this.myBalance = balance;
}

public int getMyAccountNum() {
    return myAccountNum;
}
}
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44