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
75 changes: 59 additions & 16 deletions src/main/java/tdd/setup/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'
*
*/
}
62 changes: 62 additions & 0 deletions src/test/java/tdd/setup/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}







}