Add new class files with Unit tests for tracking the history.
This commit is contained in:
55
app/src/main/java/me/zobrist/tichucounter/History.kt
Normal file
55
app/src/main/java/me/zobrist/tichucounter/History.kt
Normal file
@@ -0,0 +1,55 @@
|
||||
package me.zobrist.tichucounter
|
||||
|
||||
class History {
|
||||
private var scores: ArrayList<Round> = ArrayList()
|
||||
|
||||
fun getScoreA(): Int {
|
||||
var tempScore = 0
|
||||
scores.forEach {
|
||||
tempScore += it.scoreA
|
||||
}
|
||||
return tempScore
|
||||
}
|
||||
|
||||
fun getScoreB(): Int {
|
||||
var tempScore = 0
|
||||
scores.forEach {
|
||||
tempScore += it.scoreB
|
||||
}
|
||||
return tempScore
|
||||
}
|
||||
|
||||
fun getHistoryA(): String {
|
||||
var tempHistory = String()
|
||||
scores.forEach {
|
||||
tempHistory = tempHistory.plus(it.scoreA.toString()).plus("\n")
|
||||
}
|
||||
return tempHistory
|
||||
}
|
||||
|
||||
fun getHistoryB(): String {
|
||||
var tempHistory = String()
|
||||
scores.forEach {
|
||||
tempHistory = tempHistory.plus(it.scoreB.toString()).plus("\n")
|
||||
}
|
||||
return tempHistory
|
||||
}
|
||||
|
||||
fun logRound(round: Round) {
|
||||
scores.add(round)
|
||||
}
|
||||
|
||||
fun revertLastRound() {
|
||||
if (scores.isNotEmpty()) {
|
||||
scores.removeAt(scores.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
fun clearAll() {
|
||||
scores.clear()
|
||||
}
|
||||
|
||||
fun isEmpty(): Boolean {
|
||||
return scores.isEmpty()
|
||||
}
|
||||
}
|
||||
36
app/src/main/java/me/zobrist/tichucounter/Round.kt
Normal file
36
app/src/main/java/me/zobrist/tichucounter/Round.kt
Normal file
@@ -0,0 +1,36 @@
|
||||
package me.zobrist.tichucounter
|
||||
|
||||
class Round() {
|
||||
var scoreA: Int = 0
|
||||
var scoreB: Int = 0
|
||||
|
||||
constructor(score: Int, isScoreA: Boolean) : this() {
|
||||
if (isScoreA) {
|
||||
scoreA = score
|
||||
scoreB = calculateOtherScore(scoreA)
|
||||
} else {
|
||||
scoreB = score
|
||||
scoreA = calculateOtherScore(scoreB)
|
||||
}
|
||||
}
|
||||
|
||||
constructor(scoreA: Int, scoreB: Int) : this() {
|
||||
this.scoreA = scoreA
|
||||
this.scoreB = scoreB
|
||||
}
|
||||
|
||||
private fun calculateOtherScore(score: Int): Int {
|
||||
if (isMultipleOf100(score)) {
|
||||
return 0
|
||||
}
|
||||
return 100 - (score % 100)
|
||||
}
|
||||
|
||||
private fun isMultipleOf100(score: Int): Boolean {
|
||||
return (score / 100) >= 1 && (score % 100) == 0
|
||||
}
|
||||
|
||||
fun isValidRound(): Boolean {
|
||||
return (scoreA % 5 == 0) && (scoreB % 5 == 0) && ((scoreA + scoreB) % 100 == 0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user