Created
February 12, 2019 23:45
-
-
Save cchacin/7a911a7f933adefacb2d3d7c21de752b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package customers; | |
import org.assertj.core.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import java.util.LinkedHashMap; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.BiFunction; | |
class ExpressionTest extends Assertions { | |
@Test | |
void test() throws Exception { | |
// Given | |
final Expression expression = Expression.of("5 + 5"); | |
// When | |
final double result = expression.evaluate(); | |
// Then | |
assertThat(result).isEqualTo(10); | |
} | |
} | |
class Expression { | |
private final List<BiFunction<Double, Double, Double>> operations; | |
Expression( | |
final List<BiFunction<Double, Double, Double>> operations) { | |
this.operations = operations; | |
} | |
static Expression of(final String exp) { | |
List<BiFunction<Double, Double, Double>> operations = new LinkedList<>(); | |
final String[] split = exp.split("\\s"); | |
final int length = split.length; | |
for (int i = 0; i <= length; i++) { | |
final String s = split[i]; | |
if (s.matches("^(?:(?:-)?\\d+(?:\\.\\d+)?)$")) { | |
operations.add((left, right) -> Double.parseDouble(s)); | |
} else { | |
final BiFunction<Double, Double, Double> operation = Expression.of(s.charAt(0)); | |
operations.add(operation); | |
} | |
} | |
return new Expression(operations); | |
} | |
public double evaluate() { | |
return 0; | |
} | |
public List<BiFunction<Double, Double, Double>> getOperations() { | |
return new LinkedList<>(operations); | |
} | |
static BiFunction<Double, Double, Double> of( | |
char symbol) { | |
final Map<Character, BiFunction<Double, Double, Double>> operations = | |
new LinkedHashMap<>(); | |
operations.put('+', (left, right) -> left + right); | |
operations.put('-', (left, right) -> left - right); | |
operations.put('*', (left, right) -> left * right); | |
operations.put('/', (left, right) -> left / right); | |
return operations.get(symbol); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment