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
32 changes: 24 additions & 8 deletions src/main/java/tdd/setup/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,47 @@
// behaviour inspired by https://www.online-calculator.com/
public class Calculator {

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

private double latestValue;

private String latestOperation = "";

private boolean clearScreen = false;


public String readScreen() {
return screen;
}
public void pressDigitKey(int 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);
}
//new code, added new variable clear screen, this variable is set, when the
//operation button is pressed
if (clearScreen) {
screen = Integer.toString(digit);
clearScreen = false;
} else {
screen = screen + Integer.toString(digit);
}

if (latestOperation.isEmpty()){
latestValue = Double.parseDouble(screen);
}
}

public void pressClearKey() {
screen = "0";
latestOperation = "";
latestValue = 0.0;
clearScreen = false;
}

public void pressOperationKey(String operation) {
latestOperation = operation;
clearScreen = true;

}

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

// public void pressTwoOperationKeys

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);

default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
Expand Down
32 changes: 27 additions & 5 deletions src/test/java/tdd/setup/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,35 @@
@DisplayName("Retro calculator")
class CalculatorTest {
@Test
@DisplayName("should display result after adding two positive numbers")
void calculatorCanDoTwoPlusTwo() {
@DisplayName("should display results after 5 - 4")
void calculatorCanDo5Minus4(){
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressDigitKey(5);
calc.pressOperationKey("-");
calc.pressDigitKey(4);
calc.pressEquals();
assertEquals("1", calc.readScreen());
}
@Test
@DisplayName("should display results after press 8 ,., 5")
void calculatorCanDoDoubleNumber(){
Calculator calc = new Calculator();
calc.pressDigitKey(8);
calc.pressDotKey();
calc.pressDigitKey(5);
assertEquals("8.5", calc.readScreen());
}
@Test
@DisplayName("should display results after ")
void calculatorCanDoNumber(){
Calculator calc = new Calculator();
calc.pressDigitKey(5);
calc.pressOperationKey("+");
calc.pressDigitKey(2);
calc.pressDigitKey(1);
calc.pressDigitKey(5);
calc.pressEquals();
assertEquals("4", calc.readScreen());
assertEquals("20", calc.readScreen());
}


}