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
14 changes: 11 additions & 3 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ public void pressDigitKey(int digit) {
* im Ursprungszustand ist.
*/
public void pressClearKey() {
screen = "0";
latestOperation = "";
latestValue = 0.0;

if (!screen.equals ("0") && latestValue != 0){
screen = "0";
} else {
screen = "0";
latestOperation = "";
latestValue = 0.0;
}
}

/**
Expand All @@ -60,6 +65,9 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
if (!latestOperation.equals("")){
pressEqualsKey();
}
latestValue = Double.parseDouble(screen);
latestOperation = operation;
}
Expand Down
44 changes: 44 additions & 0 deletions app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,49 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen
@Test
@DisplayName("should repeat last operation if equals key is pressed again")
void testClearkey() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(1);
calc.pressEqualsKey();
assertEquals("2", calc.readScreen());
calc.pressEqualsKey();
assertEquals("3", calc.readScreen());
}

@Test
@DisplayName("should clear the screen after pressing the clear key")
void testClearKey() {
Calculator calc = new Calculator();
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(6);
calc.pressClearKey();
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(7);
calc.pressEqualsKey();
String expected = "12";
String actual = calc.readScreen();

assertEquals(expected, actual);
}

@Test
@DisplayName("Should display the intermediate result when an operation is performed multiple times.")
void testIntermediateResult() {
Calculator calc = new Calculator();
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
String expected = "10";
String actual = calc.readScreen();

assertEquals(expected, actual);
}
}