Helper Class that works with the Custom Interface
parent
2af4b69da3
commit
0931d91922
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue