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
27 changes: 26 additions & 1 deletion src/main/java/com/serenitydojo/calculator/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

public class Calculator {
public int evaluate(String expression) {
return 0;

String operators[] = expression.replaceAll("\\s", "").split("[0-9]+");
String operands[] = expression.replaceAll("\\s", "").split("[*/+-]");

int runningTotal = Integer.parseInt(operands[0]);

for(int i=1; i<operands.length; i++) {
if(operators[i].equals("+")) {
runningTotal += Integer.parseInt(operands[i]);
}
else if(operators[i].equals("-")) {
runningTotal -= Integer.parseInt(operands[i]);
}
else if(operators[i].equals("*")) {
runningTotal *= Integer.parseInt(operands[i]);
}
else if(operators[i].equals("/")) {
runningTotal /= Integer.parseInt(operands[i]);
}
else
{
throw new IllegalMathsOperatorException("Unknown operator ");
}
}
return runningTotal;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.serenitydojo.calculator;

public class IllegalMathsOperatorException extends RuntimeException {
public IllegalMathsOperatorException(String message) {
super(message);
}
}
69 changes: 34 additions & 35 deletions src/test/java/com/serenitydojo/calculator/WhenDoingMaths.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.serenitydojo.calculator;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -13,38 +12,38 @@ public void shouldReturnZeroForAnEmptyString() {
assertThat(calculator.evaluate("")).isEqualTo(0);
}

// @Test(expected = IllegalMathsOperatorException.class)
// public void shouldReportNonSupportedOperations() {
// calculator.evaluate("1 ^ 2");
// }
//
// @Test
// public void shouldReturnTheValueOfASingleNumber() {
// assertThat(calculator.evaluate("1")).isEqualTo(1);
// }
//
// @Test
// public void shouldAddTwoNumbers() {
// assertThat(calculator.evaluate("1 + 1")).isEqualTo(2);
// }
//
// @Test
// public void shouldAddThreeNumbers() {
// assertThat(calculator.evaluate("1 + 2 + 3")).isEqualTo(6);
// }
//
// @Test
// public void shouldAlsoSubtract() {
// assertThat(calculator.evaluate("10 - 6")).isEqualTo(4);
// }
//
// @Test
// public void shouldAddAndSubtract() {
// assertThat(calculator.evaluate("10 + 5 - 6")).isEqualTo(9);
// }
//
// @Test
// public void shouldMultiplyNumbers() {
// assertThat(calculator.evaluate("10 * 5")).isEqualTo(50);
// }
@Test(expected = IllegalMathsOperatorException.class)
public void shouldReportNonSupportedOperations() throws IllegalMathsOperatorException {
calculator.evaluate("1 ^ 2");
}

@Test
public void shouldReturnTheValueOfASingleNumber() {
assertThat(calculator.evaluate("1")).isEqualTo(1);
}

@Test
public void shouldAddTwoNumbers() {
assertThat(calculator.evaluate("1 + 1")).isEqualTo(2);
}

@Test
public void shouldAddThreeNumbers() {
assertThat(calculator.evaluate("1 + 2 + 3")).isEqualTo(6);
}

@Test
public void shouldAlsoSubtract() {
assertThat(calculator.evaluate("10 - 6")).isEqualTo(4);
}

@Test
public void shouldAddAndSubtract() {
assertThat(calculator.evaluate("10 + 5 - 6")).isEqualTo(9);
}

@Test
public void shouldMultiplyNumbers() {
assertThat(calculator.evaluate("10 * 5")).isEqualTo(50);
}
}