Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HA1 S0592192 #110

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
89 changes: 72 additions & 17 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package htw.berlin.prog2.ha1;
import java.util.ArrayList;
import java.util.List;


/**
* Eine Klasse, die das Verhalten des Online Taschenrechners imitiert, welcher auf
Expand All @@ -14,6 +17,11 @@ public class Calculator {

private String latestOperation = "";

private ArrayList<Double> latestValueSave = new ArrayList<>();
private ArrayList<String> latestOperationSave = new ArrayList<>();
private ArrayList<Double> intermediateValues = new ArrayList<>();
private ArrayList<String> remainingOperations = new ArrayList<>();

/**
* @return den aktuellen Bildschirminhalt als String
*/
Expand Down Expand Up @@ -60,8 +68,9 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
latestValue = Double.parseDouble(screen);
latestOperation = operation;
latestValueSave.add(Double.parseDouble(screen));
latestOperationSave.add(operation);
screen = "0";
}

/**
Expand All @@ -74,10 +83,11 @@ public void pressBinaryOperationKey(String operation) {
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);
double result = 0.0;
switch(operation) {
case "√" -> result = Math.sqrt(Double.parseDouble(screen));
case "%" -> result = Double.parseDouble(screen) / 100;
case "1/x" -> result = 1 / Double.parseDouble(screen);
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
Expand Down Expand Up @@ -118,16 +128,61 @@ 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);
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);
latestValueSave.add(Double.parseDouble(screen));
double result = operationCheck();
if (result % 1 >= 0){
screen = Integer.toString((int) result);
} else {
screen = Double.toString(result);
}
if (screen.equals("Infinity")) screen = "Error";
}

// public void addOperation(double value, String operation) {
// latestValueSave.add(value);
// latestOperationSave.add(operation);
//}



public double operationCheck() {


double currentValue = latestValueSave.get(0);
for (int i = 0; i < latestOperationSave.size(); i++) {
String operation = latestOperationSave.get(i);
double nextValue = latestValueSave.get(i + 1);

switch (operation) {
case "x":
currentValue *= nextValue;
break;
case "/":
currentValue /= nextValue;
break;
default:
if(screen.equals("Infinity")) screen = "Error";
intermediateValues.add(currentValue);
remainingOperations.add(operation);
currentValue = nextValue;
break;
}
}
intermediateValues.add(currentValue);

double result = intermediateValues.get(0);
for (int i = 0; i < remainingOperations.size(); i++) {
String operation = remainingOperations.get(i);
double nextValue = intermediateValues.get(i + 1);
if (operation.equals("+")) {
result += nextValue;
} else if (operation.equals("-")) {
result -= nextValue;
} else {
throw new IllegalArgumentException("Invalid operation");
}
}

return result;
}
}
75 changes: 73 additions & 2 deletions app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,82 @@ class CalculatorTest {
void testPositiveAddition() {
Calculator calc = new Calculator();

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

String expected = "40";
String expected = "60";
String actual = calc.readScreen();

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after adding two positive digits numbers")
void testSingleDigitAddition() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(2);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after adding two positive digits numbers")
void testMultiplSingleDigitAddition() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(2);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(3);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after adding two positive digits numbers")
void testPunktVorStrich() {
Calculator calc = new Calculator();

calc.pressDigitKey(3);
calc.pressBinaryOperationKey("x");
calc.pressDigitKey(2);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(3);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result after multiplying two positive digits numbers")
void testSingleDigitMultiplikation() {
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressBinaryOperationKey("x");
calc.pressDigitKey(6);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
Expand All @@ -40,6 +108,7 @@ void testSquareRoot() {
assertEquals(expected, actual);
}


@Test
@DisplayName("should display error when dividing by zero")
void testDivisionByZero() {
Expand Down Expand Up @@ -82,6 +151,8 @@ void testMultipleDecimalDots() {
calc.pressDotKey();
calc.pressDigitKey(8);



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

Expand Down
Loading