diff --git a/desktop/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/PetDialogController.java b/desktop/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/PetDialogController.java index c6794758..6badb4c6 100644 --- a/desktop/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/PetDialogController.java +++ b/desktop/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/PetDialogController.java @@ -55,6 +55,9 @@ public class PetDialogController { @FXML private VBox vbStoreField; + @FXML + private VBox vbPriceField; + @FXML private Label lblMode; @@ -80,7 +83,7 @@ public class PetDialogController { private TextField txtPetPrice; @FXML - private TextField txtPetSpecies; + private ComboBox cbPetSpecies; private String mode = null; private File selectedImageFile; @@ -127,6 +130,9 @@ public class PetDialogController { setFieldVisibility(vbCustomerField, false); setFieldVisibility(vbStoreField, false); + setFieldVisibility(vbPriceField, true); + + loadSpecies(); cbPetStatus.valueProperty().addListener((obs, oldVal, newVal) -> { updateStatusFieldVisibility(newVal); @@ -161,12 +167,16 @@ public class PetDialogController { errorMsg += Validator.isPresent(txtPetName.getText(), "Pet Name"); errorMsg += Validator.isPresent(txtPetAge.getText(), "Age"); errorMsg += Validator.isPresent(txtPetBreed.getText(), "Breed"); - errorMsg += Validator.isPresent(txtPetSpecies.getText(), "Species"); - errorMsg += Validator.isPresent(txtPetPrice.getText(), "Price"); + String speciesValue = cbPetSpecies.getValue() != null ? cbPetSpecies.getValue().trim() : ""; + if (speciesValue.isEmpty()) errorMsg += "Species is required\n"; + String selectedStatus = cbPetStatus.getValue(); + boolean needsPrice = !("Owned".equalsIgnoreCase(selectedStatus) || "Adopted".equalsIgnoreCase(selectedStatus)); + if (needsPrice) { + errorMsg += Validator.isPresent(txtPetPrice.getText(), "Price"); + } if (cbPetStatus.getSelectionModel().getSelectedItem() == null){ errorMsg += "Status is required"; } - String selectedStatus = cbPetStatus.getValue(); if ("Owned".equalsIgnoreCase(selectedStatus) && cbCustomer.getValue() == null) { errorMsg += "Customer is required for Owned status\n"; } @@ -176,13 +186,17 @@ public class PetDialogController { //Check validation (length size) errorMsg += Validator.isLessThanVarChars(txtPetName.getText(), "Pet Name", 50); - errorMsg += Validator.isLessThanVarChars(txtPetSpecies.getText(), "Species", 50); + errorMsg += Validator.isLessThanVarChars(speciesValue, "Species", 50); errorMsg += Validator.isLessThanVarChars(txtPetBreed.getText(), "Breed", 50); - errorMsg += Validator.isLessThanVarChars(txtPetPrice.getText(), "Price", 12); + if (needsPrice) { + errorMsg += Validator.isLessThanVarChars(txtPetPrice.getText(), "Price", 12); + } errorMsg += Validator.isLessThanVarChars(txtPetAge.getText(), "Age", 11); //Check validation (format) - errorMsg += Validator.isNonNegativeDouble(txtPetPrice.getText(), "Price"); + if (needsPrice) { + errorMsg += Validator.isNonNegativeDouble(txtPetPrice.getText(), "Price"); + } errorMsg += Validator.isPositiveInteger(txtPetAge.getText(), "Age"); if(errorMsg.isEmpty()){ @@ -229,13 +243,17 @@ public class PetDialogController { private PetRequest buildPetRequest() { PetRequest request = new PetRequest(); request.setPetName(txtPetName.getText()); - request.setPetSpecies(txtPetSpecies.getText()); + request.setPetSpecies(cbPetSpecies.getValue() != null ? cbPetSpecies.getValue().trim() : ""); request.setPetBreed(txtPetBreed.getText()); request.setPetStatus(cbPetStatus.getValue()); - try { - request.setPetPrice(new BigDecimal(txtPetPrice.getText())); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("Invalid price format"); + String buildStatus = cbPetStatus.getValue(); + boolean buildNeedsPrice = !("Owned".equalsIgnoreCase(buildStatus) || "Adopted".equalsIgnoreCase(buildStatus)); + if (buildNeedsPrice && txtPetPrice.getText() != null && !txtPetPrice.getText().isBlank()) { + try { + request.setPetPrice(new BigDecimal(txtPetPrice.getText())); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("Invalid price format"); + } } int age; @@ -257,6 +275,27 @@ public class PetDialogController { return request; } + private void loadSpecies() { + new Thread(() -> { + try { + List options = DropdownApi.getInstance().getPetSpecies(); + List species = options.stream() + .map(DropdownOption::getLabel) + .collect(java.util.stream.Collectors.toList()); + Platform.runLater(() -> { + String current = cbPetSpecies.getValue(); + cbPetSpecies.setItems(FXCollections.observableArrayList(species)); + if (current != null && !current.isBlank()) { + cbPetSpecies.setValue(current); + } + }); + } catch (Exception e) { + Platform.runLater(() -> ActivityLogger.getInstance().logException( + "PetDialogController.loadSpecies", e, "Loading species dropdown")); + } + }).start(); + } + private void loadCustomers() { new Thread(() -> { try { @@ -331,7 +370,7 @@ public class PetDialogController { if (pet!=null){ lblPetId.setText("ID: " + pet.getPetId()); txtPetName.setText(pet.getPetName()); - txtPetSpecies.setText(pet.getPetSpecies()); + cbPetSpecies.setValue(pet.getPetSpecies()); txtPetBreed.setText(pet.getPetBreed()); txtPetAge.setText(pet.getPetAge() + ""); txtPetPrice.setText(pet.getPetPrice() + ""); @@ -434,8 +473,10 @@ public class PetDialogController { private void updateStatusFieldVisibility(String status) { boolean needsCustomer = "Owned".equalsIgnoreCase(status) || "Adopted".equalsIgnoreCase(status); boolean storeBased = requiresStore(status); + boolean needsPrice = !needsCustomer; setFieldVisibility(vbCustomerField, needsCustomer); setFieldVisibility(vbStoreField, storeBased); + setFieldVisibility(vbPriceField, needsPrice); } private boolean requiresStore(String status) { diff --git a/desktop/src/main/resources/org/example/petshopdesktop/dialogviews/pet-dialog-view.fxml b/desktop/src/main/resources/org/example/petshopdesktop/dialogviews/pet-dialog-view.fxml index 9130513a..6f8bf1e0 100644 --- a/desktop/src/main/resources/org/example/petshopdesktop/dialogviews/pet-dialog-view.fxml +++ b/desktop/src/main/resources/org/example/petshopdesktop/dialogviews/pet-dialog-view.fxml @@ -91,11 +91,11 @@ - + - + - + @@ -139,7 +139,7 @@ - +