Arraylist JAVA doubts

Asked

Viewed 52 times

0

I have an object "Itemsoliccompra" and within that object has another object called "Product" and product has attributes (name, id, etc...). I have a list that sometimes these products repeat (id, name, Qtde).

I wonder if there is a way I can take the product "x@123" and "x@456" which has equal attributes, play in a list and add their quantities, because in my case it keeps passing infinitely through the objects and counting. The sum should give 10 but continue to perform doing (...15...20...25...30...35), and the maximum sum of the quantity of objects should be 10.

private int calculaQtde(_Produto produto, _Compra compra) {

    int somaTotal = 0;
    for(_ItemOrcamento itemOrcamento:compra.getItens()){
        for (_SolicCompra solicCompra:itemOrcamento.getOrcamento().getCotacao().getSolicitacoes()){
           for (_ItemSolicCompra itemSolicCompra:solicCompra.getItens()){
               if (itemSolicCompra.getProduto().getId() == produto.getId()) {
                            somaTotal += itemSolicCompra.getQtde();
              }
           }

        }

    }
    return somaTotal;
}
  • If I understand your code correctly, both the object ItemSolicCompra as to the object Produto that is within it have a field quantidade, correct? If this is the case, your logic fails because you are adding up the amount of each ItemSolicCompra instead of just adding to the Produto who have the same id. In this case, you would have to access the product and only then take the quantity attribute. O if would look like this: somaTotal += itemSolicCompra.getProduto().getQtde(). If I’ve got it wrong, correct me.

  • Hello @Statelessdev, the problem is that the product only has quantity in itemSolicCompra.. only the Product object does not have Qtde()

No answers

Browser other questions tagged

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