Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Mesmo sobrescrevendo equals() e hashcode() nas duas classes, continua aparecendo 3 resultados.

Segue o código da classe Funcionario, Gasto e TesteImportador:

package br.com.caelum.empresa.modelo;

import java.util.Calendar;

public class Funcionario {

    private String nome;
    private int matricula;
    private Calendar dataNascimento;

    public Funcionario(String nome, int matricula, Calendar dataNascimento) {
        this.nome = nome;
        this.matricula = matricula;
        this.dataNascimento = dataNascimento;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((dataNascimento == null) ? 0 : dataNascimento.hashCode());
        result = prime * result + matricula;
        result = prime * result + ((nome == null) ? 0 : nome.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Funcionario other = (Funcionario) obj;
        if (dataNascimento == null) {
            if (other.dataNascimento != null)
                return false;
        } else if (!dataNascimento.equals(other.dataNascimento))
            return false;
        if (matricula != other.matricula)
            return false;
        if (nome == null) {
            if (other.nome != null)
                return false;
        } else if (!nome.equals(other.nome))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Funcionario: " + nome;
    }

    public String getNome() {
        return nome;
    }

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

    public int getMatricula() {
        return matricula;
    }

    public void setMatricula(int matricula) {
        this.matricula = matricula;
    }

    public Calendar getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(Calendar dataNascimento) {
        this.dataNascimento = dataNascimento;
    }
}
package br.com.caelum.controle.modelo;

import java.util.Calendar;

import br.com.caelum.empresa.modelo.Funcionario;

public class Gasto {
    private double valor;
    private String tipo;
    private Funcionario funcionario;
    private Calendar data;

    public Gasto(double valor, String tipo, Funcionario funcionario, Calendar data) {
        super();
        this.valor = valor;
        this.tipo = tipo;
        this.funcionario = funcionario;
        this.data = data;
    }



    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((data == null) ? 0 : data.hashCode());
        result = prime * result + ((funcionario == null) ? 0 : funcionario.hashCode());
        result = prime * result + ((tipo == null) ? 0 : tipo.hashCode());
        long temp;
        temp = Double.doubleToLongBits(valor);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }



    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Gasto other = (Gasto) obj;
        if (data == null) {
            if (other.data != null)
                return false;
        } else if (!data.equals(other.data))
            return false;
        if (funcionario == null) {
            if (other.funcionario != null)
                return false;
        } else if (!funcionario.equals(other.funcionario))
            return false;
        if (tipo == null) {
            if (other.tipo != null)
                return false;
        } else if (!tipo.equals(other.tipo))
            return false;
        if (Double.doubleToLongBits(valor) != Double.doubleToLongBits(other.valor))
            return false;
        return true;
    }



    public double getValor() {
        return valor;
    }
    public void setValor(double valor) {
        this.valor = valor;
    }
    public String getTipo() {
        return tipo;
    }
    public void setTipo(String tipo) {
        this.tipo = tipo;
    }
    public Funcionario getFuncionario() {
        return funcionario;
    }
    public void setFuncionario(Funcionario funcionario) {
        this.funcionario = funcionario;
    }
    public Calendar getData() {
        return data;
    }
    public void setData(Calendar data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Tipo de gasto: " + tipo + " " + funcionario;
    }

}
package br.com.caelum.empresa;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Collection;

import br.com.caelum.empresa.leitor.ImportadorDeGastos;
import br.com.caelum.empresa.modelo.Gasto;

public class TesteImportador {

    public static void main(String[] args) throws UnsupportedEncodingException,
            ParseException {
        String conteudo = 
                "CARTAO01012011000010000123Jose da Silva                 22071983\r\n" +
                "CARTAO01012011000010000123Jose da Silva                 22071983\r\n" +
                "CARTAO01012011000010000123Jose da Silva                 22071983\r\n";

        ImportadorDeGastos importador = new ImportadorDeGastos();

        Collection<Gasto> list = importador.importa(new ByteArrayInputStream(
                conteudo.getBytes("UTF-8")));

        for (Gasto gasto : list) {
            System.out.println(gasto);
        }
    }
}

E o resultado mostrado ao rodar a TesteImportador: CARTAO do Funcionario: Jose da SilvaCARTAO do Funcionario: Jose da SilvaCARTAO do Funcionario: Jose da Silva

2 respostas

Natan pode colocar o código do seu importador de gastos, ou verificar se o mesmo está retornando HashMap, ou LinkedHashMap.

solução!

Já solucionei o problema e agora não lembro direito o que foi, mas estava trabalhando com os packages errados no Eclipse.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software