Adding Financial Balance with Jtable

Asked

Viewed 573 times

0

I’m trying to develop a sales control program in Java, but I have a problem when it comes to scheduling Jtable’s balance.

As you can see in the image below, the balance is being updated per line. However, since I have declared the starting balance equal to 0 I need it to disappear with the total value of the first row to update the first row balance, then pick up the first row balance and add up with the total value of the second row and update the second row balance(becoming a cumulative balance). How do I do it?

inserir a descrição da imagem aqui

Sales class

public class Venda { 

private String pontoVenda;
private String cliente;
private String produto;
private String tipo;
private int quantidade;
private float valor;
public float valorTotal;
public double saldo = 0;

public String getPontoVenda() {

    return pontoVenda;
}

public void setPontoVenda(String pontoVenda) {
    this.pontoVenda = pontoVenda;
}

public String getCliente() {
    return cliente;
}

public void setCliente(String cliente) {
    this.cliente = cliente;
}

public String getProduto() {
    return produto;
}

public void setProduto(String produto) {
    this.produto = produto;
}

public String getTipo() {
    return tipo;
}

public void setTipo(String tipo) {
    this.tipo = tipo;
}

public int getQuantidade() {
    return quantidade;
}

public void setQuantidade(int quantidade) {
    this.quantidade = quantidade;
}

public float getValor() {
    return valor;
}

public void setValor(float valor) {
    this.valor = valor;
}

public float getValorTotal() {
    return valorTotal;
}

public void setValorTotal(float valorTotal) {
    this.valorTotal = valor * quantidade;
}

public double getSaldo() {
    return saldo;
}

public void setSaldo(double saldo) {
    this.saldo = saldo + valorTotal;
}

}

public class ProdutoTableModel extends AbstractTableModel {

private List<Venda> dados = new ArrayList<>();
private String[] colunas = {"Ponto de Venda", "Cliente", "Produto", "Tipo", 
    "Quantidade" ,"Valor(unid.)", "Valor Total", "Saldo"};

@Override
public String getColumnName(int column){
    return colunas[column];
}
@Override
public int getRowCount() {
    return dados.size();      
}

@Override
public int getColumnCount() {
    return colunas.length;
}


@Override
public Object getValueAt(int linha, int coluna) {
    switch (coluna){
        case 0:
            return dados.get(linha).getPontoVenda();
        case 1:
            return dados.get(linha).getCliente();
        case 2:
            return dados.get(linha).getProduto();
        case 3:
            return dados.get(linha).getTipo();
        case 4:
            return dados.get(linha).getQuantidade();
        case 5:
            return dados.get(linha).getValor();
        case 6:
            return dados.get(linha).getValorTotal();
        case 7:
            return dados.get(linha).getSaldo();
    }
    return null;

}

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    Venda p = new Venda();
    p.setPontoVenda(itemPonto.getSelectedItem().toString());
    p.setCliente(itemCliente.getSelectedItem().toString());
    p.setProduto(itemProduto.getSelectedItem().toString());
    p.setTipo(itemTipo.getSelectedItem().toString());
    p.setQuantidade(Integer.parseInt(txtQuantidade.getText()));
    p.setValor(Float.parseFloat(txtValor.getText()));
    p.setValorTotal(p.valorTotal); 
    p.setSaldo(p.saldo);

    tableModel.addRow(p); 
  • If balance refers, as I understand it, to the total of various products, why are you keeping in a product? The error of logic starts there, balance should not be a product field. Tip, remove this balance column from the table, and from the Product class, put it as a Textfield below it, and update it as new products are added, it will be much easier so.

  • Speak Diego! Thanks for the return! So the product class is actually the sale. I put in the product name, but actually it is the sale. That’s why I put balance inside it. If I change the product in the combobox the sale will be inserted into Jtable anyway.

  • But it doesn’t make sense for the product class to be sales. They’re two completely different things. If you want to facilitate and not put gambiarras in your program, I suggest creating an entity sale in your table, and create an id for each sale, and leave products only for each product and its characteristics, linking the two.

  • And it’s not necessary to save total on this model that I suggested, just follow a simple formula: vendaTotal = vendaTotal + produto.preco x quantidadeComprada that for all products linked that sale.

  • Hi Diego, so I changed the name of the class there to make it easier to understand. When I input the data is not from a product but from a sale. Still you think the balance should stay off? I’m sorry if I’m saying something stupid, but I’m at the beginning anyway so I’m still very confused

  • I will post an answer explaining a more peaceful solution to give you.

Show 1 more comment

1 answer

2

The hint I give you is to remove this column and this field of Venda, it doesn’t make much sense to keep updating the balance every line of the purchase, when you only need a field being updated every product added to the sale.

As an alternative to this column, you could add a JTextFied(or Jlabel, is your choice) just below the table, and update this field with the total, making a method more or less like this:

public void atualizarSaldo(){

    float saldo, produtoValor;
    int produtoQuantidade;

        for(int i = 0; i < suaTable.getRowCount(); i++){

            produtoValor = suaTable.getModel().getValueAt(i, 5);
            produtoQuantidade = suaTable.getModel().getValueAt(i, 4);

            saldo += (produtoQuantidade * produtoValor);

        }

    campoSaldo.setText(saldo);

}

Just call this method in the same place as you add a new product to your table.

  • Thank you Diego, I’ll try to do it now! I wanted to put the balance to each row, because I will still add a column of Date and I wanted to see by date and also for a matter of software layout as if it were a bank statement

  • @Felipepaifer then there is a serious error in the layout of your program. Note that each sale only allows one product, does that make sense? At least if you store a list of products, you could even suggest something in the tablemodel itself. The way it is, it’s not gonna work without the whims that will solve this problem and create countless others in the future.

  • @Felipepaifer Did the answer help you? If yes, you can mark it as accepted by clicking on v the left of the answer :)

Browser other questions tagged

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