diff --git a/src/main/java/tdd/setup/Calculator.java b/src/main/java/tdd/setup/Calculator.java index 171f6d6..c4620aa 100644 --- a/src/main/java/tdd/setup/Calculator.java +++ b/src/main/java/tdd/setup/Calculator.java @@ -3,43 +3,75 @@ // behaviour inspired by https://www.online-calculator.com/ public class Calculator { + // Ausgabe auf dem Screen default auf 0 private String screen = "0"; + // letzter eingegebener Wert private double latestValue; + // letzte ausgewählte Operation private String latestOperation = ""; + + + /** + * Method read Screen + * Methode gibt String value der Variablen Screen zurück + * @return screen + */ public String readScreen() { return screen; } + + + /** + * Method pressDigitKey + * Method changed to Fix Multiplication of double digit values + * @param digit + */ public void pressDigitKey(int digit) { if(digit > 9 || digit < 0) throw new IllegalArgumentException(); - - if(latestOperation.isEmpty()) { - screen = screen + digit; - } else { - latestValue = Double.parseDouble(screen); - screen = Integer.toString(digit); - } + Integer.toString(digit); + screen = screen + digit; } + + // Noch nicht getestet + // Alle grauen Tests sind noch nicht getestet public void pressClearKey() { - screen = "0"; - latestOperation = ""; - latestValue = 0.0; + screen = "0"; // Ausgangszustand Screen (deault Zustand) + latestOperation = ""; // Default Zustand + latestValue = 0.0; // Latest Value wird auf 0.0 gesetzt } + /** + * Method pressOperationKey + * Fix Multiplication of double digit value + * @param operation + */ public void pressOperationKey(String operation) { latestOperation = operation; + latestValue = Double.parseDouble(screen); + screen = "0"; } + + /** + * Method pressDotKey + * .contains(".) fix to ignore the input of multiple dots in one number + */ public void pressDotKey() { - if(!screen.endsWith(".")) screen = screen + "."; - } + if(!screen.contains(".")) screen = screen + "."; + } // Wenn Screen nicht mit . endet mach nen Punkt rein + // Wenn man Zahlen zwischen die Punkte schreibt kann man mehrere Punkte in eine Zahl machen + + + + // Noch nicht getestet + public void pressNegative() { screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; } + // Wenn Screen mit - anfängt dann mach screen erster Platz im Index 1 (positiv/vorzeichnfrei) + // Wenn Screen nicht mit - anfängt dann mach "-" + screen (Minuszeichen wird gesetzt) - public void pressNegative() { - screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; - } public void pressEquals() { var result = switch(latestOperation) { @@ -50,6 +82,17 @@ public void pressEquals() { default -> throw new IllegalArgumentException(); }; screen = Double.toString(result); - if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2); + if(screen.endsWith(".0")) screen = screen.substring(0, screen.length()-2); + + + // if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2); } + + /** + * Davor Richtiges Verzeichnis in Ubuntu wählen + * git status + * git add . + * git commit -m 'Test Nachricht' + * + */ } diff --git a/src/test/java/tdd/setup/CalculatorTest.java b/src/test/java/tdd/setup/CalculatorTest.java index 3bf16e9..3ce1c55 100644 --- a/src/test/java/tdd/setup/CalculatorTest.java +++ b/src/test/java/tdd/setup/CalculatorTest.java @@ -16,4 +16,66 @@ void calculatorCanDoTwoPlusTwo() { calc.pressEquals(); assertEquals("4", calc.readScreen()); } + + + + /** + * 1. Grüner Test + * Press Negative Key + * -5 * 2 Ergebnis sollte - 100 sein + */ + @Test + void calculatorCanDoMultiplicationWithNegativeNumbers () { + Calculator calc = new Calculator(); + calc.pressDigitKey(5); + calc.pressNegative(); + calc.pressOperationKey("x"); + calc.pressDigitKey(2); + calc.pressEquals(); + assertEquals("-10", calc.readScreen()); + } + + + /** + * 1. Roter Test + * do multiplication with double digit numbers + * 20 * 50 = 1000 + */ + @Test + @DisplayName("Calculator displays correct result of the multiplication of two digit numbers!!") + void calcCanDoMultiplicationOfDoubleDigitNumbers() { + Calculator calc = new Calculator(); + calc.pressDigitKey(1); + calc.pressDigitKey(0); + calc.pressOperationKey("x"); + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressEquals(); + assertEquals("200", calc.readScreen()); + } + + + /** + * 2. Roter Test + * press Dot Key Multiple times + * Online Calculator ignores multiple dot inputs + */ + @Test + @DisplayName("Calculator ignores input of multiple dots") + void calculatorIgnoresMultipleDotsInOneNumber () { + Calculator calc = new Calculator(); + calc.pressDotKey(); + calc.pressDigitKey(8); + calc.pressDotKey(); + calc.pressDotKey(); + calc.pressDigitKey(5); + assertEquals("0.85", calc.readScreen()); + } + + + + + + + }