1 3 package org.jmock.examples.calculator; 4 5 public class Calculator 6 { 7 private Parser parser; 8 private Environment environment; 9 10 public Calculator() { 11 environment = new SimpleEnvironment(); 12 parser = new InfixParser(); 13 } 14 15 public Calculator( Parser parser, Environment environment ) { 16 this.parser = parser; 17 this.environment = environment; 18 } 19 20 public double calculate( String expressionString ) throws CalculatorException { 21 Expression expression = parser.parse(expressionString); 22 return expression.evaluate(environment); 23 } 24 25 public void setVariable( String name, String valueString ) throws CalculatorException { 26 environment.setVariable(name, parser.parse(valueString)); 27 } 28 } 29 | Popular Tags |