Using the hashmap to make comparisons

0

I have the following situation: I get a list of itensVenda , and in it I have associated with Venda . In Venda I have a list of itensVenda and Cliente , and in Client, I have associated a Sales list.

I have to rearrange the hierarchy, and return a list of clients. I made the following code (missing the else), which "fixes" this hierarchy by adding it to a hashmap, and then places it in a list of clients.

public class HierarquiaImpl implements Hierarquia {
@Override
public Collection<Cliente> montarHierarquia(Collection<ItemVenda> itens) {
    Collection<Cliente> clientes = new ArrayList<Cliente>();
    Map<Cliente,Integer> clientesMap = new HashMap<Cliente,Integer>();
    for (ItemVenda itemVenda : itens) {
        if(!clientesMap.containsKey(itemVenda.getVenda().getCliente())) {
            List<ItemVenda> itensVendas = new ArrayList<ItemVenda>();
            List<Venda> vendas = new ArrayList<Venda>();
            itensVendas.add(itemVenda);
            itemVenda.getVenda().setItems(itensVendas);
            vendas.add(itemVenda.getVenda());
            itemVenda.getVenda().getCliente().setVendas(vendas);
            Cliente cliente = itemVenda.getVenda().getCliente();
            Integer idCliente = itemVenda.getVenda().getCliente().getId();
            clientesMap.put(cliente, idCliente);
            clientes.add(itemVenda.getVenda().getCliente());
        }
        else{
            if (clientesMap.get().equals())
        }
    }
    System.out.println("teste");
    return clientes;
}

The problem is that each customer can have multiple sales, each sale can have multiple items, a sale belongs to a customer.

The above code adds to the hashmap when it does not already have a client inserted. If you have a customer, you will have to pick up the sale associated with the item Sales of this for , compare if it is equal to a sale already registered in the same client, if yes, I will just insert the item in that sale. If not, I'll make another sale for the item.

Client Class:

public class Cliente {

    private int id;
    private String nome;
    private List<Venda> vendas;

    public Cliente() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

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

    public List<Venda> getVendas() {
        return vendas;
    }

    public void setVendas(List<Venda> vendas) {
        this.vendas = vendas;
    }

}

Class Selling Class:

public class ItemVenda {

    private int id;
    private Venda venda;
    private String codigoProduto;
    private int quantidade;
    private BigDecimal valor;

    public ItemVenda() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Venda getVenda() {
        return venda;
    }

    public void setVenda(Venda venda) {
        this.venda = venda;
    }

    public String getCodigoProduto() {
        return codigoProduto;
    }

    public void setCodigoProduto(String codigoProduto) {
        this.codigoProduto = codigoProduto;
    }

    public int getQuantidade() {
        return quantidade;
    }

    public void setQuantidade(int quantidade) {
        this.quantidade = quantidade;
    }

    public BigDecimal getValor() {
        return valor;
    }

    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }

}

Sale Class:

public class Venda {

    private int codigo;
    private LocalDateTime dataVenda;
    private Cliente cliente;
    private float valorTotal;
    private List<ItemVenda> items;

    public Venda() {
    }

    public int getCodigo() {
        return codigo;
    }

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

    public LocalDateTime getDataVenda() {
        return dataVenda;
    }

    public void setDataVenda(LocalDateTime dataVenda) {
        this.dataVenda = dataVenda;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public float getValorTotal() {
        return valorTotal;
    }

    public void setValorTotal(float valorTotal) {
        this.valorTotal = valorTotal;
    }

    public List<ItemVenda> getItems() {
        return items;
    }

    public void setItems(List<ItemVenda> items) {
        this.items = items;
    }

}
    
asked by anonymous 20.07.2017 / 02:39

1 answer

0

If you compare two objects of the form above, what will be compared is the location of the same in memory and in this case will only be the same object if they point to the same position in memory.

To solve this problem implement the hashCode () and equals () methods in the classes you want to compare.

Example implementation for the Client class

 @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((nome == null) ? 0 : nome.hashCode());
        result = prime * result + ((vendas == null) ? 0 : vendas.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;
        Cliente other = (Cliente) obj;
        if (id != other.id)
            return false;
        if (nome == null) {
            if (other.nome != null)
                return false;
        } else if (!nome.equals(other.nome))
            return false;
        if (vendas == null) {
            if (other.vendas != null)
                return false;
        } else if (!vendas.equals(other.vendas))
            return false;
        return true;
    }
    
20.07.2017 / 15:01