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: 11 additions & 2 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,19 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
latestValue = Double.parseDouble(screen);
latestOperation = operation;
if(!latestOperation.isEmpty()) { //fix1
pressEqualsKey();
}
if (operation.equals("+") || operation.equals("-") || operation.equals("x") || operation.equals("/")) { //fix2
latestValue = Double.parseDouble(screen);
latestOperation = operation;
} else {
screen = "Error";
}
}



/**
* Empfängt den Wert einer gedrückten unären Operationstaste, also eine der drei Operationen
* Quadratwurzel, Prozent, Inversion, welche nur einen Operanden benötigen.
Expand Down
50 changes: 50 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,55 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen

@Test
@DisplayName("should display result after subtracting two positive multi-digit numbers")
void testPositiveSubtraction() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressDigitKey(0);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(2);
calc.pressDigitKey(0);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after adding three positive multi-digit numbers")
void testdoublePositiveAddition() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display Error when attempting an unsupported operation")
void testUnsupportedOperation() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressBinaryOperationKey("^");

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

assertEquals(expected, actual);
}
}