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..ba35578 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -31,7 +31,7 @@ public String readScreen() { public void pressDigitKey(int digit) { if(digit > 9 || digit < 0) throw new IllegalArgumentException(); - if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = ""; + if(screen.equals("0")) screen = ""; screen = screen + digit; } @@ -60,8 +60,13 @@ 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; + screen = "0"; } /** @@ -118,6 +123,11 @@ 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); @@ -130,4 +140,4 @@ 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); } -} +} \ No newline at end of file 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..5fba014 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -89,6 +89,64 @@ void testMultipleDecimalDots() { } - //TODO hier weitere Tests erstellen -} + @Test + @DisplayName("should display result after subtracting two positive multi-digit numbers") + void testPositiveSubtraction(){ + + Calculator calc = new Calculator(); + + calc.pressDigitKey(3); + calc.pressDigitKey(0); + + calc.pressBinaryOperationKey("-"); + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "10"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + + } + + @Test + @DisplayName("should display result after subtracting two positive multi-digit numbers") + void testEqualsAfterNoNewOperation(){ + + Calculator calc = new Calculator(); + + calc.pressDigitKey(5); + + calc.pressEqualsKey(); + String expected = "5"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + + } + + @Test + @DisplayName("should display result after more then 2 Operations") + void testMoreThenTwoOperations(){ + + Calculator calc = new Calculator(); + + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("-"); + calc.pressDigitKey(5); + calc.pressDigitKey(0); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(5); + calc.pressDigitKey(0); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "455"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + + } +} \ No newline at end of file