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
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,15 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.serenitydojo.Exceptions;

public class IllegalMathsOperatorException extends RuntimeException {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice use of a custom exception type.

public IllegalMathsOperatorException(String message) {
super(message);
}
}
38 changes: 37 additions & 1 deletion src/main/java/com/serenitydojo/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
package com.serenitydojo.calculator;

import com.serenitydojo.Exceptions.IllegalMathsOperatorException;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Calculator {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice job!

public int evaluate(String expression) {
return 0;
List<String> splittedExpression = Arrays.stream(expression.split(" ")).collect(Collectors.toList());
List<String> operands = List.of("1","2","3","4","5","6","7","8","9","0");

int expressionTotal =0;
String operand ="+";

if (expression.isEmpty()) {
return 0;
}
for(String character : splittedExpression) {
if (!operands.contains(character)) {
operand = character;
} else {
expressionTotal = calculateOperation(expressionTotal, operand, character);
}
}
return expressionTotal;
}

private int calculateOperation(int total, String operand, String token) {
switch (operand) {
case "+":
return total + Integer.parseInt(token);
case "-":
return total - Integer.parseInt(token);
case "*":
return total * Integer.parseInt(token);
default:
throw new IllegalMathsOperatorException(operand + "Does not exist");
}
}
}

69 changes: 35 additions & 34 deletions src/test/java/com/serenitydojo/calculator/WhenDoingMaths.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.serenitydojo.calculator;

import com.serenitydojo.Exceptions.IllegalMathsOperatorException;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

Expand All @@ -13,38 +14,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() {
calculator.evaluate("1 ^ 2");
}

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

@Test
public void shouldAddTwoNumbers() throws IllegalMathsOperatorException {
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);
}
}