Skip to content

Commit

Permalink
Add readyToClear flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jairrab committed Sep 30, 2020
1 parent 1c94e74 commit 9f99b79
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 59 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ allprojects {
}

group 'com.github.jairrab.calc'
version "3.3.0"
version "3.4.0"


java {
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/com/github/jairrab/calc/Calculator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -41,23 +41,27 @@ interface Calculator {
fun getInstance() = getInstance(
calculatorType = CalculatorType.BASIC_MDAS,
initialNumber = 0.0,
readyToClear = true,
listener = null
)

@JvmStatic
fun getInstance(listener: Listener) = getInstance(
calculatorType = CalculatorType.BASIC_MDAS,
initialNumber = 0.0,
readyToClear = true,
listener = listener
)

@JvmStatic
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -21,6 +23,7 @@ internal class ClearProcessor(
}

fun onCleared() {
entriesManager.clearLastResult()
entriesManager.clearEntries()
entriesManager.setReadyToClear(false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class PercentProcessor(
return
} else {
when {
entriesManager.lastResult != null -> {
entriesManager.isReadyToClear() -> {
outputManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY, entries))
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = ArrayList()
private var result: Double? = null
private var readyToClear = false

fun getEntries(): List<String> {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -63,7 +61,7 @@ internal class OutputManager private constructor(
}

fun getCurrentNumber(): Double {
return currentNumber
return entriesManager.getResult()
}

companion object {
Expand Down

0 comments on commit 9f99b79

Please sign in to comment.