From b7141731386d5bdb2f498b62452346f0e23b08c0 Mon Sep 17 00:00:00 2001 From: Ni Le Date: Tue, 21 Oct 2025 12:55:43 +0200 Subject: [PATCH 1/5] first commit: green test for clear key --- .../java/htw/berlin/prog2/ha1/CalculatorTest.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index ddff0da..bbcc0fe 100644 --- a/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -87,8 +87,19 @@ void testMultipleDecimalDots() { assertEquals(expected, actual); } + //TODO hier weitere Tests erstellen + @Test + @DisplayName("Screen should show 0 after clear key pressed") + void testClearKey() { + Calculator calc = new Calculator(); - //TODO hier weitere Tests erstellen -} + calc.pressDigitKey(3); + calc.pressClearKey(); + String expected = "0"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } +} From 09dcf48ceb64a060fd06da86e143e2d5fdebf64c Mon Sep 17 00:00:00 2001 From: Ni Le Date: Tue, 21 Oct 2025 13:09:49 +0200 Subject: [PATCH 2/5] second commit: red test for clear key --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index bbcc0fe..6cd9e02 100644 --- a/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -102,4 +102,23 @@ void testClearKey() { assertEquals(expected, actual); } + + + @Test + @DisplayName("should not reset calculator after press C/CE once") + void testDeleteFunction() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(3); + calc.pressBinaryOperationKey("x"); + calc.pressClearKey(); + calc.pressDigitKey(8); + calc.pressEqualsKey(); + + String expected = "24"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } } + From cf75a291dd5633fa5b23b9e659d9ffc42732930f Mon Sep 17 00:00:00 2001 From: Ni Le Date: Tue, 21 Oct 2025 13:40:29 +0200 Subject: [PATCH 3/5] third commit: fix bug for firrst red test - clear key --- src/main/java/htw/berlin/prog2/ha1/Calculator.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/src/main/java/htw/berlin/prog2/ha1/Calculator.java index 84c04f2..a5f5635 100644 --- a/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -44,10 +44,18 @@ public void pressDigitKey(int digit) { * Werte sowie der aktuelle Operationsmodus zurückgesetzt, so dass der Rechner wieder * im Ursprungszustand ist. */ + private boolean ce = false; public void pressClearKey() { - screen = "0"; - latestOperation = ""; - latestValue = 0.0; + if (!ce) { + screen = "0"; + ce = true; + + } else { + screen = "0"; + latestOperation = ""; + latestValue = 0.0; + ce = false; + } } /** From f8f10ce8b0e690325b955fad36fdcd045bd75e42 Mon Sep 17 00:00:00 2001 From: Ni Le Date: Thu, 23 Oct 2025 11:57:19 +0200 Subject: [PATCH 4/5] fourth commit: second red test - equals key --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index 6cd9e02..d1931eb 100644 --- a/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -120,5 +120,22 @@ void testDeleteFunction() { assertEquals(expected, actual); } + + @Test + @DisplayName("Pressing equals repeatedly repeats the last operation and updates the display") + void testEqualsKey() { + Calculator calc = new Calculator(); + calc.pressDigitKey(3); + calc.pressBinaryOperationKey("x"); + calc.pressDigitKey(4); + calc.pressEqualsKey(); + calc.pressEqualsKey(); + + String expected = "48"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + } From 09c159a4460eeff760dd6fef96b585f68d2c3d6a Mon Sep 17 00:00:00 2001 From: Ni Le Date: Fri, 24 Oct 2025 09:57:33 +0200 Subject: [PATCH 5/5] fifth commit: fixed bug for pressing equals twice --- .../java/htw/berlin/prog2/ha1/Calculator.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/src/main/java/htw/berlin/prog2/ha1/Calculator.java index a5f5635..b9ecdcd 100644 --- a/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -125,17 +125,34 @@ public void pressNegativeKey() { * Operation (ggf. inklusive letztem Operand) erneut auf den aktuellen Bildschirminhalt angewandt * und das Ergebnis direkt angezeigt. */ + private double saveValue = 0; + private boolean firstEqual = true; + private double result = 0; + + public void pressEqualsKey() { - var result = switch(latestOperation) { - case "+" -> latestValue + Double.parseDouble(screen); - case "-" -> latestValue - Double.parseDouble(screen); - case "x" -> latestValue * Double.parseDouble(screen); - case "/" -> latestValue / Double.parseDouble(screen); + double temp; + if (firstEqual) { + saveValue = Double.parseDouble(screen); + temp = saveValue; + firstEqual = false; + + } else { + temp = saveValue; + latestValue = Double.parseDouble(screen); + } + result = switch (latestOperation) { + case "+" -> latestValue + temp; + case "-" -> latestValue - temp; + case "x" -> latestValue * temp; + case "/" -> latestValue / temp; default -> throw new IllegalArgumentException(); }; + screen = Double.toString(result); if(screen.equals("Infinity")) screen = "Error"; if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2); if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10); + latestValue = result; } }