Skip to content

Commit 6ed0d03

Browse files
committed
Move total volume credits calc on StatementData
Add Type prop to StatementData so playFor can be removed in volumeCreditsFor calculation.
1 parent 2c151bc commit 6ed0d03

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

refactoring/theatre-practice/statement.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type EnrichedPerformance struct {
2424
Name string
2525
Amount int
2626
Audience int
27+
Type string
2728
}
2829

2930
func (s StatementPrinter) Print(invoice Invoice, plays map[string]Play) (string, error) {
@@ -43,8 +44,9 @@ func (s StatementPrinter) Print(invoice Invoice, plays map[string]Play) (string,
4344
enrichedPerformances = append(enrichedPerformances, ep)
4445
}
4546
statementData.Performances = enrichedPerformances
47+
4648
statementData.TotalAmount = statementData.totalAmount()
47-
statementData.TotalVolumeCredits = s.totalVolumeCredits()
49+
statementData.TotalVolumeCredits = statementData.totalVolumeCredits()
4850

4951
return renderPlayText(statementData)
5052
}
@@ -58,6 +60,8 @@ func (s StatementData) enrichPerformance(perf Performance) (EnrichedPerformance,
5860
result.Amount = amount
5961
result.Audience = perf.Audience
6062
result.Name = s.playFor(perf).Name
63+
result.Type = s.playFor(perf).Type
64+
6165
return result, nil
6266
}
6367

@@ -87,24 +91,6 @@ func (s StatementData) playFor(perf Performance) Play {
8791
return s.plays[perf.PlayID]
8892
}
8993

90-
func renderPlayText(data StatementData) (string, error) {
91-
result := fmt.Sprintf("Statement for %s\n", data.Customer)
92-
93-
for _, perf := range data.Performances {
94-
result += fmt.Sprintf(
95-
" %s: %s (%d seats)\n",
96-
perf.Name,
97-
usd(perf.Amount),
98-
perf.Audience,
99-
)
100-
}
101-
102-
result += fmt.Sprintf("Amount owed is %s\n", usd(data.TotalAmount))
103-
result += fmt.Sprintf("You earned %d credits\n", data.TotalVolumeCredits)
104-
105-
return result, nil
106-
}
107-
10894
func (s StatementData) totalAmount() int {
10995
result := 0
11096
for _, perf := range s.Performances {
@@ -113,29 +99,43 @@ func (s StatementData) totalAmount() int {
11399
return result
114100
}
115101

116-
func (s StatementPrinter) totalVolumeCredits() int {
102+
func (s StatementData) totalVolumeCredits() int {
117103
result := 0
118-
for _, perf := range s.invoice.Performances {
104+
for _, perf := range s.Performances {
119105
result += s.volumeCreditsFor(perf)
120106
}
121107
return result
122108
}
123109

124-
func usd(amount int) string {
125-
ac := accounting.Accounting{Symbol: "$", Precision: 2}
126-
return ac.FormatMoney(float64(amount) / 100)
127-
}
128-
129-
func (s StatementPrinter) volumeCreditsFor(perf Performance) int {
110+
func (StatementData) volumeCreditsFor(perf EnrichedPerformance) int {
130111
result := 0
131112
result += int(math.Max(float64(perf.Audience)-30, 0))
132113

133-
if s.playFor(perf).Type == "comedy" {
114+
if perf.Type == "comedy" {
134115
result += int(math.Floor(float64(perf.Audience) / 5))
135116
}
136117
return result
137118
}
138119

139-
func (s StatementPrinter) playFor(perf Performance) Play {
140-
return s.plays[perf.PlayID]
120+
func renderPlayText(data StatementData) (string, error) {
121+
result := fmt.Sprintf("Statement for %s\n", data.Customer)
122+
123+
for _, perf := range data.Performances {
124+
result += fmt.Sprintf(
125+
" %s: %s (%d seats)\n",
126+
perf.Name,
127+
usd(perf.Amount),
128+
perf.Audience,
129+
)
130+
}
131+
132+
result += fmt.Sprintf("Amount owed is %s\n", usd(data.TotalAmount))
133+
result += fmt.Sprintf("You earned %d credits\n", data.TotalVolumeCredits)
134+
135+
return result, nil
136+
}
137+
138+
func usd(amount int) string {
139+
ac := accounting.Accounting{Symbol: "$", Precision: 2}
140+
return ac.FormatMoney(float64(amount) / 100)
141141
}

0 commit comments

Comments
 (0)