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
12 changes: 12 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import tdd.setup.Calculator;

public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator(); // cmd+option und v
calculator.pressDigitKey(3);
calculator.pressOperationKey("+");
calculator.pressDigitKey(5);
calculator.pressEquals();
System.out.println(calculator.readScreen());
}
}
16 changes: 14 additions & 2 deletions src/main/java/tdd/setup/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public void pressDigitKey(int digit) {

if(latestOperation.isEmpty()) {
screen = screen + digit;
} else {
} else if (latestValue == 0.0){
latestValue = Double.parseDouble(screen);
screen = Integer.toString(digit);
}
else{
screen = screen + digit;
}
}

public void pressClearKey() {
Expand All @@ -30,7 +33,14 @@ public void pressClearKey() {
}

public void pressOperationKey(String operation) {
latestOperation = operation;
if (operation != "%") {
latestOperation = operation;
}
else {
var result = Double.parseDouble(screen) / 100.0;
screen = Double.toString(result);
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
}
}

public void pressDotKey() {
Expand All @@ -51,5 +61,7 @@ public void pressEquals() {
};
screen = Double.toString(result);
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.contains(".0E-")) screen = screen.replace(".0E","e");
if(screen.contains(".0E")) screen = screen.replace(".0E","e+");
}
}
60 changes: 60 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,64 @@ void calculatorCanDoTwoPlusTwo() {
calc.pressEquals();
assertEquals("4", calc.readScreen());
}
@Test
@DisplayName("should display zero after adding")
void calculatorCanDoTwoPlusTwoNewWithClearFunction() {
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressOperationKey("+");
calc.pressDigitKey(2);
calc.pressEquals();
calc.pressClearKey();
assertEquals("0", calc.readScreen());
}
@Test
@DisplayName("should display result of percentage")
void calculatorCanDoPercentageOperationKey() {
Calculator calc = new Calculator();
calc.pressDigitKey(8);
calc.pressOperationKey("%");
assertEquals("0.08", calc.readScreen());
}
@Test
@DisplayName("should display big number")
void calculatorCanDoCalculateBigNumber() {
Calculator calc = new Calculator();
calc.pressDigitKey(1);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressOperationKey("x");
calc.pressDigitKey(1);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressEquals();
assertEquals("1e+12", calc.readScreen());
}

@Test
@DisplayName("should display small number")
void calculatorCanDoCalculateSmallNumber() {
Calculator calc = new Calculator();
calc.pressDigitKey(1);
calc.pressOperationKey("/");
calc.pressDigitKey(1);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressDigitKey(0);
calc.pressEquals();
assertEquals("1e-7", calc.readScreen());
}
}
// andere Fälle testen