Use of hashmap to make comparisons

Asked

Viewed 202 times

0

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

I have to sort out the chain of command and return a list of clients. I made the following code (missing Else), which "fixes" this hierarchy by adding it in a hashmap, then putting 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, one sale belongs to one customer.

The above code adds to the hashmap when it does not yet have a client inserted. If you have a customer, you will have to take the sale associated with this item "for", compare if it is equal to any sale already registered with the same customer, 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;
    }

}

Itemvenda 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;
    }

}

Sales 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;
    }

}
  • I don’t get it. What’s the bullshit?

1 answer

0

If you compare two objects as 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 methods hashcode() and equals() in the classes you want to compare.

Example of 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;
    }

Browser other questions tagged

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