Skip to content

Commit b5f9bb7

Browse files
committed
Put play for a performance on enriched perf
Move `playFor` to `StatementPrinter` and add `invoice` and `plays` to it so that `plays` can be accessed in `playFor`. Replace usage of `playFor` in `PlainTextStatement` with `enrichedPerformance.play`.
1 parent 7d5e5d2 commit b5f9bb7

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

refactoring/theatre/statement.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ type Performance struct {
2222
Audience int
2323
}
2424

25-
type StatementData struct {
26-
Customer string
27-
Performances []EnrichedPerformance
25+
type StatementPrinter struct {
26+
invoice Invoice
27+
plays map[string]Play
2828
}
2929

30-
type StatementPrinter struct{}
31-
3230
func (s StatementPrinter) Print(invoice Invoice, plays map[string]Play) (string, error) {
31+
s.invoice = invoice
32+
s.plays = plays
3333
statementData := StatementData{
3434
invoice.Customer,
35-
enrichPerformances(invoice.Performances),
35+
s.enrichPerformances(invoice.Performances),
3636
}
3737
return PlainTextStatement{statementData, plays}.Render()
3838
}
@@ -43,22 +43,32 @@ type EnrichedPerformance struct {
4343
play Play
4444
}
4545

46-
func enrichPerformances(performances []Performance) []EnrichedPerformance {
46+
func (s StatementPrinter) enrichPerformances(performances []Performance) []EnrichedPerformance {
4747
var result []EnrichedPerformance
4848
for _, performance := range performances {
4949
enrichedPerformance := EnrichedPerformance{}
5050
enrichedPerformance.PlayID = performance.PlayID
5151
enrichedPerformance.Audience = performance.Audience
52+
enrichedPerformance.play = s.playFor(performance)
5253
result = append(result, enrichedPerformance)
5354
}
5455
return result
5556
}
5657

58+
func (s StatementPrinter) playFor(aPerformance Performance) Play {
59+
return s.plays[aPerformance.PlayID]
60+
}
61+
5762
type PlainTextStatement struct {
5863
data StatementData
5964
plays map[string]Play
6065
}
6166

67+
type StatementData struct {
68+
Customer string
69+
Performances []EnrichedPerformance
70+
}
71+
6272
func (s PlainTextStatement) Render() (string, error) {
6373
result := fmt.Sprintf("Statement for %s\n", s.data.Customer)
6474

@@ -69,7 +79,7 @@ func (s PlainTextStatement) Render() (string, error) {
6979
}
7080
result += fmt.Sprintf(
7181
" %s: %s (%d seats)\n",
72-
s.playFor(perf).Name,
82+
perf.play.Name,
7383
s.usd(amount),
7484
perf.Audience,
7585
)
@@ -109,22 +119,18 @@ func (PlainTextStatement) usd(number int) string {
109119
return ac.FormatMoney(number / 100)
110120
}
111121

112-
func (s PlainTextStatement) volumeCreditsFor(aPerformance EnrichedPerformance) int {
122+
func (PlainTextStatement) volumeCreditsFor(aPerformance EnrichedPerformance) int {
113123
result := int(math.Max(float64(aPerformance.Audience)-30, 0))
114124
// add extra credit for every ten comedy attendees
115-
if s.playFor(aPerformance).Type == "comedy" {
125+
if aPerformance.play.Type == "comedy" {
116126
result += int(math.Floor(float64(aPerformance.Audience) / 5))
117127
}
118128
return result
119129
}
120130

121-
func (s PlainTextStatement) playFor(aPerformance EnrichedPerformance) Play {
122-
return s.plays[aPerformance.PlayID]
123-
}
124-
125-
func (s PlainTextStatement) amountFor(aPerformance EnrichedPerformance) (int, error) {
131+
func (PlainTextStatement) amountFor(aPerformance EnrichedPerformance) (int, error) {
126132
result := 0
127-
switch s.playFor(aPerformance).Type {
133+
switch aPerformance.play.Type {
128134
case "tragedy":
129135
result = 40000
130136
if aPerformance.Audience > 30 {
@@ -137,7 +143,7 @@ func (s PlainTextStatement) amountFor(aPerformance EnrichedPerformance) (int, er
137143
}
138144
result += 300 * aPerformance.Audience
139145
default:
140-
return 0, fmt.Errorf("unknown type: %s", s.playFor(aPerformance).Type)
146+
return 0, fmt.Errorf("unknown type: %s", aPerformance.play.Type)
141147
}
142148
return result, nil
143149
}

0 commit comments

Comments
 (0)