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
39 changes: 26 additions & 13 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
public class Calculator {

private String screen = "0";

private double latestValue;

private Double latestValue = 0.0;
private String latestOperation = "";
private Double lastOperand;
private String lastOperator;

/**
* @return den aktuellen Bildschirminhalt als String
Expand Down Expand Up @@ -62,6 +62,8 @@ public void pressClearKey() {
public void pressBinaryOperationKey(String operation) {
latestValue = Double.parseDouble(screen);
latestOperation = operation;
lastOperand = null;
lastOperator = latestOperation;
}

/**
Expand All @@ -75,15 +77,15 @@ 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);
case "√" -> Math.sqrt(latestValue);
case "%" -> latestValue/ 100;
case "1/x" -> 1 / latestValue;
case "±" -> -latestValue;
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
if(screen.equals("NaN")) screen = "Error";
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

}

/**
Expand Down Expand Up @@ -118,16 +120,27 @@ 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);
case "x" -> latestValue * Double.parseDouble(screen);
case "/" -> latestValue / Double.parseDouble(screen);
// Falls keine gültige Operation vorhanden ist, nichts tun
if (latestOperation == null && lastOperator == null) {
return;
}
// Letzten Operanden und Operator speichern, falls noch nicht gesetzt
if (lastOperand == null) {
lastOperand = Double.parseDouble(screen);
lastOperator = latestOperation;
}
var result = switch(lastOperator) {
case "+" -> latestValue + lastOperand;
case "-" -> latestValue - lastOperand;
case "x" -> latestValue * lastOperand;
case "/" -> (lastOperand != 0) ? latestValue / lastOperand : Double.POSITIVE_INFINITY;
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
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);

latestValue = result;
}
}
}
51 changes: 50 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 @@ -89,6 +89,55 @@ void testMultipleDecimalDots() {
}


//TODO hier weitere Tests erstellen
@Test
@DisplayName(value = "should display the product of the numbers")
void testProductOfNumbers() {
Calculator calc = new Calculator();

calc.pressDigitKey(6);
calc.pressDigitKey(0);
calc.pressBinaryOperationKey("x");
calc.pressDigitKey(4);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display correct result when equals is pressed multiple times after subtraction")
void testRepeatedEqualsSubtraction() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(1);
calc.pressEqualsKey(); // Ergebnis sollte 4 sein
calc.pressEqualsKey(); // Ergebnis sollte 3 sein
calc.pressEqualsKey(); // Ergebnis sollte 2 sein

String expected = "2"; // Erwartetes Ergebnis nach dreimaligem Drücken von "=": 5 - 1 - 1 - 1 = 2
String actual = calc.readScreen();

assertEquals(expected, actual);
}

@Test
@DisplayName("should change the sign of the number when pressing the negation key")
void testNegationKey() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressDotKey();
calc.pressDigitKey(5);
calc.pressUnaryOperationKey("±");

String expected = "-5.5"; // Erwartet: Die Zahl sollte das Vorzeichen wechseln
String actual = calc.readScreen();

assertEquals(expected, actual);
}
}