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
13 changes: 12 additions & 1 deletion src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void pressUnaryOperationKey(String operation) {
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
if (screen.endsWith(".0")) screen = screen.substring(0, screen.length()-2);
if(screen.equals("NaN")) screen = "Error";
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

Expand All @@ -105,6 +106,16 @@ public void pressDotKey() {
* entfernt und der Inhalt fortan als positiv interpretiert.
*/
public void pressNegativeKey() {
if ("Error".equals(screen)) return;
try {
double v = Double.parseDouble(screen);
if (v == 0.0) {
screen = "0";
return;
}
} catch (NumberFormatException e) {
return;
}
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
}

Expand All @@ -130,4 +141,4 @@ public void pressEqualsKey() {
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);
}
}
}
47 changes: 45 additions & 2 deletions src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,50 @@ void testMultipleDecimalDots() {
assertEquals(expected, actual);
}

//grüner Test
@Test
@DisplayName("should convert integer to percent value")
void testConvertToPercentValue() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressDigitKey(0);
calc.pressUnaryOperationKey("%");

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

assertEquals(expected, actual);
}

//roter Test-01
@Test
@DisplayName("should not display negative zero")
void testNegativeZero() {
Calculator calc = new Calculator();

calc.pressDigitKey(0);
calc.pressNegativeKey();

//TODO hier weitere Tests erstellen
}
String expected = "0";
String actual = calc.readScreen();

assertEquals(expected, actual);
}

//roter Test-02
@Test
@DisplayName("should trim .0 for unary results like sqrt")
void testTrimDotZeroForUnaryResults() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

}