From 9f99b79d64c23231bb4bc819ffeb9a45f2a428f4 Mon Sep 17 00:00:00 2001 From: Jay Barria Date: Wed, 30 Sep 2020 13:06:32 +0800 Subject: [PATCH] Add readyToClear flag --- build.gradle | 2 +- .../com/github/jairrab/calc/Calculator.kt | 6 ++- .../jairrab/calc/lib/CalculatorUtility.kt | 7 +-- .../controls/buttons/BackspaceProcessor.kt | 4 +- .../lib/controls/buttons/ClearProcessor.kt | 9 ++-- .../lib/controls/buttons/DecimalProcessor.kt | 5 +- .../lib/controls/buttons/NumberProcessor.kt | 50 +++++++++---------- .../lib/controls/buttons/OperatorProcessor.kt | 6 +-- .../lib/controls/buttons/PercentProcessor.kt | 2 +- .../lib/controls/entries/EntriesManager.kt | 31 ++++++++---- .../lib/controls/outputs/OutputManager.kt | 14 +++--- 11 files changed, 77 insertions(+), 59 deletions(-) diff --git a/build.gradle b/build.gradle index fe62161..aae5ae6 100644 --- a/build.gradle +++ b/build.gradle @@ -19,7 +19,7 @@ allprojects { } group 'com.github.jairrab.calc' -version "3.3.0" +version "3.4.0" java { diff --git a/src/main/kotlin/com/github/jairrab/calc/Calculator.kt b/src/main/kotlin/com/github/jairrab/calc/Calculator.kt index c6a91aa..fe95dd6 100644 --- a/src/main/kotlin/com/github/jairrab/calc/Calculator.kt +++ b/src/main/kotlin/com/github/jairrab/calc/Calculator.kt @@ -10,7 +10,7 @@ import com.github.jairrab.calc.lib.controls.entries.EntriesManager interface Calculator { fun press(button: CalculatorButton) fun clear() - fun resetToNumber(number: Double) + fun resetToNumber(number: Double, readyToClear: Boolean = true) fun pressOne() fun pressTwo() fun pressThree() @@ -41,6 +41,7 @@ interface Calculator { fun getInstance() = getInstance( calculatorType = CalculatorType.BASIC_MDAS, initialNumber = 0.0, + readyToClear = true, listener = null ) @@ -48,6 +49,7 @@ interface Calculator { fun getInstance(listener: Listener) = getInstance( calculatorType = CalculatorType.BASIC_MDAS, initialNumber = 0.0, + readyToClear = true, listener = listener ) @@ -55,9 +57,11 @@ interface Calculator { fun getInstance( calculatorType: CalculatorType = CalculatorType.BASIC_MDAS, initialNumber: Double = 0.0, + readyToClear: Boolean = true, listener: Listener? = null ): Calculator = CalculatorUtility( initialNumber = initialNumber, + readyToClear = readyToClear, controlProcessor = ControlProcessor.getInstance( entriesManager = EntriesManager.getInstance(), calculatorType = calculatorType, diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/CalculatorUtility.kt b/src/main/kotlin/com/github/jairrab/calc/lib/CalculatorUtility.kt index e0cd914..defae5b 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/CalculatorUtility.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/CalculatorUtility.kt @@ -8,10 +8,11 @@ import com.github.jairrab.calc.lib.controls.ControlProcessor internal open class CalculatorUtility( initialNumber: Double, + readyToClear: Boolean, private val controlProcessor: ControlProcessor ) : Calculator { init { - resetToNumber(initialNumber) + resetToNumber(initialNumber, readyToClear) } override fun press(button: CalculatorButton) { @@ -43,8 +44,8 @@ internal open class CalculatorUtility( controlProcessor.outputManager.update(CalculatorButton.CLEAR) } - final override fun resetToNumber(number: Double) { - controlProcessor.clearProcessor.initialize(number) + final override fun resetToNumber(number: Double, readyToClear: Boolean) { + controlProcessor.clearProcessor.initialize(number, readyToClear) controlProcessor.outputManager.update(number) } diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/BackspaceProcessor.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/BackspaceProcessor.kt index 050ed5c..d07cec1 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/BackspaceProcessor.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/BackspaceProcessor.kt @@ -11,8 +11,8 @@ internal class BackspaceProcessor( fun onBackSpace() { if (entriesManager.hasEntries()) { when { - entriesManager.lastResult != null -> { - entriesManager.lastResult = null + entriesManager.isReadyToClear() -> { + entriesManager.setReadyToClear(false) entriesManager.removeLastEntry() } entriesManager.isLastEntryAPercentNumber() -> { diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/ClearProcessor.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/ClearProcessor.kt index 8263de9..252120a 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/ClearProcessor.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/ClearProcessor.kt @@ -7,8 +7,10 @@ import com.github.jairrab.calc.lib.controls.entries.EntriesManager internal class ClearProcessor( private val entriesManager: EntriesManager ) { - fun initialize(initialNumber: Double) { - entriesManager.clear() + fun initialize(initialNumber: Double, readyToClear: Boolean) { + entriesManager.clearEntries() + entriesManager.setReadyToClear(readyToClear) + if (initialNumber != 0.0) { val entry = if (initialNumber % 1 == 0.0) { initialNumber.toInt().toString() @@ -21,6 +23,7 @@ internal class ClearProcessor( } fun onCleared() { - entriesManager.clearLastResult() + entriesManager.clearEntries() + entriesManager.setReadyToClear(false) } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/DecimalProcessor.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/DecimalProcessor.kt index 5ea53cd..8eee0dc 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/DecimalProcessor.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/DecimalProcessor.kt @@ -19,8 +19,9 @@ internal class DecimalProcessor( entriesManager.addEntry(DECIMAL.tag) } else { when { - entriesManager.lastResult != null -> { - entriesManager.clearLastResult() + entriesManager.isReadyToClear() -> { + entriesManager.clearEntries() + entriesManager.setReadyToClear(false) entriesManager.addEntry(DECIMAL.tag) } entriesManager.isLastEntryAnOperator() -> entriesManager.addEntry(DECIMAL.tag) diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/NumberProcessor.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/NumberProcessor.kt index 9d433f5..5538ddf 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/NumberProcessor.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/NumberProcessor.kt @@ -11,34 +11,34 @@ internal class NumberProcessor( ) { fun processNumber(calculatorButton: CalculatorButton) { val number = calculatorButton.tag - if (entriesManager.isNoEntries()) { - entriesManager.addEntry(number) - } else { - when { - entriesManager.lastResult != null -> { - entriesManager.clearLastResult() - entriesManager.addEntry(number) - } - entriesManager.isLastEntryAnOperator() -> { - entriesManager.addEntry(number) - } - entriesManager.isLastEntryAPercentNumber() -> { - val entry = entriesManager.getLastEntry().trimEndChar() - entriesManager.setLastEntry(entry) - entriesManager.appendToLastEntry(number) - } - entriesManager.isLastEntryANumber() -> { - if (entriesManager.getLastEntry() == "0") { - entriesManager.setLastEntry(number) - } else { - entriesManager.appendToLastEntry(number) - } - } - entriesManager.isLastEntryADecimal() -> { + when { + entriesManager.isNoEntries() -> { + entriesManager.addEntry(number) + } + entriesManager.isReadyToClear() -> { + entriesManager.clearEntries() + entriesManager.setReadyToClear(false) + entriesManager.addEntry(number) + } + entriesManager.isLastEntryAnOperator() -> { + entriesManager.addEntry(number) + } + entriesManager.isLastEntryAPercentNumber() -> { + val entry = entriesManager.getLastEntry().trimEndChar() + entriesManager.setLastEntry(entry) + entriesManager.appendToLastEntry(number) + } + entriesManager.isLastEntryANumber() -> { + if (entriesManager.getLastEntry() == "0") { + entriesManager.setLastEntry(number) + } else { entriesManager.appendToLastEntry(number) } - else -> throw IllegalStateException("Invalid number command") } + entriesManager.isLastEntryADecimal() -> { + entriesManager.appendToLastEntry(number) + } + else -> throw IllegalStateException("Invalid number command") } } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/OperatorProcessor.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/OperatorProcessor.kt index b8721e2..26cb645 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/OperatorProcessor.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/OperatorProcessor.kt @@ -22,9 +22,9 @@ internal class OperatorProcessor( entriesManager.addEntry(operator) } else { when { - entriesManager.lastResult != null -> { - entriesManager.setToLastResult() - entriesManager.lastResult = null + entriesManager.isReadyToClear() -> { + entriesManager.setEntriesToResult() + entriesManager.setReadyToClear(false) entriesManager.addEntry(operator) } entriesManager.isLastEntryAnOperator() -> { diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/PercentProcessor.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/PercentProcessor.kt index 823982b..66f5234 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/PercentProcessor.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/PercentProcessor.kt @@ -20,7 +20,7 @@ internal class PercentProcessor( return } else { when { - entriesManager.lastResult != null -> { + entriesManager.isReadyToClear() -> { outputManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY, entries)) return } diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/entries/EntriesManager.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/entries/EntriesManager.kt index 727d57c..5f9e1c6 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/entries/EntriesManager.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/entries/EntriesManager.kt @@ -8,30 +8,41 @@ import com.github.jairrab.calc.lib.mathutils.OperatorUtils.operatorTags import com.github.jairrab.calc.lib.utils.trimEndChar internal class EntriesManager private constructor() { - var lastResult: Double? = null - private val entries: MutableList = ArrayList() + private var result: Double? = null + private var readyToClear = false fun getEntries(): List { return entries } - fun clear() { + fun clearEntries() { entries.clear() } - fun addEntry(entry: String) { - entries += entry + fun isReadyToClear(): Boolean { + return readyToClear } - fun clearLastResult() { - clear() - lastResult = null + fun setReadyToClear(readyToClear: Boolean) { + this.readyToClear = readyToClear + } + + fun getResult(): Double { + return result ?: 0.0 + } + + fun setResult(result: Double) { + this.result = result + } + + fun addEntry(entry: String) { + entries += entry } - fun setToLastResult() { + fun setEntriesToResult() { entries.clear() - entries += lastResult.toString() + entries += result.toString() } fun isNoEntries(): Boolean { diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/outputs/OutputManager.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/outputs/OutputManager.kt index 5f98ddd..91da371 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/outputs/OutputManager.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/outputs/OutputManager.kt @@ -17,8 +17,6 @@ internal class OutputManager private constructor( private val entriesManager: EntriesManager, private val equationSolver: EquationSolver ) { - private var currentNumber = 0.0 - fun update(button: CalculatorButton) { val entries = entriesManager.getEntries() try { @@ -35,14 +33,14 @@ internal class OutputManager private constructor( else -> equationSolver.solve(entries) } - currentNumber = result - - LOG.info("Key: ${button.tag} | Entries: $entries | Result: $result") - updateListener(CalculatorUpdate.OnUpdate(button.tag, entries, result)) + entriesManager.setResult(result) if (button == CalculatorButton.EQUALS) { - entriesManager.lastResult = result + entriesManager.setReadyToClear(true) } + + LOG.info("Key: ${button.tag} | Entries: $entries | Result: $result") + updateListener(CalculatorUpdate.OnUpdate(button.tag, entries, result)) } catch (e: DivideByZeroException) { LOG.warning("Divide by zero error") listener?.onCalculatorUpdate(Error.DivideByZero(button.tag, entries)) @@ -63,7 +61,7 @@ internal class OutputManager private constructor( } fun getCurrentNumber(): Double { - return currentNumber + return entriesManager.getResult() } companion object {