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

private String latestOperation = "";

private boolean newNumber = true;

/**
* @return den aktuellen Bildschirminhalt als String
*/
Expand All @@ -31,7 +33,13 @@ 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")) {
screen = "-" + digit;
newNumber = false;
return;
}

if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = ""; newNumber = false;

screen = screen + digit;
}
Expand Down Expand Up @@ -60,8 +68,14 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
if(!latestOperation.isEmpty()) {
pressEqualsKey();
}
latestValue = Double.parseDouble(screen);
latestOperation = operation;

//screen = "0"; // stört den Punkt mit dem Zwischenergebnis bei einer weiteren Eingabe!
newNumber = true;
}

/**
Expand Down Expand Up @@ -105,6 +119,11 @@ public void pressDotKey() {
* entfernt und der Inhalt fortan als positiv interpretiert.
*/
public void pressNegativeKey() {

if(newNumber == true) {
screen = "0";
newNumber= false;
}
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
}

Expand Down
106 changes: 106 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,111 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen

@Test
@DisplayName("should display result after adding 2 negative numbers")
void testNegativeAddition() {
Calculator calc = new Calculator();

calc.pressDigitKey(3);
calc.pressNegativeKey();
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(5);
calc.pressNegativeKey();
calc.pressEqualsKey();

String expected = "-8";
String actual = calc.readScreen();

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after subtract 2 negative numbers, when negative key is pressed before digit key")
void testNegativeSubtractionNegativeKeyBefore() {
Calculator calc = new Calculator();

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

String expected = "-4";
String actual = calc.readScreen();

assertEquals(expected, actual);

}

@Test
@DisplayName("should display result after subtract 2 negative numbers, when negative key is pressed after digit key")
void testNegativeSubtractionNegativeKeyAfter() {
Calculator calc = new Calculator();

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

String expected = "-4";
String actual = calc.readScreen();

assertEquals(expected, actual);

}
@Test
@DisplayName("display negative number, when negative key is pressed before digit key")
void testDisplayNegativeNumber() {
Calculator calc = new Calculator();

calc.pressNegativeKey();
calc.pressDigitKey(6);

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

assertEquals(expected, actual);
}
@Test
@DisplayName("display result after adding 3 positive numbers")
void testDisplayAddition3PositiveNumbers() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(5);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
@Test
@DisplayName("display result after adding 4 positive numbers")
void testDisplayAddition4PositiveNumbers() {
Calculator calc = new Calculator();

calc.pressDigitKey(8);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(2);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(1);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

}