From dce338703243c78d7815f0ced96233ce8bc58551 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Wed, 30 Mar 2022 11:17:18 +0200 Subject: [PATCH] =?UTF-8?q?Aufgabe=204=20ausgef=C3=BChrt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../prog2/fxmlcalculator/IsObservable.java | 18 ++++ .../zhaw/prog2/fxmlcalculator/IsObserver.java | 13 +++ .../ch/zhaw/prog2/fxmlcalculator/Main.java | 29 ++++-- .../fxmlcalculator/MainWindowController.java | 90 ++++++++++++++++--- .../ResultWindowController.java | 18 +++- .../fxmlcalculator/ValueHandlerDecorator.java | 60 +++++++++++++ .../zhaw/prog2/fxmlcalculator/MainWindow.fxml | 17 +++- .../prog2/fxmlcalculator/ResultWindow.fxml | 10 +-- 8 files changed, 226 insertions(+), 29 deletions(-) create mode 100644 code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/IsObservable.java create mode 100644 code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/IsObserver.java create mode 100644 code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/ValueHandlerDecorator.java diff --git a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/IsObservable.java b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/IsObservable.java new file mode 100644 index 0000000..0e9243d --- /dev/null +++ b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/IsObservable.java @@ -0,0 +1,18 @@ +package ch.zhaw.prog2.fxmlcalculator; +/** + * Most basic interface for observing an object + * @author bles + * + */ +public interface IsObservable { + /** + * Add an observer that listens for updates + * @param observer + */ + void addListener(IsObserver observer); + /** + * Remove an observer from the list + * @param observer + */ + void removeListener(IsObserver observer); +} diff --git a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/IsObserver.java b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/IsObserver.java new file mode 100644 index 0000000..2f6ded0 --- /dev/null +++ b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/IsObserver.java @@ -0,0 +1,13 @@ +package ch.zhaw.prog2.fxmlcalculator; +/** + * Most basic interface for beeing an observer + * @author bles + * + */ +public interface IsObserver { + /** + * This method is always called when an observed object + * changes + */ + void update(); +} diff --git a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/Main.java b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/Main.java index f227554..7fdb7ea 100644 --- a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/Main.java +++ b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/Main.java @@ -5,6 +5,9 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; + +import java.io.IOException; + /** * Main-Application. Opens the first window (MainWindow) and the common ValueHandler * @author @@ -14,10 +17,21 @@ public class Main extends Application { private ValueHandler valueHandler; + private static final String INFO = """ + Enter valid values to + - Initial amount (> 0) + - Return in % (can be +/- or 0) + - Annual Costs (> 0) + - Number of years (> 0) + Calculate displays the annual balance development!"; + """; + public static void main(String[] args) { launch(args); } + + @Override public void start(Stage primaryStage) { valueHandler = new ValueHandler(); @@ -27,16 +41,17 @@ public class Main extends Application { private void mainWindow(Stage primaryStage) { //load main window try { - FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindow.fxml")); - - Pane rootNode = loader.load(); - - MainWindowController mainWindowController = loader.getController(); - Scene scene = new Scene(rootNode); + FXMLLoader mainWindowLoader = new FXMLLoader(getClass().getResource("MainWindow.fxml")); + Pane rootNode = mainWindowLoader.load(); - primaryStage.setScene(scene); + MainWindowController mainWindowController = mainWindowLoader.getController(); + mainWindowController.setValueHandler(new ValueHandler()); + mainWindowController.setHelpText(INFO); + Scene mainWindowScene = new Scene(rootNode); + + primaryStage.setScene(mainWindowScene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); diff --git a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/MainWindowController.java b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/MainWindowController.java index 1ad19ac..fd1a0d7 100644 --- a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/MainWindowController.java +++ b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/MainWindowController.java @@ -1,24 +1,27 @@ package ch.zhaw.prog2.fxmlcalculator; - +import javafx.application.Platform; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.CheckMenuItem; +import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.*; import javafx.scene.paint.Color; import javafx.stage.Stage; -import javafx.event.ActionEvent; -import javafx.fxml.FXML; +import javafx.scene.Scene; + +import javax.xml.transform.Result; +import java.io.IOException; -/** - * Controller for the MainWindow. One controller per mask (or FXML file) - * Contains everything the controller has to reach in the view (controls) - * and all methods the view calls based on events. - * @author - * @version 1.0 - */ public class MainWindowController { + ValueHandlerDecorator valueHandlerDecorator; + ValueHandler valueHandler; + String helpText; + @FXML private TextField annualCost; @@ -48,12 +51,79 @@ public class MainWindowController { @FXML void calculate(ActionEvent event) { + valueHandlerDecorator.checkValuesAndCalculateResult(initialAmount.getText(), returnInPercent.getText(), annualCost.getText(), numberOfYears.getText()); + } + @FXML + void clearResults(ActionEvent event) { + valueHandlerDecorator.clearResult(); + } + + + @FXML + void clearValues(ActionEvent event) { + if(clearAnnualCosts.isSelected()){ + annualCost.clear(); + } + if(clearReturnInPercent.isSelected()){ + returnInPercent.clear(); + } + if(clearNumberOfYears.isSelected()){ + numberOfYears.clear(); + } + if(clearInitialAmount.isSelected()){ + initialAmount.clear(); + } } @FXML void close(ActionEvent event) { + Platform.exit(); + } + @FXML + void openResultWindow(ActionEvent event) { + + FXMLLoader resultWindowLoader = new FXMLLoader(getClass().getResource("ResultWindow.fxml")); + Pane resultNode = new StackPane(); + try { + resultNode = resultWindowLoader.load(); + } catch (IOException e) { + e.printStackTrace(); + } + ResultWindowController resultWindowController = resultWindowLoader.getController(); + resultWindowController.setValueHandler(valueHandler, valueHandlerDecorator); + Scene resultScene = new Scene(resultNode); + + // New window (Stage) + Stage resultStage = new Stage(); + resultStage.setTitle("Results"); + resultStage.setScene(resultScene); + + resultStage.show(); + + valueHandlerDecorator.informListener(); + } + + + @FXML + void showHelp(ActionEvent event) { + valueHandlerDecorator.showHelp(helpText); + } + + public void setValueHandler(ValueHandler valueHandler){ + this.valueHandler = valueHandler; + valueHandlerDecorator = new ValueHandlerDecorator(valueHandler); + valueHandlerDecorator.addListener(new IsObserver() { + @Override + public void update() { + results.setText(valueHandler.getResultBound()); + } + }); + } + + public void setHelpText(String helpText){ + this.helpText = helpText; } } diff --git a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/ResultWindowController.java b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/ResultWindowController.java index 2077a37..bce37fd 100644 --- a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/ResultWindowController.java +++ b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/ResultWindowController.java @@ -1,5 +1,6 @@ package ch.zhaw.prog2.fxmlcalculator; +import javafx.fxml.FXML; import javafx.scene.control.TextArea; import javafx.stage.Stage; @@ -11,15 +12,26 @@ import javafx.stage.Stage; * @version 1.0 */ public class ResultWindowController { - // add datafields + ValueHandler valueHandler; + ValueHandlerDecorator valueHandlerDecorator; - //@FXML + @FXML private TextArea results; - //@FXML + @FXML private void closeWindow() { Stage stage = (Stage) results.getScene().getWindow(); stage.close(); } + public void setValueHandler(ValueHandler valueHandler, ValueHandlerDecorator valueHandlerDecorator){ + this.valueHandler = valueHandler; + valueHandlerDecorator.addListener(new IsObserver() { + @Override + public void update() { + results.setText(valueHandler.getResultBound()); + } + }); + } + } diff --git a/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/ValueHandlerDecorator.java b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/ValueHandlerDecorator.java new file mode 100644 index 0000000..fbdb152 --- /dev/null +++ b/code/FXML-Calculator/src/main/java/ch/zhaw/prog2/fxmlcalculator/ValueHandlerDecorator.java @@ -0,0 +1,60 @@ +package ch.zhaw.prog2.fxmlcalculator; + +import ch.zhaw.prog2.fxmlcalculator.IsObservable; +import ch.zhaw.prog2.fxmlcalculator.IsObserver; +import ch.zhaw.prog2.fxmlcalculator.ValueHandler; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.Pane; +import javafx.scene.layout.StackPane; +import javafx.stage.Stage; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +/** + * Adds observable functionality to one Value Handler. + * The decorator uses the original methods of the WordModel-object. + * @author bles + * + */ +public class ValueHandlerDecorator implements IsObservable { + private final ValueHandler valueHandler; + private List listener = new ArrayList<>(); + + public ValueHandlerDecorator(ValueHandler valueHandler) { + this.valueHandler = valueHandler; + } + + @Override + public void addListener(IsObserver observer) { + listener.add(observer); + } + + @Override + public void removeListener(IsObserver observer) { + listener.remove(observer); + } + + public void checkValuesAndCalculateResult(String initialAmount, String returnInPercent, String annualCost, String numberOfYears) { + valueHandler.checkValuesAndCalculateResult(initialAmount, returnInPercent, annualCost, numberOfYears); + informListener(); + } + + public void showHelp(String text) { + valueHandler.setResultBound(text); + informListener(); + } + + public void clearResult(){ + valueHandler.clearResult(); + informListener(); + } + + public void informListener() { + for(IsObserver observer : listener) { + observer.update(); + } + } + +} diff --git a/code/FXML-Calculator/src/main/resources/ch/zhaw/prog2/fxmlcalculator/MainWindow.fxml b/code/FXML-Calculator/src/main/resources/ch/zhaw/prog2/fxmlcalculator/MainWindow.fxml index 2b2c4fa..862e156 100644 --- a/code/FXML-Calculator/src/main/resources/ch/zhaw/prog2/fxmlcalculator/MainWindow.fxml +++ b/code/FXML-Calculator/src/main/resources/ch/zhaw/prog2/fxmlcalculator/MainWindow.fxml @@ -10,6 +10,7 @@ + @@ -17,7 +18,7 @@ - + @@ -28,14 +29,17 @@ - + - + - + + + + @@ -97,6 +101,11 @@ + diff --git a/code/FXML-Calculator/src/main/resources/ch/zhaw/prog2/fxmlcalculator/ResultWindow.fxml b/code/FXML-Calculator/src/main/resources/ch/zhaw/prog2/fxmlcalculator/ResultWindow.fxml index 88b59f2..d67f9ce 100644 --- a/code/FXML-Calculator/src/main/resources/ch/zhaw/prog2/fxmlcalculator/ResultWindow.fxml +++ b/code/FXML-Calculator/src/main/resources/ch/zhaw/prog2/fxmlcalculator/ResultWindow.fxml @@ -4,12 +4,12 @@ - + + + - +
@@ -35,7 +35,7 @@ -