@@ -2,7 +2,6 @@ package theatre
22
33import (
44 "fmt"
5- "math"
65
76 "github.com/leekchan/accounting"
87)
@@ -22,110 +21,15 @@ type Performance struct {
2221 Audience int
2322}
2423
25- type EnrichedPerformance struct {
26- PlayID string
27- Audience int
28- play Play
29- amount int
30- volumeCredits int
31- }
32-
33- type StatementPrinter struct {
34- invoice Invoice
35- plays map [string ]Play
36- }
24+ type StatementPrinter struct {}
3725
3826func (s StatementPrinter ) Print (invoice Invoice , plays map [string ]Play ) (string , error ) {
39- s .invoice = invoice
40- s .plays = plays
41- statementData := StatementData {}
42- statementData .Customer = s .invoice .Customer
43-
44- var enrichedPerformances []EnrichedPerformance
45- for _ , performance := range s .invoice .Performances {
46- enrichedPerformance , err := s .enrichPerformance (performance )
47- if err != nil {
48- return "" , err
49- }
50- enrichedPerformances = append (enrichedPerformances , enrichedPerformance )
51- }
52- statementData .Performances = enrichedPerformances
53-
54- statementData .totalAmount = s .totalAmount (statementData )
55- statementData .totalVolumeCredits = s .totalVolumeCredits (statementData )
56-
57- return renderPlainText (statementData ), nil
58- }
59-
60- func (StatementPrinter ) totalVolumeCredits (data StatementData ) int {
61- result := 0
62- for _ , perf := range data .Performances {
63- result += perf .volumeCredits
64- }
65- return result
66- }
67-
68- func (StatementPrinter ) totalAmount (data StatementData ) int {
69- result := 0
70- for _ , perf := range data .Performances {
71- result += perf .amount
72- }
73- return result
74- }
75-
76- func (s StatementPrinter ) enrichPerformance (performance Performance ) (EnrichedPerformance , error ) {
77- result := EnrichedPerformance {}
78- result .PlayID = performance .PlayID
79- result .Audience = performance .Audience
80- result .play = s .playFor (performance )
81- amount , err := s .amountFor (result )
27+ statementData , err := createStatementData (invoice , plays )
8228 if err != nil {
83- return EnrichedPerformance {}, err
84- }
85- result .amount = amount
86- result .volumeCredits = s .volumeCreditsFor (result )
87-
88- return result , nil
89- }
90-
91- func (StatementPrinter ) volumeCreditsFor (aPerformance EnrichedPerformance ) int {
92- result := int (math .Max (float64 (aPerformance .Audience )- 30 , 0 ))
93- // add extra credit for every ten comedy attendees
94- if aPerformance .play .Type == "comedy" {
95- result += int (math .Floor (float64 (aPerformance .Audience ) / 5 ))
29+ return "" , err
9630 }
97- return result
98- }
99-
100- func (s StatementPrinter ) playFor (aPerformance Performance ) Play {
101- return s .plays [aPerformance .PlayID ]
102- }
10331
104- func (StatementPrinter ) amountFor (aPerformance EnrichedPerformance ) (int , error ) {
105- result := 0
106- switch aPerformance .play .Type {
107- case "tragedy" :
108- result = 40000
109- if aPerformance .Audience > 30 {
110- result += 1000 * (aPerformance .Audience - 30 )
111- }
112- case "comedy" :
113- result = 30000
114- if aPerformance .Audience > 20 {
115- result += 10000 + 500 * (aPerformance .Audience - 20 )
116- }
117- result += 300 * aPerformance .Audience
118- default :
119- return 0 , fmt .Errorf ("unknown type: %s" , aPerformance .play .Type )
120- }
121- return result , nil
122- }
123-
124- type StatementData struct {
125- Customer string
126- Performances []EnrichedPerformance
127- totalAmount int
128- totalVolumeCredits int
32+ return renderPlainText (statementData ), nil
12933}
13034
13135func renderPlainText (data StatementData ) string {
@@ -140,8 +44,8 @@ func renderPlainText(data StatementData) string {
14044 )
14145 }
14246
143- result += fmt .Sprintf ("Amount owed is %s\n " , usd (data .totalAmount ))
144- result += fmt .Sprintf ("You earned %d credits\n " , data .totalVolumeCredits )
47+ result += fmt .Sprintf ("Amount owed is %s\n " , usd (data .TotalAmount ))
48+ result += fmt .Sprintf ("You earned %d credits\n " , data .TotalVolumeCredits )
14549 return result
14650}
14751
0 commit comments