Update row in TableView - JavaFx

0

My problem is that I have an interface of a store's point of sale, where I want to add several times a product in my TableView but it does not repeat itself, as I show in the image, I want to achieve that by adding the amount of two and then add another two to the amount of that product that is updated to four and do not create another row as I show in the image. Any suggestions on how I can solve it?

@FXML
private void agregarProducto(ActionEvent event) {
    String codigo, nombre, tipoAlimento, precio, cantidad, importe;
    if(txtCodigoProductoBuscar.getText().isEmpty() || txtCantidadProductos.getText().isEmpty()){
        Alert dialogoAlert = new Alert(Alert.AlertType.INFORMATION);
        dialogoAlert.setTitle("Agregar producto");
        dialogoAlert.setHeaderText(null);
        dialogoAlert.setContentText("Rellene todos los campos por favor.");
        dialogoAlert.initStyle(StageStyle.UTILITY);
        dialogoAlert.showAndWait();
    }else{
        String consulta = "SELECT * FROM productos WHERE Codigo='".concat(txtCodigoProductoBuscar.getText()).concat("'");
        try {
            prep = cn.prepareStatement(consulta);
            result = prep.executeQuery();
            while (result.next()) {
                codigo = result.getString(2);
                nombre = result.getString(3);
                tipoAlimento = result.getString(4);
                precio = result.getString(5);
                cantidad = txtCantidadProductos.getText();
                int precioProducto = Integer.parseInt(precio);
                int cantidadProducto = Integer.parseInt(cantidad);
                importe = String.valueOf(precioProducto * cantidadProducto);
                datosProductoVenta.add(new DatosProductosVenta(codigo, nombre, tipoAlimento, precio, cantidad, importe));
                Alert dialogoAlert = new Alert(Alert.AlertType.INFORMATION);
                dialogoAlert.setTitle("Agregar producto");
                dialogoAlert.setHeaderText(null);
                dialogoAlert.setContentText("Producto agregado correctamente.");
                dialogoAlert.initStyle(StageStyle.UTILITY);
                dialogoAlert.showAndWait();
            }
        } catch (SQLException ex) {
            Alert dialogoAlert = new Alert(Alert.AlertType.INFORMATION);
            dialogoAlert.setTitle("Agregar producto");
            dialogoAlert.setHeaderText(null);
            dialogoAlert.setContentText("El producto no fue encontrado.");
            dialogoAlert.initStyle(StageStyle.UTILITY);
            dialogoAlert.showAndWait();
        }
        tbvDatosProductosVenta.setItems(datosProductoVenta);
    }
}

    
asked by AlexCs 06.05.2018 в 03:00
source

1 answer

0

What you must do first, before going to look for the product in the database, is to look for it in the table, and if you find it, you only update the amount and the amount in the table (of the GUI).

  

This is the code that should go inside your else

    codigo = txtCodigoProductoBuscar.getText();
    cantidad = txtCantidadProductos.getText();

    // Busco si ya agregué anteriormente el producto a la tabla
    boolean productoAgregado = false;
    for (DatosProductosVenta item : datosProductoVenta) {
        if (item.getCodigo().equals(codigo)) {
            item.setCantidad(item.getCantidad() + cantidad);
            item.setImporte(item.getCantidad() * item.getPrecio());

            productoAgregado = true;

            break;
        }
    }

    // Solo si no encontré (si no fue agregado) el producto dentro de la tabla lo cargo de la base de datos
    if (!productoAgregado) {
        String consulta = "SELECT * FROM productos WHERE Codigo='".concat(txtCodigoProductoBuscar.getText()).concat("'");

        try {
            prep = cn.prepareStatement(consulta);
            result = prep.executeQuery();
            while (result.next()) {
                nombre = result.getString(3);
                tipoAlimento = result.getString(4);
                precio = result.getString(5);
                int precioProducto = Integer.parseInt(precio);
                int cantidadProducto = Integer.parseInt(cantidad);
                importe = String.valueOf(precioProducto * cantidadProducto);

                datosProductoVenta.add(new DatosProductosVenta(codigo, nombre, tipoAlimento, precio, cantidad, importe));

                productoAgregado = true;
            }
        } catch (SQLException ex) {
            Alert dialogoAlert = new Alert(Alert.AlertType.INFORMATION);
            dialogoAlert.setTitle("Agregar producto");
            dialogoAlert.setHeaderText(null);
            dialogoAlert.setContentText("El producto no fue encontrado.");
            dialogoAlert.initStyle(StageStyle.UTILITY);
            dialogoAlert.showAndWait();
        }
    }

    if (productoAgregado) {
        Alert dialogoAlert = new Alert(Alert.AlertType.INFORMATION);
        dialogoAlert.setTitle("Agregar producto");
        dialogoAlert.setHeaderText(null);
        dialogoAlert.setContentText("Producto agregado correctamente.");
        dialogoAlert.initStyle(StageStyle.UTILITY);
        dialogoAlert.showAndWait();
    }

For this solution I have assumed a few things since you do not add them to your code.

1 - datosProductoVenta is an observable list of JavaFX whose statement in the following: ObservableList<DatosProductosVenta> datosProductoVenta;
2 - tbvDatosProductosVenta was declared as follows: @FXML private TableView<DatosProductosVenta> tbvDatosProductosVenta; (annotation may have been omitted if the view is not built with FXML)
3 - datosProductoVenta have a reference to the elements of the table. This reference could have been made through the call to the method getItems() of the table tbvDatosProductosVenta , in this way: datosProductoVenta = tbvDatosProductosVenta.getItems();
4 - Class DatosProductosVenta offers the following methods:

public int getCantidad();
public void setCantidad(int);
public void setImporte(double);
public double getPrecio();

Taking into consideration what was assumed in points 1 and 3 above, the line

tbvDatosProductosVenta.setItems(datosProductoVenta);

that you put after catch is not necessary.

    
answered by 06.05.2018 в 04:03