Created
May 27, 2021 03:29
-
-
Save randycasburn/c58a598c3ab79da21cab0edaec7e94f0 to your computer and use it in GitHub Desktop.
SimpleEnumCalculator
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 com.company; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.function.BinaryOperator; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| public class Main { | |
| public static void main(String[] args) { | |
| final double out = SimpleMathEnum.calculate("20 * 30"); | |
| System.out.println(out); | |
| } | |
| } | |
| enum SimpleMathEnum { | |
| PLUS((a, b) -> a + b), | |
| MINUS((a, b) -> a - b), | |
| MULTIPLY((a, b) -> a * b), | |
| DIVIDE((a, b) -> a / b); | |
| final private BinaryOperator<Double> operation; | |
| final private static Map<Character, SimpleMathEnum> operations = | |
| new HashMap<Character, SimpleMathEnum>() {{ | |
| put('+', PLUS); | |
| put('-', MINUS); | |
| put('*', MULTIPLY); | |
| put('x', MULTIPLY); | |
| put('/', DIVIDE); | |
| }}; | |
| SimpleMathEnum(BinaryOperator<Double> operation) { | |
| this.operation = operation; | |
| } | |
| public static double calculate(String math) { | |
| final char op; | |
| final double a, b; | |
| final Pattern pattern = Pattern.compile("[+\\-*x/]"); | |
| final Matcher m = pattern.matcher(math); | |
| if (m.find()) { | |
| op = math.charAt(m.start()); | |
| String[] ab = math.split(pattern.pattern()); | |
| try { | |
| a = Double.parseDouble(ab[0].trim()); | |
| b = Double.parseDouble(ab[1].trim()); | |
| } catch (NumberFormatException e) { | |
| throw new SimpleMathEnumException(); | |
| } | |
| } else { | |
| throw new SimpleMathEnumException(); | |
| } | |
| if(op == '/' && b == 0.0) throw new SimpleMathEnumException(); | |
| if (!operations.containsKey(op)) throw new SimpleMathEnumException(); | |
| return operations.get(op).operate(a,b); | |
| } | |
| public double operate(double a, double b) { | |
| return operation.apply(a, b); | |
| } | |
| static class SimpleMathEnumException extends IllegalArgumentException { | |
| public SimpleMathEnumException(){ | |
| super("Must be in the form: Number operator Number where operator is one of: + - * x /"); | |
| } | |
| } | |
| } | |
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 com.company; | |
| import org.junit.Test; | |
| import static org.junit.Assert.*; | |
| public class MainTest { | |
| @Test | |
| public void multiply() { | |
| double actual = SimpleMathEnum.calculate("20.23 * 30.44"); | |
| assertEquals (615.8012, actual, 0.01); | |
| } | |
| @Test | |
| public void add() { | |
| double actual = SimpleMathEnum.calculate("20.25 + 30.75"); | |
| assertEquals(51,actual, 0.01); | |
| } | |
| @Test | |
| public void subtract() { | |
| double actual = SimpleMathEnum.calculate("20.25 - 10.5"); | |
| assertEquals (9.75, actual, 0.01); | |
| } | |
| @Test | |
| public void divide() { | |
| double actual = SimpleMathEnum.calculate("10 / 2"); | |
| assertEquals (5, actual, 0.01); | |
| } | |
| @Test(expected = SimpleMathEnum.SimpleMathEnumException.class) | |
| public void divideByZero() { | |
| SimpleMathEnum.calculate("asdf + lkh"); | |
| SimpleMathEnum.calculate("123.33 + lkh"); | |
| SimpleMathEnum.calculate("12 - qwer"); | |
| SimpleMathEnum.calculate("11 & 3"); | |
| SimpleMathEnum.calculate("asdf + lkh"); | |
| SimpleMathEnum.calculate("asdf + lkh"); | |
| SimpleMathEnum.calculate("10 / 0"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment