diff --git a/src/main/java/tdd/setup/Calculator.java b/src/main/java/tdd/setup/Calculator.java index 171f6d6..903fb87 100644 --- a/src/main/java/tdd/setup/Calculator.java +++ b/src/main/java/tdd/setup/Calculator.java @@ -13,13 +13,17 @@ public String readScreen() { return screen; } public void pressDigitKey(int digit) { - if(digit > 9 || digit < 0) throw new IllegalArgumentException(); - if(latestOperation.isEmpty()) { - screen = screen + digit; + if (digit > 9 || digit < 0) throw new IllegalArgumentException(); + if (screen.equals("0")) { + screen = String.valueOf(digit); } else { - latestValue = Double.parseDouble(screen); - screen = Integer.toString(digit); + if (latestOperation.isEmpty()) { + screen = screen + digit; + } else { + latestValue = Double.parseDouble(screen); + screen = Integer.toString(digit); + } } } @@ -29,12 +33,29 @@ public void pressClearKey() { latestValue = 0.0; } - public void pressOperationKey(String operation) { + public void pressOperationKey(String operation) { + if(!latestOperation.isEmpty()){ + latestValue = switch (latestOperation) { + case "+" -> latestValue + Double.parseDouble(screen); + case "-" -> latestValue - Double.parseDouble(screen); + case "x" -> latestValue * Double.parseDouble(screen); + case "/" -> latestValue / Double.parseDouble(screen); + default -> throw new IllegalArgumentException(); + }; + screen = Double.toString(latestValue); + if (screen.endsWith(".0")) screen = screen.substring(0, screen.length() - 2); + }else{ + latestValue = Double.parseDouble(screen); + } + latestOperation = operation; + } public void pressDotKey() { - if(!screen.endsWith(".")) screen = screen + "."; + + if(!screen.contains(".")) screen = screen +"."; + } public void pressNegative() { diff --git a/src/test/java/tdd/setup/CalculatorTest.java b/src/test/java/tdd/setup/CalculatorTest.java index 3bf16e9..5f36b2c 100644 --- a/src/test/java/tdd/setup/CalculatorTest.java +++ b/src/test/java/tdd/setup/CalculatorTest.java @@ -16,4 +16,43 @@ void calculatorCanDoTwoPlusTwo() { calc.pressEquals(); assertEquals("4", calc.readScreen()); } -} + + @Test + @DisplayName("should display result after substracting two positive numbers") + void calculatorCanDoFiveMinusTwo() { + Calculator calc = new Calculator(); + calc.pressDigitKey(5); + calc.pressOperationKey("-"); + calc.pressDigitKey(2); + calc.pressEquals(); + assertEquals("3", calc.readScreen()); + } + + @Test + @DisplayName("should display the point number correctly") + void calculatorCanDoNumbersWithDots() { + Calculator calc = new Calculator(); + calc.pressDigitKey(5); + calc.pressDotKey(); + calc.pressDigitKey(2); + calc.pressDotKey(); + assertEquals("5.2", calc.readScreen()); + } + + @Test + @DisplayName("should calculate with multiple numbers") + void calculatorCanDoMultipleCalculations(){ + Calculator calc = new Calculator(); + calc.pressDigitKey(2); + calc.pressDotKey(); + calc.pressDigitKey(4); + calc.pressOperationKey("+"); + calc.pressDigitKey(5); + calc.pressOperationKey("-"); + calc.pressDigitKey(3); + calc.pressEquals(); + assertEquals("4.4", calc.readScreen()); + + + } +} \ No newline at end of file