com.ora.jsp.beans.calc
Class CalcBean

java.lang.Object
  |
  +--com.ora.jsp.beans.calc.CalcBean
All Implemented Interfaces:
java.io.Serializable

public class CalcBean
extends java.lang.Object
implements java.io.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.

Version:
1.0
Author:
Hans Bergsten, Gefion software
See Also:
Serialized Form

Field Summary
private static int ADD_OPER
           
private  boolean clearClicked
           
private  java.lang.String currentNumber
           
private  int currentOperation
           
private static int DIV_OPER
           
private  boolean dotClicked
           
private static int MULT_OPER
           
private  java.lang.String newDigit
           
private  int newOper
           
private static int NO_OPER
           
private  boolean operClicked
           
private  java.lang.String previousNumber
           
private  boolean reset
           
private static int SUB_OPER
           
 
Constructor Summary
CalcBean()
           
 
Method Summary
private  void calcNewNumbers()
          Processes the current input, i.e.
 java.lang.String getCurrentNumber()
          Returns the current number resulting from applying the current user input (new digit, dot or operation).
 java.lang.String getCurrentOperation()
          Returns the current operation.
 java.lang.String getPreviousNumber()
          Returns the previous number.
 boolean getReset()
          Returns the "reset flag".
 void setClear(java.lang.String value)
          Sets the "cleared" flag, indicating that the user clicked Clear.
 void setCurrentNumber(java.lang.String value)
          Sets the current number, i.e.
 void setCurrentOperation(java.lang.String value)
          Sets the current operation, i.e.
 void setDigit(java.lang.String value)
          Sets the new digit entered by the user.
 void setDot(java.lang.String value)
          Sets the decimal separator entered by the user.
 void setOper(java.lang.String value)
          Sets the operation (+, -, *, / or =) entered by the user.
 void setPreviousNumber(java.lang.String value)
          Sets the previous number, i.e.
 void setReset(boolean value)
          Sets the "reset flag" to clear the current number on the next submit (e.g.
private  java.lang.String toOperName(int value)
          Converts an operation int value to the corresponding String representation.
private  int toOperValue(java.lang.String name)
          Converts an operation String value to the corresponding int representation.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

NO_OPER

private static final int NO_OPER

ADD_OPER

private static final int ADD_OPER

SUB_OPER

private static final int SUB_OPER

DIV_OPER

private static final int DIV_OPER

MULT_OPER

private static final int MULT_OPER

newDigit

private java.lang.String newDigit

currentNumber

private java.lang.String currentNumber

previousNumber

private java.lang.String previousNumber

newOper

private int newOper

currentOperation

private int currentOperation

reset

private boolean reset

operClicked

private boolean operClicked

clearClicked

private boolean clearClicked

dotClicked

private boolean dotClicked
Constructor Detail

CalcBean

public CalcBean()
Method Detail

setDigit

public void setDigit(java.lang.String value)
Sets the new digit entered by the user.

setDot

public void setDot(java.lang.String value)
Sets the decimal separator entered by the user.

setOper

public void setOper(java.lang.String value)
Sets the operation (+, -, *, / or =) entered by the user.

setClear

public void setClear(java.lang.String value)
Sets the "cleared" flag, indicating that the user clicked Clear.

setCurrentNumber

public void setCurrentNumber(java.lang.String value)
Sets the current number, i.e. the number as it looks before applying the latest digit, dot, etc.

getCurrentNumber

public java.lang.String getCurrentNumber()
Returns the current number resulting from applying the current user input (new digit, dot or operation).

setCurrentOperation

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

getCurrentOperation

public java.lang.String getCurrentOperation()
Returns the current operation.

setPreviousNumber

public void setPreviousNumber(java.lang.String value)
Sets the previous number, i.e. the number accumulated until the an operation was requested.

getPreviousNumber

public java.lang.String getPreviousNumber()
Returns the previous number.

setReset

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

getReset

public boolean getReset()
Returns the "reset flag".

calcNewNumbers

private void calcNewNumbers()
Processes the current input, i.e. adds digits, performs the requested operation, and saves the accumulated the number, as described in the class comment.

toOperName

private java.lang.String toOperName(int value)
Converts an operation int value to the corresponding String representation.

toOperValue

private int toOperValue(java.lang.String name)
Converts an operation String value to the corresponding int representation.