diff --git a/Java Classes & Interfaces/New Calc Engine/src/DynamicHelper.java b/Java Classes & Interfaces/New Calc Engine/src/DynamicHelper.java new file mode 100644 index 0000000..b6b3791 --- /dev/null +++ b/Java Classes & Interfaces/New Calc Engine/src/DynamicHelper.java @@ -0,0 +1,29 @@ +public class DynamicHelper { + + private final MathProcessing[] handlers; + + public DynamicHelper(MathProcessing[] handlers) { + this.handlers = handlers; + } + + public void process(String statement) { + String[] parts = statement.split(MathProcessing.SEPARATOR); + String keyword = parts[0]; + double leftVal = Double.parseDouble(parts[1]); + double rightVal = Double.parseDouble(parts[2]); + + MathProcessing theHandler = null; + for(MathProcessing handler : handlers) { + if(keyword.equalsIgnoreCase(handler.getKeyWord())) { + theHandler = handler; + break; + } + } + + double result = theHandler.doCalculation(leftVal, rightVal); + if (theHandler.doFormatting() != null) + System.out.println(theHandler.doFormatting()); + else + System.out.println("result = " + result); + } +}