diff --git a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java index 84c04f2..d1048bd 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -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 @@ -14,6 +17,11 @@ public class Calculator { private String latestOperation = ""; + private ArrayList latestValueSave = new ArrayList<>(); + private ArrayList latestOperationSave = new ArrayList<>(); + private ArrayList intermediateValues = new ArrayList<>(); + private ArrayList remainingOperations = new ArrayList<>(); + /** * @return den aktuellen Bildschirminhalt als String */ @@ -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"; } /** @@ -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); @@ -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; } } diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index ddff0da..37a62e7 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -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); @@ -40,6 +108,7 @@ void testSquareRoot() { assertEquals(expected, actual); } + @Test @DisplayName("should display error when dividing by zero") void testDivisionByZero() { @@ -82,6 +151,8 @@ void testMultipleDecimalDots() { calc.pressDotKey(); calc.pressDigitKey(8); + + String expected = "1.78"; String actual = calc.readScreen();