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
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ dependencies {

// Use JUnit Jupiter Engine for testing.
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.2")

// https://mvnrepository.com/artifact/org.junit.platform/junit-platform-engine
testImplementation("org.junit.platform:junit-platform-engine:1.7.0")

}

application {
Expand Down
Binary file added out/production/main/tdd/setup/Calculator.class
Binary file not shown.
Binary file added out/test/test/tdd/setup/CalculatorTest.class
Binary file not shown.
13 changes: 12 additions & 1 deletion src/main/java/tdd/setup/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@ public class Calculator {

private double latestValue;

private String calculationOperation;

private String latestOperation = "";

public String readScreen() {
return screen;
}
public void pressDigitKey(int digit) {

if(digit > 9 || digit < 0) throw new IllegalArgumentException();

if (screen == "0") {

screen = "";
}


if(latestOperation.isEmpty()) {
screen = screen + digit;
} else {
latestValue = Double.parseDouble(screen);
calculationOperation = latestOperation;
latestOperation = "";
screen = Integer.toString(digit);
}
}
Expand All @@ -42,7 +53,7 @@ public void pressNegative() {
}

public void pressEquals() {
var result = switch(latestOperation) {
var result = switch(calculationOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "-" -> latestValue - Double.parseDouble(screen);
case "x" -> latestValue * Double.parseDouble(screen);
Expand Down
11 changes: 11 additions & 0 deletions src/main/main.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
39 changes: 38 additions & 1 deletion src/test/java/tdd/setup/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,41 @@ void calculatorCanDoTwoPlusTwo() {
calc.pressEquals();
assertEquals("4", calc.readScreen());
}
}

@Test
@DisplayName("should display 0 when clear button is used")
void calculatorShowsZeroAfterPressingClearKey() {
Calculator calc = new Calculator();
calc.pressDigitKey(3);
calc.pressOperationKey("/");
calc.pressDigitKey(4);
calc.pressClearKey();
assertEquals("0", calc.readScreen());

}

@Test
@DisplayName("should display result after multiplying double digit numbers")
void calculatorCanDoTenTimesTen() {
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressDigitKey(0);
calc.pressOperationKey("x");
calc.pressDigitKey(1);
calc.pressDigitKey(5);
calc.pressEquals();
assertEquals("300", calc.readScreen());

}

@Test
@DisplayName("should display decimal number without 0 in the beginning")
void calculatorCanDivide() {
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressDotKey();
calc.pressDigitKey(5);
assertEquals("2.5", calc.readScreen());

}
}
13 changes: 13 additions & 0 deletions src/test/test.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Gradle: org.junit.jupiter:junit-jupiter-api:5.6.2" level="project" />
<orderEntry type="module" module-name="main" scope="TEST" />
</component>
</module>