I've made a custom dialog that is a simple calculator for addition, subtraction, multiplication and division. I'm having trouble getting the logic of my code correct. Does anyone have any word of wisdom or know of resource I could check out that could help.
Below is the code for my custom dialog and the calculator Logic that i'm working on. As it is all the calculations work fine except for when i use the equals button. Calculation I do after that point don't come out right.
Thanks!
Calculator http://img546.imageshack.us/img546/9534/devicee.png
public class CustomCalcDialog extends Dialog {
public CustomCalcDialog(Context context) {
super(context);
}
EditText calcDialogDisplay;
TextView enterTotal;
TextView allClear;
TextView seven;
TextView eight;
TextView nine;
TextView division;
TextView four;
TextView five;
TextView six;
TextView multiply;
TextView one;
TextView two;
TextView three;
TextView subtract;
TextView decimal;
TextView zero;
TextView equals;
TextView addition;
ArrayList<Float> mathVariables = new ArrayList<Float>();
float mathVariable1;
float mathVariable2;
int currentOperation = 0;
int nextOperation;
final static int ADD = 1;
final static int SUBTRACT = 2;
final static int MULTIPLY = 3;
final static int DIVISION = 4;
final static int EQUALS = 5;
final static int CLEAR = 1;
final static int DONT_CLEAR = 0;
int clearCalcDisplay = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator_dialog);
calcDialogDisplay = (EditText) findViewById(R.id.calc_dialog_display);
enterTotal = (TextView) findViewById(R.id.enter_total);
allClear = (TextView) findViewById(R.id.all_clear);
seven = (TextView) findViewById(R.id.seven);
eight = (TextView) findViewById(R.id.eight);
nine = (TextView) findViewById(R.id.nine);
division =(TextView) findViewById(R.id.division);
four = (TextView) findViewById(R.id.four);
five = (TextView) findViewById(R.id.five);
six =(TextView) findViewById(R.id.six);
multiply = (TextView) findViewById(R.id.multiply);
one = (TextView) findViewById(R.id.one);
two = (TextView) findViewById(R.id.two);
three = (TextView) findViewById(R.id.three);
subtract = (TextView) findViewById(R.id.subtract);
decimal = (TextView) findViewById(R.id.decimal);
zero = (TextView) findViewById(R.id.zero);
equals = (TextView) findViewById(R.id.equals);
addition = (TextView) findViewById(R.id.addition);
calcDialogDisplay.setKeyListener(DigitsKeyListener.getInstance(true,true));
registerListeners();
}
public void registerListeners () {
enterTotal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activeTextView.setText(calcDialogDisplay.getText().toString());
mathCalculations();
CustomCalcDialog.this.dismiss();
}
});
allClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcDialogDisplay.setText("");
mathVariable1 = 0;
mathVariable2 = 0;
mathVariables.removeAll(mathVariables);
currentOperation = 0;
nextOperation = 0;
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("9");
}
});
division.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(DIVISION);
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("6");
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(MULTIPLY);
}
});
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("3");
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(SUBTRACT);
}
});
decimal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append(".");
}
});
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearCalcDisplay == CLEAR) {
calcDialogDisplay.setText("");
}
clearCalcDisplay = DONT_CLEAR;
calcDialogDisplay.append("0");
}
});
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(EQUALS);
}
});
addition.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(ADD);
}
});
}
private void calcLogic(int operator) {
mathVariables.add(Float.parseFloat(calcDialogDisplay.getText().toString()));
if (operator != EQUALS) {
nextOperation = operator;
}else if (operator == EQUALS){
nextOperation = 0;
}
switch (currentOperation) {
case ADD:
mathVariable1 = mathVariables.get(0);
mathVariable2 = mathVariables.get(1);
mathVariables.removeAll(mathVariables);
mathVariables.add(mathVariable1 + mathVariable2);
calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0)));
break;
case SUBTRACT:
mathVariable1 = mathVariables.get(0);
mathVariable2 = mathVariables.get(1);
mathVariables.removeAll(mathVariables);
mathVariables.add(mathVariable1 - mathVariable2);
calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0)));
break;
case MULTIPLY:
mathVariable1 = mathVariables.get(0);
mathVariable2 = mathVariables.get(1);
mathVariables.removeAll(mathVariables);
mathVariables.add(mathVariable1 * mathVariable2);
calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0)));
break;
case DIVISION:
mathVariable1 = mathVariables.get(0);
mathVariable2 = mathVariables.get(1);
mathVariables.removeAll(mathVariables);
mathVariables.add(mathVariable1 / mathVariable2);
calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0)));
break;
}
clearCalcDisplay = CLEAR;
currentOperation = nextOperation;
if (operator == EQUALS) {
mathVariable1 = 0;
mathVariable2 = 0;
mathVariables.removeAll(mathVariables);
}
}
}
Consider separating your app into two parts. The gui gathers the input and selects the action. Now create another class to do the calculations, say Model.java. This class should provide methods that take parameters and provide a return value. Test these isolated methods for accuracy and invalid input, out of range values and senseless input. Design the exceptions for your model to throw. Now connect the GUI to the Model.