updated service

This commit is contained in:
Nikitha
2026-02-23 08:49:12 -07:00
parent 567a77c3a7
commit 8bb502c4cc
3 changed files with 107 additions and 114 deletions

View File

@@ -4,13 +4,17 @@ import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Node;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.example.petshopdesktop.database.ServiceDB; import org.example.petshopdesktop.database.ServiceDB;
import org.example.petshopdesktop.models.Service; import org.example.petshopdesktop.models.Service;
import org.example.petshopdesktop.controllers.dialogcontrollers.ServiceDialogController; import org.example.petshopdesktop.controllers.dialogcontrollers.ServiceDialogController;
import javafx.stage.Modality;
public class ServiceController { public class ServiceController {
@@ -52,7 +56,7 @@ public class ServiceController {
@FXML @FXML
void btnAddClicked(ActionEvent event) { void btnAddClicked(ActionEvent event) {
openDialog(null); openDialog(null, "Add");
loadServices(); loadServices();
} }
@@ -66,50 +70,46 @@ public class ServiceController {
return; return;
} }
openDialog(selected); openDialog(selected, "Edit");
loadServices(); loadServices();
} }
@FXML @FXML
void btnDeleteClicked(ActionEvent event) { void btnDeleteClicked(ActionEvent e) {
Service selected = tvServices.getSelectionModel().getSelectedItem(); Service service = tvServices.getSelectionModel().getSelectedItem();
if (service == null) return;
if (selected == null) {
showAlert("Select Service", "Please select a service to delete.");
return;
}
try { try {
ServiceDB.deleteService(selected.getServiceId()); ServiceDB.deleteService(service.getServiceId());
loadServices(); loadServices();
} catch (Exception e) { } catch (Exception ex) {
showAlert("Database Error", "Unable to delete service."); ex.printStackTrace();
e.printStackTrace();
} }
} }
private void openDialog(Service service) { private void openDialog(Service service, String mode) {
try { try {
FXMLLoader loader = new FXMLLoader( FXMLLoader loader = new FXMLLoader(
getClass().getResource( getClass().getResource("/org/example/petshopdesktop/dialogviews/service-dialog-view.fxml")
"/org.example.petshopdesktop/dialogviews/ServiceDialogView.fxml"
)
); );
Parent root = loader.load();
// IMPORTANT: specify controller type
ServiceDialogController controller =
loader.getController();
controller.setService(service);
Stage stage = new Stage(); Stage stage = new Stage();
stage.setScene(new Scene(root)); stage.setScene(new Scene(loader.load()));
ServiceDialogController controller = loader.getController();
controller.setMode(mode);
if (mode.equals("Edit")) {
controller.setService(service);
}
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait(); stage.showAndWait();
loadServices();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -1,6 +1,6 @@
package org.example.petshopdesktop.controllers.dialogcontrollers; package org.example.petshopdesktop.controllers.dialogcontrollers;
import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.Button; import javafx.scene.control.Button;
@@ -10,6 +10,7 @@ import javafx.scene.input.MouseEvent;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.example.petshopdesktop.database.ServiceDB; import org.example.petshopdesktop.database.ServiceDB;
import org.example.petshopdesktop.models.Service; import org.example.petshopdesktop.models.Service;
import javafx.scene.control.Alert;
public class ServiceDialogController { public class ServiceDialogController {
@@ -37,62 +38,68 @@ public class ServiceDialogController {
@FXML @FXML
private TextField txtServicePrice; private TextField txtServicePrice;
private Service service; // holds selected service private String mode;
private Service selectedService;
// This method is called from ServiceController @FXML
public void setService(Service service) { public void initialize() {
btnSave.setOnAction(e -> saveService());
btnCancel.setOnAction(e -> close());
}
this.service = service; public void setMode(String mode) {
this.mode = mode;
lblMode.setText(mode + " Service");
if (service == null) { if (mode.equals("Add")) {
lblMode.setText("Add Service"); lblServiceId.setVisible(false);
lblServiceId.setText("New");
} else { } else {
lblMode.setText("Edit Service"); lblServiceId.setVisible(true);
lblServiceId.setText("ID: " + service.getServiceId());
txtServiceName.setText(service.getServiceName());
txtServiceDesc.setText(service.getServiceDesc());
txtServiceDuration.setText(
String.valueOf(service.getServiceDuration()));
txtServicePrice.setText(
String.valueOf(service.getServicePrice()));
} }
} }
@FXML public void setService(Service service) {
private void buttonSaveClicked(MouseEvent mouseEvent) { this.selectedService = service;
lblServiceId.setText("ID: " + service.getServiceId());
txtServiceName.setText(service.getServiceName());
txtServiceDesc.setText(service.getServiceDesc());
txtServiceDuration.setText(String.valueOf(service.getServiceDuration()));
txtServicePrice.setText(String.valueOf(service.getServicePrice()));
}
private void saveService() {
try { try {
String name = txtServiceName.getText();
String desc = txtServiceDesc.getText();
int duration = Integer.parseInt(txtServiceDuration.getText());
double price = Double.parseDouble(txtServicePrice.getText());
if (service == null) { Service service = new Service(
service = new Service(0, "", "", 0, 0); selectedService == null ? 0 : selectedService.getServiceId(),
} name,
desc,
duration,
price
);
service.setServiceName(txtServiceName.getText()); if (mode.equals("Add")) {
service.setServiceDesc(txtServiceDesc.getText());
service.setServiceDuration(
Integer.parseInt(txtServiceDuration.getText()));
service.setServicePrice(
Double.parseDouble(txtServicePrice.getText()));
if (service.getServiceId() == 0) {
ServiceDB.insertService(service); ServiceDB.insertService(service);
} else { } else {
ServiceDB.updateService(service.getServiceId(), service); ServiceDB.updateService(selectedService.getServiceId(), service);
} }
closeStage(mouseEvent); close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
new Alert(Alert.AlertType.ERROR, "Invalid Input").showAndWait();
} }
} }
@FXML private void close() {
private void closeStage(MouseEvent mouseEvent) { Stage stage = (Stage) btnSave.getScene().getWindow();
Node node = (Node) mouseEvent.getSource();
Stage stage = (Stage) node.getScene().getWindow();
stage.close(); stage.close();
} }
} }

View File

@@ -6,53 +6,46 @@ import org.example.petshopdesktop.models.Service;
import java.sql.*; import java.sql.*;
/**
* A class containing all the methods relating to CRUD on Services table
*/
public class ServiceDB { public class ServiceDB {
/** // ============================
* gets all the services into an observable list // GET ALL SERVICES
* @return a list of all the services // ============================
* @throws SQLException if failed to find services in the database
*/
public static ObservableList<Service> getServices() throws SQLException { public static ObservableList<Service> getServices() throws SQLException {
ObservableList<Service> services = FXCollections.observableArrayList(); ObservableList<Service> list = FXCollections.observableArrayList();
Connection conn = ConnectionDB.getConnection(); Connection conn = ConnectionDB.getConnection();
String sql = "SELECT * FROM service";
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM service"); ResultSet rs = stmt.executeQuery(sql);
Service service;
while (rs.next()) { while (rs.next()) {
service = new Service( Service service = new Service(
rs.getInt(1), // service_id rs.getInt("serviceId"),
rs.getString(2),// service_name rs.getString("serviceName"),
rs.getString(3),// description rs.getString("serviceDesc"),
rs.getInt(4), // duration rs.getInt("serviceDuration"),
rs.getDouble(5)// price rs.getDouble("servicePrice")
); );
services.add(service); list.add(service);
} }
conn.close(); conn.close();
return services; return list;
} }
/** // ============================
* Inserts a new service to the database // INSERT SERVICE
*/ // ============================
public static int insertService(Service service) throws SQLException { public static int insertService(Service service) throws SQLException {
int numRows;
Connection conn = ConnectionDB.getConnection(); Connection conn = ConnectionDB.getConnection();
String sql = String sql =
"INSERT INTO services (service_name, description, duration, price) " + "INSERT INTO service (serviceName, serviceDesc, serviceDuration, servicePrice) " +
"VALUES (?, ?, ?, ?)"; "VALUES (?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);
@@ -62,28 +55,23 @@ public class ServiceDB {
stmt.setInt(3, service.getServiceDuration()); stmt.setInt(3, service.getServiceDuration());
stmt.setDouble(4, service.getServicePrice()); stmt.setDouble(4, service.getServicePrice());
numRows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
conn.close(); conn.close();
return numRows; return rows;
} }
/** // ============================
* Update existing service // UPDATE SERVICE
*/ // ============================
public static int updateService(int serviceId, Service service) throws SQLException { public static int updateService(int id, Service service) throws SQLException {
int numRows;
Connection conn = ConnectionDB.getConnection(); Connection conn = ConnectionDB.getConnection();
String sql = String sql =
"UPDATE services SET " + "UPDATE service SET " +
" service_name = ?, " + "serviceName=?, serviceDesc=?, serviceDuration=?, servicePrice=? " +
" description = ?, " + "WHERE serviceId=?";
" duration = ?, " +
" price = ? " +
" WHERE service_id = ?";
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);
@@ -91,31 +79,29 @@ public class ServiceDB {
stmt.setString(2, service.getServiceDesc()); stmt.setString(2, service.getServiceDesc());
stmt.setInt(3, service.getServiceDuration()); stmt.setInt(3, service.getServiceDuration());
stmt.setDouble(4, service.getServicePrice()); stmt.setDouble(4, service.getServicePrice());
stmt.setInt(5, serviceId); stmt.setInt(5, id);
numRows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
conn.close(); conn.close();
return numRows; return rows;
} }
/** // ============================
* Delete a service // DELETE SERVICE
*/ // ============================
public static int deleteService(int serviceId) throws SQLException { public static int deleteService(int id) throws SQLException {
int numRows;
Connection conn = ConnectionDB.getConnection(); Connection conn = ConnectionDB.getConnection();
String sql = "DELETE FROM services WHERE service_id = ?"; String sql = "DELETE FROM service WHERE serviceId=?";
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, serviceId); stmt.setInt(1, id);
numRows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
conn.close(); conn.close();
return numRows; return rows;
} }
} }