Skip to content

Commit

Permalink
percent support
Browse files Browse the repository at this point in the history
  • Loading branch information
jairrab committed Sep 23, 2020
1 parent cc3ff6c commit 9bacd6d
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 115 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ plugins {
* Use plugin 'kotlin' if importing this project into Android Studio
* Use plugin 'org.jetbrains.kotlin.jvm' version '1.3.61' to build library
*/
//id 'kotlin'
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
id 'kotlin'
// id 'org.jetbrains.kotlin.jvm' version '1.4.10'
}

allprojects {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/jairrab/calc/Calculator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface Calculator {
fun setListener(listener: Listener)

interface Listener {
fun onCalculatorUpdate(key: String?, entries: List<String>, result: Double)
fun onCalculatorUpdate(update: CalculatorUpdate)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ enum class CalculatorButton(val tag:String){
MULTIPLY("*"),
DIVISION("/"),
BACKSPACE("backspace"),
PERCENT("%"),
EQUALS("=")
}
13 changes: 13 additions & 0 deletions src/main/kotlin/com/github/jairrab/calc/CalculatorUpdate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.jairrab.calc

sealed class CalculatorUpdate {
object Initializing : CalculatorUpdate()

class OnUpdate(
val key: String?,
val entries: List<String>,
val result: Double
) : CalculatorUpdate()

class InvalidKey(val invalidKeyType: InvalidKeyType) : CalculatorUpdate()
}
7 changes: 7 additions & 0 deletions src/main/kotlin/com/github/jairrab/calc/InvalidKeyType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.jairrab.calc

enum class InvalidKeyType {
INVALID_DECIMAL_ENTRY,
INVALID_OPERATOR_ENTRY,
INVALID_PERCENT_ENTRY,
}
7 changes: 2 additions & 5 deletions src/main/kotlin/com/github/jairrab/calc/driver/TestDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ package com.github.jairrab.calc.driver

import com.github.jairrab.calc.Calculator
import com.github.jairrab.calc.CalculatorType
import com.github.jairrab.calc.CalculatorUpdate

fun main() {
val calculator = Calculator.getInstance(
calculatorType = CalculatorType.BASIC_MDAS,
initialNumber = 0.0,
listener = object : Calculator.Listener {
override fun onCalculatorUpdate(
key: String?,
entries: List<String>,
result: Double
) {
override fun onCalculatorUpdate(update: CalculatorUpdate) {
//See runtime logs for output
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal open class CalculatorUtility(
CalculatorButton.MULTIPLY -> pressMultiply()
CalculatorButton.DIVISION -> pressDivide()
CalculatorButton.BACKSPACE -> backSpace()
CalculatorButton.PERCENT -> pressPercent()
CalculatorButton.EQUALS -> pressEquals()
}
}
Expand Down Expand Up @@ -119,7 +120,8 @@ internal open class CalculatorUtility(
}

override fun pressPercent() {
TODO("Not yet implemented")
controlProcessor.percentProcessor.processPercent()
controlProcessor.displayManager.update(CalculatorButton.PERCENT)
}

override fun backSpace() {
Expand All @@ -133,6 +135,6 @@ internal open class CalculatorUtility(
}

override fun setListener(listener: Calculator.Listener) {
controlProcessor.displayManager.setListener(listener)
controlProcessor.setListener(listener)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ package com.github.jairrab.calc.lib.controls

import com.github.jairrab.calc.Calculator
import com.github.jairrab.calc.CalculatorType
import com.github.jairrab.calc.lib.controls.buttons.BackspaceProcessor
import com.github.jairrab.calc.lib.controls.buttons.ClearProcessor
import com.github.jairrab.calc.lib.controls.buttons.DecimalProcessor
import com.github.jairrab.calc.lib.controls.buttons.NumberProcessor
import com.github.jairrab.calc.lib.controls.buttons.OperatorProcessor
import com.github.jairrab.calc.lib.controls.buttons.*
import com.github.jairrab.calc.lib.controls.entries.EntriesManager
import com.github.jairrab.calc.lib.controls.outputs.DisplayManager

Expand All @@ -18,20 +14,31 @@ class ControlProcessor private constructor(
val backspaceProcessor: BackspaceProcessor,
val decimalProcessor: DecimalProcessor,
val numberProcessor: NumberProcessor,
val operatorProcessor: OperatorProcessor
val operatorProcessor: OperatorProcessor,
val percentProcessor: PercentProcessor,
) {
fun setListener(listener: Calculator.Listener) {
displayManager.listener = listener
}

companion object {
fun getInstance(
entriesManager: EntriesManager,
calculatorType: CalculatorType,
listener: Calculator.Listener?
) = ControlProcessor(
displayManager = DisplayManager.getInstance(entriesManager, calculatorType, listener),
clearProcessor = ClearProcessor(entriesManager),
backspaceProcessor = BackspaceProcessor(entriesManager),
decimalProcessor = DecimalProcessor(entriesManager),
numberProcessor = NumberProcessor(entriesManager),
operatorProcessor = OperatorProcessor(entriesManager)
)
): ControlProcessor {
val displayManager = DisplayManager
.getInstance(entriesManager, calculatorType, listener)

return ControlProcessor(
displayManager = displayManager,
clearProcessor = ClearProcessor(entriesManager),
backspaceProcessor = BackspaceProcessor(entriesManager),
decimalProcessor = DecimalProcessor(entriesManager, displayManager),
numberProcessor = NumberProcessor(entriesManager),
operatorProcessor = OperatorProcessor(entriesManager, displayManager),
percentProcessor = PercentProcessor(entriesManager, displayManager)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class BackspaceProcessor(
entriesManager.lastResult = null
entriesManager.removeLastEntry()
}
entriesManager.isLastEntryAPercentNumber() -> {
val entry = entriesManager.getLastEntry().trimEndChar()
entriesManager.setLastEntry(entry)
}
entriesManager.isLastEntryANumber() -> {
val entry = entriesManager.getLastEntry().trimEndChar()
if (entry.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

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.InvalidKeyType.INVALID_DECIMAL_ENTRY
import com.github.jairrab.calc.lib.controls.entries.EntriesManager
import com.github.jairrab.calc.lib.controls.outputs.DisplayManager
import com.github.jairrab.calc.lib.utils.trimEndChar


class DecimalProcessor(
private val entriesManager: EntriesManager
private val entriesManager: EntriesManager,
private val displayManager: DisplayManager
) {
fun processDecimal() {
if (entriesManager.isNoEntries()) {
Expand All @@ -19,9 +27,17 @@ class DecimalProcessor(
entriesManager.addEntry(DECIMAL.tag)
}
entriesManager.isLastEntryAnOperator() -> entriesManager.addEntry(DECIMAL.tag)
entriesManager.isLastEntryANumberWithDecimal() -> return
entriesManager.isLastEntryANumberWithDecimal() -> {
displayManager.updateListener(InvalidKey(INVALID_DECIMAL_ENTRY))
return
}
entriesManager.isLastEntryAPercentNumber() -> {
val entry = entriesManager.getLastEntry().trimEndChar()
entriesManager.setLastEntry(entry)
entriesManager.appendToLastEntry(DECIMAL.tag)
}
entriesManager.isLastEntryANumber() -> {
entriesManager.setLastEntry(entriesManager.getLastEntry() + DECIMAL.tag)
entriesManager.appendToLastEntry(DECIMAL.tag)
}
entriesManager.isLastEntryADecimal() -> return
else -> throw IllegalStateException("Invalid decimal command")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.github.jairrab.calc.lib.controls.buttons

import com.github.jairrab.calc.CalculatorButton
import com.github.jairrab.calc.lib.controls.entries.EntriesManager
import com.github.jairrab.calc.lib.utils.trimEndChar

class NumberProcessor(
private val entriesManager: EntriesManager
Expand All @@ -14,22 +15,27 @@ class NumberProcessor(
entriesManager.addEntry(number)
} else {
when {
entriesManager.lastResult != null -> {
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.setLastEntry(entriesManager.getLastEntry() + number)
entriesManager.appendToLastEntry(number)
}
}
entriesManager.isLastEntryADecimal() -> {
entriesManager.setLastEntry(entriesManager.getLastEntry() + number)
entriesManager.appendToLastEntry(number)
}
else -> throw IllegalStateException("Invalid number command")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

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.InvalidKeyType.INVALID_OPERATOR_ENTRY
import com.github.jairrab.calc.lib.controls.entries.EntriesManager
import com.github.jairrab.calc.lib.controls.outputs.DisplayManager
import com.github.jairrab.calc.lib.utils.trimEndChar

class OperatorProcessor(
private val entriesManager: EntriesManager
private val entriesManager: EntriesManager,
private val displayManager: DisplayManager
) {
internal fun processOperator(calculatorButton: CalculatorButton) {
val operator = calculatorButton.tag
Expand All @@ -27,6 +32,9 @@ class OperatorProcessor(
entriesManager.removeLastEntry()
entriesManager.addEntry(operator)
}
entriesManager.isLastEntryAPercentNumber() -> {
entriesManager.addEntry(operator)
}
entriesManager.isLastEntryANumber() -> {
if (entriesManager.isLastEntryEndsWithDecimal()) {
entriesManager.setLastEntry(entriesManager.getLastEntry().trimEndChar())
Expand All @@ -47,7 +55,8 @@ class OperatorProcessor(
}
}
else -> {
entriesManager.addEntry(operator)
displayManager.updateListener(InvalidKey(INVALID_OPERATOR_ENTRY))
throw IllegalStateException("Invalid operator entry")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* This code is licensed under MIT license (see LICENSE.txt for details) */

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.InvalidKeyType.INVALID_PERCENT_ENTRY
import com.github.jairrab.calc.lib.controls.entries.EntriesManager
import com.github.jairrab.calc.lib.controls.outputs.DisplayManager
import com.github.jairrab.calc.lib.utils.trimEndChar

class PercentProcessor(
private val entriesManager: EntriesManager,
private val displayManager: DisplayManager
) {
internal fun processPercent() {
if (entriesManager.isNoEntries()) {
displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY))
return
} else {
when {
entriesManager.lastResult != null -> {
displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY))
return
}
entriesManager.isLastEntryAnOperator() -> {
displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY))
return
}
entriesManager.isLastEntryAPercentNumber() -> {
displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY))
return
}
entriesManager.isLastEntryANumber() -> {
if (entriesManager.isLastEntryEndsWithDecimal()) {
entriesManager.setLastEntry(entriesManager.getLastEntry().trimEndChar())
}
entriesManager.appendToLastEntry(CalculatorButton.PERCENT.tag)
}
entriesManager.isLastEntryADecimal() -> {
displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY))
return
}
else -> {
displayManager.updateListener(InvalidKey(INVALID_PERCENT_ENTRY))
throw IllegalStateException("Invalid operator entry")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

package com.github.jairrab.calc.lib.controls.entries

import com.github.jairrab.calc.CalculatorButton
import com.github.jairrab.calc.CalculatorButton.DECIMAL
import com.github.jairrab.calc.lib.mathutils.OperatorUtils.operatorTags
import com.github.jairrab.calc.lib.utils.trimEndChar

class EntriesManager private constructor() {
var lastResult: Double? = null
Expand Down Expand Up @@ -52,10 +54,23 @@ class EntriesManager private constructor() {
entries[entries.lastIndex] = entry
}

fun appendToLastEntry(text: String) {
setLastEntry(getLastEntry() + text)
}

fun getLastEntry(): String {
return entries.last()
}

fun getLastDoubleEntry(): Double {
val lastEntry = getLastEntry()
return if (lastEntry.endsWith(CalculatorButton.PERCENT.tag)) {
lastEntry.trimEndChar().toDouble() / 100.0
} else {
lastEntry.toDouble()
}
}

fun isLastEntryADecimal(): Boolean {
return getLastEntry() == DECIMAL.tag
}
Expand All @@ -64,12 +79,16 @@ class EntriesManager private constructor() {
return operatorTags.contains(getLastEntry())
}

fun isLastEntryAPercentNumber(): Boolean {
return (getLastEntry().endsWith(CalculatorButton.PERCENT.tag))
}

fun isLastEntryANumber(): Boolean {
return !isLastEntryAnOperator() && !isLastEntryADecimal()
return getLastEntry().toDoubleOrNull() != null
}

fun isLastEntryANumberWithDecimal(): Boolean {
return if (isLastEntryANumber()) {
return if (isLastEntryANumber() || isLastEntryAPercentNumber()) {
getLastEntry().contains(DECIMAL.tag)
} else false
}
Expand Down
Loading

0 comments on commit 9bacd6d

Please sign in to comment.