Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void pressUnaryOperationKey(String operation) {
screen = Double.toString(result);
if(screen.equals("NaN")) screen = "Error";
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);

}

Expand Down Expand Up @@ -118,6 +119,9 @@ public void pressNegativeKey() {
* und das Ergebnis direkt angezeigt.
*/
public void pressEqualsKey() {
if (latestOperation == null || latestOperation.isEmpty()) {
return;
}
var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "-" -> latestValue - Double.parseDouble(screen);
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,50 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen

@Test
@DisplayName("should display negative number after pressing +/- key for a positive number")
void testNegativeKey() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressNegativeKey();

String expected = "-1";
String actual = calc.readScreen();

assertEquals(expected, actual);
}

@Test
@DisplayName("should keep display unchanged when pressing = without prior operation")
void testEqualsKeyWithoutOperation() {
Calculator calc = new Calculator();

calc.pressDigitKey(4);
calc.pressEqualsKey();

String expected = "4";
String actual = calc.readScreen();

assertEquals(expected, actual);
}

@Test
@DisplayName("should display 1 after taking square root of one")
void testSquareRootOfOne() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressUnaryOperationKey("√");

String expected = "1";
String actual = calc.readScreen();

assertEquals(expected, actual);
}



}