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
30 changes: 27 additions & 3 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package htw.berlin.prog2.ha1;


/**
* Eine Klasse, die das Verhalten des Online Taschenrechners imitiert, welcher auf
* https://www.online-calculator.com/ aufgerufen werden kann (ohne die Memory-Funktionen)
Expand All @@ -8,6 +9,8 @@
*/
public class Calculator {

private boolean used = false;

private String screen = "0";

private double latestValue;
Expand Down Expand Up @@ -45,9 +48,14 @@ public void pressDigitKey(int digit) {
* im Ursprungszustand ist.
*/
public void pressClearKey() {
screen = "0";
latestOperation = "";
latestValue = 0.0;
if(!used){
screen = "0";
used = true;
}
else {
latestValue = 0.0;
latestOperation = "";
}
}

/**
Expand Down Expand Up @@ -81,6 +89,7 @@ public void pressUnaryOperationKey(String operation) {
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
if(screen.equals("Infinity")) screen = "Error";
if(screen.equals("NaN")) screen = "Error";
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

Expand All @@ -106,6 +115,7 @@ public void pressDotKey() {
*/
public void pressNegativeKey() {
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;

}

/**
Expand All @@ -130,4 +140,18 @@ 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 i = 55;
double b = 0;

Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(0);
calc.pressUnaryOperationKey("1/x");

System.out.println(calc.readScreen());
}
}
55 changes: 55 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 @@ -56,6 +56,23 @@ void testDivisionByZero() {
assertEquals(expected, actual);
}

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

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

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

assertEquals(expected, actual);
}


@Test
@DisplayName("should display error when drawing the square root of a negative number")
void testSquareRootOfNegative() {
Expand Down Expand Up @@ -88,7 +105,45 @@ void testMultipleDecimalDots() {
assertEquals(expected, actual);
}

@Test
@DisplayName("check press clear key")
void testClearKey() {
Calculator calc = new Calculator();

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

calc.pressClearKey();
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}






//TODO hier weitere Tests erstellen

@Test
@DisplayName("check inverse with division by zero")
void testPressNegative() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(0);
calc.pressUnaryOperationKey("1/x");

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

assertEquals(expected, actual);

}
}