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 9f99b79 commit 3b2c1ae
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ internal class BackspaceProcessor(
when {
entriesManager.isReadyToClear() -> {
entriesManager.setReadyToClear(false)
entriesManager.removeLastEntry()
val entry = entriesManager.getLastEntry().trimEndChar()
if (entry.isNotEmpty()) {
entriesManager.setLastEntry(entry)
} else {
entriesManager.removeLastEntry()
}
}
entriesManager.isLastEntryAPercentNumber() -> {
val entry = entriesManager.getLastEntry().trimEndChar()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ internal class DecimalProcessor(
} else {
when {
entriesManager.isReadyToClear() -> {
entriesManager.clearEntries()
entriesManager.setReadyToClear(false)
entriesManager.clearEntries()
entriesManager.addEntry(DECIMAL.tag)
}
entriesManager.isLastEntryAnOperator() -> entriesManager.addEntry(DECIMAL.tag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ internal class NumberProcessor(
val number = calculatorButton.tag
when {
entriesManager.isNoEntries() -> {
entriesManager.setReadyToClear(false)
entriesManager.addEntry(number)
}
entriesManager.isReadyToClear() -> {
entriesManager.clearEntries()
entriesManager.setReadyToClear(false)
entriesManager.clearEntries()
entriesManager.addEntry(number)
}
entriesManager.isLastEntryAnOperator() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,46 @@ internal class OperatorProcessor(
internal fun processOperator(calculatorButton: CalculatorButton) {
val operator = calculatorButton.tag

if (entriesManager.isNoEntries()) {
entriesManager.addEntry(ZERO.tag)
entriesManager.addEntry(operator)
} else {
when {
entriesManager.isReadyToClear() -> {
entriesManager.setEntriesToResult()
entriesManager.setReadyToClear(false)
when {
entriesManager.isNoEntries() -> {
entriesManager.setReadyToClear(false)
entriesManager.addEntry(ZERO.tag)
entriesManager.addEntry(operator)
}
entriesManager.isReadyToClear() -> {
entriesManager.setReadyToClear(false)
entriesManager.addEntry(operator)
}
entriesManager.isLastEntryAnOperator() -> {
entriesManager.removeLastEntry()
entriesManager.addEntry(operator)
}
entriesManager.isLastEntryAPercentNumber() -> {
entriesManager.addEntry(operator)
}
entriesManager.isLastEntryANumber() -> {
if (entriesManager.isLastEntryEndsWithDecimal()) {
entriesManager.setLastEntry(entriesManager.getLastEntry().trimEndChar())
entriesManager.addEntry(operator)
} else {
entriesManager.addEntry(operator)
}
entriesManager.isLastEntryAnOperator() -> {
}
entriesManager.isLastEntryADecimal() -> {
if (entriesManager.isSingleEntry()) {
entriesManager.removeLastEntry()
entriesManager.addEntry(ZERO.tag)
entriesManager.addEntry(operator)
}
entriesManager.isLastEntryAPercentNumber() -> {
} else {
entriesManager.removeLastEntry()
entriesManager.removeLastEntry()
entriesManager.addEntry(operator)
}
entriesManager.isLastEntryANumber() -> {
if (entriesManager.isLastEntryEndsWithDecimal()) {
entriesManager.setLastEntry(entriesManager.getLastEntry().trimEndChar())
entriesManager.addEntry(operator)
} else {
entriesManager.addEntry(operator)
}
}
entriesManager.isLastEntryADecimal() -> {
if (entriesManager.isSingleEntry()) {
entriesManager.removeLastEntry()
entriesManager.addEntry(ZERO.tag)
entriesManager.addEntry(operator)
} else {
entriesManager.removeLastEntry()
entriesManager.removeLastEntry()
entriesManager.addEntry(operator)
}
}
else -> {
val entries = entriesManager.getEntries()
outputManager.updateListener(InvalidKey(INVALID_OPERATOR_ENTRY, entries))
throw IllegalStateException("Invalid operator entry")
}
}
else -> {
val entries = entriesManager.getEntries()
outputManager.updateListener(InvalidKey(INVALID_OPERATOR_ENTRY, entries))
throw IllegalStateException("Invalid operator entry")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ internal class EntriesManager private constructor() {
entries += entry
}

fun setEntriesToResult() {
entries.clear()
entries += result.toString()
}

fun isNoEntries(): Boolean {
return entries.isEmpty()
}
Expand Down

0 comments on commit 3b2c1ae

Please sign in to comment.