Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

581090 #100

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
//Addierungen für Zwei Operator
if(!latestOperation.equals("")) {
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);
}
//
latestValue = Double.parseDouble(screen);
latestOperation = operation;
}
Expand Down Expand Up @@ -118,6 +130,15 @@ public void pressNegativeKey() {
* und das Ergebnis direkt angezeigt.
*/
public void pressEqualsKey() {
// es gibt gleiche Nummer
if (latestOperation.equals("")) {
if (screen.equals("0")) {
screen = Double.toString(latestValue);
}
return;
}


var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "-" -> latestValue - Double.parseDouble(screen);
Expand All @@ -129,5 +150,7 @@ public void pressEqualsKey() {
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);
//letzte Operation
latestOperation="";
}
}
90 changes: 90 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,95 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen

// @author mit Hilfe von Tutoren und Ahmad


@Test
@DisplayName("should display zero after pressing AC key")
void testAllClear() {
Calculator calc = new Calculator();

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

calc.pressClearKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display correct result after dividing two numbers")
void testDivision() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(1);
calc.pressDigitKey(0);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after adding two positive with comma multi-digit numbers")
void testPositiveKommaAddition() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressDotKey();
calc.pressDigitKey(0);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(2);
calc.pressDotKey();
calc.pressDigitKey(0);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
@Test
@DisplayName("it should give same number back if there are no operations")
void testGiveSameNumber() {
Calculator calc = new Calculator();

calc.pressDigitKey(3);
calc.pressEqualsKey();


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

assertEquals(expected, actual);
}
@Test
@DisplayName("it should calculate more numbers")
void testWithMoreNumbers() {
Calculator calc = new Calculator();

calc.pressDigitKey(9);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(1);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(8);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

}