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..13294289 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -48,6 +48,7 @@ public void pressClearKey() { screen = "0"; latestOperation = ""; latestValue = 0.0; + } /** @@ -72,15 +73,18 @@ 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); + case "%" -> (latestValue / 100) * Double.parseDouble(screen); + default -> throw new IllegalArgumentException(); }; screen = Double.toString(result); + if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2); if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10); } @@ -127,5 +131,12 @@ public void pressEqualsKey() { screen = Double.toString(result); if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2); if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10); + if(screen.equals("Infinity")) screen = "Error"; + if(screen.startsWith("-")) ; + } + + + + } 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..416657ab 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -26,6 +26,7 @@ void testPositiveAddition() { assertEquals(expected, actual); } + @Test @DisplayName("should display result after getting the square root of two") void testSquareRoot() { @@ -40,6 +41,132 @@ void testSquareRoot() { assertEquals(expected, actual); } - //TODO hier weitere Tests erstellen + + @Test + @DisplayName("should display result after dividing two numbers") + void testDivide() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressBinaryOperationKey("/"); + calc.pressDigitKey(5); + calc.pressEqualsKey(); + + String expected = "4"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + + //failed + @Test + @DisplayName("should display result after dividing a number with zero") + void testDivideWithZero() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressBinaryOperationKey("/"); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "Error"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + + @Test + @DisplayName("should display result after adding two comma numbers") + void testAddingTwoCommaNumbers() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressDotKey(); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(2); + calc.pressDotKey(); + calc.pressDigitKey(5); + calc.pressEqualsKey(); + + String expected = "5"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + + //failed + /*@Test + @DisplayName("should display result after multiplying two negative numbers") + void testAddingTwoNegativeNumbers() { + Calculator calc = new Calculator(); + + calc.pressNegativeKey(); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("x"); + calc.pressNegativeKey(); + calc.pressDigitKey(5); + calc.pressEqualsKey(); + + String expected = "25"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + }*/ + + @Test + @DisplayName("should clear display after pressing clear") + void clearDisplay() { + Calculator calc = new Calculator(); + + calc.pressClearKey(); + + String expected = "0"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + + /* @Test + @DisplayName("%") + void testPercentage() { + Calculator calc = new Calculator(); + + + calc.pressDigitKey(5); + calc.pressDigitKey(0); + calc.pressUnaryOperationKey("%"); + calc.pressDigitKey(7); + calc.pressDigitKey(0); + + String expected = "35"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + }*/ + + @Test + @DisplayName("should display square root ") + void testSqrt() { + Calculator calc = new Calculator(); + + + calc.pressDigitKey(8); + calc.pressUnaryOperationKey("√"); + + String expected = "2.82842712"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + } + +