Updated service (time validation)

This commit is contained in:
Nikitha
2026-02-23 16:11:20 -07:00
parent c3ed8b0921
commit 0cfb5e3cce
3 changed files with 104 additions and 28 deletions

View File

@@ -11,6 +11,8 @@ 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; import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
public class ServiceDialogController { public class ServiceDialogController {
@@ -38,11 +40,21 @@ public class ServiceDialogController {
@FXML @FXML
private TextField txtServicePrice; private TextField txtServicePrice;
@FXML
private ComboBox<Integer> cbHours;
@FXML
private ComboBox<Integer> cbMinutes;
private String mode; private String mode;
private Service selectedService; private Service selectedService;
@FXML @FXML
public void initialize() { public void initialize() {
cbHours.getItems().addAll(0, 1, 2, 3, 4);
cbMinutes.getItems().addAll(0, 15, 30, 45);
btnSave.setOnAction(e -> saveService()); btnSave.setOnAction(e -> saveService());
btnCancel.setOnAction(e -> close()); btnCancel.setOnAction(e -> close());
} }
@@ -64,26 +76,49 @@ public class ServiceDialogController {
lblServiceId.setText("ID: " + service.getServiceId()); lblServiceId.setText("ID: " + service.getServiceId());
txtServiceName.setText(service.getServiceName()); txtServiceName.setText(service.getServiceName());
txtServiceDesc.setText(service.getServiceDesc()); 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())); txtServicePrice.setText(String.valueOf(service.getServicePrice()));
} }
private void saveService() { 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 { try {
String name = txtServiceName.getText(); duration = Integer.parseInt(durationText);
String desc = txtServiceDesc.getText(); price = Double.parseDouble(priceText);
int duration = Integer.parseInt(txtServiceDuration.getText()); } catch (NumberFormatException e) {
double price = Double.parseDouble(txtServicePrice.getText()); showError("Duration must be a whole number and Price must be numeric.");
return;
}
Service service = new Service( Service service = new Service(
selectedService == null ? 0 : selectedService.getServiceId(), selectedService == null ? 0 : selectedService.getServiceId(),
name, name,
desc, desc,
duration, duration,
price price
); );
try {
if (mode.equals("Add")) { if (mode.equals("Add")) {
ServiceDB.insertService(service); ServiceDB.insertService(service);
} else { } else {
@@ -94,10 +129,17 @@ public class ServiceDialogController {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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() { private void close() {
Stage stage = (Stage) btnSave.getScene().getWindow(); Stage stage = (Stage) btnSave.getScene().getWindow();
stage.close(); stage.close();

View File

@@ -8,9 +8,9 @@ import java.sql.*;
public class ServiceDB { public class ServiceDB {
// ============================ //
// GET ALL SERVICES // GET ALL SERVICES
// ============================ //
public static ObservableList<Service> getServices() throws SQLException { public static ObservableList<Service> getServices() throws SQLException {
ObservableList<Service> list = FXCollections.observableArrayList(); ObservableList<Service> list = FXCollections.observableArrayList();
@@ -37,9 +37,9 @@ public class ServiceDB {
return list; return list;
} }
// ============================ //
// INSERT SERVICE // INSERT SERVICE
// ============================ //
public static int insertService(Service service) throws SQLException { public static int insertService(Service service) throws SQLException {
Connection conn = ConnectionDB.getConnection(); Connection conn = ConnectionDB.getConnection();
@@ -61,9 +61,9 @@ public class ServiceDB {
return rows; return rows;
} }
// ============================ //
// UPDATE SERVICE // UPDATE SERVICE
// ============================ //
public static int updateService(int id, Service service) throws SQLException { public static int updateService(int id, Service service) throws SQLException {
Connection conn = ConnectionDB.getConnection(); Connection conn = ConnectionDB.getConnection();
@@ -87,9 +87,9 @@ public class ServiceDB {
return rows; return rows;
} }
// ============================ //
// DELETE SERVICE // DELETE SERVICE
// ============================ //
public static int deleteService(int id) throws SQLException { public static int deleteService(int id) throws SQLException {
Connection conn = ConnectionDB.getConnection(); Connection conn = ConnectionDB.getConnection();

View File

@@ -11,6 +11,7 @@
<?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.Font?>
<?import javafx.scene.control.ComboBox?>
<VBox minHeight="-Infinity" minWidth="-Infinity" prefHeight="523.0" prefWidth="790.0" spacing="20.0" style="-fx-font-size: 14px;" xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.example.petshopdesktop.controllers.dialogcontrollers.ServiceDialogController"> <VBox minHeight="-Infinity" minWidth="-Infinity" prefHeight="523.0" prefWidth="790.0" spacing="20.0" style="-fx-font-size: 14px;" xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.example.petshopdesktop.controllers.dialogcontrollers.ServiceDialogController">
<children> <children>
@@ -102,19 +103,52 @@
</TextField> </TextField>
</children> </children>
</VBox> </VBox>
<VBox prefHeight="200.0" prefWidth="100.0" spacing="8.0" GridPane.rowIndex="1"> <VBox prefHeight="200.0" prefWidth="100.0"
spacing="8.0"
GridPane.rowIndex="1">
<children> <children>
<Label text="Duration: (min)" textFill="#2c3e50">
<Label text="Duration:" textFill="#2c3e50">
<font> <font>
<Font name="System Bold" size="16.0" /> <Font name="System Bold" size="16.0" />
</font> </font>
</Label> </Label>
<TextField fx:id="txtServiceDuration" style="-fx-border-color: #E8EBED; -fx-border-width: 2; -fx-border-radius: 10; -fx-background-radius: 10;">
<padding> <HBox spacing="10">
<Insets bottom="7.0" left="10.0" right="10.0" top="7.0" />
</padding> <!-- Hours Combo -->
</TextField> <ComboBox fx:id="cbHours"
promptText="Hours"
prefWidth="120"
style="-fx-border-color: #E8EBED;
-fx-border-width: 2;
-fx-border-radius: 10;
-fx-background-radius: 10;
-fx-background-color: white;">
<padding>
<Insets bottom="3.0" left="10.0" right="10.0" top="3.0" />
</padding>
</ComboBox>
<!-- Minutes Combo -->
<ComboBox fx:id="cbMinutes"
promptText="Minutes"
prefWidth="120"
style="-fx-border-color: #E8EBED;
-fx-border-width: 2;
-fx-border-radius: 10;
-fx-background-radius: 10;
-fx-background-color: white;">
<padding>
<Insets bottom="3.0" left="10.0" right="10.0" top="3.0" />
</padding>
</ComboBox>
</HBox>
</children> </children>
</VBox> </VBox>
<VBox prefHeight="200.0" prefWidth="100.0" spacing="8.0" GridPane.columnIndex="1" GridPane.rowIndex="1"> <VBox prefHeight="200.0" prefWidth="100.0" spacing="8.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<children> <children>