From fdd15afe5224ba634ebb17b5cc308624838f1187 Mon Sep 17 00:00:00 2001 From: Andriy Cherniy Date: Sat, 15 Jun 2024 03:40:35 +0300 Subject: 13-15 --- .../oop/lab56maven/entities/macro/Fortress.java | 23 ++++++++++++++++++++++ .../quga/oop/lab56maven/entities/micro/Dwarf.java | 5 +++++ .../oop/lab56maven/entities/micro/Immigrant.java | 8 +++++++- 3 files changed, 35 insertions(+), 1 deletion(-) (limited to 'src/main/java/space/m0e/quga/oop/lab56maven/entities') diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java b/src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java index 9ab9114..95d36b1 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/entities/macro/Fortress.java @@ -1,13 +1,16 @@ package space.m0e.quga.oop.lab56maven.entities.macro; +import javafx.scene.control.Alert; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.layout.*; import javafx.scene.paint.Color; import space.m0e.quga.oop.lab56maven.Main; +import space.m0e.quga.oop.lab56maven.dialogs.ChangeDialog; import space.m0e.quga.oop.lab56maven.entities.micro.Dwarf; import space.m0e.quga.oop.lab56maven.entities.micro.Immigrant; +import java.io.IOException; import java.util.ArrayList; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; @@ -29,11 +32,17 @@ public class Fortress { private double y; private VBox vBox = new VBox(); + @Override + public String toString() { + return String.format("%s{Name=%s, Position=(%f,%f)}", this.getClass().getSimpleName(), this.name, getX(), getY()); + } + private TilePane dwarvesContainer = new TilePane(10, 10); public Fortress(String name, double x, double y, ArrayList workstations, ArrayList throneRooms) { this.x = x; this.y = y; + this.name = name; nameLabel.setText(name); countLabel.setText("0"); @@ -91,7 +100,21 @@ public class Fortress { vBox.getChildren().addAll(nameLabel, insideGroup, countLabel); this.vBox.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(3)))); + + this.vBox.setOnMouseClicked(event -> { + switch (event.getButton()) { + case SECONDARY -> { + String result = Immigrant.findImmigrants(immigrant -> this.dwarves.contains(immigrant)); + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("Search result"); + alert.setContentText(result); + alert.setHeaderText("Dwarves inside Fortress:"); + alert.showAndWait(); + } + } + }); Main.root.getChildren().add(vBox); + }; public void add(Dwarf dwarf) { diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java b/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java index 5fab26b..9c3008e 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Dwarf.java @@ -31,6 +31,11 @@ public class Dwarf extends Immigrant { this.workstation = workstation; } + @Override + public String toString() { + return String.format("%s{Name=%s, Age=%d, Ability=%s, HP: %f/%f, Items: [%s], Position=(%f,%f), MacroObject=%s}", this.getClass().getSimpleName(), this.getFullName(), this.getAge(), this.getAbility(), this.getHp(), this.getMaxHp(), String.join(", ", this.getItems()), getX(), getY(), getFortress()); + } + private Fortress fortress = null; private Workstation workstation = null; public Image spriteImage = new Image(Objects.requireNonNull(Immigrant.class.getResourceAsStream("dwarf.png")));; diff --git a/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java b/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java index 34581f4..1c86fa7 100644 --- a/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java +++ b/src/main/java/space/m0e/quga/oop/lab56maven/entities/micro/Immigrant.java @@ -17,6 +17,8 @@ import space.m0e.quga.oop.lab56maven.threads.ImmigrantThread; import java.io.IOException; import java.util.*; import java.util.concurrent.ForkJoinTask; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; public class Immigrant implements Comparable, Cloneable { @@ -205,7 +207,7 @@ public class Immigrant implements Comparable, Cloneable { @Override public String toString() { - return String.format("%s{Name=%s, Age=%d, Ability=%s, HP: %f/%f, Items: [%s]}", this.getClass().getSimpleName(), this.getFullName(), this.age, this.ability, this.hp, this.maxHp, String.join(", ", this.items)); + return String.format("%s{Name=%s, Age=%d, Ability=%s, HP: %f/%f, Items: [%s], Position=(%f,%f)}", this.getClass().getSimpleName(), this.getFullName(), this.age, this.ability, this.hp, this.maxHp, String.join(", ", this.items), getX(), getY()); } @Override @@ -334,4 +336,8 @@ public class Immigrant implements Comparable, Cloneable { public Thread getThread() { return thread; } + + public static String findImmigrants(Predicate filterQuery) { + return Main.immigrants.stream().filter(filterQuery).map(Immigrant::toString).collect(Collectors.joining("\n\n")); + } } -- cgit v1.3-3-ga95d