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);
}
}