Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stedi.test
- import android.os.Bundle
- import android.support.v7.app.AppCompatActivity
- import android.text.TextUtils
- import android.view.View
- import android.widget.TextView
- class MainActivity : AppCompatActivity() {
- private val KEY_LEFT = "KEY_LEFT"
- private val KEY_OPERATION = "KEY_OPERATION"
- private val KEY_RIGHT = "KEY_RIGHT"
- private val KEY_CALCULATED = "KEY_CALCULATED"
- private lateinit var tvOut: TextView
- private var left: String? = null
- private var operation: Operation? = null
- private var right: String? = null
- private var calculated: Boolean = false
- private val numbers = intArrayOf(R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9)
- private enum class Operation(val id: Int, val symbol: String, val calculate: (left: Double, right: Double) -> Double) {
- Dot(R.id.btnDot, ".", calculate = { _, _ -> 0.0 }),
- Plus(R.id.btnPlus, "+", calculate = { left, right -> left + right }),
- Minus(R.id.btnMinus, "-", calculate = { left, right -> left - right }),
- Multi(R.id.btnMulti, "*", calculate = { left, right -> left * right }),
- Divide(R.id.btnDivide, "/", calculate = { left, right -> left / right }),
- Result(R.id.btnResult, "=", calculate = { _, _ -> 0.0 })
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.main_activity)
- tvOut = findViewById(R.id.tvOut)
- if (savedInstanceState != null) {
- left = savedInstanceState.getString(KEY_LEFT, null)
- operation = savedInstanceState.getSerializable(KEY_OPERATION) as Operation?
- right = savedInstanceState.getString(KEY_RIGHT, null)
- calculated = savedInstanceState.getBoolean(KEY_CALCULATED, false)
- }
- reCalculate()
- }
- override fun onBackPressed() {
- cancelLastInput()
- }
- override fun onSaveInstanceState(outState: Bundle?) {
- super.onSaveInstanceState(outState)
- if (outState != null) {
- outState.putString(KEY_LEFT, left)
- outState.putSerializable(KEY_OPERATION, operation)
- outState.putString(KEY_RIGHT, right)
- outState.putBoolean(KEY_CALCULATED, calculated)
- }
- }
- fun number(view: View) {
- if (calculated) {
- return
- }
- val number = numbers.indexOf(view.id)
- addSymbol(number.toString())
- reCalculate()
- }
- fun operation(view: View) {
- if (calculated) {
- return
- }
- val operation = Operation.values().single { it.id == view.id }
- addOperation(operation)
- reCalculate()
- }
- private fun addOperation(newOperation: Operation) {
- if (newOperation == Operation.Dot) {
- addSymbol(newOperation.symbol)
- return
- }
- if (newOperation == Operation.Result) {
- val right = right
- if (left != null && operation != null && right != null) {
- this.right = removeLastDot(right)
- calculated = true
- }
- return
- }
- if (operation != null) {
- return
- }
- val left = left
- if (left == null) {
- return
- } else {
- this.left = removeLastDot(left)
- }
- operation = newOperation
- }
- private fun addSymbol(symbol: String) {
- if (operation == null) {
- left = addSymbolToPart(left, symbol)
- } else {
- right = addSymbolToPart(right, symbol)
- }
- }
- private fun addSymbolToPart(to: String?, symbol: String): String? {
- if (symbol == Operation.Dot.symbol && !isDotCanBeAdded(to)) {
- return to
- }
- return if (to == null) symbol else to + symbol
- }
- private fun reCalculate() {
- val left = left
- val operation = operation
- val right = right
- if (calculated && left != null && operation != null && right != null) {
- try {
- val result = operation.calculate.invoke(left.toDouble(), right.toDouble())
- tvOut.text = getFilledOutput(result.toString())
- } catch (e: Exception) {
- e.printStackTrace()
- cancelLastInput()
- }
- } else {
- tvOut.text = getFilledOutput()
- }
- }
- private fun cancelLastInput() {
- when {
- calculated -> calculated = false
- right != null -> right = null
- operation != null -> operation = null
- left != null -> left = null
- }
- reCalculate()
- }
- private fun getFilledOutput(result: String? = null): String {
- val resultS = if (result == null) "" else "= $result"
- return "${left ?: ""} ${operation?.symbol ?: ""} ${right ?: ""} $resultS"
- }
- private fun isDotCanBeAdded(to: String?): Boolean {
- return !TextUtils.isEmpty(to) && to?.contains(Operation.Dot.symbol) == false
- }
- private fun removeLastDot(to: String): String {
- if (to.endsWith(Operation.Dot.symbol)) {
- return to.replace(Operation.Dot.symbol, "")
- }
- return to
- }
- }
Add Comment
Please, Sign In to add comment