Guest User

Untitled

a guest
Dec 15th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. package com.stedi.test
  2.  
  3. import android.os.Bundle
  4. import android.support.v7.app.AppCompatActivity
  5. import android.text.TextUtils
  6. import android.view.View
  7. import android.widget.TextView
  8.  
  9. class MainActivity : AppCompatActivity() {
  10. private val KEY_LEFT = "KEY_LEFT"
  11. private val KEY_OPERATION = "KEY_OPERATION"
  12. private val KEY_RIGHT = "KEY_RIGHT"
  13. private val KEY_CALCULATED = "KEY_CALCULATED"
  14.  
  15. private lateinit var tvOut: TextView
  16.  
  17. private var left: String? = null
  18. private var operation: Operation? = null
  19. private var right: String? = null
  20. private var calculated: Boolean = false
  21.  
  22. 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)
  23.  
  24. private enum class Operation(val id: Int, val symbol: String, val calculate: (left: Double, right: Double) -> Double) {
  25. Dot(R.id.btnDot, ".", calculate = { _, _ -> 0.0 }),
  26. Plus(R.id.btnPlus, "+", calculate = { left, right -> left + right }),
  27. Minus(R.id.btnMinus, "-", calculate = { left, right -> left - right }),
  28. Multi(R.id.btnMulti, "*", calculate = { left, right -> left * right }),
  29. Divide(R.id.btnDivide, "/", calculate = { left, right -> left / right }),
  30. Result(R.id.btnResult, "=", calculate = { _, _ -> 0.0 })
  31. }
  32.  
  33. override fun onCreate(savedInstanceState: Bundle?) {
  34. super.onCreate(savedInstanceState)
  35. setContentView(R.layout.main_activity)
  36. tvOut = findViewById(R.id.tvOut)
  37.  
  38. if (savedInstanceState != null) {
  39. left = savedInstanceState.getString(KEY_LEFT, null)
  40. operation = savedInstanceState.getSerializable(KEY_OPERATION) as Operation?
  41. right = savedInstanceState.getString(KEY_RIGHT, null)
  42. calculated = savedInstanceState.getBoolean(KEY_CALCULATED, false)
  43. }
  44.  
  45. reCalculate()
  46. }
  47.  
  48. override fun onBackPressed() {
  49. cancelLastInput()
  50. }
  51.  
  52. override fun onSaveInstanceState(outState: Bundle?) {
  53. super.onSaveInstanceState(outState)
  54. if (outState != null) {
  55. outState.putString(KEY_LEFT, left)
  56. outState.putSerializable(KEY_OPERATION, operation)
  57. outState.putString(KEY_RIGHT, right)
  58. outState.putBoolean(KEY_CALCULATED, calculated)
  59. }
  60. }
  61.  
  62. fun number(view: View) {
  63. if (calculated) {
  64. return
  65. }
  66.  
  67. val number = numbers.indexOf(view.id)
  68. addSymbol(number.toString())
  69. reCalculate()
  70. }
  71.  
  72. fun operation(view: View) {
  73. if (calculated) {
  74. return
  75. }
  76.  
  77. val operation = Operation.values().single { it.id == view.id }
  78. addOperation(operation)
  79. reCalculate()
  80. }
  81.  
  82. private fun addOperation(newOperation: Operation) {
  83. if (newOperation == Operation.Dot) {
  84. addSymbol(newOperation.symbol)
  85. return
  86. }
  87.  
  88. if (newOperation == Operation.Result) {
  89. val right = right
  90. if (left != null && operation != null && right != null) {
  91. this.right = removeLastDot(right)
  92. calculated = true
  93. }
  94. return
  95. }
  96.  
  97. if (operation != null) {
  98. return
  99. }
  100.  
  101. val left = left
  102. if (left == null) {
  103. return
  104. } else {
  105. this.left = removeLastDot(left)
  106. }
  107.  
  108. operation = newOperation
  109. }
  110.  
  111. private fun addSymbol(symbol: String) {
  112. if (operation == null) {
  113. left = addSymbolToPart(left, symbol)
  114. } else {
  115. right = addSymbolToPart(right, symbol)
  116. }
  117. }
  118.  
  119. private fun addSymbolToPart(to: String?, symbol: String): String? {
  120. if (symbol == Operation.Dot.symbol && !isDotCanBeAdded(to)) {
  121. return to
  122. }
  123. return if (to == null) symbol else to + symbol
  124. }
  125.  
  126. private fun reCalculate() {
  127. val left = left
  128. val operation = operation
  129. val right = right
  130.  
  131. if (calculated && left != null && operation != null && right != null) {
  132. try {
  133. val result = operation.calculate.invoke(left.toDouble(), right.toDouble())
  134. tvOut.text = getFilledOutput(result.toString())
  135. } catch (e: Exception) {
  136. e.printStackTrace()
  137. cancelLastInput()
  138. }
  139. } else {
  140. tvOut.text = getFilledOutput()
  141. }
  142. }
  143.  
  144. private fun cancelLastInput() {
  145. when {
  146. calculated -> calculated = false
  147. right != null -> right = null
  148. operation != null -> operation = null
  149. left != null -> left = null
  150. }
  151. reCalculate()
  152. }
  153.  
  154. private fun getFilledOutput(result: String? = null): String {
  155. val resultS = if (result == null) "" else "= $result"
  156. return "${left ?: ""} ${operation?.symbol ?: ""} ${right ?: ""} $resultS"
  157. }
  158.  
  159. private fun isDotCanBeAdded(to: String?): Boolean {
  160. return !TextUtils.isEmpty(to) && to?.contains(Operation.Dot.symbol) == false
  161. }
  162.  
  163. private fun removeLastDot(to: String): String {
  164. if (to.endsWith(Operation.Dot.symbol)) {
  165. return to.replace(Operation.Dot.symbol, "")
  166. }
  167. return to
  168. }
  169. }
Add Comment
Please, Sign In to add comment