From c1336fd2fdd45b68377704136d103696ff036fe1 Mon Sep 17 00:00:00 2001 From: Jay Barria Date: Sat, 26 Sep 2020 08:11:05 +0800 Subject: [PATCH] Updated CalculatorUpdate.kt --- build.gradle | 2 +- .../com/github/jairrab/calc/CalculatorUpdate.kt | 4 ++-- .../calc/lib/controls/buttons/DecimalProcessor.kt | 8 +++----- .../lib/controls/buttons/OperatorProcessor.kt | 6 +++--- .../calc/lib/controls/buttons/PercentProcessor.kt | 15 ++++++++------- .../calc/lib/controls/outputs/DisplayManager.kt | 2 +- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index 59d48e1..8961f7f 100644 --- a/build.gradle +++ b/build.gradle @@ -19,7 +19,7 @@ allprojects { } group 'com.github.jairrab.calc' -version "3.1.1" +version "3.2.0" java { diff --git a/src/main/kotlin/com/github/jairrab/calc/CalculatorUpdate.kt b/src/main/kotlin/com/github/jairrab/calc/CalculatorUpdate.kt index 619166d..075b8cf 100644 --- a/src/main/kotlin/com/github/jairrab/calc/CalculatorUpdate.kt +++ b/src/main/kotlin/com/github/jairrab/calc/CalculatorUpdate.kt @@ -1,7 +1,7 @@ package com.github.jairrab.calc sealed class CalculatorUpdate { - class Initializing(val number: Double) : CalculatorUpdate() + class Initializing(val number: Double, val entries: List) : CalculatorUpdate() class OnUpdate( val key: String?, @@ -9,9 +9,9 @@ sealed class CalculatorUpdate { val result: Double ) : CalculatorUpdate() - class InvalidKey(val invalidKeyType: InvalidKeyType) : CalculatorUpdate() sealed class Error : CalculatorUpdate() { class DivideByZero(val key: String?, val entries: List) : Error() + class InvalidKey(val invalidKeyType: InvalidKeyType, val entries: List) : Error() } } \ 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 ca74357..7ed4f10 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 @@ -2,11 +2,8 @@ package com.github.jairrab.calc.lib.controls.buttons -import com.github.jairrab.calc.Calculator import com.github.jairrab.calc.CalculatorButton.DECIMAL -import com.github.jairrab.calc.CalculatorUpdate -import com.github.jairrab.calc.CalculatorUpdate.InvalidKey -import com.github.jairrab.calc.InvalidKeyType +import com.github.jairrab.calc.CalculatorUpdate.Error.InvalidKey import com.github.jairrab.calc.InvalidKeyType.INVALID_DECIMAL_ENTRY import com.github.jairrab.calc.lib.controls.entries.EntriesManager import com.github.jairrab.calc.lib.controls.outputs.DisplayManager @@ -28,7 +25,8 @@ class DecimalProcessor( } entriesManager.isLastEntryAnOperator() -> entriesManager.addEntry(DECIMAL.tag) entriesManager.isLastEntryANumberWithDecimal() -> { - displayManager.updateListener(InvalidKey(INVALID_DECIMAL_ENTRY)) + val entries = entriesManager.getEntries() + displayManager.updateListener(InvalidKey(INVALID_DECIMAL_ENTRY, entries)) return } entriesManager.isLastEntryAPercentNumber() -> { 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 c5fa16d..3d62e7e 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 @@ -2,10 +2,9 @@ package com.github.jairrab.calc.lib.controls.buttons -import com.github.jairrab.calc.Calculator import com.github.jairrab.calc.CalculatorButton import com.github.jairrab.calc.CalculatorButton.ZERO -import com.github.jairrab.calc.CalculatorUpdate.InvalidKey +import com.github.jairrab.calc.CalculatorUpdate.Error.InvalidKey import com.github.jairrab.calc.InvalidKeyType.INVALID_OPERATOR_ENTRY import com.github.jairrab.calc.lib.controls.entries.EntriesManager import com.github.jairrab.calc.lib.controls.outputs.DisplayManager @@ -55,7 +54,8 @@ class OperatorProcessor( } } else -> { - displayManager.updateListener(InvalidKey(INVALID_OPERATOR_ENTRY)) + val entries = entriesManager.getEntries() + displayManager.updateListener(InvalidKey(INVALID_OPERATOR_ENTRY, entries)) throw IllegalStateException("Invalid operator entry") } } 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 c565097..f3b2a3a 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 @@ -3,7 +3,7 @@ package com.github.jairrab.calc.lib.controls.buttons import com.github.jairrab.calc.CalculatorButton -import com.github.jairrab.calc.CalculatorUpdate.InvalidKey +import com.github.jairrab.calc.CalculatorUpdate.Error.InvalidKey import com.github.jairrab.calc.InvalidKeyType.INVALID_PERCENT_ENTRY import com.github.jairrab.calc.lib.controls.entries.EntriesManager import com.github.jairrab.calc.lib.controls.outputs.DisplayManager @@ -14,21 +14,22 @@ class PercentProcessor( private val displayManager: DisplayManager ) { internal fun processPercent() { + val entries = entriesManager.getEntries() if (entriesManager.isNoEntries()) { - displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY)) + displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY, entries)) return } else { when { entriesManager.lastResult != null -> { - displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY)) + displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY, entries)) return } entriesManager.isLastEntryAnOperator() -> { - displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY)) + displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY, entries)) return } entriesManager.isLastEntryAPercentNumber() -> { - displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY)) + displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY, entries)) return } entriesManager.isLastEntryANumber() -> { @@ -38,11 +39,11 @@ class PercentProcessor( entriesManager.appendToLastEntry(CalculatorButton.PERCENT.tag) } entriesManager.isLastEntryADecimal() -> { - displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY)) + displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY, entries)) return } else -> { - displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY)) + displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY, entries)) throw IllegalStateException("Invalid operator entry") } } diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/controls/outputs/DisplayManager.kt b/src/main/kotlin/com/github/jairrab/calc/lib/controls/outputs/DisplayManager.kt index 8a349a3..1c7c15e 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/controls/outputs/DisplayManager.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/controls/outputs/DisplayManager.kt @@ -50,7 +50,7 @@ class DisplayManager private constructor( } fun update(number: Double) { - updateListener(CalculatorUpdate.Initializing(number)) + updateListener(CalculatorUpdate.Initializing(number, entriesManager.getEntries())) LOG.info("Initializing calculator") }