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 84c04f2..dd88834 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -45,9 +45,14 @@ public void pressDigitKey(int digit) { * im Ursprungszustand ist. */ public void pressClearKey() { - screen = "0"; - latestOperation = ""; - latestValue = 0.0; + + if (!screen.equals ("0") && latestValue != 0){ + screen = "0"; + } else { + screen = "0"; + latestOperation = ""; + latestValue = 0.0; + } } /** @@ -60,6 +65,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.equals("")){ + pressEqualsKey(); + } 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 ddff0da..4b8bf54 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -90,5 +90,49 @@ void testMultipleDecimalDots() { //TODO hier weitere Tests erstellen + @Test + @DisplayName("should repeat last operation if equals key is pressed again") + void testClearkey() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(1); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(1); + calc.pressEqualsKey(); + assertEquals("2", calc.readScreen()); + calc.pressEqualsKey(); + assertEquals("3", calc.readScreen()); + } + + @Test + @DisplayName("should clear the screen after pressing the clear key") + void testClearKey() { + Calculator calc = new Calculator(); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(6); + calc.pressClearKey(); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(7); + calc.pressEqualsKey(); + String expected = "12"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("Should display the intermediate result when an operation is performed multiple times.") + void testIntermediateResult() { + Calculator calc = new Calculator(); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + String expected = "10"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } }