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
8 changes: 6 additions & 2 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 @@ -19,7 +19,7 @@ public void pressDigitKey(int digit) {
screen = screen + digit;
} else {
latestValue = Double.parseDouble(screen);
screen = Integer.toString(digit);
screen = Integer.toString(digit); //Hier muss doch Double.toString(digit) stehen?
}
}

Expand All @@ -31,6 +31,7 @@ public void pressClearKey() {

public void pressOperationKey(String operation) {
latestOperation = operation;
latestValue = 1;
}

public void pressDotKey() {
Expand All @@ -41,15 +42,18 @@ public void pressNegative() {
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
}


public void pressEquals() {
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);
case "%" -> latestValue / 100;
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
}

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


@Test
@DisplayName("should display result after dividing two positive numbers")
void calculatorCanDoEightDividedByFour() {
Calculator c1 = new Calculator();
c1.pressDigitKey(8);
c1.pressOperationKey("/");
c1.pressDigitKey(4);
c1.pressEquals();
assertEquals("2", c1.readScreen());
}

@Test
@DisplayName("should display a correct number with a dot") //Code von 2.2.8 auf 1.1+1.1 und davon auf 1.1
void calculatorCanAdditionWithTwoDots() {
Calculator c2 = new Calculator();
c2.pressDigitKey(1);
c2.pressDotKey();
c2.pressDigitKey(1);
/* c2.pressOperationKey("+");
c2.pressDigitKey(1);
c2.pressDotKey();
c2.pressDigitKey(1); */
assertEquals("1.1", c2.readScreen());

}

@Test
@DisplayName("should display result after pressing percentage")
void calculatorCanPercentage() {
Calculator c3 = new Calculator();
c3.pressDigitKey(1);
c3.pressOperationKey("%");
c3.pressEquals();
assertEquals("0.01", c3.readScreen());
}

}