-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.rkt
More file actions
73 lines (64 loc) · 2.96 KB
/
processing.rkt
File metadata and controls
73 lines (64 loc) · 2.96 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
#lang racket
(provide (all-defined-out))
(require "preprocessing.rkt")
;; Creates a list of only the account numbers in the users account list
(define (only-account-numbers accounts-list)
(map user-acct-num accounts-list))
;; Filters the list of accounts using a given account number
(define (searched-user-account all-accounts account-num)
(first
(filter
(lambda (user)
(= (user-acct-num user)
account-num))
all-accounts)))
;; Filters the list of transactions using a given account number
(define (filter-by-account all-transactions account-number)
(filter (λ (transaction)
(= (transaction-acct-num transaction) account-number))
all-transactions))
#| Given a list of filtered transactions under 1 user account number,
use the amount tag for each payment and add them together to create the
sum payment amount for the account's history|#
(define (sum-payments filtered-transactions)
(foldl (lambda (transaction acc)
(cond [(cash? transaction)
(+ (cash-amount transaction) acc)]
[(check? transaction)
(+ (check-amount transaction) acc)]
[(card? transaction)
(+ (card-amount transaction) acc)]
[else acc]))
0
filtered-transactions))
#| Given a list of filtered transactions under 1 user account number,
use the amount tag for each purchase and add them together to create the
sum purchase amount for the account's history|#
(define (sum-purchases filtered-transactions)
(foldl (lambda (transaction acc)
(if (purchase? transaction)
(+ (purchase-amount transaction) acc)
acc))
0
filtered-transactions))
#| Given a starting balance, total-payments, and total-purchases value,
calculate the amount to add or subtract from the starting balance based
on the change of money given through subtracting the total payments from
purchases. It also allows an easy point of diagnosis, considering that
the net change in any given day can be easily extrapolated if necessary|#
(define (calculate-ending-balance starting-balance
total-payments
total-purchases)
(+ (- total-purchases total-payments) starting-balance))
(define (process-account all-transactions all-accounts account-num)
(let* ([filtered-transactions (filter-by-account all-transactions
account-num)]
[total-payments (sum-payments filtered-transactions)]
[total-purchases (sum-purchases filtered-transactions)]
[user-account (searched-user-account all-accounts account-num)]
[ending-balance (calculate-ending-balance
(user-balance user-account)
total-payments
total-purchases)])
;; Output or return the aggregated data
(values total-payments total-purchases ending-balance)))