The solution was to remove static from my variables in the Account class. The code now works as intended
I am trying to make a method that will check if a number is in an arraylist of objects
My initial thoughts were this but this is not working currently. My main class is this withoutirrelevant code
private static ArrayList<Account> accounts = new ArrayList<Account>();
public static void main(String[] args)
{
//Creates the three default accounts
Account ac1 = new Account("account1", 0001, 0);
Account ac2 = new Account("account2", 0002, 0);
Account ac3 = new Account("account3", 0003, 0);
accounts.add(ac1);
accounts.add(ac2);
accounts.add(ac3);
}
public static void createAccount()
{
Boolean doesContain = false;
String logAccNum = JOptionPane.showInputDialog("please enter your account Number");
int tempAccNum = Integer.parseInt(logAccNum);
doesContain = checkArray(tempAccNum);
}
public static boolean checkArray(int checkNum)
{
for(int i = 0; i <accounts.size(); i++)
{
if(accounts.get(i).getMyAccountNum() == checkNum)
{
return true;
}
}
return false;
}
My code in my Account class without irrelevant code is
private int myAccountNum;
private int myBalance;
private String myName;
public Account(String name, int accNum, int balance)
{
this.myName = name;
this.myAccountNum = accNum;
this.myBalance = balance;
}
public int getMyAccountNum()
{
return myAccountNum;
}
The current issue with my code is that checkArray will only return true if the number entered in createAccount is the last default account created. I believe that the myAccountNum variable in the Account class is staying the same after the last instance of it being constructed.
Lastly, I do not want to use iterators in the solution unless necessary, if you could use arrayList and for loops to create it that'd be greatly appreciated