Checking for the existence of an object in an arraylist

0

I have a class PessoaFisica and a class PessoaJuridica , both inherit from class Cliente , whose attribute identify is the code.

I am creating a method to check if the element already exists in the list (checking the code), and if there is no add obj passed as parameter.

Client class:

public class Cliente implements Serializable {
int codigo;
private String nome;
private String endereco;
private String telefone;
private String Tipo;

public Cliente(int codigo, String nome, String endereco, String telefone, String Tipo) {
    this.codigo = codigo;
    this.nome = nome;
    this.endereco = endereco;
    this.telefone = telefone;
    this.Tipo = Tipo;
}

public int getCodigo() {
    return codigo;
}

public void setCodigo(int codigo) {
    this.codigo = codigo;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getEndereco() {
    return endereco;
}

public void setEndereco(String endereco) {
    this.endereco = endereco;
}

public String getTelefone() {
    return telefone;
}

public void setTelefone(String telefone) {
    this.telefone = telefone;
}

public String getTipo() {
    return Tipo;
}

public void setTipo(String Tipo) {
    this.Tipo = Tipo;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 89 * hash + this.codigo;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }

 if(this.getCodigo()==((Cliente)obj).getCodigo()){
     return true;
 }
        return true;
}


@Override
public String toString() {
    return   "" + codigo + ";" + nome + ";" + endereco + ";" + telefone + ";" + Tipo + ';';
}

The list is in the data class

method and the following

public void salvar(Cliente obj){

    if(Dados.listaClientes.contains(obj)!= true)
    {
        Dados.listaClientes.add(obj);
        System.out.println("item adicionado");
    }
    else {
        System.out.println("impossivel adicionar");

    }
} 

I am replacing the equals method in the client class as follows

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}

 if(this.getCodigo()==((Cliente)obj).getCodigo()){
 return true;
 }
      return true;
 }

I'm not having compile errors, but the method just does not work, even adds elements in equal codes

So this is my test

 public static void main(String[] args) {
{
{

        DaoPessoaJuridica dao = new DaoPessoaJuridica();

     PessoaJuridica c1 = new PessoaJuridica(10,"Daniel","ovidio vilela","993911490","F","46353698895",151515);
     PessoaJuridica c2 = new PessoaJuridica(10,"Daniel","ovidio vilela","993911490","F","4635369895",151515);
    PessoaFisica c3 = new PessoaFisica(10,"Daniel","ovidio vilela","993911490","F","4635369895");
       dao.salvar(c1);
       dao.salvar(c2);
       dao.salvar(c3);

     // Dados.listaClientes.add(c1);

        System.out.println(Dados.listaClientes.toString());

}  
    }}

I changed my code attribute to

 private Integer codigo;

I used the following method, when comparing objects of the same type it works fine, but when I compare a physical person and a legal person with the same code, he lets me add, what should not happen

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Cliente cliente = (Cliente) obj;
    if (codigo == null) {
        if (cliente.codigo != null)
            return false;
    } else if (!codigo.equals(cliente.codigo))
        return false;
    return true;
}

I was able to make it work by comparing different objects as well, however using only the following code in equals

@Override
 public boolean equals(Object obj) {
 if(this.getCodigo()!=((Cliente)obj).getCodigo()){
 return false; }
 if(obj==null) 
 return false; 
 if(this==obj) return true;
 return true;
 }

Can this code result in some future problem?

    
asked by anonymous 13.05.2018 / 01:01

1 answer

0

First declare your identifier as:

//objeto do tipo inteiro    
private Integer codigo;

Then replace your equals method with:

@Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Cliente cliente = (Cliente) obj;
        if (codigo == null) {
            if (cliente.codigo != null)
                return false;
        } else if (!codigo.equals(cliente.codigo))
            return false;
        return true;
    }

And your hashCode by:

@Override
    public int hashCode() {
        final int hash = 31;
        int result = 1;
        result = hash * result + (codigo == null) ? 0 : id.hashCode());
        return result;
    }
    
13.05.2018 / 02:38