-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
248 lines (214 loc) · 7.55 KB
/
main.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"TakeoffHomework/Data"
"bufio"
"errors"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
"syscall"
"time"
)
const StartingCash float64 = 10000
var workingCash float64
var workingAccount *Data.Account
func main() {
fmt.Fprintln(os.Stdout, "Welcome to my example ATM program please authorize an account or "+
"start with the help command if you don't know what to do!")
scanner := bufio.NewScanner(os.Stdin)
log.SetOutput(os.Stdout)
workingCash = StartingCash
for scanner.Scan() {
userInput := scanner.Text()
command := strings.Split(userInput, " ")
switch command[0] {
case "authorize":
//Ignore extra args I guess?
if len(command) >= 3 {
err := handleAuthorize(command[1], command[2])
if err != nil {
log.Println(err)
}
} else {
log.Println("Not enough arguments to complete command")
}
case "withdraw":
//Ignore extra args I guess?
if len(command) >= 2 {
if isAccountAuthorized(workingAccount) {
resetAuthTime(workingAccount)
amount, err := strconv.Atoi(command[1])
if err != nil {
log.Println(err)
}
if amount%20 > 0 {
log.Println("Withdrawal amount must be divisible evenly by 20")
} else {
floatAmount := float64(amount)
err = handleWithdraw(floatAmount, workingAccount)
if err != nil {
log.Println(err)
}
}
}
}
case "deposit":
//Ignore extra args I guess?
if len(command) >= 2 {
if isAccountAuthorized(workingAccount) {
resetAuthTime(workingAccount)
amount, err := strconv.ParseFloat(command[1], 64)
if err != nil {
log.Println(err)
}
handleDeposit(amount, workingAccount)
addHistoryRecord(workingAccount, amount)
}
}
case "balance":
if isAccountAuthorized(workingAccount) {
resetAuthTime(workingAccount)
handleBalance(workingAccount)
}
case "history":
handleHistory(workingAccount)
case "logout":
handleLogout(workingAccount)
case "help":
if len(command) >= 2 {
handleHelp(command[1])
} else {
handleHelp("")
}
case "end":
syscall.Exit(0)
default:
log.Println(fmt.Sprintf("Unrecognized command %s", command[0]))
handleHelp("")
}
}
}
func resetAuthTime(account *Data.Account) {
if account != nil {
account.AuthorizationTime = time.Now()
}
}
func isAccountAuthorized(account *Data.Account) bool {
if account == nil {
log.Println("Authorization Required")
return false
} else if account.AuthorizationTime.Add(2 * time.Minute).Before(time.Now()) {
account.AuthorizationTime = time.Unix(0, 0)
log.Println("Authorization Required")
return false
}
return true
}
func handleAuthorize(accountId string, pin string) error {
//Check to see if we know about the account
if accountToAuthorize, ok := Data.Accounts[accountId]; ok {
if accountToAuthorize.Pin == pin {
accountToAuthorize.AuthorizationTime = time.Now()
workingAccount = accountToAuthorize
log.Println(fmt.Sprintf("%s successfully authorized", accountToAuthorize.AccountId))
} else {
return errors.New("authorization failed")
}
} else {
return errors.New("authorization failed")
}
return nil
}
func handleWithdraw(value float64, account *Data.Account) error {
//I'm going to refuse to dispense money if their account is at 0 as well because otherwise that would be cruel...
if account.Balance <= 0 {
return errors.New("your account is overdrawn! You may not make withdrawals at this time")
} else {
if workingCash == 0 {
return errors.New("unable to process your withdrawal at this time")
} else if workingCash < value {
//How do I adjust value here best?
twentiesLeft := workingCash / 20
twentiesLeft = math.Trunc(twentiesLeft)
value = twentiesLeft * 20
log.Println("unable to dispense full amount requested at this time")
}
account.Balance = account.Balance - value
workingCash = workingCash - value
log.Println(fmt.Sprintf("Amount dispensed: $%.2f", value))
if account.Balance < 0 {
account.Balance = account.Balance - 5
log.Println(fmt.Sprintf("You have been charged an overdraft fee of $5. Current balance: $%.2f", account.Balance))
}
addHistoryRecord(workingAccount, -value)
}
return nil
}
func handleDeposit(amount float64, account *Data.Account) {
account.Balance = account.Balance + amount
workingCash = workingCash + amount
log.Println(fmt.Sprintf("Current balance: %.2f", account.Balance))
}
func handleBalance(account *Data.Account) {
log.Println(fmt.Sprintf("Current balance: %.2f", account.Balance))
}
func addHistoryRecord(account *Data.Account, amount float64) {
account.AccountHistory = append(account.AccountHistory, fmt.Sprintf("%s %.2f %.2f",
time.Now().Format(time.RFC3339), amount, account.Balance))
}
func handleHistory(account *Data.Account) {
if account != nil && len(account.AccountHistory) > 0 {
for i := len(account.AccountHistory) - 1; i >= 0; i-- {
fmt.Println(account.AccountHistory[i])
}
} else {
log.Println(fmt.Sprintf("No history found for account: %s", account.AccountId))
}
}
func handleLogout(account *Data.Account) {
if account != nil && account.AccountId != "" {
//Doesn't matter if the account in no longer authorized so no need to check
account.AuthorizationTime = time.Unix(0, 0)
log.Println(fmt.Sprintf("Account: %s logged out.", account.AccountId))
//Need to reset working account here
workingAccount = &Data.Account{}
} else {
log.Println("No account is currently authorized")
}
}
func handleHelp(extraOutput string) {
//Not using the logger here because it puts ugly timestamps in front of the lines
fmt.Fprintln(os.Stdout, "Welcome to my example ATM program, you may use the following commands: ")
fmt.Fprintln(os.Stdout, "authorize <account_id> <pin>")
fmt.Fprintln(os.Stdout, "Authorizes an account locally until they are logged out. "+
"Will be logged out if there is no activity for 2 minutes")
fmt.Fprintln(os.Stdout, "-------------------------------------------------------------------------")
fmt.Fprintln(os.Stdout, "withdraw <value>")
fmt.Fprintln(os.Stdout, "Removes value from the authorized account. Must be a multiple of 20")
fmt.Fprintln(os.Stdout, "-------------------------------------------------------------------------")
fmt.Fprintln(os.Stdout, "deposit <value>")
fmt.Fprintln(os.Stdout, "Adds value to the authorized account. The deposited amount does not need to be a multiple of 20.")
fmt.Fprintln(os.Stdout, "-------------------------------------------------------------------------")
fmt.Fprintln(os.Stdout, "balance")
fmt.Fprintln(os.Stdout, "Returns the account’s current balance.")
fmt.Fprintln(os.Stdout, "-------------------------------------------------------------------------")
fmt.Fprintln(os.Stdout, "history")
fmt.Fprintln(os.Stdout, "Returns the account’s transaction history.")
fmt.Fprintln(os.Stdout, "-------------------------------------------------------------------------")
fmt.Fprintln(os.Stdout, "logout")
fmt.Fprintln(os.Stdout, "Removes authorization from the current account")
fmt.Fprintln(os.Stdout, "-------------------------------------------------------------------------")
fmt.Fprintln(os.Stdout, "end")
fmt.Fprintln(os.Stdout, "Ends the example ATM program, thanks for trying it out!")
fmt.Fprintln(os.Stdout, "-------------------------------------------------------------------------")
if extraOutput == "secret" {
fmt.Fprintln(os.Stdout, "Known Accounts")
for _, account := range Data.Accounts {
fmt.Fprintf(os.Stdout, "Account ID: %s, PIN: %s\n", account.AccountId, account.Pin)
}
fmt.Fprintln(os.Stdout, "-------------------------------------------------------------------------")
}
}