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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Hausaufgabe 1
# Hausaufgabe 1

Deadline für Abgabe per Pull Request: 24.10.2021 23:59 Uhr bzw. 27.10.2021 23:59 Uhr je nach dem in welcher Übungsgruppe Sie eingeschrieben sind.

Expand Down
9 changes: 8 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 @@ -82,7 +82,9 @@ public void pressUnaryOperationKey(String operation) {
};
screen = Double.toString(result);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

if(screen.contains("Infinity")) {
screen = "Error";
}
}

/**
Expand Down Expand Up @@ -122,10 +124,15 @@ public void pressEqualsKey() {
case "-" -> latestValue - Double.parseDouble(screen);
case "x" -> latestValue * Double.parseDouble(screen);
case "/" -> latestValue / Double.parseDouble(screen);
case "" -> Double.parseDouble(screen);
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);
if(screen.contains("Infinity")) {
screen = "Error";
}
}

}
60 changes: 58 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 @@ -40,6 +40,62 @@ void testSquareRoot() {
assertEquals(expected, actual);
}

//TODO hier weitere Tests erstellen
}
@Test
@DisplayName("should clear the screen and set into the default value (0)")
void testClearKey() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressClearKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display Error after dividing number 1 with 0")
void testDivisionWithZero() {
Calculator calc = new Calculator();

calc.pressDigitKey(7);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(0);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display Error after press inversion without press digit key")
void testPressInversionKey() {
Calculator calc = new Calculator();

calc.pressUnaryOperationKey("1/x");

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display decimal numbers on screen like 4.5 after pressing Equalskey")
void testPressDotkeyAndPressEqualskey() {
Calculator calc = new Calculator();

calc.pressDigitKey(4);
calc.pressDotKey();
calc.pressDigitKey(5);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
}