Skip to content

Commit 1df7999

Browse files
committed
ch01: 공연 객체 복사하여 전달할 수 있도록 수정, mutableMap에서 data class쓰도록 변경
1 parent 93a85a9 commit 1df7999

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/main/kotlin/chapter01/Invoice.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ data class Invoice(
1111
@Serializable
1212
data class Performance(
1313
val playID: String,
14-
val audience: Int
14+
val audience: Int,
1515
)

src/main/kotlin/chapter01/Statement.kt

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,35 @@ package chapter01
33
import java.text.NumberFormat
44
import java.util.Locale.US
55

6+
data class StatementData(
7+
val customer: String, val performances: List<EnrichedPerformance>
8+
)
9+
10+
data class EnrichedPerformance(
11+
val playID: String, val audience: Int, val play: Play
12+
)
13+
614
fun statement(invoice: Invoice, plays: Plays): String {
7-
val statementData = mutableMapOf<String, Any>()
8-
statementData.put("customer", invoice.customer)
15+
fun playFor(aPerformance: Performance): Play {
16+
return plays[aPerformance.playID]!!
17+
}
918

10-
fun enrichPerformance(performance: Performance): Performance {
11-
val result = performance.copy()
12-
return result
19+
fun enrichPerformance(performance: Performance): EnrichedPerformance {
20+
return EnrichedPerformance(
21+
performance.playID, performance.audience, playFor(performance)
22+
)
1323
}
14-
statementData.put("performances", invoice.performances.map { enrichPerformance(it) })
1524

25+
val statementData = StatementData(invoice.customer, invoice.performances.map { enrichPerformance(it) })
1626
return renderPlainText(statementData, plays)
1727
}
1828

19-
private fun renderPlainText(data: MutableMap<String, Any>, plays: Plays): String {
20-
fun playFor(aPerformance: Performance): Play {
29+
private fun renderPlainText(data: StatementData, plays: Plays): String {
30+
fun playFor(aPerformance: EnrichedPerformance): Play {
2131
return plays[aPerformance.playID]!!
2232
}
2333

24-
fun amountFor(aPerformance: Performance): Int {
34+
fun amountFor(aPerformance: EnrichedPerformance): Int {
2535
var result = 0
2636
when (playFor(aPerformance).type) {
2737
"tragedy" -> {
@@ -47,10 +57,9 @@ private fun renderPlainText(data: MutableMap<String, Any>, plays: Plays): String
4757
return result
4858
}
4959

50-
fun volumeCreditsFor(aPerformance: Performance): Int {
60+
fun volumeCreditsFor(aPerformance: EnrichedPerformance): Int {
5161
var result = maxOf(aPerformance.audience - 30, 0)
52-
if (playFor(aPerformance).type == "comedy")
53-
result += aPerformance.audience / 5
62+
if (playFor(aPerformance).type == "comedy") result += aPerformance.audience / 5
5463
return result
5564
}
5665

@@ -60,22 +69,22 @@ private fun renderPlainText(data: MutableMap<String, Any>, plays: Plays): String
6069

6170
fun totalVolumeCredits(): Int {
6271
var result = 0
63-
for (perf in data["performances"] as List<Performance>) {
72+
for (perf in data.performances) {
6473
result += volumeCreditsFor(perf)
6574
}
6675
return result
6776
}
6877

6978
fun totalAmount(): Int {
7079
var result = 0
71-
for (perf in data["performances"] as List<Performance>) {
80+
for (perf in data.performances) {
7281
result += amountFor(perf)
7382
}
7483
return result
7584
}
7685

77-
var result = "청구 내역 (고객명: ${data["customer"]})\n"
78-
for (perf in data["performances"] as List<Performance>) {
86+
var result = "청구 내역 (고객명: ${data.customer})\n"
87+
for (perf in data.performances) {
7988
// 청구 내역을 출력한다.
8089
result += " ${playFor(perf).name}: ${usd(amountFor(perf))} (${perf.audience}석)\n"
8190
}

0 commit comments

Comments
 (0)