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..ab4452c1 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,10 @@ public void pressClearKey() { * @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division */ public void pressBinaryOperationKey(String operation) { + // Multiple numbers + if (!latestOperation.isEmpty()) { + pressEqualsKey(); // Perform the pending operation + } latestValue = Double.parseDouble(screen); latestOperation = operation; } @@ -103,8 +107,12 @@ public void pressDotKey() { * aktualisiert und die Inhalt fortan als negativ interpretiert. * Zeigt der Bildschirm bereits einen negativen Wert mit führendem Minus an, dann wird dieses * entfernt und der Inhalt fortan als positiv interpretiert. + * Zeigt der Bildschirm eine Null an, so wird keine Anpassung vorgenommen, da die Negation von Null immer Null ist. */ public void pressNegativeKey() { + if (screen.equals("0")) { + return; + } screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; } @@ -130,4 +138,5 @@ public void pressEqualsKey() { if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2); if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10); } + } 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..51e3c9ed 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,51 @@ void testMultipleDecimalDots() { assertEquals(expected, actual); } + @Test + @DisplayName("should display the correct result after inverting a non-zero number") + void testInversion() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressUnaryOperationKey("1/x"); + + String expected = "0.5"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display zero when attempting to negate zero") + void testNegateZero() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(0); + calc.pressNegativeKey(); + + String expected = "0"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display result after adding four positive one-digit numbers") + void testMultiplePositiveAddition() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(3); + calc.pressEqualsKey(); + + String expected = "13"; // 8 + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } - //TODO hier weitere Tests erstellen }