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..b110c48b 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(); + } latestValue = Double.parseDouble(screen); latestOperation = operation; } @@ -94,9 +97,12 @@ public void pressUnaryOperationKey(String operation) { * Beim zweimaligem Drücken, oder wenn bereits ein Trennzeichen angezeigt wird, passiert nichts. */ public void pressDotKey() { - if(!screen.contains(".")) screen = screen + "."; + if (!screen.contains(".")) { + screen = screen + "."; + } } + /** * Empfängt den Befehl der gedrückten Vorzeichenumkehrstaste ("+/-"). * Zeigt der Bildschirm einen positiven Wert an, so wird ein "-" links angehängt, der Bildschirm @@ -118,6 +124,9 @@ public void pressNegativeKey() { * und das Ergebnis direkt angezeigt. */ public void pressEqualsKey() { + if(latestOperation.isEmpty()) { + return; + } var result = switch(latestOperation) { case "+" -> latestValue + Double.parseDouble(screen); case "-" -> latestValue - Double.parseDouble(screen); 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..c8bd70fa 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -88,7 +88,54 @@ void testMultipleDecimalDots() { assertEquals(expected, actual); } +//Teilaufgabe 1: + @Test + @DisplayName("should display result after subtracting two positive multi-digit numbers") + void testNegativeAddition() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(7); + calc.pressDigitKey(1); + calc.pressBinaryOperationKey("-"); + calc.pressDigitKey(5); + calc.pressDigitKey(1); + calc.pressEqualsKey(); + + String expected = "20"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + +//Teilaufgabe 2 - Test 1: + @Test + @DisplayName("tests if the calculator uses the latest result to continue calculating") + void testMultipleAddition() { + Calculator calc = new Calculator(); + calc.pressDigitKey(7); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(1); + calc.pressEqualsKey(); + + String expected = "13"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } +//Teilaufgabe 2 - Test 2: + @Test + @DisplayName("tests if 0 is displayed after pressing the equal key when not pressed a digit before") + void testEqualsKey() { + Calculator calc = new Calculator(); + calc.pressEqualsKey(); + String expected = "0"; + String actual = calc.readScreen(); - //TODO hier weitere Tests erstellen + assertEquals(expected, actual); } + + +} \ No newline at end of file