Skip to content
Open

ha1 #94

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
18 changes: 16 additions & 2 deletions src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Calculator {

private String latestOperation = "";

private boolean clearKeyPressedLast = false;

/**
* @return den aktuellen Bildschirminhalt als String
*/
Expand All @@ -29,6 +31,7 @@ public String readScreen() {
* @param digit Die Ziffer, deren Taste gedrückt wurde
*/
public void pressDigitKey(int digit) {
clearKeyPressedLast = false;
if(digit > 9 || digit < 0) throw new IllegalArgumentException();

if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = "";
Expand All @@ -45,9 +48,12 @@ public void pressDigitKey(int digit) {
* im Ursprungszustand ist.
*/
public void pressClearKey() {
if(clearKeyPressedLast){
latestOperation = "";
latestValue = 0.0;
}
screen = "0";
latestOperation = "";
latestValue = 0.0;
clearKeyPressedLast = true;
}

/**
Expand All @@ -60,6 +66,7 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
clearKeyPressedLast = false;
latestValue = Double.parseDouble(screen);
latestOperation = operation;
}
Expand All @@ -72,6 +79,7 @@ public void pressBinaryOperationKey(String operation) {
* @param operation "√" für Quadratwurzel, "%" für Prozent, "1/x" für Inversion
*/
public void pressUnaryOperationKey(String operation) {
clearKeyPressedLast = false;
latestValue = Double.parseDouble(screen);
latestOperation = operation;
var result = switch(operation) {
Expand All @@ -94,6 +102,7 @@ public void pressUnaryOperationKey(String operation) {
* Beim zweimaligem Drücken, oder wenn bereits ein Trennzeichen angezeigt wird, passiert nichts.
*/
public void pressDotKey() {
clearKeyPressedLast = false;
if(!screen.contains(".")) screen = screen + ".";
}

Expand All @@ -105,6 +114,7 @@ public void pressDotKey() {
* entfernt und der Inhalt fortan als positiv interpretiert.
*/
public void pressNegativeKey() {
clearKeyPressedLast = false;
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
}

Expand All @@ -118,6 +128,10 @@ public void pressNegativeKey() {
* und das Ergebnis direkt angezeigt.
*/
public void pressEqualsKey() {
clearKeyPressedLast = false;
if(latestOperation.equals("")){
return;
}
var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "-" -> latestValue - Double.parseDouble(screen);
Expand Down
50 changes: 47 additions & 3 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 @@ -89,6 +88,51 @@ void testMultipleDecimalDots() {
}


//TODO hier weitere Tests erstellen
@Test
@DisplayName("soll bei Gleich ohne Operation nichts verändern")
void testEqualsWithoutOperation(){
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("c soll nur aktuelle Eingabe löschen")
void testPressClearKey(){
Calculator calc = new Calculator();


calc.pressDigitKey(8);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(2);
calc.pressClearKey();
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("soll Vorzeichen zweimal umkehren")
void testToggleNegative(){
Calculator calc = new Calculator();

calc.pressDigitKey(7);
calc.pressNegativeKey();
calc.pressNegativeKey();

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

assertEquals(expected, actual);
}
}