change operation constants to strings

This commit is contained in:
tibbi 2015-12-30 15:35:50 +01:00
parent 920b8f87a6
commit 1969300620
3 changed files with 23 additions and 23 deletions

View File

@ -6,8 +6,8 @@ public class CalculatorImpl {
private double baseValue; private double baseValue;
private double secondValue; private double secondValue;
private boolean resetValue; private boolean resetValue;
private int lastKey; private String lastKey;
private int lastOperation; private String lastOperation;
private Calculator callback; private Calculator callback;
public CalculatorImpl(Calculator calculatorInterface) { public CalculatorImpl(Calculator calculatorInterface) {
@ -26,12 +26,12 @@ public class CalculatorImpl {
baseValue = 0; baseValue = 0;
secondValue = 0; secondValue = 0;
resetValue = false; resetValue = false;
lastKey = 0; lastKey = "";
lastOperation = 0; lastOperation = "";
callback.setValue("0"); callback.setValue("0");
} }
public void setLastKey(int lastKey) { public void setLastKey(String lastKey) {
this.lastKey = lastKey; this.lastKey = lastKey;
} }
@ -116,18 +116,18 @@ public class CalculatorImpl {
updateResult(resultValue); updateResult(resultValue);
} }
public void handleOperation(int operation) { public void handleOperation(String operation) {
if (lastKey == operation) if (lastKey.equals(operation))
return; return;
if (lastKey == Constants.DIGIT) if (lastKey.equals(Constants.DIGIT))
handleResult(); handleResult();
resetValue = true; resetValue = true;
lastKey = operation; lastKey = operation;
lastOperation = operation; lastOperation = operation;
if (operation == Constants.ROOT) if (operation.equals(Constants.ROOT))
calculateResult(); calculateResult();
} }
@ -156,10 +156,10 @@ public class CalculatorImpl {
} }
public void handleEquals() { public void handleEquals() {
if (lastKey == Constants.EQUALS) if (lastKey.equals(Constants.EQUALS))
calculateResult(); calculateResult();
if (lastKey != Constants.DIGIT) if (!lastKey.equals(Constants.DIGIT))
return; return;
secondValue = getDisplayedNumberAsDouble(); secondValue = getDisplayedNumberAsDouble();
@ -182,7 +182,7 @@ public class CalculatorImpl {
} }
public void numpadClicked(View view) { public void numpadClicked(View view) {
if (lastKey == Constants.EQUALS) if (lastKey.equals(Constants.EQUALS))
lastOperation = Constants.EQUALS; lastOperation = Constants.EQUALS;
lastKey = Constants.DIGIT; lastKey = Constants.DIGIT;
resetValueIfNeeded(); resetValueIfNeeded();

View File

@ -1,13 +1,13 @@
package calculator.simplemobiletools.com.simple_calculator; package calculator.simplemobiletools.com.simple_calculator;
public class Constants { public class Constants {
public static final int DIGIT = 0; public static final String DIGIT = "digit";
public static final int EQUALS = 1; public static final String EQUALS = "equals";
public static final int PLUS = 2; public static final String PLUS = "plus";
public static final int MINUS = 3; public static final String MINUS = "minus";
public static final int MULTIPLY = 4; public static final String MULTIPLY = "multiply";
public static final int DIVIDE = 5; public static final String DIVIDE = "divide";
public static final int MODULO = 6; public static final String MODULO = "modulo";
public static final int POWER = 7; public static final String POWER = "power";
public static final int ROOT = 8; public static final String ROOT = "root";
} }

View File

@ -148,11 +148,11 @@ public class MainActivityTest {
activity.setValueDouble(d); activity.setValueDouble(d);
} }
private void handleOperation(int operation) { private void handleOperation(String operation) {
activity.getCalc().handleOperation(operation); activity.getCalc().handleOperation(operation);
} }
private String calcResult(double baseValue, int operation, double secondValue) { private String calcResult(double baseValue, String operation, double secondValue) {
setDouble(baseValue); setDouble(baseValue);
handleOperation(operation); handleOperation(operation);
setDouble(secondValue); setDouble(secondValue);