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
20 changes: 17 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;
/**
* @return den aktuellen Bildschirminhalt als String
*/
Expand All @@ -29,6 +30,7 @@ public String readScreen() {
* @param digit Die Ziffer, deren Taste gedrückt wurde
*/
public void pressDigitKey(int digit) {
clearPressedOnce = false;
if(digit > 9 || digit < 0) throw new IllegalArgumentException();

if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = "";
Expand All @@ -45,9 +47,15 @@ public void pressDigitKey(int digit) {
* im Ursprungszustand ist.
*/
public void pressClearKey() {
screen = "0";
latestOperation = "";
latestValue = 0.0;
if (clearPressedOnce) {
screen = "0";
latestOperation = "";
latestValue = 0.0;
clearPressedOnce = false;
} else {
screen = "0";
clearPressedOnce = true;
}
}

/**
Expand Down Expand Up @@ -118,14 +126,20 @@ public void pressNegativeKey() {
* und das Ergebnis direkt angezeigt.
*/
public void pressEqualsKey() {
if (latestOperation == null || latestOperation.isEmpty()) {
return;
}

var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "-" -> latestValue - Double.parseDouble(screen);
case "x" -> latestValue * Double.parseDouble(screen);
case "/" -> latestValue / Double.parseDouble(screen);
default -> throw new IllegalArgumentException();
};

screen = Double.toString(result);

if(screen.equals("Infinity")) screen = "Error";
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);
Expand Down
33 changes: 33 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,38 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen
@Test
void percentOf50Is0Point5() {
Calculator calculator = new Calculator();

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

assertEquals("0.5", calculator.readScreen());
}
@Test
void equalsWithoutPreviousOperationDoesNothing() {
Calculator calculator = new Calculator();

calculator.pressDigitKey(7);
calculator.pressEqualsKey();

assertEquals("7", calculator.readScreen());
}
@Test
void firstClearOnlyClearsCurrentEntryButKeepsPendingOperation() {
Calculator calculator = new Calculator();

calculator.pressDigitKey(7);
calculator.pressBinaryOperationKey("+");
calculator.pressDigitKey(3);

calculator.pressClearKey(); // nur aktuelle Eingabe löschen
calculator.pressDigitKey(2);
calculator.pressEqualsKey();

assertEquals("9", calculator.readScreen());
}
}