implement division

This commit is contained in:
tibbi 2015-12-24 22:48:07 +01:00
parent 3be2aee4d3
commit 9b1c979be6
1 changed files with 34 additions and 9 deletions

View File

@ -23,6 +23,7 @@ public class MainActivity extends AppCompatActivity {
private static final int PLUS = 2;
private static final int MINUS = 3;
private static final int MULTIPLY = 4;
private static final int DIVIDE = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -76,6 +77,15 @@ public class MainActivity extends AppCompatActivity {
baseValue = resultValue;
}
private void divideNumbers() {
double resultValue = 0;
if (secondValue != 0)
resultValue = baseValue / secondValue;
result.setText(Formatter.doubleToString(resultValue));
baseValue = resultValue;
}
private void handleOperation(int operation) {
if (lastKey == operation)
return;
@ -105,6 +115,11 @@ public class MainActivity extends AppCompatActivity {
handleOperation(MULTIPLY);
}
@OnClick(R.id.btn_divide)
public void divideClicked() {
handleOperation(DIVIDE);
}
@OnClick(R.id.btn_equals)
public void equalsClicked() {
if (lastKey == EQUALS) {
@ -120,15 +135,6 @@ public class MainActivity extends AppCompatActivity {
lastKey = EQUALS;
}
private void handleEquals() {
if (lastOperation == PLUS)
addNumbers();
else if (lastOperation == MINUS)
subtractNumbers();
else if (lastOperation == MULTIPLY)
multiplyNumbers();
}
@OnClick(R.id.btn_decimal)
public void decimalClicked() {
String value = getDisplayedNumber();
@ -147,6 +153,25 @@ public class MainActivity extends AppCompatActivity {
lastKey = DIGIT;
}
private void handleEquals() {
switch (lastOperation) {
case PLUS:
addNumbers();
break;
case MINUS:
subtractNumbers();
break;
case MULTIPLY:
multiplyNumbers();
break;
case DIVIDE:
divideNumbers();
break;
default:
break;
}
}
@OnClick({R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9})
public void digitClicked(View view) {
lastKey = DIGIT;