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
7 changes: 5 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 @@ -16,7 +16,10 @@ public void pressDigitKey(int digit) {
if(digit > 9 || digit < 0) throw new IllegalArgumentException();

if(latestOperation.isEmpty()) {
screen = screen + digit;
if (screen == "0")
screen = Integer.toString(digit);
else screen = screen + digit;

} else {
latestValue = Double.parseDouble(screen);
screen = Integer.toString(digit);
Expand Down
34 changes: 34 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,38 @@ void calculatorCanDoTwoPlusTwo() {
calc.pressEquals();
assertEquals("4", calc.readScreen());
}

@Test
@DisplayName("should display result 0 after pressing the clear key")
void calculatorCanDoClear() {
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressOperationKey("x");
calc.pressDigitKey(5);
calc.pressEquals();
calc.pressClearKey();
assertEquals("0", calc.readScreen());
}

@Test
@DisplayName("should display result after pressing a dezimal number")
void calculatorCanDoDotKey() {
Calculator calc = new Calculator();
calc.pressDigitKey(0);
calc.pressDotKey();
calc.pressDigitKey(1);
assertEquals("0.1", calc.readScreen());
}

@Test
@DisplayName("should display result after pressing a negative number")
void calculatorCanDoNegative() {
Calculator calc = new Calculator();
calc.pressNegative();
calc.pressDigitKey(5);
assertEquals("-5", calc.readScreen());
}



}