Skip to content

Commit 6634710

Browse files
committed
ch01: PerformanceCalculator and EnrichedPerformance 실습하기 위해 책과 비슷하게 수정
1 parent 9bfa69b commit 6634710

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

.idea/gradle.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/kotlinc.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/kotlin/chapter01/CreateStatementData.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ data class StatementData(
88
var totalVolumeCredits: Int = 0
99
}
1010

11-
class EnrichedPerformance(
12-
val playID: String, val audience: Int, val play: Play
13-
) {
11+
class EnrichedPerformance() {
12+
var playID: String = ""
13+
var audience: Int = 0
1414
var amount: Int = 0
1515
var volumeCredits: Int = 0
16+
var play: Play? = null
1617
var performanceCalculator: PerformanceCalculator? = null
18+
19+
constructor(aPerformance: Performance) : this() {
20+
this.playID = aPerformance.playID
21+
this.audience = aPerformance.audience
22+
}
1723
}
1824

1925
class PerformanceCalculator {
20-
var aPerformance: EnrichedPerformance
26+
var aPerformance: Performance
2127

22-
constructor(aPerformance: EnrichedPerformance) {
28+
constructor(aPerformance: Performance) {
2329
this.aPerformance = aPerformance
2430
}
2531
}
@@ -31,7 +37,7 @@ internal fun createStatementData(plays: Plays, invoice: Invoice): StatementData
3137

3238
fun amountFor(aPerformance: EnrichedPerformance): Int {
3339
var result = 0
34-
when (aPerformance.play.type) {
40+
when (aPerformance.play?.type) {
3541
"tragedy" -> {
3642
result = 40000
3743
if (aPerformance.audience > 30) {
@@ -48,7 +54,7 @@ internal fun createStatementData(plays: Plays, invoice: Invoice): StatementData
4854
}
4955

5056
else -> {
51-
throw Error("알 수 없는 장르: ${aPerformance.play.type}")
57+
throw Error("알 수 없는 장르: ${aPerformance.play?.type}")
5258
}
5359
}
5460

@@ -57,7 +63,7 @@ internal fun createStatementData(plays: Plays, invoice: Invoice): StatementData
5763

5864
fun volumeCreditsFor(aPerformance: EnrichedPerformance): Int {
5965
var result = maxOf(aPerformance.audience - 30, 0)
60-
if (aPerformance.play.type == "comedy") result += aPerformance.audience / 5
66+
if (aPerformance.play?.type == "comedy") result += aPerformance.audience / 5
6167
return result
6268
}
6369

@@ -70,16 +76,16 @@ internal fun createStatementData(plays: Plays, invoice: Invoice): StatementData
7076
}
7177

7278
fun enrichPerformance(aPerformance: Performance): EnrichedPerformance {
73-
val result = EnrichedPerformance(
74-
aPerformance.playID, aPerformance.audience, playFor(aPerformance)
75-
)
79+
val calculator = PerformanceCalculator(aPerformance)
80+
val result = EnrichedPerformance(aPerformance)
81+
result.play = playFor(aPerformance)
7682
result.amount = amountFor(result)
7783
result.volumeCredits = volumeCreditsFor(result)
78-
result.performanceCalculator = PerformanceCalculator(result)
7984
return result
8085
}
8186

82-
val statementData = StatementData(invoice.customer, invoice.performances.map { enrichPerformance(it) }).apply {
87+
val statementData = StatementData(
88+
invoice.customer, invoice.performances.map { enrichPerformance(it) }).apply {
8389
totalAmount = totalAmount(this)
8490
totalVolumeCredits = totalVolumeCredits(this)
8591
}

src/main/kotlin/chapter01/Statement.kt

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

6-
fun usd(number: Int): String {
7-
return NumberFormat.getCurrencyInstance(US).format(number / 100.0)
8-
}
9-
106
fun statement(invoice: Invoice, plays: Plays): String {
117
return renderPlainText(createStatementData(plays, invoice), plays)
128
}
139

1410
private fun renderPlainText(data: StatementData, plays: Plays): String {
11+
fun usd(number: Int): String {
12+
return NumberFormat.getCurrencyInstance(US).format(number / 100.0)
13+
}
14+
1515
var result = "청구 내역 (고객명: ${data.customer})\n"
1616
for (perf in data.performances) {
1717
// 청구 내역을 출력한다.
18-
result += " ${perf.play.name}: ${usd(perf.amount)} (${perf.audience}석)\n"
18+
result += " ${perf.play?.name}: ${usd(perf.amount)} (${perf.audience}석)\n"
1919
}
2020
result += "총액: ${usd(data.totalAmount)}\n"
2121
result += "적립 포인트: ${data.totalVolumeCredits}"
@@ -27,11 +27,15 @@ fun htmlStatement(invoice: Invoice, plays: Plays): String {
2727
}
2828

2929
fun renderHtml(data: StatementData): String {
30+
fun usd(number: Int): String {
31+
return NumberFormat.getCurrencyInstance(US).format(number / 100.0)
32+
}
33+
3034
var result = "<h1>청구 내역 (고객명: ${data.customer})</h1>\n"
3135
result += "<table>\n"
3236
result += "<tr><th>연극</th><th>좌석 수</th><th>금액</th></tr>\n"
3337
for (perf in data.performances) {
34-
result += "<tr><td>${perf.play.name}</td><td>${perf.audience}</td>"
38+
result += "<tr><td>${perf.play?.name}</td><td>${perf.audience}</td>"
3539
result += "<td>${usd(perf.amount)}</td></tr>\n"
3640
}
3741
result += "</table>\n"

0 commit comments

Comments
 (0)