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 objectProduto
that is within it have a fieldquantidade
, correct? If this is the case, your logic fails because you are adding up the amount of eachItemSolicCompra
instead of just adding to theProduto
who have the sameid
. In this case, you would have to access the product and only then take the quantity attribute. Oif
would look like this:somaTotal += itemSolicCompra.getProduto().getQtde()
. If I’ve got it wrong, correct me.– StatelessDev
Hello @Statelessdev, the problem is that the product only has quantity in itemSolicCompra.. only the Product object does not have Qtde()
– Gabriel Dias