removing some old redundant operations

This commit is contained in:
tibbi
2020-11-07 17:57:14 +01:00
parent 7d165ae0e7
commit a0bc9395f3
12 changed files with 23 additions and 134 deletions

View File

@ -2,7 +2,6 @@ package com.simplemobiletools.calculator.helpers
import android.content.Context import android.content.Context
import com.simplemobiletools.calculator.R import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.operation.PercentOperation
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import net.objecthunter.exp4j.ExpressionBuilder import net.objecthunter.exp4j.ExpressionBuilder
@ -106,8 +105,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there // handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
private fun handlePercent() { private fun handlePercent() {
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation) val result = calculatePercentage(baseValue, getSecondValue(), lastOperation)
val result = operation.getResult()
showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%") showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
inputDisplayedFormula = result.format() inputDisplayedFormula = result.format()
showNewResult(result.format()) showNewResult(result.format())
@ -168,6 +166,28 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
} }
} }
private fun calculatePercentage(baseValue: Double, secondValue: Double, sign: String): Double {
return when (sign) {
MULTIPLY -> {
val partial = 100 / secondValue
baseValue / partial
}
DIVIDE -> {
val partial = 100 / secondValue
baseValue * partial
}
PLUS -> {
val partial = baseValue / (100 / secondValue)
baseValue.plus(partial)
}
MINUS -> {
val partial = baseValue / (100 / secondValue)
baseValue.minus(partial)
}
else -> baseValue / (100 * secondValue)
}
}
private fun showNewResult(value: String) { private fun showNewResult(value: String) {
callback!!.showNewResult(value, context) callback!!.showNewResult(value, context)
} }

View File

@ -1,9 +0,0 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation
class DivideOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult() = baseValue / secondValue
}

View File

@ -1,9 +0,0 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation
class MinusOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult() = baseValue - secondValue
}

View File

@ -1,9 +0,0 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation
class MultiplyOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult() = baseValue * secondValue
}

View File

@ -1,19 +0,0 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.helpers.*
import com.simplemobiletools.calculator.operation.base.Operation
object OperationFactory {
fun forId(id: String, baseValue: Double, secondValue: Double): Operation? {
return when (id) {
PLUS -> PlusOperation(baseValue, secondValue)
MINUS -> MinusOperation(baseValue, secondValue)
DIVIDE -> DivideOperation(baseValue, secondValue)
MULTIPLY -> MultiplyOperation(baseValue, secondValue)
POWER -> PowerOperation(baseValue, secondValue)
ROOT -> RootOperation(baseValue, secondValue)
else -> null
}
}
}

View File

@ -1,35 +0,0 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.helpers.DIVIDE
import com.simplemobiletools.calculator.helpers.MINUS
import com.simplemobiletools.calculator.helpers.MULTIPLY
import com.simplemobiletools.calculator.helpers.PLUS
import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation
class PercentOperation(baseValue: Double, secondValue: Double, val sign: String) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): Double {
return when (sign) {
MULTIPLY -> {
val partial = 100 / secondValue
baseValue / partial
}
DIVIDE -> {
val partial = 100 / secondValue
baseValue * partial
}
PLUS -> {
val partial = baseValue / (100 / secondValue)
baseValue.plus(partial)
}
MINUS -> {
val partial = baseValue / (100 / secondValue)
baseValue.minus(partial)
}
else -> {
baseValue / (100 * secondValue)
}
}
}
}

View File

@ -1,9 +0,0 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation
class PlusOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult() = baseValue + secondValue
}

View File

@ -1,18 +0,0 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation
import kotlin.math.pow
class PowerOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): Double {
val result = baseValue.pow(secondValue)
return if (java.lang.Double.isInfinite(result) || java.lang.Double.isNaN(result))
0.0
else {
result
}
}
}

View File

@ -1,10 +0,0 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation
import kotlin.math.sqrt
class RootOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult() = sqrt(secondValue) * baseValue
}

View File

@ -1,3 +0,0 @@
package com.simplemobiletools.calculator.operation.base
open class BinaryOperation protected constructor(protected val baseValue: Double, protected val secondValue: Double)

View File

@ -1,5 +0,0 @@
package com.simplemobiletools.calculator.operation.base
interface Operation {
fun getResult(): Double
}

View File

@ -1,5 +0,0 @@
package com.simplemobiletools.calculator.operation.base
import java.math.BigDecimal
open class UnaryOperation protected constructor(protected var value: BigDecimal)