Updated service (time validation)
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user