mirror of
https://github.com/ysoftdevs/Theatrical-Players-Refactoring-Kata.git
synced 2026-07-04 12:01:58 +02:00
Converted code to kotlin
This commit is contained in:
@@ -0,0 +1 @@
|
||||
data class Invoice(val customer: String, val performances: List<Performance>)
|
||||
@@ -0,0 +1 @@
|
||||
data class Performance(val playID: String, val audience: Int)
|
||||
@@ -0,0 +1 @@
|
||||
data class Play(val name: String, val type: String)
|
||||
@@ -0,0 +1,51 @@
|
||||
import java.text.NumberFormat
|
||||
import java.util.Locale
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.max
|
||||
|
||||
class StatementPrinter {
|
||||
|
||||
fun print(invoice: Invoice, plays: Map<String, Play>): String {
|
||||
var totalAmount = 0
|
||||
var volumeCredits = 0
|
||||
var result = "Statement for ${invoice.customer}\n"
|
||||
|
||||
val frmt = NumberFormat.getCurrencyInstance(Locale.US)
|
||||
|
||||
for ((playID, audience) in invoice.performances) {
|
||||
val play = plays[playID]
|
||||
var thisAmount = 0
|
||||
|
||||
when (play?.type) {
|
||||
"tragedy" -> {
|
||||
thisAmount = 40000
|
||||
if (audience > 30) {
|
||||
thisAmount += 1000 * (audience - 30)
|
||||
}
|
||||
}
|
||||
"comedy" -> {
|
||||
thisAmount = 30000
|
||||
if (audience > 20) {
|
||||
thisAmount += 10000 + 500 * (audience - 20)
|
||||
}
|
||||
thisAmount += 300 * audience
|
||||
}
|
||||
else -> throw Error("unknown type: {play.type}")
|
||||
}
|
||||
|
||||
// add volume credits
|
||||
volumeCredits += max(audience - 30, 0)
|
||||
// add extra credit for every ten comedy attendees
|
||||
if ("comedy" == play.type) volumeCredits += floor((audience / 5).toDouble()).toInt()
|
||||
|
||||
// print line for this order
|
||||
result += " ${play.name}: ${frmt.format((thisAmount / 100).toLong())} ($audience seats)\n"
|
||||
|
||||
totalAmount += thisAmount
|
||||
}
|
||||
result += "Amount owed is ${frmt.format((totalAmount / 100).toLong())}\n"
|
||||
result += "You earned $volumeCredits credits\n"
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class StatementPrinterTests {
|
||||
|
||||
@Test
|
||||
internal fun exampleStatement() {
|
||||
val expected = "Statement for BigCo\n" +
|
||||
" Hamlet: \$650.00 (55 seats)\n" +
|
||||
" As You Like It: \$580.00 (35 seats)\n" +
|
||||
" Othello: \$500.00 (40 seats)\n" +
|
||||
"Amount owed is \$1,730.00\n" +
|
||||
"You earned 47 credits\n"
|
||||
|
||||
val plays = mapOf(
|
||||
"hamlet" to Play("Hamlet", "tragedy"),
|
||||
"as-like" to Play("As You Like It", "comedy"),
|
||||
"othello" to Play("Othello", "tragedy")
|
||||
)
|
||||
|
||||
val invoice = Invoice(
|
||||
"BigCo", listOf(
|
||||
Performance("hamlet", 55),
|
||||
Performance("as-like", 35),
|
||||
Performance("othello", 40)
|
||||
)
|
||||
)
|
||||
|
||||
val statementPrinter = StatementPrinter()
|
||||
val result = statementPrinter.print(invoice, plays)
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun statementWithNewPlayTypes() {
|
||||
val plays = mapOf(
|
||||
"henry-v" to Play("Henry V", "history"),
|
||||
"as-like" to Play("As You Like It", "pastoral")
|
||||
)
|
||||
|
||||
val invoice = Invoice(
|
||||
"BigCo", listOf(
|
||||
Performance("henry-v", 53),
|
||||
Performance("as-like", 55)
|
||||
)
|
||||
)
|
||||
|
||||
val statementPrinter = StatementPrinter()
|
||||
assertThrows(Error::class.java) { statementPrinter.print(invoice, plays) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user