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: 12 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 @@ -31,7 +31,7 @@ public String readScreen() {
public void pressDigitKey(int digit) {
if(digit > 9 || digit < 0) throw new IllegalArgumentException();

if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = "";
if(screen.equals("0")) screen = "";

screen = screen + digit;
}
Expand Down Expand Up @@ -60,8 +60,13 @@ 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;
screen = "0";
}

/**
Expand Down Expand Up @@ -118,6 +123,11 @@ 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 All @@ -130,4 +140,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);
}
}
}
62 changes: 60 additions & 2 deletions app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,64 @@ 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(3);
calc.pressDigitKey(0);

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

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

assertEquals(expected, actual);

}

@Test
@DisplayName("should display result after subtracting two positive multi-digit numbers")
void testEqualsAfterNoNewOperation(){

Calculator calc = new Calculator();

calc.pressDigitKey(5);

calc.pressEqualsKey();

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

assertEquals(expected, actual);

}

@Test
@DisplayName("should display result after more then 2 Operations")
void testMoreThenTwoOperations(){

Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(5);
calc.pressDigitKey(0);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(5);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressEqualsKey();

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

assertEquals(expected, actual);

}
}