Why is the method calculating 1 product only?

Asked

Viewed 114 times

0

I have a problem with a method in which you are calculating the amount of a single product instead of calculating all that I select.

I’m sure the problem lies in the position I passed to the variables when I start them.

Follows the method:

private void setEvents() {

    //Já está passando o preço do produto, mas ainda preciso calcular o preço de todos, vezes a quantidade.
    final List<Double> listaValorDoProduto = new ArrayList<Double>();
    final List<Integer> listaQuantidadeDoProduto = new ArrayList<Integer>();
    final List<Double> listaValorTotalDoProduto = new ArrayList<Double>();


    /*final double[] valorDoProduto = new double[1];
    final int[] quantidadeDoProduto = new int[1];
    final double[] valorTotalDoProduto = new double[1];*/

    Button botaoFinalizarPedido = (Button) findViewById(R.id.btn_finalizar_pedido);

    botaoFinalizarPedido.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            double total;
            double valor = 0;
            int quantidade = 0;
            int x = 0;
            int y = 0;

            Intent intent = new Intent(ProdutoActivity.this, ServicoDeEmailActivity.class);

            Bundle params = new Bundle();

            /*valorDoProduto[0] = app.getValorDoProduto();
            quantidadeDoProduto[0] = app.getTotal();

            total = valorDoProduto[0] * quantidadeDoProduto[0] ;*/

            listaValorDoProduto.add(app.getValorDoProduto());
            listaQuantidadeDoProduto.add(app.getTotal());

            for (Double valorDoProduto : listaValorDoProduto){
               while(x < valorDoProduto) {

                   valor = valorDoProduto;
                   x++;
               }
             }

            for (Integer quantidadeDoProduto :listaQuantidadeDoProduto){
                while(y < quantidadeDoProduto) {

                    quantidade = quantidadeDoProduto;
                    y ++;
                }
            }

            total = valor * quantidade;

            params.putDouble("ValorDoProduto", total);
            intent.putExtras(params);
            startActivity(intent);

        }
    });
}

I reedited the method, I think it’s better this way.

Below are the methods used in the App class:

//Adicionando a lista de Produtos dentro de Compra (Carrinho de Compra).
    public void adicionarProduto(ItemCompra itemCompra) {

        //Percorrendo os itens da compra e passando os itens para o carrinho.
        for (ItemCompra itemCompraDoCarrinho : compra.getItens()) {
            //Se o item do carrinho for igual ao item da lista de compra...
            if (itemCompraDoCarrinho.equals(itemCompra)) {
                //Retorno a quantidade de item e somando + 1.
                itemCompraDoCarrinho.setQuantidade(itemCompraDoCarrinho.getQuantidade() + 1);
                return;
            }
        }



    //Adiciona a quantidade na lista de compras
            itemCompra.setQuantidade(1);
            compra.getItens().add(itemCompra);
        }

public double getValorDoProduto(/*ItemCompra itemCompra*/) {

        Log.i("App", "Dentro do método getValorDoProduto");
        for (ItemCompra itemCompraDoCarrinho : compra.getItens()) {

            //if (itemCompraDoCarrinho.equals(itemCompra)) {
                Log.i("App", "Obtendo o valor do item...");
                return itemCompraDoCarrinho.getProduto().getValor();

            //}
        }
        Log.i("App", "Não conseguiu pegar o valor do produto e retornou 0");
        return 0;
    }

public int getTotal() {
        //int quantidade = 0;
        for (ItemCompra itemCompraDoCarrinho : compra.getItens()) {

           //quantidade = itemCompraDoCarrinho.getQuantidade();
            return itemCompraDoCarrinho.getQuantidade();
        }
        return 0;
    }
  • 2

    Eric, Visualizing your code you are placing all values in the first element of each list/array, another thing I did not find is a loop being made between products, I believe that there should be some code of the app informing the selected products so that you can loop (iterate) for each of them doing their account. Loop each selected element by passing them and letting the code do the calculations.

  • So the method that iterates the products is in the same App class. I think the problem is that I limit the vector to a position, which in this case is [0]. I tried using Arraylist, but it still doesn’t work.

  • I don’t understand why you’re putting everything in an array. Another strange thing is you are Multiplying the value of the product by the Total Value, Suppose you have 1 products of R $ 10,00, so your valueTotaldoProduct will be 100, because 10 x 10. In my opinion, the calculation should be valuedProduct[0] * quantityProduct[0] or the totalTest[0] direct.

  • I re-edited and put the methods used in the App class, still do not know where is the error.

  • Is there any reason for the variables to be final?

  • The variables are at the end because I did not declare them in the scope of the class, so the IDE is asking to put the end.

  • I already started not understanding the pq listaValorDoProduto is an array. You do a foreach to retrieve the last array value. : S. (in the first for of the botaoFinalizarPedido) the same thing goes for the second for, you are only recovering the last value of the array. It was very confusing that.

  • So Juarez, even if I don’t declare a list, Android Studio itself tells me to implement an array of these attributes.

Show 3 more comments
No answers

Browser other questions tagged

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