Skip to content
Open

HA1 #116

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
11 changes: 10 additions & 1 deletion 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,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.isEmpty()) {
pressEqualsKey();
}
latestValue = Double.parseDouble(screen);
latestOperation = operation;
}
Expand Down Expand Up @@ -94,9 +97,12 @@ public void pressUnaryOperationKey(String operation) {
* Beim zweimaligem Drücken, oder wenn bereits ein Trennzeichen angezeigt wird, passiert nichts.
*/
public void pressDotKey() {
if(!screen.contains(".")) screen = screen + ".";
if (!screen.contains(".")) {
screen = screen + ".";
}
}


/**
* Empfängt den Befehl der gedrückten Vorzeichenumkehrstaste ("+/-").
* Zeigt der Bildschirm einen positiven Wert an, so wird ein "-" links angehängt, der Bildschirm
Expand All @@ -118,6 +124,9 @@ public void pressNegativeKey() {
* und das Ergebnis direkt angezeigt.
*/
public void pressEqualsKey() {
if(latestOperation.isEmpty()) {
return;
}
var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "-" -> latestValue - Double.parseDouble(screen);
Expand Down
49 changes: 48 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,54 @@ void testMultipleDecimalDots() {
assertEquals(expected, actual);
}

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

calc.pressDigitKey(7);
calc.pressDigitKey(1);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(5);
calc.pressDigitKey(1);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

//Teilaufgabe 2 - Test 1:
@Test
@DisplayName("tests if the calculator uses the latest result to continue calculating")
void testMultipleAddition() {
Calculator calc = new Calculator();
calc.pressDigitKey(7);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(1);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
//Teilaufgabe 2 - Test 2:
@Test
@DisplayName("tests if 0 is displayed after pressing the equal key when not pressed a digit before")
void testEqualsKey() {
Calculator calc = new Calculator();
calc.pressEqualsKey();
String expected = "0";
String actual = calc.readScreen();

//TODO hier weitere Tests erstellen
assertEquals(expected, actual);
}



}