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?
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.
– user28595
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.
– Felipe Paifer
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.
– user28595
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.– user28595
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
– Felipe Paifer
I will post an answer explaining a more peaceful solution to give you.
– user28595