FileDocCategorySizeDatePackage
CalcBean.javaAPI DocExample7831Thu Jun 28 16:14:16 BST 2001com.ora.jsp.beans.calc

CalcBean

public class CalcBean extends Object implements Serializable
This class implements a simple calculator for a JSP page. It's a stateless bean, so all state must be carried by hidden fields in the JSP page, and set before the first getter method is called. In order to set all properties with the <jsp:setProperty name="foo" property="*" /> syntax (i.e. no control over the order of setter calls), all processing to figure out the new values of all properties is performed in the getCurrentNumber() method.

Basically, the bean accumulates digits and the decimal dot in currentNumber until an operation is requested. The accumulated number is then saved as previousNumber, and the next number starts to accumulate in currentNumber. When an operation is requested the second time, it's carried out based on the previousNumber and currentNumber, and the result becomes the new previousNumber.

Note! This bean is just used as an example of how to use a JSP error page to handle exceptions. Even though it appear to work as a typical calculator, it's not been heavily tested so I make no guarantees that it handles error cases gracefully. Besides, I'm not so sure a web based calculator is such a good idea. An applet would probably be a better choice.

author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private static final int
NO_OPER
private static final int
ADD_OPER
private static final int
SUB_OPER
private static final int
DIV_OPER
private static final int
MULT_OPER
private String
newDigit
private String
currentNumber
private String
previousNumber
private int
newOper
private int
currentOperation
private boolean
reset
private boolean
operClicked
private boolean
clearClicked
private boolean
dotClicked
Constructors Summary
Methods Summary
private voidcalcNewNumbers()
Processes the current input, i.e. adds digits, performs the requested operation, and saves the accumulated the number, as described in the class comment.

        if (dotClicked) {
            if (currentNumber.length() == 0) {
                currentNumber = "0.";
            }
            else if (currentNumber.indexOf(".") == -1) {
                currentNumber += ".";
            }
            dotClicked = false;
            reset = false;
        }
        else if (operClicked) {
            if (previousNumber.length() > 0 && currentNumber.length() > 0) {
                BigDecimal firstNumber = null;
                if (previousNumber.indexOf(".") == -1) {
                    // Make it decimal to get real division
                    firstNumber = new BigDecimal(previousNumber + ".0");
                }
                else {
                    firstNumber = new BigDecimal(previousNumber);
                }
                BigDecimal secondNumber = new BigDecimal(currentNumber);
                BigDecimal result = null;
                switch (currentOperation) {
                    case ADD_OPER:
                        result = firstNumber.add(secondNumber);
                        break;
                    case SUB_OPER:
                        result = firstNumber.subtract(secondNumber);
                        break;
                    case DIV_OPER:
                        result = firstNumber.divide(secondNumber, 
                            BigDecimal.ROUND_HALF_UP);
                        break;
                    case MULT_OPER:
                        result = firstNumber.multiply(secondNumber);
                        break;
                }
                if (result != null) {
                    currentNumber = result.toString();
                }
            }
            previousNumber = currentNumber;
            currentOperation = newOper;
            reset = true;
        }
        else if (clearClicked) {
            currentNumber = "";
            previousNumber = currentNumber;
            currentOperation = NO_OPER;
        }
        else if (reset) {
            currentNumber = "";
            reset = false;
        }
    
public java.lang.StringgetCurrentNumber()
Returns the current number resulting from applying the current user input (new digit, dot or operation).

        calcNewNumbers();
        return currentNumber + newDigit;
    
public java.lang.StringgetCurrentOperation()
Returns the current operation.

        return toOperName(currentOperation);
    
public java.lang.StringgetPreviousNumber()
Returns the previous number.

        return previousNumber;
    
public booleangetReset()
Returns the "reset flag".

        return reset;
    
public voidsetClear(java.lang.String value)
Sets the "cleared" flag, indicating that the user clicked Clear.

        clearClicked = true;
    
public voidsetCurrentNumber(java.lang.String value)
Sets the current number, i.e. the number as it looks before applying the latest digit, dot, etc.

        currentNumber = value;
    
public voidsetCurrentOperation(java.lang.String value)
Sets the current operation, i.e. the operation that will be applied the next time an operation is requested.

        currentOperation = toOperValue(value);
    
public voidsetDigit(java.lang.String value)
Sets the new digit entered by the user.


                 
        
        newDigit = value.trim();
    
public voidsetDot(java.lang.String value)
Sets the decimal separator entered by the user.

        dotClicked = true;
    
public voidsetOper(java.lang.String value)
Sets the operation (+, -, *, / or =) entered by the user.

        newOper = toOperValue(value.trim());
        operClicked = true;
    
public voidsetPreviousNumber(java.lang.String value)
Sets the previous number, i.e. the number accumulated until the an operation was requested.

        previousNumber = value;
    
public voidsetReset(boolean value)
Sets the "reset flag" to clear the current number on the next submit (e.g. after performing an operation).

        reset =  value;
    
private java.lang.StringtoOperName(int value)
Converts an operation int value to the corresponding String representation.

        String name = "";
        switch (value) {
            case ADD_OPER:
                name = "+";
                break;
            case SUB_OPER:
                name = "-";
                break;
            case DIV_OPER:
                name = "/";
                break;
            case MULT_OPER:
                name = "*";
        }
        return name;
    
private inttoOperValue(java.lang.String name)
Converts an operation String value to the corresponding int representation.

        int value = NO_OPER;
        if ("+".equals(name)) {
            value = ADD_OPER;
        }
        else if ("-".equals(name)) {
            value = SUB_OPER;
        }
        else if ("/".equals(name)) {
            value = DIV_OPER;
        }
        else if ("*".equals(name)) {
            value = MULT_OPER;
        }
        return value;