minor refactoring, no functionality change

This commit is contained in:
tibbi 2015-12-24 23:07:50 +01:00
parent 637c47f5b5
commit c6aae26f94
1 changed files with 14 additions and 28 deletions

View File

@ -59,31 +59,12 @@ public class MainActivity extends AppCompatActivity {
resetValue = false; resetValue = false;
} }
private void addNumbers() {
final double resultValue = baseValue + secondValue;
result.setText(Formatter.doubleToString(resultValue));
baseValue = resultValue;
}
private void subtractNumbers() {
final double resultValue = baseValue - secondValue;
result.setText(Formatter.doubleToString(resultValue));
baseValue = resultValue;
}
private void multiplyNumbers() {
final double resultValue = baseValue * secondValue;
result.setText(Formatter.doubleToString(resultValue));
baseValue = resultValue;
}
private void divideNumbers() { private void divideNumbers() {
double resultValue = 0; double resultValue = 0;
if (secondValue != 0) if (secondValue != 0)
resultValue = baseValue / secondValue; resultValue = baseValue / secondValue;
result.setText(Formatter.doubleToString(resultValue)); updateResult(resultValue);
baseValue = resultValue;
} }
private void handleOperation(int operation) { private void handleOperation(int operation) {
@ -92,7 +73,7 @@ public class MainActivity extends AppCompatActivity {
if (lastKey == DIGIT) { if (lastKey == DIGIT) {
secondValue = getDisplayedNumberAsDouble(); secondValue = getDisplayedNumberAsDouble();
handleEquals(); calculateResult();
baseValue = getDisplayedNumberAsDouble(); baseValue = getDisplayedNumberAsDouble();
} }
resetValue = true; resetValue = true;
@ -123,7 +104,7 @@ public class MainActivity extends AppCompatActivity {
@OnClick(R.id.btn_equals) @OnClick(R.id.btn_equals)
public void equalsClicked() { public void equalsClicked() {
if (lastKey == EQUALS) { if (lastKey == EQUALS) {
handleEquals(); calculateResult();
return; return;
} }
@ -131,7 +112,7 @@ public class MainActivity extends AppCompatActivity {
return; return;
secondValue = getDisplayedNumberAsDouble(); secondValue = getDisplayedNumberAsDouble();
handleEquals(); calculateResult();
lastKey = EQUALS; lastKey = EQUALS;
} }
@ -144,21 +125,26 @@ public class MainActivity extends AppCompatActivity {
public void zeroClicked() { public void zeroClicked() {
String value = getDisplayedNumber(); String value = getDisplayedNumber();
if (!value.isEmpty() && !value.equals("0")) if (!value.equals("0"))
value += "0"; value += "0";
result.setText(value); result.setText(value);
} }
private void handleEquals() { private void updateResult(double value) {
result.setText(Formatter.doubleToString(value));
baseValue = value;
}
private void calculateResult() {
switch (lastOperation) { switch (lastOperation) {
case PLUS: case PLUS:
addNumbers(); updateResult(baseValue + secondValue);
break; break;
case MINUS: case MINUS:
subtractNumbers(); updateResult(baseValue - secondValue);
break; break;
case MULTIPLY: case MULTIPLY:
multiplyNumbers(); updateResult(baseValue * secondValue);
break; break;
case DIVIDE: case DIVIDE:
divideNumbers(); divideNumbers();