diff --git a/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/ServiceDialogController.java b/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/ServiceDialogController.java index 4151808f..3daac3af 100644 --- a/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/ServiceDialogController.java +++ b/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/ServiceDialogController.java @@ -11,6 +11,8 @@ import javafx.stage.Stage; import org.example.petshopdesktop.database.ServiceDB; import org.example.petshopdesktop.models.Service; import javafx.scene.control.Alert; +import javafx.scene.control.ComboBox; + public class ServiceDialogController { @@ -38,11 +40,21 @@ public class ServiceDialogController { @FXML private TextField txtServicePrice; + @FXML + private ComboBox cbHours; + + @FXML + private ComboBox cbMinutes; + private String mode; private Service selectedService; + + @FXML public void initialize() { + cbHours.getItems().addAll(0, 1, 2, 3, 4); + cbMinutes.getItems().addAll(0, 15, 30, 45); btnSave.setOnAction(e -> saveService()); btnCancel.setOnAction(e -> close()); } @@ -64,26 +76,49 @@ public class ServiceDialogController { lblServiceId.setText("ID: " + service.getServiceId()); txtServiceName.setText(service.getServiceName()); txtServiceDesc.setText(service.getServiceDesc()); - txtServiceDuration.setText(String.valueOf(service.getServiceDuration())); + int totalMinutes = service.getServiceDuration(); + cbHours.setValue(totalMinutes / 60); + cbMinutes.setValue(totalMinutes % 60); txtServicePrice.setText(String.valueOf(service.getServicePrice())); } private void saveService() { + String name = txtServiceName.getText(); + String desc = txtServiceDesc.getText(); + String durationText = txtServiceDuration.getText(); + String priceText = txtServicePrice.getText(); + + // Empty checks + Integer hours = cbHours.getValue(); + Integer minutes = cbMinutes.getValue(); + + if (hours == null || minutes == null) { + showError("Please select hours and minutes."); + return; + } + + int duration = (hours * 60) + minutes; + double price; + + // Number validation try { - String name = txtServiceName.getText(); - String desc = txtServiceDesc.getText(); - int duration = Integer.parseInt(txtServiceDuration.getText()); - double price = Double.parseDouble(txtServicePrice.getText()); + duration = Integer.parseInt(durationText); + price = Double.parseDouble(priceText); + } catch (NumberFormatException e) { + showError("Duration must be a whole number and Price must be numeric."); + return; + } - Service service = new Service( - selectedService == null ? 0 : selectedService.getServiceId(), - name, - desc, - duration, - price - ); + Service service = new Service( + selectedService == null ? 0 : selectedService.getServiceId(), + name, + desc, + duration, + price + ); + try { if (mode.equals("Add")) { ServiceDB.insertService(service); } else { @@ -94,10 +129,17 @@ public class ServiceDialogController { } catch (Exception e) { e.printStackTrace(); - new Alert(Alert.AlertType.ERROR, "Invalid Input").showAndWait(); + showError("Database error while saving service."); } } + private void showError(String msg) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setHeaderText("Invalid Input"); + alert.setContentText(msg); + alert.showAndWait(); + } + private void close() { Stage stage = (Stage) btnSave.getScene().getWindow(); stage.close(); diff --git a/src/main/java/org/example/petshopdesktop/database/ServiceDB.java b/src/main/java/org/example/petshopdesktop/database/ServiceDB.java index 1d35f436..c5ca9ff7 100644 --- a/src/main/java/org/example/petshopdesktop/database/ServiceDB.java +++ b/src/main/java/org/example/petshopdesktop/database/ServiceDB.java @@ -8,9 +8,9 @@ import java.sql.*; public class ServiceDB { - // ============================ + // // GET ALL SERVICES - // ============================ + // public static ObservableList getServices() throws SQLException { ObservableList list = FXCollections.observableArrayList(); @@ -37,9 +37,9 @@ public class ServiceDB { return list; } - // ============================ + // // INSERT SERVICE - // ============================ + // public static int insertService(Service service) throws SQLException { Connection conn = ConnectionDB.getConnection(); @@ -61,9 +61,9 @@ public class ServiceDB { return rows; } - // ============================ + // // UPDATE SERVICE - // ============================ + // public static int updateService(int id, Service service) throws SQLException { Connection conn = ConnectionDB.getConnection(); @@ -87,9 +87,9 @@ public class ServiceDB { return rows; } - // ============================ + // // DELETE SERVICE - // ============================ + // public static int deleteService(int id) throws SQLException { Connection conn = ConnectionDB.getConnection(); diff --git a/src/main/resources/org/example/petshopdesktop/dialogviews/service-dialog-view.fxml b/src/main/resources/org/example/petshopdesktop/dialogviews/service-dialog-view.fxml index ccd4c24f..327ce2d0 100644 --- a/src/main/resources/org/example/petshopdesktop/dialogviews/service-dialog-view.fxml +++ b/src/main/resources/org/example/petshopdesktop/dialogviews/service-dialog-view.fxml @@ -11,6 +11,7 @@ + @@ -102,19 +103,52 @@ - + + - +