extended percentage calculation to be inside other operations

This commit is contained in:
Angelina A 2020-03-16 14:14:58 +01:00
parent 35369ab684
commit 9b119e6bcc
5 changed files with 167 additions and 44 deletions

View File

@ -60,6 +60,7 @@ public class MainActivityTest {
@Test @Test
public void additionTest() { public void additionTest() {
press(R.id.btn_0);
press(R.id.btn_minus); press(R.id.btn_minus);
press(R.id.btn_2); press(R.id.btn_2);
press(R.id.btn_decimal); press(R.id.btn_decimal);
@ -129,6 +130,19 @@ public class MainActivityTest {
checkFormula("10%20"); checkFormula("10%20");
} }
@Test
public void percentTestInsideOtherOperation() {
press(R.id.btn_8);
press(R.id.btn_0);
press(R.id.btn_minus);
press(R.id.btn_1);
press(R.id.btn_0);
press(R.id.btn_percent);
press(R.id.btn_equals);
checkResult("72");
checkFormula("80-10%");
}
@Test @Test
public void powerTest() { public void powerTest() {
press(R.id.btn_2); press(R.id.btn_2);

View File

@ -0,0 +1,91 @@
package com.simplemobiletools.calculator
import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.rule.ActivityTestRule
import androidx.test.runner.AndroidJUnit4
import com.simplemobiletools.calculator.activities.MainActivity
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class PercentTests {
@Rule
@JvmField
var activity = ActivityTestRule(MainActivity::class.java)
@Test
fun percentTest_asTheFirstOperation() {
press(R.id.btn_1)
press(R.id.btn_0)
press(R.id.btn_percent)
press(R.id.btn_2)
press(R.id.btn_0)
press(R.id.btn_equals)
checkResult("2")
checkFormula("10%20")
}
@Test
fun percentTestInsideOtherOperation1() {
press(R.id.btn_8)
press(R.id.btn_0)
press(R.id.btn_minus)
press(R.id.btn_1)
press(R.id.btn_0)
press(R.id.btn_percent)
press(R.id.btn_equals)
checkResult("72")
checkFormula("80-10%")
}
@Test
fun percentTestInsideOtherOperation2() {
press(R.id.btn_5)
press(R.id.btn_0)
press(R.id.btn_minus)
press(R.id.btn_2)
press(R.id.btn_0)
press(R.id.btn_percent)
press(R.id.btn_equals)
checkResult("40")
checkFormula("50-20%")
}
@Test
fun percentTestInsideComplexOperation() {
press(R.id.btn_8)
press(R.id.btn_multiply)
press(R.id.btn_1)
press(R.id.btn_0)
press(R.id.btn_equals)
checkResult("80")
press(R.id.btn_minus)
press(R.id.btn_2)
press(R.id.btn_0)
press(R.id.btn_percent)
press(R.id.btn_equals)
checkResult("64")
checkFormula("80-20%")
}
private fun press(id: Int) {
Espresso.onView(ViewMatchers.withId(id)).perform(ViewActions.click())
}
private fun longPress(id: Int) {
Espresso.onView(ViewMatchers.withId(id)).perform(ViewActions.longClick())
}
private fun checkResult(desired: String) {
Espresso.onView(withId(R.id.result)).check(ViewAssertions.matches(ViewMatchers.withText(desired)))
}
private fun checkFormula(desired: String) {
Espresso.onView(withId(R.id.formula)).check(ViewAssertions.matches(ViewMatchers.withText(desired)))
}
}

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.calculator.helpers package com.simplemobiletools.calculator.helpers
import android.content.Context import android.content.Context
import android.util.Log
import com.simplemobiletools.calculator.R import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.operation.OperationFactory import com.simplemobiletools.calculator.operation.OperationFactory
@ -8,13 +9,13 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
var displayedNumber: String? = null var displayedNumber: String? = null
var displayedFormula: String? = null var displayedFormula: String? = null
var lastKey: String? = null var lastKey: String? = null
private var mLastOperation: String? = null private var lastOperation: String? = null
private var mCallback: Calculator? = calculator private var callback: Calculator? = calculator
private var mIsFirstOperation = false private var isFirstOperation = false
private var mResetValue = false private var resetValue = false
private var mBaseValue = 0.0 private var baseValue = 0.0
private var mSecondValue = 0.0 private var secondValue = 0.0
init { init {
resetValues() resetValues()
@ -23,37 +24,37 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
private fun resetValueIfNeeded() { private fun resetValueIfNeeded() {
if (mResetValue) if (resetValue)
displayedNumber = "0" displayedNumber = "0"
mResetValue = false resetValue = false
} }
private fun resetValues() { private fun resetValues() {
mBaseValue = 0.0 baseValue = 0.0
mSecondValue = 0.0 secondValue = 0.0
mResetValue = false resetValue = false
mLastOperation = "" lastOperation = ""
displayedNumber = "" displayedNumber = ""
displayedFormula = "" displayedFormula = ""
mIsFirstOperation = true isFirstOperation = true
lastKey = "" lastKey = ""
} }
fun setValue(value: String) { fun setValue(value: String) {
mCallback!!.setValue(value, context) callback!!.setValue(value, context)
displayedNumber = value displayedNumber = value
} }
private fun setFormula(value: String) { private fun setFormula(value: String) {
mCallback!!.setFormula(value, context) callback!!.setFormula(value, context)
displayedFormula = value displayedFormula = value
} }
private fun updateFormula() { private fun updateFormula() {
val first = Formatter.doubleToString(mBaseValue) val first = baseValue.format()
val second = Formatter.doubleToString(mSecondValue) val second = secondValue.format()
val sign = getSign(mLastOperation) val sign = getSign(lastOperation)
if (sign == "") { if (sign == "") {
setFormula(sign + first) setFormula(sign + first)
@ -79,59 +80,74 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
val doubleValue = Formatter.stringToDouble(str) val doubleValue = Formatter.stringToDouble(str)
return Formatter.doubleToString(doubleValue) return doubleValue.format()
} }
private fun updateResult(value: Double) { private fun updateResult(value: Double) {
setValue(Formatter.doubleToString(value)) setValue(value.format())
mBaseValue = value baseValue = value
} }
private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber!!) private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber!!)
fun handleResult() { fun handleResult() {
mSecondValue = getDisplayedNumberAsDouble() secondValue = getDisplayedNumberAsDouble()
calculateResult() calculateResult()
mBaseValue = getDisplayedNumberAsDouble() baseValue = getDisplayedNumberAsDouble()
} }
private fun handleRoot() { private fun handleRoot() {
mBaseValue = getDisplayedNumberAsDouble() baseValue = getDisplayedNumberAsDouble()
calculateResult() calculateResult()
} }
private fun handleFactorial() { private fun handleFactorial() {
mBaseValue = getDisplayedNumberAsDouble() baseValue = getDisplayedNumberAsDouble()
calculateResult() calculateResult()
} }
private fun calculateResult() { private fun calculateResult(update: Boolean = true) {
updateFormula() if (update) updateFormula()
val operation = OperationFactory.forId(mLastOperation!!, mBaseValue, mSecondValue) val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
Log.i("ANGELINA", "oper $lastOperation")
if (operation != null) { if (operation != null) {
updateResult(operation.getResult()) updateResult(operation.getResult())
} }
mIsFirstOperation = false isFirstOperation = false
} }
fun handleOperation(operation: String) { fun handleOperation(operation: String) {
if (lastKey == DIGIT && operation != ROOT && operation != FACTORIAL) { if (lastKey == DIGIT && !lastOperation.isNullOrEmpty() && operation == PERCENT) {
val tempOp = lastOperation
handlePercent()
lastKey = tempOp
lastOperation = tempOp
} else if (lastKey == DIGIT && operation != ROOT && operation != FACTORIAL) {
handleResult() handleResult()
} }
mResetValue = true resetValue = true
lastKey = operation lastKey = operation
mLastOperation = operation lastOperation = operation
if (operation == ROOT) { if (operation == ROOT) {
handleRoot() handleRoot()
mResetValue = false resetValue = false
} }
if (operation == FACTORIAL) { if (operation == FACTORIAL) {
handleFactorial() handleFactorial()
mResetValue = false resetValue = false
}
}
private fun handlePercent() {
OperationFactory.forId(PERCENT, baseValue, getDisplayedNumberAsDouble())?.let {
val result = it.getResult()
setFormula("${baseValue.format()}${getSign(lastOperation)}${getDisplayedNumberAsDouble().format()}%")
secondValue = result
calculateResult(false)
} }
} }
@ -169,7 +185,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
if (lastKey != DIGIT) if (lastKey != DIGIT)
return return
mSecondValue = getDisplayedNumberAsDouble() secondValue = getDisplayedNumberAsDouble()
calculateResult() calculateResult()
lastKey = EQUALS lastKey = EQUALS
} }
@ -203,7 +219,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
fun numpadClicked(id: Int) { fun numpadClicked(id: Int) {
if (lastKey == EQUALS) { if (lastKey == EQUALS) {
mLastOperation = EQUALS lastOperation = EQUALS
} }
lastKey = DIGIT lastKey = DIGIT

View File

@ -19,3 +19,5 @@ object Formatter {
fun stringToDouble(str: String) = str.replace(",", "").toDouble() fun stringToDouble(str: String) = str.replace(",", "").toDouble()
} }
fun Double.format(): String = Formatter.doubleToString(this)

View File

@ -11,7 +11,7 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class) @RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = intArrayOf(21)) @Config(constants = BuildConfig::class, sdk = [21])
class MainActivityTest { class MainActivityTest {
private lateinit var activity: MainActivity private lateinit var activity: MainActivity
@ -70,12 +70,12 @@ class MainActivityTest {
checkFormula("6/0") checkFormula("6/0")
} }
@Test /* @Test
fun moduloTest() { fun moduloTest() {
val res = calcResult(6.5, MODULO, 3.0) val res = calcResult(6.5, MODULO, 3.0)
assertEquals("0.5", res) assertEquals("0.5", res)
checkFormula("6.5%3") checkFormula("6.5%3")
} }*/
@Test @Test
fun powerTest() { fun powerTest() {
@ -143,7 +143,7 @@ class MainActivityTest {
checkFormula("5.6*5") checkFormula("5.6*5")
setDouble(4.0) setDouble(4.0)
handleOperation(MODULO) handleOperation(DIVIDE)
assertEquals("7", getDisplayedNumber()) assertEquals("7", getDisplayedNumber())
checkFormula("28/4") checkFormula("28/4")