From c4544f46574938f303b43d9519a2a6ae2e637ce0 Mon Sep 17 00:00:00 2001 From: George177013 <george172@outlook.com> Date: Mon, 24 Mar 2025 09:21:52 +0100 Subject: [PATCH] Code review --- .../application/HeatCalculatorFX.java | 40 +++++++++++++++++- .../HeatCalculatorController.java | 41 +++++++++++++++++++ .../model/CuboidHeatstorage.java | 12 ++++++ .../model/GeneralHeatstorage.java | 12 ------ 4 files changed, 91 insertions(+), 14 deletions(-) rename HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/{application => controller}/HeatCalculatorController.java (50%) diff --git a/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/application/HeatCalculatorFX.java b/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/application/HeatCalculatorFX.java index fa5258f..34435b5 100644 --- a/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/application/HeatCalculatorFX.java +++ b/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/application/HeatCalculatorFX.java @@ -12,28 +12,64 @@ import javafx.stage.Stage; import java.util.ArrayList; import java.util.List; +/** + * Main application class for the Heat Calculator FX application. + * Extends the JavaFX Application class to manage the lifecycle of the application. + */ public class HeatCalculatorFX extends Application { + + /** + * Entry point for the JavaFX application. + * Initializes the models, controller, and view, and starts the application. + * + * @param stage The primary stage for the application. + * @throws Exception If an error occurs during initialization. + */ @Override public void start(Stage stage) throws Exception { + // Create the list of heat storage models List<GeneralHeatstorage> storages = createModels(); + + // Initialize the controller with the models HeatCalculatorController controller = new HeatCalculatorController(storages); + + // Create the view and associate it with the controller HeatstorageView_1 view = new HeatstorageView_1(stage, controller); controller.setView(view); + + // Start the view controller.startView(); } + /** + * Main method to launch the JavaFX application. + * + * @param args Command-line arguments. + */ public static void main(String[] args) { launch(args); } - protected List<GeneralHeatstorage> createModels(){ + /** + * Creates and initializes a list of heat storage models. + * + * @return A list of GeneralHeatstorage objects representing different heat storages. + */ + protected List<GeneralHeatstorage> createModels() { ArrayList<GeneralHeatstorage> storages = new ArrayList<>(); - Heatstorage heatstorage_1 = new Heatstorage("Speicher 1",23.3f,45f,22f); + + // Create and add a basic heat storage model + Heatstorage heatstorage_1 = new Heatstorage("Speicher 1", 23.3f, 45f, 22f); storages.add(heatstorage_1); + + // Create and add a cylindrical heat storage model CylindricalHeatstorage heatstorage_2 = new CylindricalHeatstorage("Speicher 2", 50f, 12f, 70f, 43f); storages.add(heatstorage_2); + + // Create and add a cuboid heat storage model CuboidHeatstorage heatstorage_3 = new CuboidHeatstorage("Speicher 3", 80f, 20f, 40f, 60f, 55.3f); storages.add(heatstorage_3); + return storages; } } \ No newline at end of file diff --git a/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/application/HeatCalculatorController.java b/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/controller/HeatCalculatorController.java similarity index 50% rename from HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/application/HeatCalculatorController.java rename to HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/controller/HeatCalculatorController.java index 6e4e5b6..0bfbcb2 100644 --- a/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/application/HeatCalculatorController.java +++ b/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/controller/HeatCalculatorController.java @@ -9,29 +9,60 @@ import javafx.event.ActionEvent; import java.util.ArrayList; import java.util.List; +/** + * Controller class for the Heat Calculator FX application. + * Manages the interaction between the models and the view. + */ public class HeatCalculatorController implements javafx.event.EventHandler<ActionEvent> { + // List of heat storage models protected List<GeneralHeatstorage> models; + + // Reference to the view interface protected ViewInterface view; + + // Index of the currently displayed heat storage protected int currentHeatstorage = 0; + /** + * Constructor for the HeatCalculatorController. + * + * @param models A list of GeneralHeatstorage objects representing the heat storage models. + */ public HeatCalculatorController(List<GeneralHeatstorage> models) { this.models = models; } + /** + * Handles action events, such as button clicks, to show the next heat storage. + * + * @param actionEvent The action event triggered by the user. + */ @Override public void handle(ActionEvent actionEvent) { showNextHeatstorage(); } + /** + * Sets the view for the controller. + * + * @param view The view interface to be associated with the controller. + */ public void setView(ViewInterface view) { this.view = view; } + /** + * Starts the view by invoking its startView method. + */ public void startView() { view.startView(); } + /** + * Cycles through the list of heat storages and updates the view to display the next one. + * Resets to the first heat storage if the end of the list is reached. + */ public void showNextHeatstorage() { if (currentHeatstorage < models.size() - 1) { currentHeatstorage++; @@ -41,10 +72,20 @@ public class HeatCalculatorController implements javafx.event.EventHandler<Actio view.startView(); } + /** + * Retrieves the properties of the currently displayed heat storage. + * + * @return A list of Property objects representing the current heat storage's properties. + */ public ArrayList<Property> getHeatstorage() { return models.get(currentHeatstorage).getProperties(); } + /** + * Retrieves a string representation of the currently displayed heat storage. + * + * @return A string describing the current heat storage. + */ public String getHeatcalculator() { return models.get(currentHeatstorage).toString(); } diff --git a/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/model/CuboidHeatstorage.java b/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/model/CuboidHeatstorage.java index edcbc61..53bcad9 100644 --- a/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/model/CuboidHeatstorage.java +++ b/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/model/CuboidHeatstorage.java @@ -77,6 +77,18 @@ public class CuboidHeatstorage extends GeneralHeatstorage{ this.depth = checkValue(0, depth); } + /** + * Berechnet Volumen des Wärmespeichers mittels Höhe, Breite und Tiefe + * + * @param height + * @param width + * @param depth + * @return Volumen des Wärmespeichers + */ + protected static float calculateVolumeWithHeightWidthAndDepth(float height, float width, float depth) { + return (float) (height * width * depth) / 1000; + } + @Override public String toString() { return "Name: " + name diff --git a/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/model/GeneralHeatstorage.java b/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/model/GeneralHeatstorage.java index 43ab11b..2619381 100644 --- a/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/model/GeneralHeatstorage.java +++ b/HeatCalculatorFX/src/main/java/ch/iet_gibb/heatcalculatorfx/model/GeneralHeatstorage.java @@ -143,18 +143,6 @@ public abstract class GeneralHeatstorage implements HeatstorageInterface { return (float) (Math.PI * Math.pow((diameter / 2), 2) * height) / 1000; } - /** - * Berechnet Volumen des Wärmespeichers mittels Höhe, Breite und Tiefe - * - * @param height - * @param width - * @param depth - * @return Volumen des Wärmespeichers - */ - protected static float calculateVolumeWithHeightWidthAndDepth(float height, float width, float depth) { - return (float) (height * width * depth) / 1000; - } - protected float checkValue(int minValue, float valueToCheck) { if (valueToCheck < minValue) { throw new IllegalArgumentException("Value must be greater than " + minValue); -- GitLab