Merge pull request #183 from studentPavel/patch-1

Make symbol appear in input
This commit is contained in:
Tibor Kaputa
2020-11-05 21:35:13 +01:00
committed by GitHub

View File

@ -10,6 +10,7 @@ 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 inputDisplayedFormula: String? = null
private var lastOperation: String? = null private var lastOperation: String? = null
private var callback: Calculator? = calculator private var callback: Calculator? = calculator
@ -17,11 +18,14 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
private var resetValue = false private var resetValue = false
private var baseValue: BigDecimal = BigDecimal.ZERO private var baseValue: BigDecimal = BigDecimal.ZERO
private var secondValue: BigDecimal = BigDecimal.ZERO private var secondValue: BigDecimal = BigDecimal.ZERO
private val operations = listOf("+", "-", "*", "/", "^", "%")
private var moreOperationsInRaw = false
init { init {
resetValues() resetValues()
setValue("0") setValue("0")
setFormula("") setFormula("")
inputDisplayedFormula = "0"
} }
private fun resetValueIfNeeded() { private fun resetValueIfNeeded() {
@ -71,9 +75,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
fun addDigit(number: Int) { fun addDigit(number: Int) {
val currentValue = displayedNumber inputDisplayedFormula += number
val newValue = formatString(currentValue!! + number) setValue(inputDisplayedFormula.toString())
setValue(newValue)
} }
private fun formatString(str: String): String { private fun formatString(str: String): String {
@ -95,9 +98,15 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
private fun getDisplayedNumberAsDouble() = Formatter.stringToBigDecimal(displayedNumber!!) private fun getDisplayedNumberAsDouble() = Formatter.stringToBigDecimal(displayedNumber!!)
fun handleResult() { fun handleResult() {
if(moreOperationsInRaw){
val index = displayedNumber!!.indexOfAny(operations,0,false)
displayedNumber = displayedNumber!!.substring(index + 1)
}
moreOperationsInRaw = false
secondValue = getDisplayedNumberAsDouble() secondValue = getDisplayedNumberAsDouble()
calculateResult() calculateResult()
baseValue = getDisplayedNumberAsDouble() baseValue = getDisplayedNumberAsDouble()
setValue(inputDisplayedFormula!!)
} }
private fun handleRoot() { private fun handleRoot() {
@ -117,6 +126,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
if (operation != null) { if (operation != null) {
try { try {
updateResult(operation.getResult()) updateResult(operation.getResult())
inputDisplayedFormula = displayedNumber
} catch (e: Exception) { } catch (e: Exception) {
context.toast(R.string.unknown_error_occurred) context.toast(R.string.unknown_error_occurred)
} }
@ -126,6 +136,18 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
fun handleOperation(operation: String) { fun handleOperation(operation: String) {
if(operation != ROOT) {
if (inputDisplayedFormula!!.last() == '+' || inputDisplayedFormula!!.last() == '-' || inputDisplayedFormula!!.last() == '*' || inputDisplayedFormula!!.last() == '/' || inputDisplayedFormula!!.last() == '^' || inputDisplayedFormula!!.last() == '%') {
inputDisplayedFormula = inputDisplayedFormula!!.dropLast(1)
inputDisplayedFormula += getSign(operation)
} else {
if(!inputDisplayedFormula!!.contains('+') && !inputDisplayedFormula!!.contains('-') && !inputDisplayedFormula!!.contains('*') && !inputDisplayedFormula!!.contains('/') && !inputDisplayedFormula!!.contains('^') && !inputDisplayedFormula!!.contains('%')) {
inputDisplayedFormula += getSign(operation)
}else{
moreOperationsInRaw = true
}
}
}
if (lastKey == DIGIT && !lastOperation.isNullOrEmpty() && operation == PERCENT) { if (lastKey == DIGIT && !lastOperation.isNullOrEmpty() && operation == PERCENT) {
val tempOp = lastOperation val tempOp = lastOperation
handlePercent() handlePercent()
@ -138,6 +160,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
resetValue = true resetValue = true
lastKey = operation lastKey = operation
lastOperation = operation lastOperation = operation
setValue(inputDisplayedFormula!!)
if (operation == ROOT) { if (operation == ROOT) {
handleRoot() handleRoot()
@ -174,8 +197,16 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
newValue = newValue.replace("\\.$".toRegex(), "") newValue = newValue.replace("\\.$".toRegex(), "")
newValue = formatString(newValue) if(!newValue.contains('+' ) && !newValue.contains('-' ) && !newValue.contains('*' ) && !newValue.contains('/' ) && !newValue.contains('%' ) && !newValue.contains('^' ) ){
newValue = formatString(newValue)
}
setValue(newValue) setValue(newValue)
if(newValue!="0") {
inputDisplayedFormula = newValue
}else{
inputDisplayedFormula = ""
}
} }
} }
@ -183,6 +214,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
resetValues() resetValues()
setValue("0") setValue("0")
setFormula("") setFormula("")
inputDisplayedFormula=""
} }
fun handleEquals() { fun handleEquals() {
@ -192,21 +224,32 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
if (lastKey != DIGIT) if (lastKey != DIGIT)
return return
displayedNumber = displayedNumber!!.substring(displayedNumber!!.indexOfAny(operations,0,false) + 1)
secondValue = getDisplayedNumberAsDouble() secondValue = getDisplayedNumberAsDouble()
calculateResult() calculateResult()
lastKey = EQUALS lastKey = EQUALS
inputDisplayedFormula = displayedNumber
} }
private fun decimalClicked() { private fun decimalClicked() {
var value = displayedNumber var value = displayedNumber
if (!value!!.contains(".")) { if (!value!!.contains(".")) {
value += "." if(value.toString().equals("0")){
inputDisplayedFormula = "0."
}else{
inputDisplayedFormula += "."
}
}else{
value = displayedNumber!!.substring(displayedNumber!!.indexOfAny(operations,0,false) + 1)
if (!value!!.contains(".")) {
inputDisplayedFormula += "."
}
} }
setValue(value) setValue(inputDisplayedFormula!!)
} }
private fun zeroClicked() { private fun zeroClicked() {
val value = displayedNumber val value = inputDisplayedFormula
if (value != "0") { if (value != "0") {
addDigit(0) addDigit(0)
} }