Skip to content

Commit c2e5194

Browse files
committed
ch01: renderPlainText 함수 안에 totalVolumeCredits, totalAmount 제거
1 parent 8d2282a commit c2e5194

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/main/kotlin/chapter01/Statement.kt

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import java.util.Locale.US
55

66
data class StatementData(
77
val customer: String, val performances: List<EnrichedPerformance>
8-
)
8+
) {
9+
var totalAmount: Int = 0
10+
var totalVolumeCredits: Int = 0
11+
}
912

1013
class EnrichedPerformance(
1114
val playID: String, val audience: Int, val play: Play
@@ -51,6 +54,22 @@ fun statement(invoice: Invoice, plays: Plays): String {
5154
return result
5255
}
5356

57+
fun totalVolumeCredits(data: StatementData): Int {
58+
var result = 0
59+
for (perf in data.performances) {
60+
result += perf.volumeCredits
61+
}
62+
return result
63+
}
64+
65+
fun totalAmount(data: StatementData): Int {
66+
var result = 0
67+
for (perf in data.performances) {
68+
result += perf.amount
69+
}
70+
return result
71+
}
72+
5473
fun enrichPerformance(aPerformance: Performance): EnrichedPerformance {
5574
val result = EnrichedPerformance(
5675
aPerformance.playID, aPerformance.audience, playFor(aPerformance)
@@ -60,7 +79,10 @@ fun statement(invoice: Invoice, plays: Plays): String {
6079
return result
6180
}
6281

63-
val statementData = StatementData(invoice.customer, invoice.performances.map { enrichPerformance(it) })
82+
val statementData = StatementData(invoice.customer, invoice.performances.map { enrichPerformance(it) }).apply {
83+
totalAmount = totalAmount(this)
84+
totalVolumeCredits = totalVolumeCredits(this)
85+
}
6486
return renderPlainText(statementData, plays)
6587
}
6688

@@ -69,28 +91,13 @@ private fun renderPlainText(data: StatementData, plays: Plays): String {
6991
return NumberFormat.getCurrencyInstance(US).format(number / 100.0)
7092
}
7193

72-
fun totalVolumeCredits(): Int {
73-
var result = 0
74-
for (perf in data.performances) {
75-
result += perf.volumeCredits
76-
}
77-
return result
78-
}
79-
80-
fun totalAmount(): Int {
81-
var result = 0
82-
for (perf in data.performances) {
83-
result += perf.amount
84-
}
85-
return result
86-
}
8794

8895
var result = "청구 내역 (고객명: ${data.customer})\n"
8996
for (perf in data.performances) {
9097
// 청구 내역을 출력한다.
9198
result += " ${perf.play.name}: ${usd(perf.amount)} (${perf.audience}석)\n"
9299
}
93-
result += "총액: ${usd(totalAmount())}\n"
94-
result += "적립 포인트: ${totalVolumeCredits()}"
100+
result += "총액: ${usd(data.totalAmount)}\n"
101+
result += "적립 포인트: ${data.totalVolumeCredits}"
95102
return result
96103
}

0 commit comments

Comments
 (0)