"algaworks" - Bigdecimal - Sum of values

Asked

Viewed 250 times

-1

I am trying to add the values added in the column "Value", IE, to each entry of a recipe the total field below needs to update doing the sum of the values in the column value, however I am with the type Bigdecimal, so I can’t do the sum and in the view no result appears, only the outputLabel value="Total REVENUE" which is the string.

Model:

@NotNull
@DecimalMin("0")
@Column(precision = 10, scale = 2, nullable = false)
private BigDecimal valor;

get e set

This method is in the Lancamento class (model)

@Transient
public void valorTotal(Lancamento lancamento) {
    BigDecimal total = BigDecimal.ZERO;
    total = total.add(this.getValor());
    this.setValor(total);
}

Enum

RECEITA("Receita"), 
DESPESA("Despesa");

get e set

Na View:

<p:dataTable value="#{consultaLancamentosBean.lancamentos}" var="lancamento">

<p:column>
<p:outputLabel value="Total RECEITA:" style="font-weight: bold; text-align: right; font-size: 1.1em" />
    <h:outputText id="totalReceita" value="#{lancamento.valorTotal(lancamento)}">
        <f:convertNumber type="currency" />
    </h:outputText>
</p:column>

Another problem:

Exibi.
<p:outputLabel value="Total RECEITA: />

Não exibi o valorTotal
<h:outputText value="#{lancamento.valorTotal(lancamento)}" />

2 answers

0

You have two problems with your code:

  1. You are not returning any value in your function valorTotal
  2. Your total value calculation logic is confused, a method is calculating and changing and at the same time is displaying on screen while loading

Anyway, to start you must return the value in your method used in outputText:

@Transient
public BigDecimal getValorTotal(Lancamento lancamento) {
    BigDecimal total = BigDecimal.ZERO;
    return total.add(this.getValor());
}

And change to use this method:

<h:outputText value="#{lancamento.getValorTotal(lancamento)}" />

I suggest you also refactor to perform the calculations before loading the screen in this situation, to avoid a method aggregate many responsibilities.

  • With this method the value is not being added to each insertion, it is only taking value that is already in the value column, without adding values.

0

1º - Your Total value method is set to VOID. Therefore, you cannot use the method to return the value in the outputext;

2- Another problem is that you Zera the value of the total local variable and then add this.value() - This doesn’t make any sense - .

You pass a release on your method - getValorTotal(lancamento lancamento) - probably will be he the guy you want to manipulate or add values;

Browser other questions tagged

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