From 0aaa9978f479d375dac9e3877c60d1184b46028c Mon Sep 17 00:00:00 2001 From: 0593504 Date: Sun, 28 Apr 2024 09:48:07 +0200 Subject: [PATCH 1/5] Neur gruener Test --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 160 +++++++++++++++++- 1 file changed, 157 insertions(+), 3 deletions(-) diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index ddff0daf..ac0b5a4f 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -6,7 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; @DisplayName("Retro calculator") -class CalculatorTest { +public CalculatorTest { @Test @DisplayName("should display result after adding two positive multi-digit numbers") @@ -20,7 +20,7 @@ void testPositiveAddition() { calc.pressDigitKey(0); calc.pressEqualsKey(); - String expected = "40"; + String expected = "4000"; String actual = calc.readScreen(); assertEquals(expected, actual); @@ -87,8 +87,162 @@ void testMultipleDecimalDots() { assertEquals(expected, actual); } + @Test + @DisplayName("should not allow multiple decimal dots") + void testMultipleDecimalDots() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(1); + calc.pressDotKey(); + calc.pressDigitKey(7); + calc.pressDotKey(); + calc.pressDigitKey(8); + + String expected = "1.78"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display result after subtracting two positive multi-digit numbers") + void testNegativeSubstraction(){ + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressBineryOperationKey("-"); + calc.pressDigitKey(1); + calc.pressDigitKey(0); + + String expected = "10" + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + +package htw.berlin.prog2.ha1; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DisplayName("Retro calculator") +public CalculatorTest { + + @Test + @DisplayName("should display result after adding two positive multi-digit numbers") + void testPositiveAddition() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "4000"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display result after getting the square root of two") + void testSquareRoot() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressUnaryOperationKey("√"); + + String expected = "1.41421356"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display error when dividing by zero") + void testDivisionByZero() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(7); + calc.pressBinaryOperationKey("/"); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "Error"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display error when drawing the square root of a negative number") + void testSquareRootOfNegative() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(7); + calc.pressNegativeKey(); + calc.pressUnaryOperationKey("√"); + + String expected = "Error"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should not allow multiple decimal dots") + void testMultipleDecimalDots() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(1); + calc.pressDotKey(); + calc.pressDigitKey(7); + calc.pressDotKey(); + calc.pressDigitKey(8); + + String expected = "1.78"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + @Test + @DisplayName("should not allow multiple decimal dots") + void testMultipleDecimalDots() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(1); + calc.pressDotKey(); + calc.pressDigitKey(7); + calc.pressDotKey(); + calc.pressDigitKey(8); + + String expected = "1.78"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display result after subtracting two positive multi-digit numbers") + void testNegativeSubstraction(){ + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressBinaryOperationKey("-"); + calc.pressDigitKey(1); + calc.pressDigitKey(0); + + String expected = "10"; + String actual = calc.readScreen(); + assertEquals(expected, actual); + } - //TODO hier weitere Tests erstellen } From eede671ebcbd5b04a398eb0b76b15b7aa93058f7 Mon Sep 17 00:00:00 2001 From: 0593504 Date: Sun, 28 Apr 2024 16:08:58 +0200 Subject: [PATCH 2/5] Neuer roter Test --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index ac0b5a4f..e2de72dc 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -243,6 +243,24 @@ void testNegativeSubstraction(){ assertEquals(expected, actual); } + @Test + @DisplayName("should finish the first Operation before going on with the second one") + void testTwoOperations() { + Calculator calc = new Calculator(); -} + calc.pressDigitKey(1); + calc.pressBinaryOperationKey('+'); // Use quotes around operations + calc.pressDigitKey(2); + calc.pressBinaryOperationKey('*'); // Use quotes around operations + calc.pressDigitKey(2); + calc.pressEqualsKey(); // You need to press equals to get the result of the operation + + String expected = "6"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + + } From f08c28982d2d2a0d942988bdbfc16d31130454ca Mon Sep 17 00:00:00 2001 From: 0593504 Date: Sun, 28 Apr 2024 20:45:07 +0200 Subject: [PATCH 3/5] Bugfix roter Test --- app/src/main/java/htw/berlin/prog2/ha1/Calculator.java | 3 +++ .../test/java/htw/berlin/prog2/ha1/CalculatorTest.java | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java index 84c04f21..92444506 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -60,6 +60,9 @@ public void pressClearKey() { * @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division */ public void pressBinaryOperationKey(String operation) { + if (!latestOperation.isEmpty()) { + pressEqualsKey();// This completes the previous operation using the current screen value + } latestValue = Double.parseDouble(screen); latestOperation = operation; } diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index e2de72dc..7441c5b4 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -143,7 +143,7 @@ void testPositiveAddition() { calc.pressDigitKey(0); calc.pressEqualsKey(); - String expected = "4000"; + String expected = "40"; String actual = calc.readScreen(); assertEquals(expected, actual); @@ -237,6 +237,7 @@ void testNegativeSubstraction(){ calc.pressBinaryOperationKey("-"); calc.pressDigitKey(1); calc.pressDigitKey(0); + calc.pressEqualsKey(); String expected = "10"; String actual = calc.readScreen(); @@ -249,11 +250,11 @@ void testTwoOperations() { Calculator calc = new Calculator(); calc.pressDigitKey(1); - calc.pressBinaryOperationKey('+'); // Use quotes around operations + calc.pressBinaryOperationKey('+'); calc.pressDigitKey(2); - calc.pressBinaryOperationKey('*'); // Use quotes around operations + calc.pressBinaryOperationKey('*'); calc.pressDigitKey(2); - calc.pressEqualsKey(); // You need to press equals to get the result of the operation + calc.pressEqualsKey(); String expected = "6"; String actual = calc.readScreen(); @@ -262,5 +263,6 @@ void testTwoOperations() { } + } From 0f9e1e9c6092362fe5bd3f74ac498d71f41e5456 Mon Sep 17 00:00:00 2001 From: 0593504 Date: Sun, 28 Apr 2024 20:47:59 +0200 Subject: [PATCH 4/5] 2. neuer roter Test --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index 7441c5b4..6cc99f43 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -261,6 +261,23 @@ void testTwoOperations() { assertEquals(expected, actual); } + @Test + @DisplayName("should repeat the last operation again on the new result by pressing equals key") + void testPressEqualsKeyDouble() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(5); + calc.pressBinaryOperationKey('*'); + calc.pressDigitKey(3); + calc.pressEqualsKey(); // First equals to perform the operation 5 * 3 + calc.pressEqualsKey(); // Second equals to repeat the last operation on the result, 15 * 3 + + String expected = "45"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + From 8bf0398476d26faeb35533097a733cc341f82511 Mon Sep 17 00:00:00 2001 From: 0593504 Date: Sun, 28 Apr 2024 21:05:03 +0200 Subject: [PATCH 5/5] Bugfix 2. roter Test --- .../java/htw/berlin/prog2/ha1/Calculator.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java index 92444506..11c4a4f1 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -113,21 +113,31 @@ public void pressNegativeKey() { /** * Empfängt den Befehl der gedrückten "="-Taste. - * Wurde zuvor keine Operationstaste gedrückt, passiert nichts. - * Wurde zuvor eine binäre Operationstaste gedrückt und zwei Operanden eingegeben, wird das - * Ergebnis der Operation angezeigt. Falls hierbei eine Division durch Null auftritt, wird "Error" angezeigt. - * Wird die Taste weitere Male gedrückt (ohne andere Tasten dazwischen), so wird die letzte - * Operation (ggf. inklusive letztem Operand) erneut auf den aktuellen Bildschirminhalt angewandt - * und das Ergebnis direkt angezeigt. + * - Wurde zuvor keine Operationstaste gedrückt, passiert nichts. + * - Wurde zuvor eine binäre Operationstaste gedrückt und zwei Operanden eingegeben, wird das + * Ergebnis der Operation angezeigt. Nach der Berechnung wird das Ergebnis in latestValue gespeichert, + * um die gleiche Operation bei erneutem Drücken der "="-Taste auf das Ergebnis anzuwenden. + * - Falls bei einer Operation, wie der Division, ein Fehler auftritt (z.B. Division durch Null), + * wird "Error" auf dem Bildschirm angezeigt. + * - Wird die Taste weitere Male gedrückt (ohne andere Tasten dazwischen), so wird die letzte + * Operation (ggf. inklusive letztem Operand) erneut auf den aktuellen Bildschirminhalt angewandt + * und das Ergebnis direkt angezeigt. */ public void pressEqualsKey() { + if (latestOperation.isEmpty()) { + return; // If no operation was set, do nothing. + } + var inputValue = Double.parseDouble(screen); var result = switch(latestOperation) { - case "+" -> latestValue + Double.parseDouble(screen); - case "-" -> latestValue - Double.parseDouble(screen); - case "x" -> latestValue * Double.parseDouble(screen); - case "/" -> latestValue / Double.parseDouble(screen); + case "+" -> latestValue + inputValue; + case "-" -> latestValue - inputValue; + case "x" -> latestValue * inputValue; + case "/" -> latestValue / inputValue; default -> throw new IllegalArgumentException(); }; + + // Update latestValue with the result for potential repeated operations + latestValue = result; screen = Double.toString(result); if(screen.equals("Infinity")) screen = "Error"; if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);