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
10 changes: 8 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") || latestValue == Double.parseDouble(screen) && !screen.endsWith(".")) screen = "";

screen = screen + digit;
}
Expand Down Expand Up @@ -104,7 +104,13 @@ public void pressDotKey() {
* entfernt und der Inhalt fortan als positiv interpretiert.
*/
public void pressNegativeKey() {
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
if (screen.startsWith("-")){
screen.substring(1);
} else if (latestOperation.equals("-") || latestOperation.equals("+")){
screen = "-" + "0";
} else {
screen = "-" + screen;
}
}

/**
Expand Down
55 changes: 54 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 @@ -40,6 +40,59 @@ void testSquareRoot() {
assertEquals(expected, actual);
}

//TODO hier weitere Tests erstellen
@Test
@DisplayName("should display result after subtracting two decimal numbers")
void testDecimalSubtraction() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressDotKey();
calc.pressDigitKey(1);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(2);
calc.pressDotKey();
calc.pressDigitKey(1);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after subtracting a positive number and a negative number")
void testPositiveSubtraction(){
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressBinaryOperationKey("-");
calc.pressNegativeKey();
calc.pressDigitKey(2);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after multiplying a decimal number and a natural number")
void testDecimalNumbers() {
Calculator calc = new Calculator();

calc.pressDigitKey(0);
calc.pressDotKey();
calc.pressDigitKey(9);
calc.pressBinaryOperationKey("x");
calc.pressDigitKey(3);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
}