|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.ora.jsp.beans.calc.CalcBean
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.
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, meaning adding digits, performing the requested operation, and saving the accumulated the number, as described in the class comment. |
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, meaning the number as it looks before applying the latest digit, dot, etc. |
void |
setCurrentOperation(java.lang.String value)
Sets the current operation, meaning the operation that will be applied the next time an operation is requested. |
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, meaning the number accumulated until the an operation was requested. |
void |
setReset(boolean value)
Sets the "reset flag" to clear the current number on the next submit (for instance, after performing an operation). |
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, toString, wait, wait, wait |
Field Detail |
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 java.lang.String newDigit
private java.lang.String currentNumber
private java.lang.String previousNumber
private int newOper
private int currentOperation
private boolean reset
private boolean operClicked
private boolean clearClicked
private boolean dotClicked
Constructor Detail |
public CalcBean()
Method Detail |
public void setDigit(java.lang.String value)
public void setDot(java.lang.String value)
public void setOper(java.lang.String value)
public void setClear(java.lang.String value)
public void setCurrentNumber(java.lang.String value)
public java.lang.String getCurrentNumber()
public void setCurrentOperation(java.lang.String value)
public java.lang.String getCurrentOperation()
public void setPreviousNumber(java.lang.String value)
public java.lang.String getPreviousNumber()
public void setReset(boolean value)
public boolean getReset()
private void calcNewNumbers()
private java.lang.String toOperName(int value)
private int toOperValue(java.lang.String name)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |