-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.bak
More file actions
158 lines (147 loc) · 5.78 KB
/
output.bak
File metadata and controls
158 lines (147 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#lang racket
(require "preprocessing.rkt"
"processing.rkt"
racket/format)
(provide (all-defined-out))
(define create-output #t)
;; Sorts the transactions based on their timestamp, using Timsort algo
(define (sort-transactions transactions)
(sort transactions
(lambda (a b)
(< (transaction-timestamp a)
(transaction-timestamp b)))))
;; Sorts the accounts based on their account number
(define (sort-accounts accounts)
(sort accounts
(lambda (a b)
(< (user-acct-num a)
(user-acct-num b)))))
;; Helper function to format a transaction into a 54 character string
(define (format-transaction transaction)
(let* ([timestamp (transaction-timestamp transaction)]
[type (string-titlecase
(symbol->string
(transaction-type transaction)))]
[details (cond
[(purchase? transaction)
(purchase-merchant transaction)]
[(cash? transaction)
"Cash"]
[(check? transaction)
"Check"]
[(card? transaction)
(string-titlecase
(symbol->string
(card-method transaction)))])]
[amount (cond
[(purchase? transaction)
(purchase-amount transaction)]
[(cash? transaction)
(cash-amount transaction)]
[(check? transaction)
(check-amount transaction)]
[(card? transaction)
(card-amount transaction)])])
(fprintf (current-output-port)
"~a ~a ~a ~a"
(~a timestamp
#:width 7
#:align `left
#:right-pad-string " ")
(~a type
#:width 11
#:align `left
#:right-pad-string " ")
(~s details
#:width 39
#:max-width 39
#:align `left
#:right-pad-string " ")
(~a
(~r amount
#:precision '(= 2))
#:min-width 10
#:align `right
#:left-pad-string " "))))
;; Function to format and create the output file
(define (create-output-file accounts transactions)
(call-with-output-file "STATEMENT.TXT"
(lambda (out-port)
(for-each
(lambda (account)
(let* ([acct-num
(user-acct-num account)]
[user-name
(user-name account)]
[starting-balance
(user-balance account)]
[filtered-transactions
(filter-by-account transactions acct-num)]
[total-payments
(sum-payments filtered-transactions)]
[total-purchases
(sum-purchases filtered-transactions)]
[ending-balance
(calculate-ending-balance starting-balance
total-payments
total-purchases)]
[sorted-transactions
(sort-transactions filtered-transactions)])
;; Write the statement header
(fprintf out-port "STATEMENT OF ACCOUNT\n")
(fprintf out-port "~a~aStarting Balance: ~a\n\n"
(~a acct-num
#:min-width 20
#:align 'left
#:right-pad-string " ")
(~a user-name
#:min-width 20
#:align 'left
#:right-pad-string " ")
(~a
(~r starting-balance
#:precision '(= 2))
#:min-width 12
#:align 'right
#:left-pad-string " "))
;; Write the transactions
(for-each
(lambda (transaction)
(fprintf out-port "~a\n"
(format-transaction transaction)))
sorted-transactions)
;; Write the summary
(fprintf out-port "\nTotal Purchases:~a\n"
(~a (~r total-purchases
#:precision '(= 2))
#:min-width 16
#:align 'right
#:left-pad-string " "))
(fprintf out-port "Total Payments: ~a\n"
(~a (~r total-payments
#:precision '(= 2))
#:min-width 16
#:align 'right
#:left-pad-string " "))
(fprintf out-port "Ending Balance: ~a\n"
(~a (~r ending-balance
#:precision '(= 2))
#:min-width 16
#:align 'right
#:left-pad-string " "))
;; Write the separator
(fprintf out-port
(string-append "\n" (make-string 70 #\*) "\n"))))
accounts))
#:exists 'truncate))
;; This function is intended to execute only if line 5 is set to true
(define (create-output-check accounts transactions)
(if create-output
(let* ([sorted-accounts (sort-accounts accounts)]
[sorted-transactions (sort-transactions transactions)])
(create-output-file sorted-accounts sorted-transactions))
(begin
(displayln "Output file has not been created due to
initial Bool call.")
(displayln "To create the output file, change 'create-output'
on line 8 to '#t' and try again"))))