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..0bcd0592 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -11,6 +11,7 @@ public class Calculator { private String screen = "0"; private double latestValue; + private double latestLatestValue; private String latestOperation = ""; @@ -31,7 +32,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") || latestValue == Double.parseDouble(screen) /*|| latestValue == Double.parseDouble(screen) + 1*/) screen = ""; screen = screen + digit; } @@ -59,8 +60,18 @@ public void pressClearKey() { * auf dem Bildschirm angezeigt. Falls hierbei eine Division durch Null auftritt, wird "Error" angezeigt. * @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division */ + public void pressBinaryOperationKey(String operation) { + if(!(latestValue ==0)) { + latestLatestValue = latestValue; + } + + /*if(!latestOperation.equals("")) { + latestValue += Double.parseDouble(screen ); + } + else {*/ latestValue = Double.parseDouble(screen); + // } latestOperation = operation; } @@ -118,7 +129,7 @@ public void pressNegativeKey() { */ public void pressEqualsKey() { var result = switch(latestOperation) { - case "+" -> latestValue + Double.parseDouble(screen); + case "+" -> latestValue + Double.parseDouble(screen) + latestLatestValue; case "-" -> latestValue - Double.parseDouble(screen); case "x" -> latestValue * Double.parseDouble(screen); case "/" -> latestValue / Double.parseDouble(screen); @@ -127,5 +138,6 @@ 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.contains("Infinity")) screen = "Error"; } -} +} \ 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 addc5f26..5fbba088 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -41,5 +41,50 @@ void testSquareRoot() { } //TODO hier weitere Tests erstellen -} + @Test + @DisplayName("should display 0.0 after pressing the clear key") + void testClearKey() { + Calculator calc = new Calculator(); + calc.pressDigitKey(3); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(7); + calc.pressDigitKey(1); + calc.pressClearKey(); + + String expected = "0"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + @Test + @DisplayName("should display Error after pressing the equals key") + void testDivideThroughZero() { + Calculator calc = new Calculator(); + calc.pressDigitKey(3); + calc.pressBinaryOperationKey("/"); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "Error"; + String actual = calc.readScreen(); + assertEquals(expected, actual); + } + @Test + @DisplayName("should display 6 after adding 1, 2 and 3") + void testTwoOperations() { + Calculator calc = new Calculator(); + calc.pressDigitKey(1); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(2); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(3); + calc.pressEqualsKey(); + + String expected = "6"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + +}