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: 27 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 @@ -14,13 +14,21 @@ public class Calculator {

private String latestOperation = "";


/**
* @return den aktuellen Bildschirminhalt als String
*/
public String readScreen() {

if(screen.equals("NaN")){
screen = "Error";
}

return screen;
}



/**
* Empfängt den Wert einer gedrückten Zifferntaste. Da man nur eine Taste auf einmal
* drücken kann muss der Wert positiv und einstellig sein und zwischen 0 und 9 liegen.
Expand All @@ -34,6 +42,7 @@ public void pressDigitKey(int digit) {
if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = "";

screen = screen + digit;

}

/**
Expand All @@ -60,6 +69,11 @@ 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.equals(""))) {
pressEqualsKey();
}

latestValue = Double.parseDouble(screen);
latestOperation = operation;
}
Expand All @@ -72,17 +86,19 @@ public void pressBinaryOperationKey(String operation) {
* @param operation "√" für Quadratwurzel, "%" für Prozent, "1/x" für Inversion
*/
public void pressUnaryOperationKey(String operation) {

latestValue = Double.parseDouble(screen);
latestOperation = operation;

var result = switch(operation) {
case "√" -> Math.sqrt(Double.parseDouble(screen));
case "%" -> Double.parseDouble(screen) / 100;
case "1/x" -> 1 / Double.parseDouble(screen);
default -> throw new IllegalArgumentException();
};

screen = Double.toString(result);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

}

/**
Expand All @@ -104,7 +120,14 @@ public void pressDotKey() {
* entfernt und der Inhalt fortan als positiv interpretiert.
*/
public void pressNegativeKey() {
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
//screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;

if(screen.startsWith("-")){
screen = screen.substring(1);

} else{
screen = "-" + screen;
}
}

/**
Expand All @@ -117,6 +140,7 @@ public void pressNegativeKey() {
* und das Ergebnis direkt angezeigt.
*/
public void pressEqualsKey() {

var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "-" -> latestValue - Double.parseDouble(screen);
Expand All @@ -128,4 +152,5 @@ 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);
}

}
50 changes: 50 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 @@ -4,6 +4,7 @@
import org.junit.jupiter.api.Test;

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

@DisplayName("Retro calculator")
class CalculatorTest {
Expand Down Expand Up @@ -41,5 +42,54 @@ void testSquareRoot() {
}

//TODO hier weitere Tests erstellen

@Test
@DisplayName("should display result after adding a dot")
void testDot(){
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressDotKey();
calc.pressDigitKey(1);

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

assertEquals(expected, actual);
}


@Test
@DisplayName("should display result after adding three positive single-digit numbers")
void testAdditionThree(){
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

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

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

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

assertEquals(expected, actual);
}
}