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 aaef8862..2a1526f5 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -14,13 +14,21 @@ public class Calculator { private String latestOperation = ""; + /** * @return den aktuellen Bildschirminhalt als String */ public String readScreen() { + + if(screen.equals("NaN")){ + screen = "Error"; + } + return screen; } + + /** * Empfängt den Wert einer gedrückten Zifferntaste. Da man nur eine Taste auf einmal * drücken kann muss der Wert positiv und einstellig sein und zwischen 0 und 9 liegen. @@ -34,6 +42,7 @@ public void pressDigitKey(int digit) { if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = ""; screen = screen + digit; + } /** @@ -60,6 +69,11 @@ 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; } @@ -72,17 +86,19 @@ public void pressBinaryOperationKey(String operation) { * @param operation "√" für Quadratwurzel, "%" für Prozent, "1/x" für Inversion */ public void pressUnaryOperationKey(String operation) { + latestValue = Double.parseDouble(screen); latestOperation = operation; + var result = switch(operation) { case "√" -> Math.sqrt(Double.parseDouble(screen)); case "%" -> Double.parseDouble(screen) / 100; case "1/x" -> 1 / Double.parseDouble(screen); default -> throw new IllegalArgumentException(); }; + screen = Double.toString(result); if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10); - } /** @@ -104,7 +120,14 @@ public void pressDotKey() { * entfernt und der Inhalt fortan als positiv interpretiert. */ public void pressNegativeKey() { - screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; + //screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; + + if(screen.startsWith("-")){ + screen = screen.substring(1); + + } else{ + screen = "-" + screen; + } } /** @@ -117,6 +140,7 @@ public void pressNegativeKey() { * und das Ergebnis direkt angezeigt. */ public void pressEqualsKey() { + var result = switch(latestOperation) { case "+" -> latestValue + Double.parseDouble(screen); case "-" -> latestValue - Double.parseDouble(screen); @@ -128,4 +152,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 addc5f26..273dc471 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; @DisplayName("Retro calculator") class CalculatorTest { @@ -41,5 +42,54 @@ void testSquareRoot() { } //TODO hier weitere Tests erstellen + + @Test + @DisplayName("should display result after adding a dot") + void testDot(){ + Calculator calc = new Calculator(); + + calc.pressDigitKey(5); + calc.pressDotKey(); + calc.pressDigitKey(1); + + String expected = "5.1"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + + @Test + @DisplayName("should display result after adding three positive single-digit numbers") + void testAdditionThree(){ + Calculator calc = new Calculator(); + + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(1); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(4); + calc.pressEqualsKey(); + + String expected = "10"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display text after getting the square root of minus two") + void testNegaitveSquareRoot(){ + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressNegativeKey(); + calc.pressUnaryOperationKey("√"); + + String expected = "Error"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } }