001: package examples.Calculator;
002:
003: import java.beans.*;
004: import java.math.*;
005:
006: public class CalculatorEngine {
007: public static final String DISPLAY_TEXT_PROPERTY = "displayText";
008:
009: public static final int ADD = 0;
010: public static final int SUBTRACT = 1;
011: public static final int MULTIPLY = 2;
012: public static final int DIVIDE = 3;
013: public static final int RESULT = 4;
014:
015: private int operation = -1;
016: private boolean clear = true; // true to clear on next key
017: private String displayText = "0";
018: private BigDecimal value;
019: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
020: this );
021:
022: public String getDisplayText() {
023: return displayText;
024: }
025:
026: public void setDisplayText(String displayText) {
027: String oldDisplayText = this .displayText;
028: this .displayText = displayText;
029: firePropertyChange(DISPLAY_TEXT_PROPERTY, oldDisplayText,
030: displayText);
031: }
032:
033: public void clear() {
034: clearEntry();
035: value = new BigDecimal(0);
036: operation = -1;
037: }
038:
039: public void clearEntry() {
040: setDisplayText("0");
041: clear = true;
042: }
043:
044: private void checkClear() {
045: if (clear) {
046: setDisplayText("");
047: clear = false;
048: }
049: }
050:
051: public void digit(int digit) {
052: checkClear();
053: setDisplayText(getDisplayText() + String.valueOf(digit));
054: }
055:
056: public void dot() {
057: checkClear();
058: if (getDisplayText().indexOf('.') == -1) {
059: if (getDisplayText().length() == 0)
060: setDisplayText("0.");
061: else
062: setDisplayText(getDisplayText() + '.');
063: }
064: }
065:
066: public void toggleSign() {
067: String text = getDisplayText();
068: if (text.startsWith("-"))
069: text = text.substring(1);
070: else if (!text.equals("0"))
071: text = '-' + text;
072: setDisplayText(text);
073: }
074:
075: public void equal() {
076: BigDecimal displayValue = new BigDecimal(getDisplayText());
077: BigDecimal newValue = displayValue;
078: switch (operation) {
079: case ADD:
080: newValue = value.add(displayValue);
081: break;
082: case SUBTRACT:
083: newValue = value.subtract(displayValue);
084: break;
085: case MULTIPLY:
086: newValue = value.multiply(displayValue);
087: break;
088: case DIVIDE:
089: newValue = value.divide(displayValue, 8,
090: BigDecimal.ROUND_HALF_UP);
091: break;
092: }
093: value = newValue;
094: setDisplayText(toString(newValue));
095: clear = true;
096: operation = -1;
097: }
098:
099: public static String toString(BigDecimal decimal) {
100: // can't use stripTrailingZeros, as it wasn't introduced until 1.5
101: String result = decimal.toString();
102: if (result.indexOf(".") != -1) {
103: while (result.endsWith("0"))
104: result = result.substring(0, result.length() - 1);
105: if (result.endsWith("."))
106: result = result.substring(0, result.length() - 1);
107: }
108: return result;
109: }
110:
111: public void operation(int operation) {
112: if (this .operation != -1)
113: equal();
114: else {
115: value = new BigDecimal(getDisplayText());
116: clear = true;
117: }
118: this .operation = operation;
119: }
120:
121: public void add() {
122: operation(ADD);
123: }
124:
125: public void subtract() {
126: operation(SUBTRACT);
127: }
128:
129: public void multiply() {
130: operation(MULTIPLY);
131: }
132:
133: public void divide() {
134: operation(DIVIDE);
135: }
136:
137: public void addPropertyChangeListener(
138: PropertyChangeListener listener) {
139: propertyChangeSupport.addPropertyChangeListener(listener);
140: }
141:
142: public void addPropertyChangeListener(String property,
143: PropertyChangeListener listener) {
144: propertyChangeSupport.addPropertyChangeListener(property,
145: listener);
146: }
147:
148: public void removePropertyChangeListener(
149: PropertyChangeListener listener) {
150: propertyChangeSupport.removePropertyChangeListener(listener);
151: }
152:
153: public void removePropertyChangeListener(String property,
154: PropertyChangeListener listener) {
155: propertyChangeSupport.removePropertyChangeListener(property,
156: listener);
157: }
158:
159: protected void firePropertyChange(String property, Object oldValue,
160: Object newValue) {
161: propertyChangeSupport.firePropertyChange(property, oldValue,
162: newValue);
163: }
164: }
|