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
24 changes: 14 additions & 10 deletions src/main/java/tdd/setup/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package tdd.setup;
import java.util.Scanner;

// behaviour inspired by https://www.online-calculator.com/
public class Calculator {

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

private double latestValue;

private String latestOperation = "";

public String readScreen() {
public String readScreen()
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Das mit den geschweiften Klammern in einer neuen Zeile finde ich sehr gewöhnungsbedüftig und zählt nicht zu den Best Practices in Sachen Code Style

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

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

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

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



public void pressEquals() {
var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
Expand All @@ -53,3 +55,5 @@ public void pressEquals() {
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
}
}

//hallo
47 changes: 47 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,51 @@ void calculatorCanDoTwoPlusTwo() {
calc.pressEquals();
assertEquals("4", calc.readScreen());
}

//successful
@Test
@DisplayName("should display result after multiplying two positive numbers")
void calculatorCanDoTwoTimesFour()
{
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressOperationKey("x");
calc.pressDigitKey(4);
calc.pressEquals();
assertEquals("8", calc.readScreen());
System.out.println(calc.readScreen());
}



//fail
@Test
@DisplayName("should display result after addition with dot ")
void calculatorCanDoAdditionWithDot()
{
Calculator calc = new Calculator();

calc.pressDigitKey(5);
calc.pressDotKey();
calc.pressDigitKey(2);
calc.pressOperationKey("+");
calc.pressDigitKey(3);
calc.pressDotKey();
calc.pressDigitKey(5);
calc.pressEquals();
assertEquals("8.7", calc.readScreen());
System.out.println(calc.readScreen());
}

@Test
@DisplayName("should display number negative")
void calculatorCanDoNegative()
{
Calculator calc = new Calculator();
calc.pressDigitKey(3);
calc.pressNegative();
assertEquals("-3", calc.readScreen());
System.out.println(calc.readScreen());
}

}