Skip to content

Commit 33ad923

Browse files
committed
ch01: statement에 필요한 데이터 처리를 하는 함수 추출
1 parent 7097c11 commit 33ad923

File tree

2 files changed

+78
-72
lines changed

2 files changed

+78
-72
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package chapter01
2+
3+
4+
data class StatementData(
5+
val customer: String, val performances: List<EnrichedPerformance>
6+
) {
7+
var totalAmount: Int = 0
8+
var totalVolumeCredits: Int = 0
9+
}
10+
11+
class EnrichedPerformance(
12+
val playID: String, val audience: Int, val play: Play
13+
) {
14+
var amount: Int = 0
15+
var volumeCredits: Int = 0
16+
}
17+
18+
internal fun createStatementData(plays: Plays, invoice: Invoice): StatementData {
19+
fun playFor(aPerformance: Performance): Play {
20+
return plays[aPerformance.playID]!!
21+
}
22+
23+
fun amountFor(aPerformance: EnrichedPerformance): Int {
24+
var result = 0
25+
when (aPerformance.play.type) {
26+
"tragedy" -> {
27+
result = 40000
28+
if (aPerformance.audience > 30) {
29+
result += 1000 * (aPerformance.audience - 30)
30+
}
31+
}
32+
33+
"comedy" -> {
34+
result = 30000
35+
if (aPerformance.audience > 20) {
36+
result += 10000 + 500 * (aPerformance.audience - 20)
37+
}
38+
result += 300 * aPerformance.audience
39+
}
40+
41+
else -> {
42+
throw Error("알 수 없는 장르: ${aPerformance.play.type}")
43+
}
44+
}
45+
46+
return result
47+
}
48+
49+
fun volumeCreditsFor(aPerformance: EnrichedPerformance): Int {
50+
var result = maxOf(aPerformance.audience - 30, 0)
51+
if (aPerformance.play.type == "comedy") result += aPerformance.audience / 5
52+
return result
53+
}
54+
55+
fun totalVolumeCredits(data: StatementData): Int {
56+
return data.performances.sumOf { it.volumeCredits }
57+
}
58+
59+
fun totalAmount(data: StatementData): Int {
60+
return data.performances.sumOf { it.amount }
61+
}
62+
63+
fun enrichPerformance(aPerformance: Performance): EnrichedPerformance {
64+
val result = EnrichedPerformance(
65+
aPerformance.playID, aPerformance.audience, playFor(aPerformance)
66+
)
67+
result.amount = amountFor(result)
68+
result.volumeCredits = volumeCreditsFor(result)
69+
return result
70+
}
71+
72+
val statementData = StatementData(invoice.customer, invoice.performances.map { enrichPerformance(it) }).apply {
73+
totalAmount = totalAmount(this)
74+
totalVolumeCredits = totalVolumeCredits(this)
75+
}
76+
return statementData
77+
}

src/main/kotlin/chapter01/Statement.kt

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,8 @@ 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-
var totalAmount: Int = 0
10-
var totalVolumeCredits: Int = 0
11-
}
12-
13-
class EnrichedPerformance(
14-
val playID: String, val audience: Int, val play: Play
15-
) {
16-
var amount: Int = 0
17-
var volumeCredits: Int = 0
18-
}
19-
206
fun statement(invoice: Invoice, plays: Plays): String {
21-
fun playFor(aPerformance: Performance): Play {
22-
return plays[aPerformance.playID]!!
23-
}
24-
25-
fun amountFor(aPerformance: EnrichedPerformance): Int {
26-
var result = 0
27-
when (aPerformance.play.type) {
28-
"tragedy" -> {
29-
result = 40000
30-
if (aPerformance.audience > 30) {
31-
result += 1000 * (aPerformance.audience - 30)
32-
}
33-
}
34-
35-
"comedy" -> {
36-
result = 30000
37-
if (aPerformance.audience > 20) {
38-
result += 10000 + 500 * (aPerformance.audience - 20)
39-
}
40-
result += 300 * aPerformance.audience
41-
}
42-
43-
else -> {
44-
throw Error("알 수 없는 장르: ${aPerformance.play.type}")
45-
}
46-
}
47-
48-
return result
49-
}
50-
51-
fun volumeCreditsFor(aPerformance: EnrichedPerformance): Int {
52-
var result = maxOf(aPerformance.audience - 30, 0)
53-
if (aPerformance.play.type == "comedy") result += aPerformance.audience / 5
54-
return result
55-
}
56-
57-
fun totalVolumeCredits(data: StatementData): Int {
58-
return data.performances.sumOf { it.volumeCredits }
59-
}
60-
61-
fun totalAmount(data: StatementData): Int {
62-
return data.performances.sumOf { it.amount }
63-
}
64-
65-
fun enrichPerformance(aPerformance: Performance): EnrichedPerformance {
66-
val result = EnrichedPerformance(
67-
aPerformance.playID, aPerformance.audience, playFor(aPerformance)
68-
)
69-
result.amount = amountFor(result)
70-
result.volumeCredits = volumeCreditsFor(result)
71-
return result
72-
}
73-
74-
val statementData = StatementData(invoice.customer, invoice.performances.map { enrichPerformance(it) }).apply {
75-
totalAmount = totalAmount(this)
76-
totalVolumeCredits = totalVolumeCredits(this)
77-
}
78-
return renderPlainText(statementData, plays)
7+
return renderPlainText(createStatementData(plays, invoice), plays)
798
}
809

8110
private fun renderPlainText(data: StatementData, plays: Plays): String {

0 commit comments

Comments
 (0)