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
23 changes: 20 additions & 3 deletions src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Calculator {

private String latestOperation = "";

private boolean clearPressedOnce = false; // Neue Ergänzung
/**
* @return den aktuellen Bildschirminhalt als String
*/
Expand Down Expand Up @@ -44,12 +45,24 @@ public void pressDigitKey(int digit) {
* Werte sowie der aktuelle Operationsmodus zurückgesetzt, so dass der Rechner wieder
* im Ursprungszustand ist.
*/
public void pressClearKey() {

/**public void pressClearKey() {
screen = "0";
latestOperation = "";
latestValue = 0.0;
}*/

public void pressClearKey() {
if (!clearPressedOnce) {
screen = "0"; // nur den Bildschirm löschen
clearPressedOnce = true; // nur einmal gedrückt
} else {
screen = "0"; // sonst kompletter Reset
latestOperation = "";
latestValue = 0.0;
clearPressedOnce = false; // zurücksetzen
}
}

/**
* Empfängt den Wert einer gedrückten binären Operationstaste, also eine der vier Operationen
* Addition, Substraktion, Division, oder Multiplikation, welche zwei Operanden benötigen.
Expand Down Expand Up @@ -77,7 +90,11 @@ public void pressUnaryOperationKey(String operation) {
var result = switch(operation) {
case "√" -> Math.sqrt(Double.parseDouble(screen));
case "%" -> Double.parseDouble(screen) / 100;
case "1/x" -> 1 / Double.parseDouble(screen);
//case "1/x" -> 1 / Double.parseDouble(screen);
case "1/x" -> {
if (Double.parseDouble(screen) == 0) yield Double.NaN;
else yield 1 / Double.parseDouble(screen);
}
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,52 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen

@Test
@DisplayName("should correctly calculate percent")
void testPercentOperation() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressDigitKey(0);
calc.pressUnaryOperationKey("%");

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should not reset memory on first clear press")
void testClearKeyOnce() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display error when inverting zero")
void testInversionOfZero() {
Calculator calc = new Calculator();

calc.pressDigitKey(0);
calc.pressUnaryOperationKey("1/x");

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

assertEquals(expected, actual);
}
}