From the ArrayList.contains(Object o) javadoc:
Returns true if this list contains the specified element. More
formally, returns true if and only if this list contains at least one
element e such that (o==null ? e==null : o.equals(e)).
It means, to make your method work correctly, you have to implement the hashcode/equals method in the Account class.
I have created a mock implementation of your code:
No hashcode/equals method defined -> Object class's default hashcode/equals method used which treats each new Object differently.
import java.util.ArrayList;
import java.util.List;
public class Test{
static class Account {
private String AccountName;
private int AccountNumber;
private double AccountBalance;
public Account(String accountName, int accountNumber, double accountBalance)
{
this.AccountName = accountName;
this.AccountNumber = accountNumber;
this.AccountBalance = accountBalance;
}
public Account()
{
}
public String getAccountName() {
return AccountName;
}
public void setAccountName(String accountName) {
AccountName = accountName;
}
public int getAccountNumber() {
return AccountNumber;
}
public void setAccountNumber(int accountNumber) {
AccountNumber = accountNumber;
}
public double getAccountBalance() {
return AccountBalance;
}
public void setAccountBalance(double accountBalance) {
AccountBalance = accountBalance;
}
}
static class Bank {
private List<Account> accountList = new ArrayList<>();
private String inputStr = "";
private int inputInt = 0;
public Bank() {
}
public Bank(String string, int i) {
inputStr = string;
inputInt = i;
}
public boolean addAccount(Account account) {
if(!accountList.contains(account)) {
accountList.add(account);
return true;
}
return false;
}
}
public static void main(String[] args)
{
Bank bank = new Bank("qLOwyONvKsM58ZdV& &yo", 1);
Account account = new Account("aucchQitgyzLV", 6329668, 479389.0);
Account account5 = new Account("aucchQitgyzLV", 6329668, 479389.0);
Account account2 = new Account();
Account account3 = new Account();
Account account4 = new Account("sgdgrt", 0, 0.1);
Account account6 = new Account("sgdgrt", 0, 0.1);
System.out.println( "account added: "+bank.addAccount(account));
System.out.println("account5 added: "+bank.addAccount(account5));
System.out.println( "account2 added: "+bank.addAccount(account2));
System.out.println("account3 added: "+bank.addAccount(account3));
System.out.println("account4 added: "+bank.addAccount(account4));
System.out.println("account6 added: "+bank.addAccount(account6));
}
}
Result:
account added: true
account5 added: true
account2 added: true
account3 added: true
account4 added: true
account6 added: true
Which obviously not correct.
If you would insert the following hashcode/equals methods into the Account class:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(AccountBalance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((AccountName == null) ? 0 : AccountName.hashCode());
result = prime * result + AccountNumber;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (Double.doubleToLongBits(AccountBalance) != Double.doubleToLongBits(other.AccountBalance))
return false;
if (AccountName == null) {
if (other.AccountName != null)
return false;
} else if (!AccountName.equals(other.AccountName))
return false;
if (AccountNumber != other.AccountNumber)
return false;
return true;
}
Result would be:
account added: true
account5 added: false
account2 added: true
account3 added: false
account4 added: true
account6 added: false
So your method would work correctly.