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..00bf6146 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -60,10 +60,19 @@ public void pressClearKey() { * @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division */ public void pressBinaryOperationKey(String operation) { - latestValue = Double.parseDouble(screen); - latestOperation = operation; + if(!latestOperation.isEmpty()) { //fix1 + pressEqualsKey(); + } + if (operation.equals("+") || operation.equals("-") || operation.equals("x") || operation.equals("/")) { //fix2 + latestValue = Double.parseDouble(screen); + latestOperation = operation; + } else { + screen = "Error"; + } } + + /** * Empfängt den Wert einer gedrückten unären Operationstaste, also eine der drei Operationen * Quadratwurzel, Prozent, Inversion, welche nur einen Operanden benötigen. 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..bb06f053 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -90,5 +90,55 @@ 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(2); + calc.pressDigitKey(0); + calc.pressBinaryOperationKey("-"); + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "0"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display result after adding three positive multi-digit numbers") + void testdoublePositiveAddition() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(3); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(5); + calc.pressEqualsKey(); + + String expected = "10"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("should display Error when attempting an unsupported operation") + void testUnsupportedOperation() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressBinaryOperationKey("^"); + + String expected = "Error"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } }