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
5 changes: 4 additions & 1 deletion src/main/java/tdd/setup/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public String readScreen() {
public void pressDigitKey(int digit) {
if(digit > 9 || digit < 0) throw new IllegalArgumentException();

if(latestOperation.isEmpty()) {
if(latestOperation.isEmpty() || screen.substring(screen.length()-1).equals(".")) {
screen = screen + digit;
} else {
latestValue = Double.parseDouble(screen);
Expand All @@ -30,6 +30,9 @@ public void pressClearKey() {
}

public void pressOperationKey(String operation) {
if (!this.latestOperation.isEmpty()) {
pressEquals() ;
}
latestOperation = operation;
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/tdd/setup/CalculatorRoundingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tdd.setup;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

@DisplayName("Retro calculator")
class CalculatorRoundingTest {
@Test
@DisplayName("should display a rounded number")
void calculatorCanDoTwoPlusTwo() {
Calculator calc = new Calculator();
calc.pressDigitKey(1);
calc.pressDotKey();
calc.pressDigitKey(5);
calc.pressOperationKey("+");
calc.pressDigitKey(1);
calc.pressDotKey();
calc.pressDigitKey(5);
calc.pressEquals();
assertEquals("3", calc.readScreen());
}
}
22 changes: 22 additions & 0 deletions src/test/java/tdd/setup/CalculatorTest2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tdd.setup;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

@DisplayName("Retro calculator2")
class CalculatorTest2 {
@Test
@DisplayName("should display negative result after adding two numbers")
void calculatorCanDosixPlusTen() {
Calculator calc = new Calculator();
calc.pressDigitKey(6);
calc.pressOperationKey("-");
calc.pressDigitKey(9);
calc.pressEquals();
assertEquals("-3", calc.readScreen());



}
}
21 changes: 21 additions & 0 deletions src/test/java/tdd/setup/MultipleOperationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tdd.setup;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

@DisplayName("Retro calculator")
class MultipleOperationTest {
@Test
@DisplayName("should display a result after multiple additions")
void calculatorCanDoMultipleAddition() {
Calculator calc = new Calculator();
calc.pressDigitKey(1);
calc.pressOperationKey("+");
calc.pressDigitKey(3);
calc.pressOperationKey("+");
calc.pressDigitKey(5);
calc.pressEquals();
assertEquals("9", calc.readScreen());
}
}