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
10 changes: 8 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 @@ -31,9 +31,14 @@ 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 = "";

screen = screen + digit;
if(latestOperation.isEmpty()) {
screen = screen + digit;
} else {
latestValue = Double.parseDouble(screen);
screen = Integer.toString(digit);
}
}

/**
Expand Down Expand Up @@ -77,6 +82,7 @@ public void pressUnaryOperationKey(String operation) {
var result = switch(operation) {
case "√" -> Math.sqrt(Double.parseDouble(screen));
case "%" -> Double.parseDouble(screen) / 100;
case "%%" -> latestValue / 10 * latestValue / 10;
case "1/x" -> 1 / Double.parseDouble(screen);
default -> throw new IllegalArgumentException();
};
Expand Down
74 changes: 73 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 @@ -40,6 +40,78 @@ void testSquareRoot() {
assertEquals(expected, actual);
}

//TODO hier weitere Tests erstellen
@Test
@DisplayName("should operate the percent function")
void ProzentFunction() {

Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should operate the percent function + ")
void ProzentFunctionTimes() {

Calculator calc = new Calculator();

calc.pressDigitKey(4);
calc.pressDotKey();
calc.pressDigitKey(0);
calc.pressUnaryOperationKey("%%");


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

assertEquals(expected, actual);
}
@Test
@DisplayName("Display out the 1/x Operation")

void XFunktion(){

Calculator calc = new Calculator();

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


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

assertEquals(expected, actual);

}
@Test
@DisplayName("+/- Taste am anfang drücken")

void MinusFunktion(){

Calculator calc = new Calculator();

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



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

assertEquals(expected, actual);

}

}