Skip to content
20 changes: 17 additions & 3 deletions src/main/java/tdd/setup/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// behaviour inspired by https://www.online-calculator.com/
public class Calculator {

private String screen = "0";
private String screen = "";

private double latestValue;

Expand All @@ -13,11 +13,16 @@ public String readScreen() {
return screen;
}
public void pressDigitKey(int digit) {

//Bugfix zum 2.Test
if (digit != 0 && screen.startsWith("0")) screen = "";

if(digit > 9 || digit < 0) throw new IllegalArgumentException();

if(latestOperation.isEmpty()) {
screen = screen + digit;
} else {
}
else {
latestValue = Double.parseDouble(screen);
screen = Integer.toString(digit);
}
Expand All @@ -30,10 +35,19 @@ public void pressClearKey() {
}

public void pressOperationKey(String operation) {
latestOperation = operation;
//Bugfix zum 1.Test
if(operation != "%") {
latestOperation = operation;
}
else { var result = Double.parseDouble(screen) / 100;
screen = Double.toString(result);
if(screen.endsWith(".0"))
screen = screen.substring(0, screen.length()-1);
}
}

public void pressDotKey() {
//Bugfix zum Dot Roten Test
if(!screen.endsWith(".")) screen = screen + ".";
}

Expand Down
41 changes: 41 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,45 @@ void calculatorCanDoTwoPlusTwo() {
calc.pressEquals();
assertEquals("4", calc.readScreen());
}

@Test
// neuer grüner Test
void calculatorCanDoTwoMinusTwo() {
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressOperationKey("-");
calc.pressDigitKey(2);
calc.pressEquals();
assertEquals("0", calc.readScreen());
}
@Test
// neuer roter Test
void calculatorPercentage() {
Calculator calc = new Calculator();
calc.pressDigitKey(5);
calc.pressDigitKey(0);
calc.pressOperationKey("%");
assertEquals("0.5", calc.readScreen());
}

@Test
// 2.roter Test
void NumberStartsWith0() {
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressDigitKey(0);
assertEquals("20", calc.readScreen());
}

@Test
// 3.roter Test
void Dot() {
Calculator calc = new Calculator();
calc.pressDigitKey(5);
calc.pressDotKey();
calc.pressDigitKey(5);
assertEquals("5.5", calc.readScreen());

}

}