com.ls.util.math
Class FormulaCalculator

java.lang.Object
  |
  +--com.ls.util.math.FormulaCalculator

public class FormulaCalculator
extends Object

This class performs calculation of a generic String expression (mathematic formula). The class is able to perform plus, subtract, multiply and divide. Considered rules: Multiplication and Division carried out before Addition and Subtraction. Brackets can be set and have a higher priority than the above rule. If necessary it takes the value of a formula-variable from a given Object Hashtable. If something goes wrong the class throws the FormulaCalculationException.

Version:
$Revision: 1.3 $
Author:
Last modified by $Author: MFehrenbach $

Constructor Summary
FormulaCalculator()
          Default Constructor..
FormulaCalculator(int scale)
          Constructor which sets the scale to which results are set.
 
Method Summary
 BigDecimal calculateEquation(String equation)
          Calculating the given equation (formula).
 BigDecimal calculateEquation(String equation, Hashtable values)
          Calculating the given equation (formula).
 int getScale()
          Returns the currently set scale.
static void main(String[] args)
          Only for testing purposes.
 void setScale(int scale)
          Set the scale to which results are set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FormulaCalculator

public FormulaCalculator(int scale)
Constructor which sets the scale to which results are set.
Parameters:
scale - - the scale to which results are set.

FormulaCalculator

public FormulaCalculator()
Default Constructor..
Method Detail

calculateEquation

public BigDecimal calculateEquation(String equation)
                             throws FormulaCalculationException
Calculating the given equation (formula). Variables are not allowed a part of the equation! If the equation is invalid or a variable is specified in the equation a FormulaCalculationException is thrown.
Parameters:
equation - - a String which represents the equation
Returns:
BigDecimal - the result of the calculation
Throws:
FormulaCalculationException - If something went wrong

calculateEquation

public BigDecimal calculateEquation(String equation,
                                    Hashtable values)
                             throws FormulaCalculationException
Calculating the given equation (formula). Variables which may part of the equation are substituted by a value of the give Hashtable where the key equals the variable. If the equation is invalid or a variable doesn't exist in the given hashtable a FormulaCalculationException is thrown.
Parameters:
equation - - a String which represents the equation
values - - a Hashtable which contains the needed values to substitute the variables in the equation.
Returns:
BigDecimal - the result of the calculation
Throws:
FormulaCalculationException - If something went wrong

setScale

public void setScale(int scale)
Set the scale to which results are set.
Parameters:
scale - - the scale to which results are set.

getScale

public int getScale()
Returns the currently set scale.
Returns:
int - the currently set scale.

main

public static void main(String[] args)
Only for testing purposes.
Parameters:
args - the scale to which results are set.