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.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<Integer> cbHours;
@FXML
private ComboBox<Integer> 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();

View File

@@ -8,9 +8,9 @@ import java.sql.*;
public class ServiceDB {
// ============================
//
// GET ALL SERVICES
// ============================
//
public static ObservableList<Service> getServices() throws SQLException {
ObservableList<Service> 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();

View File

@@ -11,6 +11,7 @@
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?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">
<children>
@@ -102,19 +103,52 @@
</TextField>
</children>
</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>
<Label text="Duration: (min)" textFill="#2c3e50">
<Label text="Duration:" textFill="#2c3e50">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Label>
<TextField fx:id="txtServiceDuration" style="-fx-border-color: #E8EBED; -fx-border-width: 2; -fx-border-radius: 10; -fx-background-radius: 10;">
<padding>
<Insets bottom="7.0" left="10.0" right="10.0" top="7.0" />
</padding>
</TextField>
<HBox spacing="10">
<!-- Hours Combo -->
<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>
</VBox>
<VBox prefHeight="200.0" prefWidth="100.0" spacing="8.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<children>