Forked from trinhlbk1991/gist:b2e12dd5ac81ea899db289bd531c520a
Created
March 19, 2020 05:10
-
-
Save kojilin/5af7f27802d8e4b8f4d4e46597549267 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CalculatorPresenter : Contract.Presenter { | |
| private lateinit var view: Contract.View | |
| private val data: Stack<String> = Stack() | |
| override fun recordInput(value: String, currentResult: String) { | |
| if (value.isOperator()) { | |
| data.push(currentResult) | |
| if (value == "=") { | |
| calculate() | |
| } else { | |
| data.push(value) | |
| } | |
| } else { | |
| val lastValue = data.peek() | |
| if (lastValue == null || lastValue.isOperator()) { | |
| view.updateResult(value.toInt()) | |
| } else { | |
| view.appendResult(value.toInt()) | |
| } | |
| } | |
| } | |
| override fun calculate() { | |
| var result = 0 | |
| var operator: String? = null | |
| while (data.isNotEmpty()) { | |
| val value = data.pop() | |
| when { | |
| value.isDigit() -> { | |
| result = if (operator == null) { | |
| value.toInt() | |
| } else { | |
| calculate(result, value.toInt(), operator) | |
| } | |
| } | |
| value.isOperator() -> { | |
| operator = value | |
| } | |
| } | |
| } | |
| } | |
| private fun calculate(first: Int, second: Int, operator: String): Int { | |
| return when (operator) { | |
| "+" -> first + second | |
| else -> throw Exception("Not support opperator") | |
| } | |
| } | |
| override fun attach(view: Contract.View) { | |
| this.view = view | |
| } | |
| override fun detach() { | |
| } | |
| private fun String.isDigit(): Boolean { | |
| return when (this) { | |
| "1", "2", "3" -> true | |
| else -> false | |
| } | |
| } | |
| private fun String.isOperator(): Boolean { | |
| return when (this) { | |
| "+", "=" -> true | |
| else -> false | |
| } | |
| } | |
| } | |
| sealed class CalculatorInput(val rawData: String) { | |
| class DigitInput(rawData: String) : CalculatorInput(rawData) { | |
| fun getValue(): Int { | |
| return rawData.toInt() | |
| } | |
| } | |
| class OperatorInput(rawData: String) : CalculatorInput(rawData) { | |
| fun getValue(): Operations { | |
| return when (rawData) { | |
| "+" -> Operations.PLUS | |
| "-" -> Operations.EQUAL | |
| else -> throw Exception("Invalid operator") | |
| } | |
| } | |
| } | |
| } | |
| enum class Operations { | |
| PLUS, | |
| EQUAL | |
| } | |
| ======= | |
| package com.icetea09.calculator.components | |
| import android.os.Bundle | |
| import android.view.View | |
| import android.widget.Button | |
| import com.icetea09.bucketlist.R | |
| import com.icetea09.bucketlist.base.BaseActivityV2 | |
| import kotlinx.android.synthetic.main.activity_calculator.* | |
| class CalculatorActivity : BaseActivityV2(), Contract.View, View.OnClickListener { | |
| private val presenter: Contract.Presenter = CalculatorPresenter() | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_calculator) | |
| presenter.attach(this) | |
| btn_1.setOnClickListener(this) | |
| btn_2.setOnClickListener(this) | |
| btn_3.setOnClickListener(this) | |
| btn_plus.setOnClickListener(this) | |
| btn_equal.setOnClickListener(this) | |
| } | |
| override fun onClick(v: View) { | |
| if (v !is Button) { | |
| return | |
| } | |
| when (v.id) { | |
| R.id.btn_1, | |
| R.id.btn_2, | |
| R.id.btn_3, | |
| R.id.btn_plus, | |
| R.id.btn_equal | |
| -> presenter.recordInput(v.text.toString(), tv_result.text.toString()) | |
| // -> presenter.calculate() | |
| } | |
| } | |
| override fun updateResult(value: Int) { | |
| tv_result.text = "$value" | |
| } | |
| override fun appendResult(value: Int) { | |
| tv_result.text = "${tv_result.text}$value" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment