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
6 changes: 6 additions & 0 deletions src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public void pressDigitKey(int digit) {

if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = "";

if (screen.length() < 9) {
screen = screen + digit;
}
}

/**
Expand Down Expand Up @@ -74,6 +76,10 @@ public void pressBinaryOperationKey(String operation) {
public void pressUnaryOperationKey(String operation) {
latestValue = Double.parseDouble(screen);
latestOperation = operation;
if (operation.equals("1/x") && Double.parseDouble(screen) == 0) {
screen = "Error";
return;
}
var result = switch(operation) {
case "√" -> Math.sqrt(Double.parseDouble(screen));
case "%" -> Double.parseDouble(screen) / 100;
Expand Down
48 changes: 44 additions & 4 deletions src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package htw.berlin.prog2.ha1;

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

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

@DisplayName("Retro calculator")
class CalculatorTest {

Expand Down Expand Up @@ -88,7 +87,48 @@ void testMultipleDecimalDots() {
assertEquals(expected, actual);
}

@Test
@DisplayName("should display entered number after pressing digit keys")
void testEnteredNumber() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressDigitKey(2);
calc.pressDigitKey(3);

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display error when calculating inverse of zero")
void testInverseOfZero() {
Calculator calc = new Calculator();

//TODO hier weitere Tests erstellen
}
calc.pressDigitKey(0);
calc.pressUnaryOperationKey("1/x");

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should not allow more than 9 digits on display")
void testMaxDigits() {
Calculator calc = new Calculator();

for (int i = 0; i < 11; i++) {
calc.pressDigitKey(1);
}

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

assertEquals(expected, actual);
}

}