CalculatorAgent extends AbstractAgent { @Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); Expression exp = null; try { exp = new ExpressionBuilder(request.getText()).build(); } catch (IllegalArgumentException e) { logger.debug("CalculatorAgent could not parse " + request.getText()); return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } double result = 0; try { result = exp.evaluate(); } catch (IllegalArgumentException e) { logger.debug("CalculatorAgent could not parse " + request.getText()); return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } String answer; if (result % 1 == 0) { answer = String.valueOf((int)result); } else { answer = String.valueOf(result); } return new AgentResponse(answer); } CalculatorAgent(); @Override AgentResponse process(AgentRequest request);  }
@Test public void testSimpleAddition() { Agent agent = new CalculatorAgent(); AgentResponse response = agent.process(new AgentRequest("56 + 89")); assertEquals("145", response.getText()); }
@Test public void testBadExpressions() { Agent agent = new CalculatorAgent(); AgentResponse response = agent.process(new AgentRequest("2 + ")); assertFalse(response.isSuccess()); response = agent.process(new AgentRequest("two plus two")); assertFalse(response.isSuccess()); }