From d0f951643060821673e060eea7376000418c457b Mon Sep 17 00:00:00 2001 From: Andriy Cherniy Date: Sat, 15 Jun 2024 10:45:30 +0300 Subject: 16 --- .../quga/oop/lab56maven/dialogs/InsertDialog.java | 13 ++-- .../quga/oop/lab56maven/dialogs/SearchDialog.java | 11 ++-- .../quga/oop/lab56maven/dialogs/SortDialog.java | 74 ++++++++++++++++++++++ 3 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 src/main/java/space/m0e/quga/oop/lab56maven/dialogs/SortDialog.java (limited to 'src/main/java/space/m0e/quga/oop/lab56maven/dialogs') diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/InsertDialog.java b/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/InsertDialog.java index c0c72f3..13226c8 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/InsertDialog.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/InsertDialog.java @@ -25,6 +25,8 @@ import java.util.Arrays; import java.util.Objects; import java.util.ResourceBundle; +import static space.m0e.quga.oop.lab56maven.entities.common.Utils.sendErrorAlert; + public class InsertDialog implements Initializable { public static Stage window = null; public static Scene scene; @@ -63,13 +65,6 @@ public class InsertDialog implements Initializable { classSelection.setValue("Immigrant"); } - public void sendError(String title, String description) { - Alert alert = new Alert(Alert.AlertType.ERROR); - alert.setTitle("Error"); - alert.setHeaderText(title); - alert.setContentText(description); - alert.showAndWait(); - } @FXML public void submitButtonHandler() { @@ -81,7 +76,7 @@ public class InsertDialog implements Initializable { ability.getValue() == null || classSelection.getValue() == null ) { - sendError("You must enter all the fields", null); + sendErrorAlert("You must enter all the fields", null); return; } int inputAge = Integer.parseInt(age.getText()); @@ -89,7 +84,7 @@ public class InsertDialog implements Initializable { double inputY = Double.parseDouble(y.getText()); Ability inputAbility = Ability.valueOf(ability.getValue().toString()); if (inputAge < 0) { - sendError("You must enter a valid age", null); + sendErrorAlert("You must enter a valid age", null); return; } diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/SearchDialog.java b/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/SearchDialog.java index 014c7c0..df8ceae 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/SearchDialog.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/SearchDialog.java @@ -14,6 +14,7 @@ import javafx.stage.Modality; import javafx.stage.Stage; import space.m0e.quga.oop.lab56maven.Main; import space.m0e.quga.oop.lab56maven.entities.common.Ability; +import space.m0e.quga.oop.lab56maven.entities.common.Utils; import space.m0e.quga.oop.lab56maven.entities.micro.Immigrant; import java.io.IOException; @@ -56,8 +57,8 @@ public class SearchDialog implements Initializable { alert.showAndWait(); } - public void submitButtonHandler(MouseEvent mouseEvent) { - String result = Immigrant.findImmigrants(immigrant -> { + public void submitButtonHandler(MouseEvent mouseEvent) throws IOException { + Utils.findImmigrants("Search result", immigrant -> { switch (searchCombo.getValue().toString()) { case "Name" -> { return immigrant.getFullName().contains(searchQuery.getText()); @@ -68,10 +69,6 @@ public class SearchDialog implements Initializable { } return false; }); - Alert alert = new Alert(Alert.AlertType.INFORMATION); - alert.setTitle("Search result"); - alert.setContentText(result); - alert.setHeaderText("Search result"); - alert.showAndWait(); + window.close(); } } diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/SortDialog.java b/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/SortDialog.java new file mode 100644 index 0000000..a9d313e --- /dev/null +++ b/src/main/java/space/m0e/quga/oop/lab56maven/dialogs/SortDialog.java @@ -0,0 +1,74 @@ +package space.m0e.quga.oop.lab56maven.dialogs; + +import javafx.fxml.FXMLLoader; +import javafx.fxml.Initializable; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.ComboBox; +import javafx.scene.control.TextField; +import javafx.scene.input.MouseEvent; +import javafx.stage.Modality; +import javafx.stage.Stage; +import space.m0e.quga.oop.lab56maven.entities.common.Utils; +import space.m0e.quga.oop.lab56maven.entities.micro.Immigrant; + +import java.io.IOException; +import java.net.URL; +import java.util.*; +import java.util.stream.Collectors; + +public class SortDialog implements Initializable { + public static Stage window = null; + public static Scene scene; + public ComboBox searchCombo; + public Button submitButton; + + private static ArrayList sortInput = new ArrayList<>(); + private static String name; + + public static void display(ArrayList sort, String alertName) throws IOException { + Parent root = FXMLLoader.load(Objects.requireNonNull(SortDialog.class.getResource("sort_dialog.fxml"))); + sortInput.addAll(sort); + name = alertName; + + window = new Stage(); + window.initModality(Modality.APPLICATION_MODAL); + window.setTitle("Sort"); + + scene = new Scene(root, 440, 350); + window.setScene(scene); + window.showAndWait(); + } + + @Override + public void initialize(URL location, ResourceBundle resources) { + searchCombo.getItems().addAll("Name", "Age"); + searchCombo.setValue("Name"); + } + + public void submitButtonHandler(MouseEvent mouseEvent) { + sortInput.sort(new Comparator() { + + @Override + public int compare(Immigrant o1, Immigrant o2) { + switch (searchCombo.getValue().toString()) { + case "Name" -> { + return o1.getFullName().compareTo(o2.getFullName()); + } + case "Age" -> { + return o1.getAge() - o2.getAge(); + } + } + return 0; + } + }); + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle(name); + alert.setContentText(sortInput.stream().map(Immigrant::toString).collect(Collectors.joining("\n\n"))); + alert.setHeaderText(name); + alert.showAndWait(); + window.close(); + } +} -- cgit v1.3-3-ga95d