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
9 changes: 9 additions & 0 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
// Multiple numbers
if (!latestOperation.isEmpty()) {
pressEqualsKey(); // Perform the pending operation
}
latestValue = Double.parseDouble(screen);
latestOperation = operation;
}
Expand Down Expand Up @@ -103,8 +107,12 @@ public void pressDotKey() {
* aktualisiert und die Inhalt fortan als negativ interpretiert.
* Zeigt der Bildschirm bereits einen negativen Wert mit führendem Minus an, dann wird dieses
* entfernt und der Inhalt fortan als positiv interpretiert.
* Zeigt der Bildschirm eine Null an, so wird keine Anpassung vorgenommen, da die Negation von Null immer Null ist.
*/
public void pressNegativeKey() {
if (screen.equals("0")) {
return;
}
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
}

Expand All @@ -130,4 +138,5 @@ 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);
}

}
46 changes: 45 additions & 1 deletion app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,51 @@ void testMultipleDecimalDots() {
assertEquals(expected, actual);
}

@Test
@DisplayName("should display the correct result after inverting a non-zero number")
void testInversion() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressUnaryOperationKey("1/x");

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display zero when attempting to negate zero")
void testNegateZero() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after adding four positive one-digit numbers")
void testMultiplePositiveAddition() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(3);
calc.pressEqualsKey();

String expected = "13"; // 8
String actual = calc.readScreen();

assertEquals(expected, actual);
}

//TODO hier weitere Tests erstellen
}