Methods 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.
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.String | getCurrentNumber()Returns the current number resulting from applying the
current user input (new digit, dot or operation).
calcNewNumbers();
return currentNumber + newDigit;
|
public java.lang.String | getCurrentOperation()Returns the current operation.
return toOperName(currentOperation);
|
public java.lang.String | getPreviousNumber()Returns the previous number.
return previousNumber;
|
public boolean | getReset()Returns the "reset flag".
return reset;
|
public void | setClear(java.lang.String value)Sets the "cleared" flag, indicating that the
user clicked Clear.
clearClicked = true;
|
public void | setCurrentNumber(java.lang.String value)Sets the current number, meaning the number as it
looks before applying the latest digit, dot, etc.
currentNumber = value;
|
public void | setCurrentOperation(java.lang.String value)Sets the current operation, meaning the operation that
will be applied the next time an operation is requested.
currentOperation = toOperValue(value);
|
public void | setDigit(java.lang.String value)Sets the new digit entered by the user.
newDigit = value.trim();
|
public void | setDot(java.lang.String value)Sets the decimal separator entered by the user.
dotClicked = true;
|
public void | setOper(java.lang.String value)Sets the operation (+, -, *, / or =) entered by the user.
newOper = toOperValue(value.trim());
operClicked = true;
|
public void | setPreviousNumber(java.lang.String value)Sets the previous number, meaning the number accumulated
until the an operation was requested.
previousNumber = value;
|
public void | setReset(boolean value)Sets the "reset flag" to clear the current number on
the next submit (for instance, after performing an operation).
reset = value;
|
private java.lang.String | toOperName(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 int | toOperValue(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;
|