Skip to content
Open

HA1 #115

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
12 changes: 10 additions & 2 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Calculator {
private double latestValue;

private String latestOperation = "";
private boolean pressedClearEntryKey = false;

/**
* @return den aktuellen Bildschirminhalt als String
Expand Down Expand Up @@ -45,9 +46,16 @@ public void pressDigitKey(int digit) {
* im Ursprungszustand ist.
*/
public void pressClearKey() {
screen = "0";
if (!pressedClearEntryKey) {
screen = "0";
pressedClearEntryKey = true;
} else if (pressedClearEntryKey) {
screen = "0";
latestOperation = "";
latestValue = 0.0;
pressedClearEntryKey = false;

}
}

/**
Expand Down Expand Up @@ -94,7 +102,7 @@ public void pressUnaryOperationKey(String operation) {
* Beim zweimaligem Drücken, oder wenn bereits ein Trennzeichen angezeigt wird, passiert nichts.
*/
public void pressDotKey() {
if(!screen.contains(".")) screen = screen + ".";
if(!screen.contains(".") && screen.length() < 9 ) screen = screen + ".";
}

/**
Expand Down
64 changes: 64 additions & 0 deletions app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,69 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen
@Test
@DisplayName("should display result after adding two positive single-digit numbers")
void testSimpleAddition() {
Calculator calc = new Calculator();

calc.pressDigitKey(3);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(7);
calc.pressEqualsKey();

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


assertEquals(expected, actual);
}

@Test
@DisplayName("")
void test() {
Calculator calc = new Calculator();

calc.pressDigitKey(3);
calc.pressDigitKey(3);
calc.pressDigitKey(3);
calc.pressDigitKey(3);
calc.pressDigitKey(3);
calc.pressDigitKey(3);
calc.pressDigitKey(3);
calc.pressDigitKey(3);
calc.pressDigitKey(3);
calc.pressDotKey();


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


assertEquals(expected, actual);
}

@Test
@DisplayName("")
public void pressClearKey() {
Calculator calc = new Calculator();

calc.pressDigitKey(3);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(7);
calc.pressClearKey();
calc.pressDigitKey(3);
calc.pressEqualsKey();


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


assertEquals(expected, actual);
}
}