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

private String latestOperation = "";

// Add a Logger instance
private Logger logger = new Logger();

/**
* @return den aktuellen Bildschirminhalt als String
*/
Expand All @@ -34,6 +37,14 @@ public void pressDigitKey(int digit) {
if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = "";

screen = screen + digit;

logger.logButtonClick(String.valueOf(digit));
if (!latestOperation.isEmpty()){
logger.logCurrentEquation(String.valueOf(latestValue)+latestOperation+screen);
}else{
logger.logCurrentEquation(String.valueOf(screen));
}

}

/**
Expand All @@ -48,6 +59,9 @@ public void pressClearKey() {
screen = "0";
latestOperation = "";
latestValue = 0.0;

logger.logButtonClick("C");
logger.logCurrentEquation(String.valueOf(latestValue)+latestOperation+screen);
}

/**
Expand All @@ -60,8 +74,27 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
// TODO: Error, falls nach Division Null eingegeben wird
// TODO: Wenn es nicht der erste Operand ist, dann zeige Zwischenergebnis auf Bildschirm an
if(operation.matches("[x/]") && latestOperation.matches("[+-]") ){

} else if (!latestOperation.isEmpty()){
// printe zwischenergebnis auf 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);
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
}

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

logger.logButtonClick(operation);
logger.logCurrentEquation(screen+latestOperation);
}

/**
Expand All @@ -84,6 +117,8 @@ public void pressUnaryOperationKey(String operation) {
if(screen.equals("NaN")) screen = "Error";
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

logger.logButtonClick(operation);
logger.logCurrentEquation(screen);
}

/**
Expand All @@ -95,6 +130,9 @@ public void pressUnaryOperationKey(String operation) {
*/
public void pressDotKey() {
if(!screen.contains(".")) screen = screen + ".";

logger.logButtonClick(".");
logger.logCurrentEquation(screen);
}

/**
Expand All @@ -106,6 +144,13 @@ public void pressDotKey() {
*/
public void pressNegativeKey() {
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;

logger.logButtonClick("+/-");
if (!latestOperation.isEmpty()){
logger.logCurrentEquation(String.valueOf(latestValue)+latestOperation+screen);
}else {
logger.logCurrentEquation(screen);
}
}

/**
Expand All @@ -128,6 +173,10 @@ public void pressEqualsKey() {
screen = Double.toString(result);
if(screen.equals("Infinity")) screen = "Error";
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.endsWith(".")) screen = screen.substring(0,screen.length()-1);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

logger.logButtonClick("=");
logger.logCurrentEquation(screen);
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/htw/berlin/prog2/ha1/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package htw.berlin.prog2.ha1;

public class Logger {
public void logButtonClick(String button) {
System.out.println("Button clicked: " + button);
}

public void logCurrentEquation(String equation) {
System.out.println("Current equation: " + equation);
}
}

77 changes: 77 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 @@ -26,6 +26,56 @@ void testPositiveAddition() {
assertEquals(expected, actual);
}

@DisplayName("should display result after subtracting a number from another number")
void testSubtraction() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

@DisplayName("should display result after multiplying two numbers")
void testMultiplication() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}

@DisplayName("should display result after dividing a number by another")
void testDivision() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressDigitKey(0);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(4);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after getting the square root of two")
void testSquareRoot() {
Expand All @@ -40,6 +90,33 @@ void testSquareRoot() {
assertEquals(expected, actual);
}

@Test
@DisplayName("should show intermediate result after multiple binary operands")
void testScreenOutputAfterMultipleBinaryOperands(){
Calculator calc = new Calculator();
calc.pressDigitKey(7);
calc.pressBinaryOperationKey("x");
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("x");

String actual = calc.readScreen();
double expected = 35;

assertEquals(expected, Double.valueOf(actual));
}
@Test
@DisplayName("should remove point if it is at the end")
void testRemovePoint(){
Calculator calc = new Calculator();
calc.pressDigitKey(5);
calc.pressDotKey();
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}
@Test
@DisplayName("should display error when dividing by zero")
void testDivisionByZero() {
Expand Down