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
18 changes: 15 additions & 3 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Calculator {
private String screen = "0";

private double latestValue;
private double latestLatestValue;

private String latestOperation = "";

Expand All @@ -31,7 +32,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) /*|| latestValue == Double.parseDouble(screen) + 1*/) screen = "";

screen = screen + digit;
}
Expand Down Expand Up @@ -59,8 +60,18 @@ public void pressClearKey() {
* auf dem Bildschirm angezeigt. Falls hierbei eine Division durch Null auftritt, wird "Error" angezeigt.
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/

public void pressBinaryOperationKey(String operation) {
if(!(latestValue ==0)) {
latestLatestValue = latestValue;
}

/*if(!latestOperation.equals("")) {
latestValue += Double.parseDouble(screen );
}
else {*/
latestValue = Double.parseDouble(screen);
// }
latestOperation = operation;
}

Expand Down Expand Up @@ -118,7 +129,7 @@ public void pressNegativeKey() {
*/
public void pressEqualsKey() {
var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "+" -> latestValue + Double.parseDouble(screen) + latestLatestValue;
case "-" -> latestValue - Double.parseDouble(screen);
case "x" -> latestValue * Double.parseDouble(screen);
case "/" -> latestValue / Double.parseDouble(screen);
Expand All @@ -127,5 +138,6 @@ public void pressEqualsKey() {
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";
}
}
}
47 changes: 46 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 @@ -41,5 +41,50 @@ void testSquareRoot() {
}

//TODO hier weitere Tests erstellen
}
@Test
@DisplayName("should display 0.0 after pressing the clear key")
void testClearKey() {
Calculator calc = new Calculator();
calc.pressDigitKey(3);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(7);
calc.pressDigitKey(1);
calc.pressClearKey();

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

assertEquals(expected, actual);
}
@Test
@DisplayName("should display Error after pressing the equals key")
void testDivideThroughZero() {
Calculator calc = new Calculator();
calc.pressDigitKey(3);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(0);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
@Test
@DisplayName("should display 6 after adding 1, 2 and 3")
void testTwoOperations() {
Calculator calc = new Calculator();
calc.pressDigitKey(1);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(2);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(3);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

}