-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunBank.java
503 lines (445 loc) · 14.7 KB
/
RunBank.java
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
* @author Ricardo Pena and Aaron Alarcon
* PinkBlink
* November 26, 2019
* CS3331
* Daniel Mejia
* Programming Assignment 5
*
* The template for this class is from Ricardo Pena.
* Individual modifications are noted above each method.
*
* Create a customer which has checking/savings/credit accounts and simulate various transactions.
*
* I confirm that the work of this assignment is completely my own.By turning in this assignment,
* I declare that I did not receive unauthorized assistance. Moreover, all the deliverables
* including, but not limited to the source code, lab report and output files were written and
* produced by me alone.
*/
import java.util.*;
import java.io.*;
public class RunBank {
static Scanner keyboard = new Scanner(System.in);
static HashMap<String, Customer> hmap = new HashMap<String, Customer>();
static String row;
static String ans = "y";
static BankManager manager = new BankManager();
static String importFile = "Bank_Users_5.txt";
static String fileName = "Log.csv";
public static void main(String[] args) {
readCustomerData();
createLog();
access();
finalBalance();
keyboard.close();
}
/**
* This function allows the user to select the function they will to undertake.
* The user has the ability to move from one function to another until the exit command has been entered.
*
*/
public static void access() {
String selection;
while(true) {
System.out.println("Enter type of access.");
System.out.println("1. Bank Manager" +"\n2. Customer" + "\n3. Transaction Reader" + "\n4. Exit");
selection = keyboard.nextLine();
if(selection.equals("1")) {
managerRole();
continue;
}
else if(selection.equals("2")) {
customerRole();
continue;
}
else if(selection.equals("3")) {
transactionReader();
continue;
}
else if(selection.equals("4")) {
break;
}
else {
System.out.println("Entry was INVALID. Please try again.");
}
}
}
/**
* Created by: Aaron Alarcon
* Reads text file and inserts into a HashMap of customers based off the info.
* The key for each user is the full name of the customer.
*/
public static void readCustomerData() {
String line;
String key = null;
try {
BufferedReader br = new BufferedReader(new FileReader(importFile));
//Array of names we're looking for in our info line
String[] names = {"First Name", "Last Name", "Date of Birth", "Identification Number","Address", "Phone Number", "Checking Account Number", "Savings Account Number","Credit Account Number","Checking Starting Balance","Savings Starting Balance","Credit Starting Balance", "Credit Max", "Password", "email"};
String[] str = br.readLine().split("\t");
int largest = str.length;
int[] result = {largest, largest, largest, largest, largest, largest, largest, largest, largest, largest, largest, largest, largest, largest, largest};
//Searching for our names and if it's found, setting the correct index in the result array
for(int i = 0; i < names.length; i++){
for(int j = 0; j<str.length; j++){
if(names[i].equalsIgnoreCase(str[j])){
result[i] = j;
break;
}
}
}
while ((line = br.readLine()) != null) {
line = line.concat("\t ");
String[] D = line.split("\t");
D[largest] = "";
D[result[3]] = D[result[3]].replace("-","");
//make a new customer from the data
Customer C = new Customer(D[result[0]], D[result[1]], D[result[2]], getInt(D[result[3]]), D[result[4]], D[result[5]], new Checking(getInt(D[result[6]]), getDoub(D[result[9]])), new Savings(getInt(D[result[7]]), getDoub(D[result[10]])), new Credit(getInt(D[result[8]]), getDoub(D[result[11]]), getDoub(D[result[12]])), D[result[13]], D[result[14]]);
//Use the full name as a key
key = C.getFullName().toUpperCase();
hmap.put(key, C.deepCopy());
//clearing up variables
key = null;
C = null;
}
br.close();
} catch (IOException e) {
System.out.println("There was an error opening the file. Please make sure everything is named correctly and your file exists");
System.exit(-1);
}
}
/**
* Created by: Aaron Alarcon
* Utility method to get an integer from a string or return 0 if it's null
* @param s - the string to get an integer from
* @return - an integer representation of the string
*/
public static int getInt(String s){
if(s.equals("")){
return 0;
}
return Integer.valueOf(s);
}
/**
* Created by: Aaron Alarcon
* Utility method to get a double from a string or return 0 if it's null
* @param s - the string to get a double from
* @return - a double representation of the string
*/
public static double getDoub(String s){
if(s.equals("")){
return 0.0;
}
return Double.valueOf(s);
}
/**
* Method provides functionality for manager role
* Manager may inquire about customers, add new customer, or print a customer bank statement.
*/
public static void managerRole() {
String selection;
while(true) {
System.out.println("What would you like to do?");
System.out.println("1. Inquire" +"\n2. Add New Customer" + "\n3. Create Bank Statement" + "\n4. Exit");
selection = keyboard.nextLine();
if(selection.equals("1")) {
manager.inquire(hmap);
continue;
}
else if(selection.equals("2")) {
manager.newCustomer(hmap);
continue;
}
else if(selection.equals("3")) {
manager.createStatement(hmap);
continue;
}
else if(selection.equals("4")) {
break;
}
else {
System.out.println("Entry was INVALID. Please try again.");
}
}
}
/**
* Method provides functionality for Customer role.
* Customer may inquire, deposit, withdraw, transfer, pay, or print bank statement.
*/
public static void customerRole() {
while(ans.equalsIgnoreCase("y")) {
System.out.println("Enter your full name...");
String customer = keyboard.nextLine().toUpperCase();
if(customer.equals("0")) {
break;
}
if(!hmap.containsKey(customer)) {
System.out.println("Customer does not exist or entry is invalid. Please re-enter name or \"0\" to EXIT.");
continue;
}
System.out.println("Enter password...");
String password = keyboard.nextLine();
if(!hmap.get(customer).getPassword().equals(password)) {
System.out.println("Password is invalid. Please try again or \"0\" to EXIT.");
continue;
}
transaction(customer);
System.out.println("Continue with another transaction?");
keyboard.nextLine();
System.out.println("Press \"y\" or \"Y\" to continue.");
ans = keyboard.nextLine();
}
}
/**
* Method reads specific commands from a file.
* Transactions are organized to be executed based on key words.
* Key words: Deposits, Withdraws, Transfers, Pays
*/
public static void transactionReader() {
try {
String name1, name2, account1, account2, type, amount;
name1 = "";
account1 ="";
//Open file to read-only mode
FileReader read = new FileReader(new File("Transaction Actions.csv"));
//Use buffered reader to read
BufferedReader readCSV = new BufferedReader(read);
//Skip the file headers
row = readCSV.readLine();
int size = row.split(",").length;
String[] col = new String[size];
//row = readCSV.readLine();
//Read each line from the file and pass it to the customer constructor
while ((row = readCSV.readLine()) != null) {
//Separate each column
String[] temp = row.split(",|%");
for(int i = 0; i < temp.length; i++) {
col[i] = temp[i];
}
//System.out.println(col[0]+col[1]+col[2]+col[4]+col[5]);
//Transaction Type and Amount
type = col[3];
amount = col[7];
//Customer 1 Information
if(!col[0].isBlank()) {
name1 = (col[0] + " " + col[1]).toUpperCase();
account1 = col[2];
}
//Customer 2 Information
name2 = (col[4] + " " + col[5]).toUpperCase();
account2 = col[6];
System.out.println(name1+account1+type+name2+account2+amount);
transaction(type,name1,account1,name2,account2,amount);
}
//Close file
readCSV.close();
}
catch (IOException e) {
System.out.println("File not found.");
}
}
/**
* Based on the transaction type selected by the user, the operation is executed.
* @param customer Full name of customer
*/
public static void transaction(String customer) {
double amount = 0;
String acct2;
String type = transactionSelect();
System.out.println("Select account:");
String account = acctSel();
switch (type.toUpperCase()) {
case "INQUIRE":
hmap.get(customer).inquire(account);
break;
case "DEPOSIT":
System.out.println("Enter amount to " + type);
amount = Double.parseDouble(keyboard.nextLine());
if(amount <= 0)
break;
hmap.get(customer).deposit(account,amount);
break;
case "WITHDRAW":
System.out.println("Enter amount to " + type);
amount = Double.parseDouble(keyboard.nextLine());
if(amount <= 0)
break;
hmap.get(customer).withdraw(account,amount);
break;
case "TRANSFER":
System.out.println("Select account to transfer to:");
acct2 = acctSel();
System.out.println("Enter amount to " + type);
amount = Double.parseDouble(keyboard.nextLine());
if(amount <= 0)
break;
hmap.get(customer).Pay(account, acct2, null, amount);
break;
case "PAY":
System.out.println("Enter recipients name...");
String cust2 = keyboard.nextLine().toUpperCase();
System.out.println("Enter amount to " + type);
amount = Double.parseDouble(keyboard.nextLine());
if(!hmap.containsKey(cust2)) {
System.out.println("Customer does not exist or entry is invalid. Transaction terminated.");
break;
}
System.out.println("Select account:");
acct2 = acctSel();
if(amount <= 0) {
break;
}
hmap.get(customer).Pay(account, acct2, hmap.get(cust2), amount);
break;
case "STATEMENT":
System.out.println("Writing your statement in a new file");
try {
hmap.get(customer).getStatement().writeBankStatement();
}
catch(NullPointerException e) {
System.out.println("Entry is not valid. Ending transaction.");
break;
}
break;
default:
System.out.println("Entry is not valid.");
}
}
/**
* Transactions are performed automatically for all respective users and accounts.
* @param type Transaction Type: Deposit, Withdraw, Transfer, Pay
* @param cust1 Customer initiating transaction
* @param acct1 Account type of customer initiating transaction
* @param cust2 Customer on receiving end of transaction (optional)
* @param acct2 Account of receiving Customer (optional)
* @param amnt Amount of the transaction
*/
public static void transaction(String type,String cust1,String acct1,String cust2,String acct2,String amnt) {
double amount = Double.parseDouble(amnt);
switch (type.toUpperCase()) {
case "INQUIRES":
hmap.get(cust1).inquire(acct1);
break;
case "DEPOSITS":
if(amount <= 0)
break;
hmap.get(cust1).deposit(acct1,amount);
break;
case "WITHDRAWS":
if(amount <= 0)
break;
hmap.get(cust1).withdraw(acct1,amount);
break;
case "TRANSFERS":
if(!hmap.containsKey(cust2))
break;
if(amount <= 0)
break;
hmap.get(cust1).Pay(acct1, acct2, null, amount);
break;
case "PAYS":
if(!hmap.containsKey(cust2)) {
break;
}
if(amount <= 0) {
break;
}
hmap.get(cust1).Pay(acct1, acct2, hmap.get(cust2), amount);
break;
default:
System.out.println("Entry is not valid.");
}
}
/**
* Clears out the previous log file
*/
public static void createLog() {
try {
BufferedWriter b = new BufferedWriter(new FileWriter("Log.txt"));
b.write("");
b.close();
}catch (IOException I) {
I.printStackTrace();
}
}
/**
* Prints out Final Balance for all customers.
* Output is generated in alphabetical order A-Z.
* Output file is CSV file.
*/
public static void finalBalance() {
//Export all keys into an array to be sorted
Set<String> key = hmap.keySet();
String[] arr = key.toArray(new String[key.size()]);
//Sort keys by first name
Arrays.sort(arr);
String log;
String header = ("First Name,Last Name,Date of Birth,Identification,Address,Phone Number," +
"Checking Account,Savings Account,Credit Account,Checking Balance,Savings Balance,Credit Balance");
try {
FileWriter writeCSV = new FileWriter("BalanceSheet.csv", false);
writeCSV.append(header);
writeCSV.append(System.lineSeparator());
for(int i = 0; i < arr.length; i++) {
log = "\n" + hmap.get(arr[i]).toString();
//writeCSV.append(System.lineSeparator());
writeCSV.append(log);
}
writeCSV.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
/**
* This method request the user to select the account to be used for the transaction.
* @return Returns the account or null if the selection is invalid.
*/
public static String acctSel() {
keyboard = new Scanner(System.in);
System.out.println("Please select account: ");
System.out.println("For Checking Account enter: 1");
System.out.println("For Savings Account enter: 2");
System.out.println("For Credit Account enter: 3");
String n = keyboard.nextLine();
switch (n) {
case "1":
return "checking";
case "2":
return "savings";
case "3":
return "credit";
}
return null;
}
/**
* This method requests the user to select the type of transaction to be executed.
* @return Returns String value of transaction or null value if selection is in valid.
*/
public static String transactionSelect() {
keyboard = new Scanner(System.in);
System.out.println("How can we help you today?");
System.out.println("If you would like to make an inquiry, enter \"1\".");
System.out.println("If you would like to make a deposit, enter \"2\".");
System.out.println("If you would like to make a withdrawal, enter \"3\".");
System.out.println("If you would like to make a transfer, enter \"4\".");
System.out.println("If you would like to make a payment, enter \"5\".");
System.out.println("If you would like to print statement, enter \"6\".");
String n = keyboard.nextLine();
switch (n) {
case "1":
return "INQUIRE";
case "2":
return "DEPOSIT";
case "3":
return "WITHDRAW";
case "4":
return "TRANSFER";
case "5":
return "PAY";
case "6":
return "STATEMENT";
}
return " ";
}
}