-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
358 lines (287 loc) · 9.08 KB
/
ATM.java
File metadata and controls
358 lines (287 loc) · 9.08 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import java.text.DecimalFormat;
public class ATM {
public static void main(String[] args) {
// Initialize GUI
Gui gui = new Gui();
// / Initialize Bank
Bank bank = new Bank("Bank of Java");
// Add a user and create savings account
User user = bank.addUser("John", "Smith", "1234");
// Add a checking account for our user
Account newAccount = new Account("Checking", user, bank);
user.addAccount(newAccount);
bank.addAccount(newAccount);
User currentUser;
while (true) {
// Stay in the login prompt until successful login
currentUser = ATM.mainMenuPrompt(bank, gui);
// Stay in main menu until user quits
gui.setMainDisplay("");
ATM.printUserMenu(currentUser, gui);
}
}
/**
* Print the ATM's login menu
*
* @param bank
* The Bank object whose accounts to use
* @param gui
* The Gui object used for user input
* @return Authenticated user
*/
private static User mainMenuPrompt(Bank bank, Gui gui) {
// Initialize
String userID;
String pin;
User authUser;
// Prompt the user for user ID/pin combo until a correct on is reached
do {
// System.out.printf
gui.appendMainDisplay(String.format("Welcome to %s.\n",
bank.getName()));
gui.appendMainDisplay("Enter user ID: ");
userID = gui.getInput();
gui.appendMainDisplay("\nEnter PIN: ");
gui.setPwMasker(true);
pin = gui.getInput();
gui.setPwMasker(false);
// Try to get the user object corresponding to the ID/pin combo
authUser = bank.userLogin(userID, pin);
if (authUser == null) {
gui.setMainDisplay("Incorrect user ID/PIN combination. "
+ "Please try again.\n\n");
}
} while (authUser == null); // Loop until successful login
return authUser;
}
/**
* Prints the main menu once a valid customer has logged in
*
* @param user
* The logged-in User object
* @param gui
* The Gui object used for user input
*/
private static void printUserMenu(User user, Gui gui) {
// Print a summary of the user's accounts
user.printAccountsSummary(gui);
// Initialize
int choice;
// User menu
do {
gui.appendMainDisplay(String.format(
"\nWelcome %s, what would you like to do?\n",
user.getFirstName()));
gui.appendMainDisplay(" 1) Show account transaction history\n");
gui.appendMainDisplay(" 2) Withdraw\n");
gui.appendMainDisplay(" 3) Deposit\n");
gui.appendMainDisplay(" 4) Transfer\n");
gui.appendMainDisplay(" 5) Quit\n");
choice = Integer.parseInt(gui.getInput());
if (choice < 1 || choice > 5) {
gui.setMainDisplay("Invalid choice. Please choose 1-5\n\n");
}
} while (choice < 1 || choice > 5);
// Process the choice
switch (choice) {
case 1:
ATM.showTransHistory(user, gui);
break;
case 2:
ATM.withdrawFunds(user, gui);
gui.setMainDisplay("");
break;
case 3:
ATM.depositFunds(user, gui);
gui.setMainDisplay("");
break;
case 4:
ATM.transferFunds(user, gui);
gui.setMainDisplay("");
break;
case 5:
gui.setMainDisplay("");
}
// Redisplay the menu unit the user wants to quit
if (choice != 5) {
ATM.printUserMenu(user, gui);
}
}
/**
* Process a fund deposit to an account
*
* @param user
* The logged-in User object
* @param gui
* The Gui object used for user input
*/
private static void depositFunds(User user, Gui gui) {
// Initialize
int toAccount;
double amount;
DecimalFormat df = new DecimalFormat("#.00");
gui.setMainDisplay("");
// Get the account to deposit to
do {
gui.appendMainDisplay(String.format(
"Enter the number (1-%d) of the account "
+ "to deposit to: ", user.numAccounts()));
toAccount = Integer.parseInt(gui.getInput()) - 1;
if (toAccount < 0 || toAccount >= user.numAccounts()) {
gui.setMainDisplay("Invalid choice. Please try again.\n\n");
}
} while (toAccount < 0 || toAccount >= user.numAccounts());
// Get the amount to deposit
do {
gui.appendMainDisplay("\nEnter the amount to deposit: $");
gui.setDecimal(true);
amount = Double.parseDouble(gui.getInput());
gui.setDecimal(false);
if (amount < 0.01) {
gui.setMainDisplay("Amount must be greater than zero.\n");
}
} while (amount < 0.01);
// Finally, do the deposit
user.addAcctTransaction(toAccount, amount, "Deposit");
gui.setMainDisplay("");
}
/**
* Process a fund withdraw from an account
*
* @param user
* The logged-in User object
* @param gui
* The Gui object used for user input
*/
private static void withdrawFunds(User user, Gui gui) {
// Initialize
int fromAccount;
double amount;
double accountBalance;
String memo;
gui.setMainDisplay("");
// Get the account to withdraw from
do {
gui.appendMainDisplay(String.format(
"Enter the number (1-%d) of the account "
+ "to withdraw from: ", user.numAccounts()));
fromAccount = Integer.parseInt(gui.getInput()) - 1;
if (fromAccount < 0 || fromAccount >= user.numAccounts()) {
gui.setMainDisplay("Invalid choice. Please try again.\n\n");
} else if (user.getAccountBalance(fromAccount) < 0.01) {
gui.setMainDisplay("Insufficient funds in that account.\n\n");
printUserMenu(user, gui);
}
} while (fromAccount < 0 || fromAccount >= user.numAccounts());
accountBalance = user.getAccountBalance(fromAccount);
// Get the amount to withdraw
do {
gui.appendMainDisplay(String.format(
"\nEnter the amount to withdraw (max $%.02f): $",
accountBalance));
gui.setDecimal(true);
amount = Double.parseDouble(gui.getInput());
gui.setDecimal(false);
if (amount < 0.01) {
gui.setMainDisplay("Amount must be greater than zero.\\nn");
} else if (amount > accountBalance) {
gui.setMainDisplay(String.format(
"Amount must not be greater than "
+ "the balance of $%.02f.\n", accountBalance));
}
} while (amount < 0.01 || amount > accountBalance);
// Finally, do the withdrawal
user.addAcctTransaction(fromAccount, -1 * amount, "Withdrawal");
}
/**
* Shows the transaction history for an account
*
* @param user
* The logged-in User object
* @param gui
* The Gui object used for user input
*/
private static void showTransHistory(User user, Gui gui) {
int account;
// Get the account whose transaction history to look at
do {
gui.setMainDisplay(String.format(
"Enter the number (1-%d) of the account "
+ "whose transactions you want to see: ",
user.numAccounts()));
account = Integer.parseInt(gui.getInput()) - 1;
if (account < 0 || account >= user.numAccounts()) {
gui.appendMainDisplay("Invalid account. Please try again.\n\n");
}
} while (account < 0 || account >= user.numAccounts());
// Print the transaction history
user.printAccountSummary(account, gui);
}
/**
* Process transferring funds from one account to another
*
* @param user
* The logged-in User object
* @param gui
* The Gui object used for user input
*/
private static void transferFunds(User user, Gui gui) {
// Initialize
int fromAccount;
int toAccount;
double amount;
double accountBalance;
gui.setMainDisplay("");
// Get the account to transfer from
do {
gui.appendMainDisplay(String.format(
"Enter the number (1-%d) of the account "
+ "to transer from: ", user.numAccounts()));
fromAccount = Integer.parseInt(gui.getInput()) - 1;
if (fromAccount < 0 || fromAccount >= user.numAccounts()) {
gui.setMainDisplay("Invalid choice. Please try again\n\n");
} else if (user.getAccountBalance(fromAccount) < 0.01) {
gui.setMainDisplay("Insufficient funds in that account.\n\n");
printUserMenu(user, gui);
}
} while (fromAccount < 0 || fromAccount >= user.numAccounts());
accountBalance = user.getAccountBalance(fromAccount);
// Get the account to transfer to
do {
gui.appendMainDisplay(String.format(
"\nEnter the number (1-%d) of the account "
+ "to transer to: ", user.numAccounts()));
toAccount = Integer.parseInt(gui.getInput()) - 1;
if (toAccount < 0 || toAccount >= user.numAccounts()) {
gui.setMainDisplay("Invalid choice. Please try again.\n");
}
} while (toAccount < 0 || toAccount >= user.numAccounts());
// Get the amount to transfer
do {
gui.appendMainDisplay(String.format(
"\nEnter the amount to transfer (max $%.02f): $",
accountBalance));
gui.setDecimal(true);
amount = Double.parseDouble(gui.getInput());
gui.setDecimal(false);
if (amount < 0.01) {
gui.setMainDisplay("Amount must be greater than zero.\n");
} else if (amount > accountBalance) {
gui.setMainDisplay(String.format(
"Amount must not be greater than "
+ "the balance of $%.02f.\n", accountBalance));
}
} while (amount < 0.01 || amount > accountBalance);
// Finally, do the transfer
user.addAcctTransaction(
fromAccount,
-1 * amount,
String.format("Transfer to account: %s",
user.getAcctUUID(toAccount)));
user.addAcctTransaction(
toAccount,
amount,
String.format("Transfer from account: %s",
user.getAcctUUID(fromAccount)));
}
}