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
23 changes: 17 additions & 6 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 @@ -12,25 +12,31 @@ public class Calculator {
public String readScreen() {
return screen;
}

public void pressDigitKey(int digit) {
if(digit > 9 || digit < 0) throw new IllegalArgumentException();

if(latestOperation.isEmpty()) {
screen = screen + digit;
screen += digit;
} else if(screen.contains(".")) {
screen += Integer.toString(digit);
System.out.println(latestValue);
} else {
latestValue = Double.parseDouble(screen);
screen = Integer.toString(digit);
System.out.println(latestValue);
}
}

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

}

public void pressOperationKey(String operation) {
latestOperation = operation;
latestValue = Double.parseDouble(screen);
screen = "";
}

public void pressDotKey() {
Expand All @@ -42,14 +48,19 @@ public void pressNegative() {
}

public void pressEquals() {
System.out.println(screen);
System.out.println(latestValue +", screen: " +screen + "operation: " + (latestValue * Double.parseDouble(screen)));
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);

screen = Double.toString(Math.round( 100.0 * result) / 100.0);
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
latestOperation = "";

}
}
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 @@ -6,6 +6,7 @@

@DisplayName("Retro calculator")
class CalculatorTest {

@Test
@DisplayName("should display result after adding two positive numbers")
void calculatorCanDoTwoPlusTwo() {
Expand All @@ -16,4 +17,41 @@ void calculatorCanDoTwoPlusTwo() {
calc.pressEquals();
assertEquals("4", calc.readScreen());
}

@Test
@DisplayName("should display result after subtracting two positiv digits" )
void calculatorCanDoTwoMinusTwo() {
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressOperationKey("-");
calc.pressDigitKey(2);
calc.pressEquals();
assertEquals("0", calc.readScreen());
}

@Test
@DisplayName("should display a double digit")
void calculatorCanDisplaySomething() {
Calculator calc = new Calculator();
calc.pressDigitKey(3);
assertEquals("3", calc.readScreen());
calc.pressClearKey();
assertEquals("0", calc.readScreen());
}

@Test
@DisplayName("should display a double digit")
void calculatorCanDisplayDigit() {
Calculator calc = new Calculator();
calc.pressDigitKey(3);
calc.pressDotKey();
calc.pressDigitKey(3);
assertEquals("3.3", calc.readScreen());
calc.pressOperationKey("x");
calc.pressDigitKey(3);
calc.pressDotKey();
calc.pressDigitKey(3);
calc.pressEquals();
assertEquals("10.89", calc.readScreen());
}
}