diff --git a/pom.xml b/pom.xml index f4d0ad5..c24faeb 100644 --- a/pom.xml +++ b/pom.xml @@ -23,21 +23,21 @@ junit 4.13 test + + + org.assertj + assertj-core + 3.18.0 com.google.guava guava - 30.0-jre + 30.1-jre org.apache.commons commons-text - 1.8 - - - org.assertj - assertj-core - 3.18.0 + 1.9 diff --git a/src/main/java/com/serenitydojo/calculator/Calculator.java b/src/main/java/com/serenitydojo/calculator/Calculator.java index 8e88bf0..c37a46b 100644 --- a/src/main/java/com/serenitydojo/calculator/Calculator.java +++ b/src/main/java/com/serenitydojo/calculator/Calculator.java @@ -1,7 +1,45 @@ package com.serenitydojo.calculator; +import com.google.common.base.Splitter; + +import java.util.List; + +import static org.apache.commons.lang3.StringUtils.isNumeric; + public class Calculator { public int evaluate(String expression) { - return 0; + if (expression.isEmpty()) { + return 0; + } + List tokens = Splitter.on(" ").splitToList(expression); + + int runningTotal = 0; + String nextOperator = "+"; + + for(String token : tokens) { + if (!isNumeric(token)) { + nextOperator = token; + } else { + runningTotal = process(runningTotal, nextOperator, token); + } + } + + return runningTotal; + } + + private int process(int runningTotal, String nextOperator, String token) { + switch (nextOperator) { + case "+": + return runningTotal + Integer.parseInt(token); + case "-": + return runningTotal - Integer.parseInt(token); + case "*": + return runningTotal * Integer.parseInt(token); + case "/": + return runningTotal / Integer.parseInt(token); + default: + throw new IllegalMathsOperatorException("Unknown operator " + nextOperator); + } } + } diff --git a/src/main/java/com/serenitydojo/calculator/IllegalMathsOperatorException.java b/src/main/java/com/serenitydojo/calculator/IllegalMathsOperatorException.java new file mode 100644 index 0000000..bc31a0a --- /dev/null +++ b/src/main/java/com/serenitydojo/calculator/IllegalMathsOperatorException.java @@ -0,0 +1,8 @@ +package com.serenitydojo.calculator; + +public class IllegalMathsOperatorException extends RuntimeException { + public IllegalMathsOperatorException(String message) { + super(message); + } + +} diff --git a/src/test/java/com/serenitydojo/calculator/WhenDoingMaths.java b/src/test/java/com/serenitydojo/calculator/WhenDoingMaths.java index eac4b31..eeeadfb 100644 --- a/src/test/java/com/serenitydojo/calculator/WhenDoingMaths.java +++ b/src/test/java/com/serenitydojo/calculator/WhenDoingMaths.java @@ -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; @@ -13,38 +12,53 @@ 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() { + 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 + public void shouldDivideTwoNumbers(){ + assertThat(calculator.evaluate("10 / 2")).isEqualTo(5); + } + + @Test + public void shouldDivideAndAddOneNumber(){ + assertThat(calculator.evaluate("10 / 2 + 2")).isEqualTo(7); + } + + @Test + public void shouldDivideAndMultiplyOneNumber(){ + assertThat(calculator.evaluate("10 / 2 * 5")).isEqualTo(25); + } }