-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1.java
More file actions
839 lines (820 loc) · 28.1 KB
/
p1.java
File metadata and controls
839 lines (820 loc) · 28.1 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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
import java.util.*;
import java.io.*;
import java.sql.*;
class p1 {
private static String driver;
private static String url;
private static String username;
private static String password;
private static int state;
private static int custId;
public static void main(String argv[]) {
if (argv.length != 1) {
System.out.println("Need database properties filename");
} else {
init(argv[0]);
try {
Class.forName(driver); //load the driver
Connection con = DriverManager.getConnection(url, username, password); //Create the connection
Statement stmt = con.createStatement(); //Create a statement
state = 0;
custId = -1;
Scanner scan = new Scanner(System.in);
while(state > -1){
if(state == 0){
mainMenu(scan);
}
else if(state == 1){
newCust(stmt, scan); //add customer menu
}
else if(state == 2) {
custLogin(stmt, scan); //customer login menu
}
else if(state == 3) {
custMain(stmt, scan); //customer main menu
}
else if(state == 31) {
custAcctOpen(stmt, scan); //open account
}
else if(state == 32) {
custAcctClose(stmt, scan); //close account
}
else if(state == 33) {
custDep(stmt, scan); //deposit into account
}
else if(state == 34) {
custWith(stmt, scan); //withdraw from account
}
else if(state == 35) {
custTrns(stmt, scan); //transfer between accounts
}
else if(state == 36) {
custAcctSum(stmt); //summary of customer's accounts
}
else if(state == 4) {
adminMain(stmt, scan); //admin main menu
}
else if(state == 41) {
adminAcctSum(stmt, scan); //see account summary of specified customer
}
else if(state == 42) {
adminReportA(stmt); //Report A/all customer info and total baalnce
}
else if(state == 43) {
adminReportB(stmt, scan); //Report B/average total balance of age range
}
}
System.out.println("Exiting");
scan.close();
stmt.close(); //Close the statement after we are done with the statement
con.close(); //Close the connection after we are done with everything
} catch (Exception e) {
System.out.println("Exception in main()");
e.printStackTrace();
}
}
}//main
static void init(String filename) {
try {
Properties props = new Properties(); //Create a new Properties object
FileInputStream input = new FileInputStream(filename); //Create a new FileInputStream object using our filename parameter
props.load(input); //Load the file contents into the Properties object
driver = props.getProperty("jdbc.driver"); //Load the driver
url = props.getProperty("jdbc.url"); //Load the url
username = props.getProperty("jdbc.username"); //Load the username
password = props.getProperty("jdbc.password"); //Load the password
} catch (Exception e) {
System.out.println("Exception in init()");
e.printStackTrace();
}
}//init
static void mainMenu(Scanner scan){
System.out.println("Welcome to the Self Services Banking System! - Main Menu"); //print menu
System.out.println("1. New Customer");
System.out.println("2. Customer Login");
System.out.println("3. Exit");
String in = "";
while(!in.equals("1") && !in.equals("2") && !in.equals("3")){ //only allow the numbers to be inputted
in = scan.next();
if(!in.equals("1") && !in.equals("2") && !in.equals("3")){
System.out.println("Please enter 1, 2, or 3");
}
}
if(in.equals("1")){ //change state depending on option chosen
state = 1;
}
else if(in.equals("2")){
state = 2;
}
else{
state = -1;
}
}
static void newCust(Statement stmt, Scanner scan){
Boolean cont = true;
String name = "";
String gender = "";
int age = -1;
int pin = -1;
System.out.println("New Customer Creation");
System.out.println("What is the name of the new customer?"); //take in inputs
name=scan.next();
System.out.println("What is the gender of the new customer? (M or F)");
gender=scan.next();
System.out.println("What is the age of the new customer?");
while(cont){
try{
age = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Invalid age. Please enter a valid integer.");
scan.nextLine();
}
}
System.out.println("What is the pin of the new customer?");
cont = true;
while(cont){
try{
pin = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Invalid pin. Please enter a valid integer.");
scan.nextLine();
}
}
System.out.println("******************************");
cont = true;
if(gender.length() > 1 || (!gender.equalsIgnoreCase("M") && !gender.equalsIgnoreCase("F"))){ //checking for invalid inputs
System.out.println("Failure. Invalid gender.");
cont = false;
}
if(age <= 0){
System.out.println("Failure. Invalid age.");
cont = false;
}
if(pin < 0){
System.out.println("Failure. Invalid pin.");
cont = false;
}
if(cont){ //perform insert and find id based on table size
String query = "insert into p1.customer(Name, Gender, Age, Pin) values('" +
name + "', '" + gender.toUpperCase() + "', " + age + ", " + pin + ")";
String countStmt = "select count(*) from p1.customer";
try{
stmt.executeUpdate(query);
ResultSet rs = stmt.executeQuery(countStmt);
rs.next();
int c = rs.getInt(1) + 99;
System.out.println("Success! Customer ID is: " + c);
rs.close();
}
catch (Exception e) {
System.out.println("Exception in newCust()");
e.printStackTrace();
}
}
System.out.println("******************************");
state = 0;
}
static void custLogin(Statement stmt, Scanner scan){
boolean cont = true;
int id = -1;
int pin = -1;
System.out.println("Customer Login");
System.out.println("Input Customer ID"); // take in inputs
while(cont){
try{
id = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Invalid ID. Please enter a valid integer.");
scan.nextLine();
}
}
System.out.println("Input pin");
cont = true;
while(cont){
try{
pin = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Invalid pin. Please enter a valid integer.");
scan.nextLine();
}
}
System.out.println("******************************");
cont = true; //check for invalid inputs
if(id < 0){
System.out.println("Failure. Invalid ID.");
cont = false;
state = 0;
}
if(pin < 0){
System.out.println("Failure. Invalid pin.");
cont = false;
state = 0;
}
if(cont) { //continue if valid inputs
if(pin == 0 && id == 0) { //Special case for admin menu where you set state and make view to be used by admin menus
state = 4;
}
else { //check for valid login
String check = "select count(*) from p1.customer where ID=" + id + " and Pin=" + pin;
try{
ResultSet rs = stmt.executeQuery(check);
rs.next();
int c = rs.getInt(1);
if(c == 0) { //if no matching pin and id combo, go back to main menu
System.out.println("Failure. Invalid ID and/or pin.");
state = 0;
}
else { //if combo found go to customer main menu
System.out.println("Successful Login");
custId = id;
state = 3;
}
rs.close();
}
catch (Exception e) {
System.out.println("Exception in custLogin()");
e.printStackTrace();
}
}
}
System.out.println("******************************");
}
static void custMain(Statement stmt, Scanner scan) {
System.out.println("Customer Main Menu"); //print menu
System.out.println("1. Open Account");
System.out.println("2. Close Account");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Transfer");
System.out.println("6. Account Summary");
System.out.println("7. Exit");
int in = -1;
while(in < 1 || in > 7) { //get valid input of one of the menu options
try {
in = scan.nextInt();
if(in < 1 || in > 7) {
System.out.println("Please enter 1, 2, 3, 4, 5, 6, or 7");
}
}
catch(InputMismatchException e){
System.out.println("Please enter 1, 2, 3, 4, 5, 6, or 7");
scan.nextLine();
}
}
if(in == 7) { //option 7 is return to main menu
state = 0;
}
else { //other options means going to respective menus
state = 30 + in;
}
}
static void custAcctOpen(Statement stmt, Scanner scan) {
int id = -1;
String type = "";
int balance = -1;
boolean cont = true;
System.out.println("Open Account"); //take in inputs
System.out.println("Enter customer ID");
while(cont) {
try {
id = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("Enter account type ('S' for savings or 'C' for checking)");
type = scan.next();
System.out.println("Enter initial balance");
cont = true;
while(cont) {
try {
balance = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("******************************");
cont = true; //error checking
if(id < 100) {
System.out.println("Failure. Invalid customer ID.");
cont = false;
}
if(!type.equalsIgnoreCase("S") && !type.equalsIgnoreCase("C")) {
System.out.println("Failure. Invalid account type.");
cont = false;
}
if(balance < 0) {
System.out.println("Failure. Invalid balance.");
cont = false;
}
if(cont) { //passed error checking
String check = "select count(*) from p1.customer where ID=" + id; //checking for existence of id
String count = "select count(*) from p1.account"; //get count in account for acct number
String insert = "insert into p1.account(ID, Balance, Type, Status) values(" + //insert statement
id + ", " + balance + ", '" + type.toUpperCase() + "', 'A')";
try {
ResultSet rs = stmt.executeQuery(check);
rs.next();
int c = rs.getInt(1);
if(c == 0) { //no such given id in customer table
System.out.println("Failure. Invalid customer ID.");
}
else {
stmt.executeUpdate(insert);
ResultSet rs2 = stmt.executeQuery(count);
rs2.next();
int num = rs2.getInt(1) + 999;
System.out.println("Success! Account number = " + num);
rs2.close();
}
rs.close();
}
catch (Exception e) {
System.out.println("Exception in custAcctOpen()");
e.printStackTrace();
}
}
System.out.println("******************************");
state = 3;
}
static void custAcctClose(Statement stmt, Scanner scan) {
int num = -1;
boolean cont = true;
System.out.println("Close Account"); //take in inputs
System.out.println("Enter account number");
while(cont) {
try {
num = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("******************************");
if(num < 1000) { //check to see if within range of account numbers
System.out.println("Failure. Invalid account number.");
}
else {
String check = "select count(*) from p1.account where Number=" + num; //check if account number valid
String update = "update p1.account set Status='I', Balance=0 where Number=" + num;//update statement to close account
try {
ResultSet rs = stmt.executeQuery(check); //check if there is an account with given number
rs.next();
int c = rs.getInt(1);
if(c == 0) {
System.out.println("Failure. Invalid account number.");
}
else {
stmt.executeUpdate(update); //close account
System.out.println("Account closed");
}
rs.close();
}
catch (Exception e) {
System.out.println("Exception in custAcctClose()");
e.printStackTrace();
}
}
System.out.println("******************************");
state = 3;
}
static void custDep(Statement stmt, Scanner scan) {
int num = -1;
int ammt = -1;
boolean cont = true;
System.out.println("Deposit into Account"); //take in inputs
System.out.println("Enter account number");
while(cont) {
try {
num = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("Enter deposit ammount");
cont = true;
while(cont) {
try {
ammt = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("******************************");
cont = true;
if(num < 1000) { //check to see if within range of account numbers
System.out.println("Failure. Invalid account number.");
cont = false;
}
if(ammt < 0) { //check to see if valid amount
System.out.println("Failure. Invalid deposit amount.");
cont = false;
}
if(cont) {
String check = "select count(*) from p1.account where Status='A' and Number=" + num; //check if account number valid
String update = "update p1.account set Balance=Balance+" + ammt + " where Number=" + num;//update statement to deposit
try {
ResultSet rs = stmt.executeQuery(check); //check if there is an account with given number
rs.next();
int c = rs.getInt(1);
if(c == 0) {
System.out.println("Failure. Invalid account number or account may be closed.");
}
else {
stmt.executeUpdate(update); //deposit to account
System.out.println("Deposit Successful");
}
rs.close();
}
catch (Exception e) {
System.out.println("Exception in custDep()");
e.printStackTrace();
}
}
System.out.println("******************************");
state = 3;
}
static void custWith(Statement stmt, Scanner scan) {
int num = -1;
int ammt = -1;
boolean cont = true;
System.out.println("Withdraw from Account"); //take in inputs
System.out.println("Enter account number. Can only withdraw from your own account.");
while(cont) {
try {
num = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("Enter withdraw ammount");
cont = true;
while(cont) {
try {
ammt = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("******************************");
cont = true;
if(num < 1000) { //check to see if within range of account numbers
System.out.println("Failure. Invalid account number.");
cont = false;
}
if(ammt < 0) { //check to see if valid amount
System.out.println("Failure. Invalid withdraw amount.");
cont = false;
}
if(cont) {
String check = "select ID, Balance from p1.account where Status='A' and Number=" + num; //check if account number valid
String update = "update p1.account set Balance=Balance-" + ammt + " where Number=" + num; //update statement to withdraw
try {
ResultSet rs = stmt.executeQuery(check); //check if there is an account with given number
int i = -1;
int b = -1;
while(rs.next()) { //get id and balance if there is one
i = rs.getInt(1);
b = rs.getInt(2);
}
if(i < 100 || b < 0) { //failure state of no account selected
System.out.println("Failure. Invalid account number. Account may be closed/does not exist.");
cont = false;
}
else if(i != custId) { //if not customer they can't withdraw
System.out.println("Failure. Invalid account number. Can only withdraw from own account.");
cont = false;
}
else if(b < ammt) { //if balance is too low can't withdraw
System.out.println("Failure. Not enough funds.");
cont = false;
}
if(cont) {
stmt.executeUpdate(update); //withdraw from account
System.out.println("Withdraw Successful");
}
rs.close();
}
catch (Exception e) {
System.out.println("Exception in custWith()");
e.printStackTrace();
}
}
System.out.println("******************************");
state = 3;
}
static void custTrns(Statement stmt, Scanner scan) {
int srcNum = -1;
int destNum = -1;
int ammt = -1;
boolean cont = true;
System.out.println("Transfer Between Accounts"); //take in inputs
System.out.println("Enter source account number. Can only withdraw from your own account.");
while(cont) {
try {
srcNum = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("Enter destination account number");
cont = true;
while(cont) {
try {
destNum = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("Enter transfer ammount");
cont = true;
while(cont) {
try {
ammt = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
System.out.println("******************************");
cont = true;
if(srcNum < 1000) { //check to see if within range of account numbers
System.out.println("Failure. Invalid source account number.");
cont = false;
}
if(destNum < 1000) { //check to see if within range of account numbers
System.out.println("Failure. Invalid destination account number.");
cont = false;
}
if(ammt < 0) { //check to see if valid amount
System.out.println("Failure. Invalid withdraw amount.");
cont = false;
}
if(cont) {
String checkSrc = "select ID, Balance from p1.account where Status='A' and Number=" + srcNum; //check if account number valid
String checkDest = "select count(*) from p1.account where Status='A' and Number=" + destNum; //check if account number valid
String withdraw = "update p1.account set Balance=Balance-" + ammt + " where Number=" + srcNum; //update statement to withdraw
String deposit = "update p1.account set Balance=Balance+" + ammt + " where Number=" + destNum; //update statement to deposit
try {
ResultSet rs = stmt.executeQuery(checkSrc); //check if there is an account with given number
int i = -1;
int b = -1;
while(rs.next()) { //get id and balance if accoutn exists
i = rs.getInt(1);
b = rs.getInt(2);
}
if(i < 100 || b < 0) { //failed check no acccount
System.out.println("Failure. Invalid source account number. Account may be closed/does not exist.");
cont = false;
}
else if(i != custId) { //not transfering from customer
System.out.println("Failure. Invalid source account number. Can only transfer from own account.");
cont = false;
}
else if(b < ammt) { //not enough funds
System.out.println("Failure. Not enough funds.");
cont = false;
}
ResultSet rs2 = stmt.executeQuery(checkDest); //check to see if destination account exists
rs2.next();
i = rs2.getInt(1);
if(i == 0) { //account doesn't exist
System.out.println("Failure. Invalid destination account number. Account may be closed/does not exist.");
cont = false;
}
if(cont) {
stmt.executeUpdate(withdraw); //withdraw from source account
stmt.executeUpdate(deposit); //deposit to destination account
System.out.println("Transfer Successful");
}
rs2.close();
rs.close();
}
catch (Exception e) {
System.out.println("Exception in custTrns()");
e.printStackTrace();
}
}
System.out.println("******************************");
state = 3;
}
static void custAcctSum(Statement stmt) {
int sum = 0;
String select = "select Number, Balance from p1.account where Status='A' and ID=" + custId;//select all account under customer's id
System.out.println("******************************");
try {
System.out.println("Account Summary");
System.out.println("--------------------");
ResultSet rs = stmt.executeQuery(select);
System.out.printf("%-11s %-11s%n", "NUMBER", "BALANCE");
System.out.println("----------- -----------");
while(rs.next()) { //go through each row and print their values and add balance to sum
int n = rs.getInt(1);
int b = rs.getInt(2);
System.out.printf("%11s %11s%n", n+"", b+"");
sum += b;
}
System.out.println("-----------------------");
System.out.printf("%-11s %11s%n", "TOTAL", sum+"");//print out total balance
rs.close();
}
catch (Exception e) {
System.out.println("Exception in custAcctSum()");
e.printStackTrace();
}
System.out.println("******************************");
state = 3;
}
static void adminMain(Statement stmt, Scanner scan) {
System.out.println("Administrator Main Menu"); //print menu
System.out.println("1. Account Summary for a Customer");
System.out.println("2. Report A::Customer Information with Total Balance in Decreasing Order");
System.out.println("3. Report B::Find the Average Total Balance Between Age Groups");
System.out.println("4. Exit");
int in = -1;
while(in < 1 || in > 4) { //get valid input of one of the menu options
try {
in = scan.nextInt();
if(in < 1 || in > 4) {
System.out.println("Please enter 1, 2, 3, or 4");
}
}
catch(InputMismatchException e){
System.out.println("Please enter 1, 2, 3, or 4");
scan.nextLine();
}
}
if(in == 4) { //option 4 is return to main menu and delete view
state = 0;
}
else { //other options means going to respective menus
state = 40 + in;
}
}
static void adminAcctSum(Statement stmt, Scanner scan) {
int sum = 0;
int id = -1;
boolean cont = true;
System.out.println("Enter customer ID"); //take in input
while(cont) {
try {
id = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
String select = "select Number, Balance from p1.account where Status='A' and ID=" + id;//select all account under customer's id that are active
System.out.println("******************************");
if(id < 100) {
System.out.println("Failure. Invalid customer ID.");
}
else {
try {
System.out.println("Account Summary");
System.out.println("--------------------");
ResultSet rs = stmt.executeQuery(select);
System.out.printf("%-11s %-11s%n", "NUMBER", "BALANCE");
System.out.println("----------- -----------");
int n = -1;
int b = -1;
while(rs.next()) { //go through each row and print their values and add balance to sum
n = rs.getInt(1);
b = rs.getInt(2);
System.out.printf("%11s %11s%n", n+"", b+"");
sum += b;
}
if(n < 0 || b < 0) { //failure to get results
System.out.println("Failure. Invalid customer ID or no open accounts under ID.");
}
else {
System.out.println("-----------------------");
System.out.printf("%-11s %11s%n", "TOTAL", sum+"");//print out total balance
}
rs.close();
}
catch (Exception e) {
System.out.println("Exception in adminAcctSum");
e.printStackTrace();
}
}
System.out.println("******************************");
state = 4;
}
static void adminReportA(Statement stmt) {
String join = "(select c.ID, c.name, c.age, c.gender, a.balance from p1.customer c inner join p1.account a on a.ID=c.ID)"; //join statement
String select = "select max(ID) id, max(name) name, max(age), max(gender) gender, sum(balance) total from " + join +
" group by id order by total desc"; //select the attributes needed for the table
try {
System.out.println("******************************");
System.out.println("Report A");
System.out.println("--------------------");
ResultSet rs = stmt.executeQuery(select);
System.out.printf("%-11s %-15s %-6s %-6s %-11s%n", "ID", "NAME", "GENDER", "AGE", "TOTAL");
System.out.println("----------- --------------- ------ ------ -----------");
while(rs.next()) { //print out every customer's info
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String gender = rs.getString(4);
int balance = rs.getInt(5);
System.out.printf("%11s %-15s %-6s %6s %11s%n", id+"", name, gender, age+"", balance+"");
}
System.out.println("******************************");
rs.close();
}
catch (Exception e) {
System.out.println("Exception in adminReportA()");
e.printStackTrace();
}
state = 4;
}
static void adminReportB(Statement stmt, Scanner scan) {
int min = -1;
int max = -1;
boolean cont = true;
System.out.println("Enter minimum age of range");
while(cont) {
try {
min = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
cont = true;
System.out.println("Enter maximum age of range");
while(cont) {
try {
max = scan.nextInt();
cont = false;
}
catch(InputMismatchException e){
System.out.println("Please enter a valid integer");
scan.nextLine();
}
}
String join = "(select c.ID, c.age, a.balance from p1.customer c inner join p1.account a on a.ID=c.ID)"; //join statement
String totalBalance = "(select sum(balance) balance, ID from " + join + " where age>=" + min +" and age<=" + max +
" group by ID)"; //get everyone's total balances
String select = "select avg(balance) avg from " + totalBalance; //select the attributes needed for the table
try {
ResultSet rs = stmt.executeQuery(select); //run query and print report
rs.next();
int b = rs.getInt(1);
System.out.println("******************************");
System.out.println("Report B");
System.out.println("--------------------");
System.out.println("AVERAGE TOTAL BALANCE");
System.out.println("---------------------");
System.out.println(b);
System.out.println("******************************");
rs.close();
}
catch (Exception e) {
System.out.println("Exception in adminReportB()");
e.printStackTrace();
}
state = 4;
}
}