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
33 changes: 23 additions & 10 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,9 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
if (!latestOperation.isEmpty()) {
pressEqualsKey();// This completes the previous operation using the current screen value
}
latestValue = Double.parseDouble(screen);
latestOperation = operation;
}
Expand Down Expand Up @@ -110,21 +113,31 @@ public void pressNegativeKey() {

/**
* Empfängt den Befehl der gedrückten "="-Taste.
* Wurde zuvor keine Operationstaste gedrückt, passiert nichts.
* Wurde zuvor eine binäre Operationstaste gedrückt und zwei Operanden eingegeben, wird das
* Ergebnis der Operation angezeigt. Falls hierbei eine Division durch Null auftritt, wird "Error" angezeigt.
* Wird die Taste weitere Male gedrückt (ohne andere Tasten dazwischen), so wird die letzte
* Operation (ggf. inklusive letztem Operand) erneut auf den aktuellen Bildschirminhalt angewandt
* und das Ergebnis direkt angezeigt.
* - Wurde zuvor keine Operationstaste gedrückt, passiert nichts.
* - Wurde zuvor eine binäre Operationstaste gedrückt und zwei Operanden eingegeben, wird das
* Ergebnis der Operation angezeigt. Nach der Berechnung wird das Ergebnis in latestValue gespeichert,
* um die gleiche Operation bei erneutem Drücken der "="-Taste auf das Ergebnis anzuwenden.
* - Falls bei einer Operation, wie der Division, ein Fehler auftritt (z.B. Division durch Null),
* wird "Error" auf dem Bildschirm angezeigt.
* - Wird die Taste weitere Male gedrückt (ohne andere Tasten dazwischen), so wird die letzte
* Operation (ggf. inklusive letztem Operand) erneut auf den aktuellen Bildschirminhalt angewandt
* und das Ergebnis direkt angezeigt.
*/
public void pressEqualsKey() {
if (latestOperation.isEmpty()) {
return; // If no operation was set, do nothing.
}
var inputValue = Double.parseDouble(screen);
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);
case "+" -> latestValue + inputValue;
case "-" -> latestValue - inputValue;
case "x" -> latestValue * inputValue;
case "/" -> latestValue / inputValue;
default -> throw new IllegalArgumentException();
};

// Update latestValue with the result for potential repeated operations
latestValue = result;
screen = Double.toString(result);
if(screen.equals("Infinity")) screen = "Error";
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
Expand Down
199 changes: 195 additions & 4 deletions app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

@DisplayName("Retro calculator")
class CalculatorTest {
public CalculatorTest {

@Test
@DisplayName("should display result after adding two positive multi-digit numbers")
Expand All @@ -20,7 +20,7 @@ void testPositiveAddition() {
calc.pressDigitKey(0);
calc.pressEqualsKey();

String expected = "40";
String expected = "4000";
String actual = calc.readScreen();

assertEquals(expected, actual);
Expand Down Expand Up @@ -87,8 +87,199 @@ void testMultipleDecimalDots() {

assertEquals(expected, actual);
}
@Test
@DisplayName("should not allow multiple decimal dots")
void testMultipleDecimalDots() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressDotKey();
calc.pressDigitKey(7);
calc.pressDotKey();
calc.pressDigitKey(8);

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after subtracting two positive multi-digit numbers")
void testNegativeSubstraction(){
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressDigitKey(0);
calc.pressBineryOperationKey("-");
calc.pressDigitKey(1);
calc.pressDigitKey(0);

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

assertEquals(expected, actual);
}

package htw.berlin.prog2.ha1;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@DisplayName("Retro calculator")
public CalculatorTest {

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

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

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after getting the square root of two")
void testSquareRoot() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressUnaryOperationKey("√");

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display error when dividing by zero")
void testDivisionByZero() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display error when drawing the square root of a negative number")
void testSquareRootOfNegative() {
Calculator calc = new Calculator();

calc.pressDigitKey(7);
calc.pressNegativeKey();
calc.pressUnaryOperationKey("√");

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should not allow multiple decimal dots")
void testMultipleDecimalDots() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressDotKey();
calc.pressDigitKey(7);
calc.pressDotKey();
calc.pressDigitKey(8);

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

//TODO hier weitere Tests erstellen
}
assertEquals(expected, actual);
}
@Test
@DisplayName("should not allow multiple decimal dots")
void testMultipleDecimalDots() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressDotKey();
calc.pressDigitKey(7);
calc.pressDotKey();
calc.pressDigitKey(8);

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after subtracting two positive multi-digit numbers")
void testNegativeSubstraction(){
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressDigitKey(0);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(1);
calc.pressDigitKey(0);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
@Test
@DisplayName("should finish the first Operation before going on with the second one")
void testTwoOperations() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressBinaryOperationKey('+');
calc.pressDigitKey(2);
calc.pressBinaryOperationKey('*');
calc.pressDigitKey(2);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
@Test
@DisplayName("should repeat the last operation again on the new result by pressing equals key")
void testPressEqualsKeyDouble() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressBinaryOperationKey('*');
calc.pressDigitKey(3);
calc.pressEqualsKey(); // First equals to perform the operation 5 * 3
calc.pressEqualsKey(); // Second equals to repeat the last operation on the result, 15 * 3

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

assertEquals(expected, actual);
}




}