mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-06-05 21:49:13 +02:00
Polymorphic calculator operations
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.simplemobiletools.calculator;
|
||||
|
||||
import com.simplemobiletools.calculator.operation.OperationFactory;
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
|
||||
public class CalculatorImpl {
|
||||
private String mDisplayedValue;
|
||||
private String mDisplayedFormula;
|
||||
@@ -110,60 +113,19 @@ public class CalculatorImpl {
|
||||
}
|
||||
|
||||
public void calculateResult() {
|
||||
if (!mIsFirstOperation)
|
||||
if (!mIsFirstOperation) {
|
||||
updateFormula();
|
||||
|
||||
switch (mLastOperation) {
|
||||
case Constants.PLUS:
|
||||
updateResult(mBaseValue + mSecondValue);
|
||||
break;
|
||||
case Constants.MINUS:
|
||||
updateResult(mBaseValue - mSecondValue);
|
||||
break;
|
||||
case Constants.MULTIPLY:
|
||||
updateResult(mBaseValue * mSecondValue);
|
||||
break;
|
||||
case Constants.DIVIDE:
|
||||
divideNumbers();
|
||||
break;
|
||||
case Constants.MODULO:
|
||||
moduloNumbers();
|
||||
break;
|
||||
case Constants.POWER:
|
||||
powerNumbers();
|
||||
break;
|
||||
case Constants.ROOT:
|
||||
updateResult(Math.sqrt(mBaseValue));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Operation operation = OperationFactory.forId(mLastOperation, mBaseValue, mSecondValue);
|
||||
|
||||
if (operation != null) {
|
||||
updateResult(operation.getResult());
|
||||
}
|
||||
|
||||
mIsFirstOperation = false;
|
||||
}
|
||||
|
||||
private void divideNumbers() {
|
||||
double resultValue = 0;
|
||||
if (mSecondValue != 0)
|
||||
resultValue = mBaseValue / mSecondValue;
|
||||
|
||||
updateResult(resultValue);
|
||||
}
|
||||
|
||||
private void moduloNumbers() {
|
||||
double resultValue = 0;
|
||||
if (mSecondValue != 0)
|
||||
resultValue = mBaseValue % mSecondValue;
|
||||
|
||||
updateResult(resultValue);
|
||||
}
|
||||
|
||||
private void powerNumbers() {
|
||||
double resultValue = Math.pow(mBaseValue, mSecondValue);
|
||||
if (Double.isInfinite(resultValue) || Double.isNaN(resultValue))
|
||||
resultValue = 0;
|
||||
updateResult(resultValue);
|
||||
}
|
||||
|
||||
public void handleOperation(String operation) {
|
||||
if (mLastKey.equals(Constants.DIGIT))
|
||||
handleResult();
|
||||
|
@@ -0,0 +1,20 @@
|
||||
package com.simplemobiletools.calculator.operation;
|
||||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation;
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
|
||||
public class DivideOperation extends BinaryOperation implements Operation {
|
||||
|
||||
public DivideOperation(double baseValue, double secondValue) {
|
||||
super(baseValue, secondValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getResult() {
|
||||
double result = 0;
|
||||
if (secondValue != 0) {
|
||||
result = baseValue / secondValue;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package com.simplemobiletools.calculator.operation;
|
||||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation;
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
|
||||
public class MinusOperation extends BinaryOperation implements Operation {
|
||||
protected MinusOperation(double baseValue, double secondValue) {
|
||||
super(baseValue, secondValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getResult() {
|
||||
return baseValue - secondValue;
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package com.simplemobiletools.calculator.operation;
|
||||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation;
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
|
||||
public class ModuloOperation extends BinaryOperation implements Operation {
|
||||
protected ModuloOperation(double baseValue, double secondValue) {
|
||||
super(baseValue, secondValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getResult() {
|
||||
double result = 0;
|
||||
if (secondValue != 0) {
|
||||
result = baseValue % secondValue;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package com.simplemobiletools.calculator.operation;
|
||||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation;
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
|
||||
public class MultiplyOperation extends BinaryOperation implements Operation {
|
||||
protected MultiplyOperation(double baseValue, double secondValue) {
|
||||
super(baseValue, secondValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getResult() {
|
||||
return baseValue * secondValue;
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.simplemobiletools.calculator.operation;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.simplemobiletools.calculator.Constants;
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
|
||||
public class OperationFactory {
|
||||
|
||||
@Nullable
|
||||
public static Operation forId(String id, double baseValue, double secondValue) {
|
||||
switch (id) {
|
||||
case Constants.PLUS:
|
||||
return new PlusOperation(baseValue, secondValue);
|
||||
case Constants.MINUS:
|
||||
return new MinusOperation(baseValue, secondValue);
|
||||
case Constants.DIVIDE:
|
||||
return new DivideOperation(baseValue, secondValue);
|
||||
case Constants.MULTIPLY:
|
||||
return new MultiplyOperation(baseValue, secondValue);
|
||||
case Constants.MODULO:
|
||||
return new ModuloOperation(baseValue, secondValue);
|
||||
case Constants.POWER:
|
||||
return new PowerOperation(baseValue, secondValue);
|
||||
case Constants.ROOT:
|
||||
return new RootOperation(baseValue);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.simplemobiletools.calculator.operation;
|
||||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation;
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
|
||||
public class PlusOperation extends BinaryOperation implements Operation {
|
||||
|
||||
protected PlusOperation(double baseValue, double secondValue) {
|
||||
super(baseValue, secondValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getResult() {
|
||||
return baseValue + secondValue;
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.simplemobiletools.calculator.operation;
|
||||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation;
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
import com.simplemobiletools.calculator.operation.base.UnaryOperation;
|
||||
|
||||
public class PowerOperation extends BinaryOperation implements Operation {
|
||||
|
||||
protected PowerOperation(double baseValue, double secondValue) {
|
||||
super(baseValue, secondValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getResult() {
|
||||
double result = Math.pow(baseValue, secondValue);
|
||||
if (Double.isInfinite(result) || Double.isNaN(result))
|
||||
result = 0;
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.simplemobiletools.calculator.operation;
|
||||
|
||||
import com.simplemobiletools.calculator.operation.base.Operation;
|
||||
import com.simplemobiletools.calculator.operation.base.UnaryOperation;
|
||||
|
||||
public class RootOperation extends UnaryOperation implements Operation {
|
||||
|
||||
protected RootOperation(double value) {
|
||||
super(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getResult() {
|
||||
return Math.sqrt(value);
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.simplemobiletools.calculator.operation.base;
|
||||
|
||||
public class BinaryOperation {
|
||||
protected double baseValue;
|
||||
protected double secondValue;
|
||||
|
||||
protected BinaryOperation(double baseValue, double secondValue) {
|
||||
this.baseValue = baseValue;
|
||||
this.secondValue = secondValue;
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package com.simplemobiletools.calculator.operation.base;
|
||||
|
||||
public interface Operation {
|
||||
double getResult();
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package com.simplemobiletools.calculator.operation.base;
|
||||
|
||||
public class UnaryOperation {
|
||||
|
||||
protected double value;
|
||||
|
||||
protected UnaryOperation(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user