Increase amount of equal service

0

I have a table with the service code, service, quantity, value, and total. When I add in the table an ITEM 1 for example, the data of this service will be added to the table. But if I add again this same ITEM 1 in the table, I wanted to increase the amount that the user was typing and not create a new line repeating, ITEM 1 and ITEM 1. Below is my code to add to the table.

I know I have to loop through the table rows and inside the loop check if the current iteration row has the Item being added. If you have, you increment the amount of this Item and give return; to exit the method. If the item does not already exist in the table, the loop will cycle through all lines and the execution will exit the loop, after exiting the loop you add the new Item.

But how do you do this for Jtable?

private void insereItem()
{
    if (txtQtde.getValue().equals(0))
    {
        JOptionPane.showMessageDialog(this, "Informe uma quantidade para adicionar o serviço na tabela", "Atenção", JOptionPane.ERROR_MESSAGE);
        txtQtde.setBorder(BorderFactory.createLineBorder(Color.RED));
    } else
    {
        try
        {
            txtQtde.setBorder(BorderFactory.createLineBorder(Color.GRAY));
            DefaultTableModel modelo = (DefaultTableModel) tbOs.getModel();
            String valUni = txtValor.getText().replace("R$", "").replace(" ", "").replace(".", "").replace(",", ".");
            double vtt = Double.parseDouble(valUni) * Integer.parseInt(txtQtde.getValue().toString());

            String total = String.valueOf(vtt);
            if (total.substring(total.lastIndexOf(".") + 1).length() < 2)
            {
                total = total + "0";
            }
            String unitario = txtValor.getText().replace("R$", "").replace(" ", "").replace(".", "").replace(",", ".");
            if (unitario.substring(unitario.lastIndexOf(".") + 1).length() < 2)
            {
                unitario = unitario + "0";
            }
            Object[] linha =
            {
                txtCodigoServico.getText(), cbbServico.getSelectedItem(), txtQtde.getValue().toString(), unitario, total
            };

            modelo.addRow(linha);

            double valorSubTotal = vtt + Double.parseDouble(txtSubTotal.getText().replace("R$", "").replace(" ", "").replace(".", "").replace(",", "."));
            setValorSubTotal(valorSubTotal);
            tbOs.setModel(modelo);
            atualizaSoma();
        } catch (Exception e)
        {
        }
    }
}
    
asked by anonymous 08.05.2017 / 19:13

0 answers