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
29 changes: 24 additions & 5 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ public void pressDigitKey(int digit) {
* im Ursprungszustand ist.
*/
public void pressClearKey() {
screen = "0";
latestOperation = "";
latestValue = 0.0;
if(!screen.equals("0")){
screen = "0";
} else {
screen = "0";
latestOperation = "";
latestValue = 0.0;
}

//C taste fehlt bei wiederholtem drücken CE
}

/**
Expand Down Expand Up @@ -105,7 +111,11 @@ public void pressDotKey() {
* entfernt und der Inhalt fortan als positiv interpretiert.
*/
public void pressNegativeKey() {
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
if (screen.equals("0") || screen.equals(-0)) {
screen = "0";
} else{
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
}
}

/**
Expand All @@ -130,4 +140,13 @@ public void pressEqualsKey() {
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);
}
}
/*
public static void main(String[]args){
double a = 7;
double b = 9;

System.out.println(b / a);
*/

}

88 changes: 87 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 @@ -88,7 +88,93 @@ void testMultipleDecimalDots() {
assertEquals(expected, actual);
}


//TODO hier weitere Tests erstellen

@Test
@DisplayName("should display positive result after division of two positive numbers")
void testPositiveDivision(){
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display at most 10 characters on screen")
void testScreenLengthLimit(){
Calculator calc = new Calculator();

calc.pressDigitKey(9);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(7);
calc.pressEqualsKey();

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

System.out.println(actual);
System.out.println(actual.length());

assertEquals(true, actual.length()<= 10);
}

@Test
@DisplayName("should only clear screen on first press of C and full reeset on second")
void testClearKey(){
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);

}

@Test
@DisplayName("should not allow two points in one number")

void testDoubleDot(){
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressDotKey();
calc.pressDigitKey(6);
calc.pressDotKey();
calc.pressDigitKey(7);

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

assertEquals(expected,actual);
}

@Test
@DisplayName("should not write a negative sign before 0")
void testNegativeKeyOnZero(){
Calculator calc = new Calculator();

calc.pressNegativeKey();

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

assertEquals(expected, actual);
}

}