Click here to Skip to main content
14,928,926 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a cart page where users can checkout after purchasing the products/medicines. I am having a button named checkout on that page. After clicking that button "CheckOut" I want to update multiple records in MySQL from JavaFX after clicking that button. I want the quantity of each item/medicine in the tableView to decreased according to the quantity purchased. I have tried putting the query in for loop I tried to selectAll from the table view. I will provide the code I wrote and a screenshot from the JavaFX tableView with the buttons. Any help is appreciated. Thanks for your time and cooperation from now.

package Patient;

import Admin.Doctor.AddDoctor;
import com.jfoenix.controls.JFXButton;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;


import java.sql.PreparedStatement;

import static Admin.AdminLogin.rightDown;
import static Connection.DBConnection.*;
import static sample.ChoicePage.scene;

public class CompletePurchase
{
    private StackPane tablePane;
    private TableView<Cart> cartTableView;

    private JFXButton remove;
    private JFXButton complete;

    public void complete()
    {
        rightDown.getChildren().clear();

        new AddDoctor().back();

        tablePane = new StackPane();
        tablePane.setMinWidth(700);
        tablePane.setMinHeight(500);
        tablePane.setLayoutY(90);
        tablePane.setLayoutX(150);

        tablePane.getChildren().clear();

        remove = new JFXButton("REMOVE");
        remove.setPrefWidth(200);
        remove.setPrefHeight(35);
        remove.setLayoutX(300);
        remove.setLayoutY(600);
        remove.getStyleClass().add("loginButton");
        remove.setOnAction((e)->{
            try
            {
                Cart cart = cartTableView.getSelectionModel().getSelectedItem();

                String removeQuery = "DELETE FROM Cart WHERE MedicineID = ?";
                preparedStatement = connection.prepareStatement(removeQuery);
                preparedStatement.setString(1,cart.getMedicineID());
                preparedStatement.executeUpdate();
                preparedStatement.close();

                cartTableView.getItems().removeAll(cartTableView.getSelectionModel().getSelectedItem());
            }
            catch(Exception e1)
            {
                e1.printStackTrace();
            }
        });
        rightDown.getChildren().add(remove);

        complete = new JFXButton("CHECKOUT");
        complete.setPrefWidth(200);
        complete.setPrefHeight(35);
        complete.setLayoutX(600);
        complete.setLayoutY(600);
        complete.getStyleClass().add("loginButton");
        complete.setOnAction((e)->{
            try
            {
                cartTableView.getSelectionModel().selectAll();
                int num = cartTableView.getItems().size();
                Cart cart = cartTableView.getSelectionModel().getSelectedItem();

                String updateQuery = "UPDATE Medicine SET Quantity = Quantity - ? WHERE MedicineID = ?";
                preparedStatement = connection.prepareStatement(updateQuery);
                preparedStatement.setString(1,cart.getQuantity());
                preparedStatement.setString(2,cart.getMedicineID());

                preparedStatement.executeUpdate();
                preparedStatement.close();
            }

            catch(Exception e1)
            {
                e1.printStackTrace();
            }
        });
        rightDown.getChildren().add(complete);

        ObservableList<Cart> cartObservableList = FXCollections.observableArrayList();

        TableColumn<Cart,String> PatientID = new TableColumn<>("PATIENT ID");
        PatientID.setCellValueFactory(new PropertyValueFactory<>("PatientID"));
        PatientID.setPrefWidth(116);

        TableColumn<Cart,String> MedicineID = new TableColumn<>("MEDICINE ID");
        MedicineID.setCellValueFactory(new PropertyValueFactory<>("MedicineID"));
        MedicineID.setPrefWidth(116);

        TableColumn<Cart,String> MedicineName = new TableColumn<>("MEDICINE NAME");
        MedicineName.setCellValueFactory(new PropertyValueFactory<>("MedicineName"));
        MedicineName.setPrefWidth(116);

        TableColumn<Cart,String> Price = new TableColumn<>("PRICE");
        Price.setCellValueFactory(new PropertyValueFactory<>("Price"));
        Price.setPrefWidth(116);

        TableColumn<Cart,String> Quantity = new TableColumn<>("QUANTITY");
        Quantity.setCellValueFactory(new PropertyValueFactory<>("Quantity"));
        Quantity.setPrefWidth(116);

        TableColumn<Cart,String> TotalPrice = new TableColumn<>("TOTAL PRICE");
        TotalPrice.setCellValueFactory(new PropertyValueFactory<>("TotalPrice"));
        TotalPrice.setPrefWidth(116);

        try
        {
            resultSet = connection.createStatement().executeQuery("SELECT * FROM Cart");

            while (resultSet.next())
            {
                cartObservableList.add(new Cart(
                        resultSet.getString("PatientID"),
                        resultSet.getString("MedicineID"),
                        resultSet.getString("MedicineName"),
                        resultSet.getString("Price"),
                        resultSet.getString("Quantity"),
                        resultSet.getString("TotalPrice")
                ));
            }
        }

        catch(Exception e1)
        {
            e1.printStackTrace();
        }

        cartTableView = new TableView<>();
        cartTableView.getColumns().addAll(PatientID,MedicineID,MedicineName,Price,Quantity,TotalPrice);
        cartTableView.setItems(cartObservableList);
        cartTableView.getSelectionModel().setSelectionMode(
                SelectionMode.MULTIPLE
        );

        cartTableView.setMinWidth(700);
        cartTableView.setMinHeight(500);
        cartTableView.setLayoutY(0);
        cartTableView.setLayoutX(0);

        tablePane.getChildren().add(cartTableView);
        rightDown.getChildren().addAll(tablePane);

        scene.getStylesheets().add("Admin/Doctor/AddDoctor.css");
    }
}


What I have tried:

I have tried using for loop.
I have tried using selectALL in TableView.
But no one did work
Posted
Comments
Gerry Schmitz 7-Mar-21 14:20pm
   
Looks like business logic mixed in with screen handling; a partitioning problem ... can't tell what needs to be done, where.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900