From b4c45dabce49f19d8552cf9002fb76105252c608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hallvard=20Tr=C3=A6tteberg?= Date: Thu, 12 Aug 2021 12:23:01 +0000 Subject: [PATCH] Some fixes, create initial test class --- javafx-template/src/main/java/app/App.java | 2 - .../src/main/java/app/AppController.java | 44 +++++++++++++++---- .../src/main/resources/app/App.fxml | 6 ++- .../src/test/java/app/AppTest.java | 24 ++++++++++ 4 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 javafx-template/src/test/java/app/AppTest.java diff --git a/javafx-template/src/main/java/app/App.java b/javafx-template/src/main/java/app/App.java index bbe9412..b27d095 100644 --- a/javafx-template/src/main/java/app/App.java +++ b/javafx-template/src/main/java/app/App.java @@ -13,8 +13,6 @@ import java.io.IOException; */ public class App extends Application { - private static Scene scene; - @Override public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("App.fxml")); diff --git a/javafx-template/src/main/java/app/AppController.java b/javafx-template/src/main/java/app/AppController.java index ab72ff9..d622f38 100644 --- a/javafx-template/src/main/java/app/AppController.java +++ b/javafx-template/src/main/java/app/AppController.java @@ -2,6 +2,8 @@ package app; import java.util.List; import java.util.function.BinaryOperator; +import java.util.function.Consumer; +import java.util.function.UnaryOperator; import javafx.event.ActionEvent; import javafx.fxml.FXML; @@ -91,27 +93,37 @@ public class AppController { void handlePoint() { var operandString = getOperandString(); if (operandString.contains(".")) { - setOperand(operandString.substring(operandString.indexOf(".") + 1)); + setOperand(operandString.substring(0, operandString.indexOf(".") + 1)); } else { appendToOperand("."); } } - private void performOperation(boolean swap, BinaryOperator op) { + @FXML + void handleClear() { + clearOperand(); + } + + private void withOperand(Runnable proc) { if (hasOperand()) { calc.pushOperand(getOperand()); clearOperand(); } - if (swap) { - calc.swap(); - } - calc.performOperation(op); + proc.run(); updateOperandsView(); } - @FXML - void handlePi() { - setOperand(Math.PI); + private void performOperation(UnaryOperator op) { + withOperand(() -> calc.performOperation(op)); + } + + private void performOperation(boolean swap, BinaryOperator op) { + withOperand(() -> { + if (swap) { + calc.swap(); + } + calc.performOperation(op); + }); } @FXML @@ -133,4 +145,18 @@ public class AppController { void handleOpDiv() { performOperation(true, (op1, op2) -> op1 / op2); } + + @FXML + void handleOpSquareRoot() { + performOperation(op1 -> Math.sqrt(op1)); + } + + @FXML + void handlePi() { + withOperand(() -> calc.pushOperand(Math.PI)); + } + + public static void main(String[] args) { + System.out.println("\u221A"); + } } diff --git a/javafx-template/src/main/resources/app/App.fxml b/javafx-template/src/main/resources/app/App.fxml index d9b4203..4669ea8 100644 --- a/javafx-template/src/main/resources/app/App.fxml +++ b/javafx-template/src/main/resources/app/App.fxml @@ -38,7 +38,7 @@ GridPane.rowIndex="5" GridPane.columnIndex="0"/>