Checking the existence of an object in an arraylist

Asked

Viewed 392 times

0

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

I am creating a method to check if the element already exists in the list (checking the code), and if it does not exist add the obj passed as parameter.

Customer 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 given class

the 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 customer 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 am not having compile error, but simply the method does not work, adds even with elements in equal codes

so this 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;

And I used the following method,

@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, but 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;
 }

this code may result in some future problem ?

  • Add the full client class by editing the query.

  • ready I added ( to string is adding ; in all attributes for later saving to txt files)

1 answer

0

First declares 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;
    }
  • /* if (code == null)-- bad operant types for Binary Operator '==' { if (.codigo client != null)-- same error Return false; } Else if (!codigo.equals(.codigo client))-- int be dereferenced Return false; Return true;*/ I’m having the following error on these lines -

  • is because we are trying to compare an int with an object, I will edit in the reply.

  • edited in the question the result

Browser other questions tagged

You are not signed in. Login or sign up in order to post.