-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
277 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/github/jairrab/calc/CalculatorUpdate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/kotlin/com/github/jairrab/calc/lib/controls/buttons/PercentProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.