-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankApp.java
More file actions
669 lines (583 loc) · 21.6 KB
/
BankApp.java
File metadata and controls
669 lines (583 loc) · 21.6 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
import java.util.Scanner;
import java.util.ArrayList;
// Interface tells us what to do but does not tell us how to do
// interface is a fully abstract class
// in interface we only have declaration and definition is done in the class that implements it
// no partial implementation is allowed so all the account services must be implemente
interface AccountServices {
public String getIFSC_CODE();
public String getBranchName();
public String getBankName();
public void CheckBalance();
public String getAccountHolderName();
public int getAccountNumber();
public void fundTransfer(BankAccount fromAccount ,BankAccount toAccount, double amount);
public void Deposit(double amount);
public void Deposit(double amount,String method);
public void Withdraw(double amount);
public void Withdraw(double amount , String mode);
}
// creating a base class called Bank Account from which we create an object and that object must have all the services accessible
class BankAccount implements AccountServices{
private int accountNumber;
private String accountHolderName;
private double balance;
private static final String IFSC_CODE = "CSBS000A682";
private static final String Bank_Name = "CSBS BANK";
private static final String Branch_Name = "Avalahalli ";
// Constructor with only account holder name
public BankAccount(String accountHolderName) {
this.accountNumber = BankUtil.generateAccountNumber();
this.accountHolderName = accountHolderName; // this keyword used to avoid instance variable hiding
this.balance = 0.0;
}
// Constructor with account holder name and initial balance
public BankAccount(String accountHolderName, double balance) {
this.accountNumber = BankUtil.generateAccountNumber();
this.accountHolderName = accountHolderName; // this keyword used to avoid instance variable hiding
this.balance = balance;
}
public String getBranchName() {
return Branch_Name;
}
public String getBankName(){
return Bank_Name;
}
public String getIFSC_CODE(){
return IFSC_CODE;
}
// Method to get the Account Number
public int getAccountNumber() {
return accountNumber;
}
// Method to get the AccountHolder Name only
public String getAccountHolderName() {
return accountHolderName;
}
// Method to get the Balance of the account
public double getBalance() {
return balance;
}
public double setBalance(double value) {
return balance = value;
}
// Mehtod to get Check balance and displace balance
public void CheckBalance(){
System.out.println("Your Account balance is :"+getBalance());
}
public void fundTransfer(BankAccount fromAccount ,BankAccount toAccount, double sum){
if(sum > 0 && sum <= this.balance){
fromAccount.balance -= sum;
toAccount.balance += sum ;
System.out.println("Transferred "+ sum+" from "+ this.accountNumber +" to "+ toAccount.getAccountNumber());
}
}
// Creating Method to Deposit Money
public void Deposit(double amount){
balance += amount;
System.out.println("Deposited "+ amount +" to your account "+ accountNumber +" successfully ");
}
// overloading the Deposit method if the user wants to deposit using other methods
public void Deposit(double amount , String method){
balance += amount;
System.out.println(" Deposited "+ amount +" in to your account "+ accountNumber +" thorugh "+ method);
}
// Creating Method to Withdraw Money from your account
public void Withdraw(double amount){
if(amount <= balance) {
balance -= amount;
System.out.println("Withdraw"+amount+" from your account "+ accountNumber);
System.out.println("Balance remaining in your account is "+ getBalance());
}
else{
System.out.println("Insufficient Balance to withdraw that much amount");
}
}
// overloading Method to Withdraw Money from your account
public void Withdraw(double amount,String mode){
if(amount <= balance) {
balance -= amount;
System.out.println("Withdraw"+ amount +"from your account"+ accountNumber +"throught"+ mode);
System.out.println("Balance remaining in your account is "+getBalance());
}
else{
System.out.println("Insufficient Balance to withdraw that much amount");
}
}
}
// creating a Subclass called Savings Account which inherits the properties of base class BankAccount
class SavingsAccount extends BankAccount{
private static final double MIN_BALANCE = 500.0;
private static final double INTREST_RATE = 0.03; // this is annual intrest rate
// constructor of Savings Account
public SavingsAccount(String accountHolderName) {
super(accountHolderName);//super keyword usedcall the constructor of the superclass,usefull when we want initialize base class variables // here the balance automatically gets initialized to zero as we did not pass the balance argument
}
// another constructor of savings account with two arguments or parameter
public SavingsAccount(String accountHolderName , double balance) {
super(accountHolderName , balance);
// here the balance is given so balance will get initialized to the given parameter
}
// Overriding the withdraw & deposit method defined in the base class
@Override
public void Deposit(double amount) {
if(amount >= 0){
super.Deposit(amount); // here is the second usage of super keyword to access the superclass method .... to access superclass method we use dot-operator(.) ..where as for constructor we dont use the dot-operator.
}
else {
System.out.println("Cannot deposit negative number :)) ");
}
}
// Overriding the deposit method defined in the base class with another parameter method
@Override
public void Deposit(double amount, String method) {
if(amount >= 0){
super.Deposit(amount,method);
}
else {
System.out.println("Cannot deposit negative number :)) ");
}
}
// overriding the withdraw method
@Override
public void Withdraw(double amount){
if( getBalance() - amount >= MIN_BALANCE) {
super.Withdraw(amount);
}
else {
System.out.println("Cannot Withdraw amount of rs"+ amount +"because of Minimum balance policy of atleast rs"+ MIN_BALANCE);
}
}
// overriding the withdraw method
@Override
public void Withdraw(double amount,String mode){
if( getBalance() - amount >= MIN_BALANCE) {
super.Withdraw(amount,mode);
}
else {
System.out.println("Cannot Withdraw amount of rs "+ amount + " because of Minimum balance policy of atleast rs"+ MIN_BALANCE);
}
}
public double CalculateIntrest(int days) {
double intrestperday = INTREST_RATE /365;
return getBalance() * intrestperday * days ;
}
}
// Creating a subclass called Current Account with
class CurrentAccount extends BankAccount {
private static final double OVERDRAFT_LIMIT = 1000;
public CurrentAccount(String accountHolderName){
super(accountHolderName);
}
public CurrentAccount(String accountHolderName , double balance) {
super(accountHolderName,balance);
}
// overriding Methods to withdraw and deposit
@Override
public void Deposit(double amount){
if(amount >= 0){
super.Deposit(amount);
}
else {
System.out.println("Deposit amount should be a non negative number ");
}
}
@Override
public void Deposit(double amount, String method) {
if(amount >= 0){
super.Deposit(amount,method);
}
else {
System.out.println("Cannot deposit negative number :)) ");
}
}
//Override Withdraw method
@Override
public void Withdraw(double amount){
if( getBalance() - amount >= -OVERDRAFT_LIMIT) {
//super.Withdraw(amount); // not using the suepr method
double result = getBalance() - amount;
setBalance(result);
System.out.println("Withdraw "+ amount +" from your account "+ getAccountNumber());
System.out.println("Balance remaining in your account is "+getBalance());
}
else {
System.out.println("Cannot Withdraw amount of rs "+ amount+" as you have exhausted your OVERDRAFT LIMIT of rs "+ OVERDRAFT_LIMIT);
}
}
// overriding the withdraw method
@Override
public void Withdraw(double amount,String mode){
if( getBalance() - amount >= -OVERDRAFT_LIMIT) {
//super.Withdraw(amount,mode); // but we wont use the super method as that will not allow the customer to withdraw the overdraft limit
double result = getBalance() - amount;
setBalance(result);
System.out.println(" Withdraw "+ amount +" from your account "+ getAccountNumber() +" throught "+ mode);
System.out.println(" Balance remaining in your account is "+getBalance());
}
else {
System.out.println("Cannot Withdraw amount of rs "+ amount +" as you have exhausted your OVERDRAFT LIMIT of rs "+OVERDRAFT_LIMIT);
}
}
}
class BankUtil{
private static int counter = 0;
// static method to access the static variables
static int generateAccountNumber() {
return (++counter);
}
}
// abstract class - restricts from creation of objects
abstract class PremiumAccount extends BankAccount {
public PremiumAccount(String accountHolderName) {
super(accountHolderName);
}
public PremiumAccount(String accountHolderName, double balance) {
super(accountHolderName, balance);
}
// abstract method for getbenefits ( with no definition)
// abstract method has only interface and no implementation
// implementation is different for every class which implements it
public abstract String getBenefits();
}
class PremiumSavingsAccount extends PremiumAccount {
private static final double MIN_BALANCE = 250.0; // lower minimum balance
private static final double INTEREST_RATE = 0.05; // Higher annual interest rate for premium accounts
public PremiumSavingsAccount(String accountHolderName) {
super(accountHolderName);
}
public PremiumSavingsAccount(String accountHolderName, double balance) {
super(accountHolderName, balance);
}
@Override
public void Withdraw(double amount) {
if (getBalance() - amount >= MIN_BALANCE) {
super.Withdraw(amount);
} else {
System.out.println("Cannot withdraw. Minimum balance of " + MIN_BALANCE + " must be maintained.");
}
}
@Override
public void Withdraw(double amount, String mode) {
if (getBalance() - amount >= MIN_BALANCE) {
super.Withdraw(amount, mode);
} else {
System.out.println("Cannot withdraw amount of Rs " + amount + " because of Minimum balance policy of at least Rs " + MIN_BALANCE);
}
}
@Override
public void Deposit(double amount) {
if (amount > 0) {
super.Deposit(amount);
} else {
System.out.println("Cannot deposit a non-positive amount.");
}
}
@Override
public void Deposit(double amount, String type) {
if (amount > 0) {
super.Deposit(amount, type);
} else {
System.out.println("Cannot deposit a non-positive amount.");
}
}
// Method to calculate interest earned for n number of days
public double calculateInterest(int days) {
double interestPerDay = INTEREST_RATE / 365;
return getBalance() * interestPerDay * days;
}
// Implementing the abstract method to get benefits
@Override
public String getBenefits() {
return "Premium Savings Account Benefits: Higher interest rate, free international transactions , lower minimum Balance, priority customer support";
}
}
class PremiumCurrentAccount extends PremiumAccount {
private static final double OVERDRAFT_LIMIT = 2000.0;
public PremiumCurrentAccount(String accountHolderName) {
super(accountHolderName);
}
public PremiumCurrentAccount(String accountHolderName, double balance) {
super(accountHolderName, balance);
}
@Override
public void Withdraw(double amount){
if( getBalance() - amount >= -OVERDRAFT_LIMIT) {
//super.Withdraw(amount,mode); again same as above we wont use the super method as that will not allow the customer to withdraw the overdraft limit
double result = getBalance() - amount;
setBalance(result);
System.out.println(" Withdraw "+ amount +" from your account "+ getAccountNumber());
System.out.println(" Balance remaining in your account is "+getBalance());
}
else {
System.out.println("Cannot Withdraw amount of rs "+ amount +" as you have exhausted your OVERDRAFT LIMIT of rs "+OVERDRAFT_LIMIT);
}
}
@Override
public void Withdraw(double amount,String mode){
if( getBalance() - amount >= -OVERDRAFT_LIMIT) {
//super.Withdraw(amount,mode); againb same as above
double result = getBalance() - amount;
setBalance(result);
System.out.println(" Withdraw "+ amount +" from your account "+ getAccountNumber() +" throught "+ mode);
System.out.println(" Balance remaining in your account is "+getBalance());
}
else {
System.out.println("Cannot Withdraw amount of rs "+ amount +" as you have exhausted your OVERDRAFT LIMIT of rs "+OVERDRAFT_LIMIT);
}
}
@Override
public void Deposit(double amount) {
if (amount >= 0) {
super.Deposit(amount);
} else {
System.out.println("Deposit amount should be a non-negative number.");
}
}
// Implementing the abstract method to get benefits
@Override
public String getBenefits() {
return "Premium Current Account Benefits: higher Overdraft facility, priority customer support.";
}
}
public class BankApp{
static ArrayList<BankAccount> data = new ArrayList<>(); // array to store the details of the customers
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
BankAccount account = null;
while(true){
System.out.println("\nWelcome to CSBS Banking Application!");
System.out.println("1. Create Savings Account");
System.out.println("2. Create Current Account");
System.out.println("3. Create Premium Savings Account");
System.out.println("4. Create Premium Current Account");
System.out.println("5. Show Account Number");
System.out.println("6. Show Account Holder Name");
System.out.println("7. Check Balance");
System.out.println("8. Deposit Money");
System.out.println("9. Withdraw Money");
System.out.println("10. Fund Transfer");
System.out.println("11. View Benefits (Premium Accounts)");
System.out.println("12. Calculate Interest (Applicable Accounts)");
System.out.println("13. View All Accounts");
System.out.println("14. Exit");
System.out.print("Choose an option: ");
int choice = sc.nextInt();
switch(choice){
case 1 :
System.out.println("Enter the Account Holder Name : ");
sc.nextLine();
String SavName = sc.nextLine();
System.out.println("Enter the initial balance (leave empty its optional) : ");
double initialbalance = sc.nextDouble();
if(initialbalance == 0){
account = new SavingsAccount(SavName);
}
else {
account = new SavingsAccount(SavName, initialbalance );
}
data.add(account);
System.out.println("Savings Account created successfully for "+ SavName);
break;
case 2 :
System.out.println("Enter the Account Holder Name for current account : ");
sc.nextLine();
String CurName = sc.nextLine();
System.out.println("Enter the initial balance (leave empty its optional) : ");
double CurIbalance = sc.nextDouble();
if(CurIbalance == 0){
account = new CurrentAccount(CurName);
}
else {
account = new CurrentAccount(CurName, CurIbalance);
}
data.add(account);
System.out.println("Current Account created successfully for "+ CurName);
break;
case 3 :
System.out.println("Enter the Account Holder Name for Premium Savings account : ");
sc.nextLine();
String PsavName = sc.nextLine();
System.out.println("Enter the initial balance (leave empty its optional) : ");
double PresIbalance = sc.nextDouble();
if(PresIbalance == 0){
account = new PremiumSavingsAccount(PsavName);
}
else {
account = new PremiumSavingsAccount(PsavName,PresIbalance);
}
data.add(account);
System.out.println("Premium Savings Account created successfully for"+PsavName);
break;
case 4 :
System.out.println("Enter the Account Holder Name for Premium Current account : ");
sc.nextLine();
String PcurName = sc.nextLine();
System.out.println("Enter the initial balance (leave empty its optional) : ");
double PreCIbalance = sc.nextDouble();
if(PreCIbalance == 0){
account = new PremiumCurrentAccount(PcurName);
}
else {
account = new PremiumCurrentAccount(PcurName,PreCIbalance);
}
data.add(account);
System.out.println("Premium Current Account created successfully for"+PcurName);
break;
case 5 :
System.out.println("Enter the Account Holder Name: ");
sc.nextLine();
String accHolderName = sc.nextLine();
for (BankAccount acc : data) {
if (acc.getAccountHolderName() == accHolderName) {
System.out.println("Account Number: " + acc.getAccountNumber());
break;
}
}
break;
case 6 :
System.out.println("Enter the Account Number: ");
int accNumber = sc.nextInt();
for( BankAccount acc : data){
if(acc.getAccountNumber() == accNumber){
System.out.println("Account Holder Name :"+acc.getAccountHolderName());
break;
}
}
break;
case 7 :
System.out.println("Enter your Account Number :");
accNumber = sc.nextInt();
for(BankAccount acc : data){
if(acc.getAccountNumber() == accNumber){
System.out.println("Balance is :"+acc.getBalance());
break;
}
}
break;
case 8 :
System.out.println("Enter your Account Number :");
accNumber = sc.nextInt();
System.out.println("Enter the amount to deposit :");
double amount = sc.nextDouble();
System.out.println("Enter the mode of deposit :");
sc.nextLine();
String mode = sc.nextLine();
for(BankAccount acc : data){
if(acc.getAccountNumber() == accNumber){
if(mode.isEmpty()){
acc.Deposit(amount);
break;
}
else {
acc.Deposit(amount,mode);
}
break;
}
}
break;
case 9 :
System.out.println("Enter your Account Number :");
accNumber = sc.nextInt();
System.out.println("Enter the amount to be withdrawen :");
double withdrawAmount = sc.nextDouble();
System.out.println("Enter the mode of withdraw :");
sc.nextLine();
String thru = sc.nextLine();
for (BankAccount acc : data) {
if (acc.getAccountNumber() == accNumber) {
if(thru.isEmpty()){
acc.Withdraw(withdrawAmount);
}
else {
acc.Withdraw(withdrawAmount,thru);
}
break;
}
}
break;
case 10 :
System.out.println("Enter from account number :");
int fromAccountNumber = sc.nextInt();
System.out.println("Enter to account number :");
int toAccountNumber = sc.nextInt();
System.out.println("Enter amount to transfer :");
double transferAmount = sc.nextDouble();
BankAccount fromAccount = null;
BankAccount toAccount = null;
for (BankAccount acc : data) {
if (acc.getAccountNumber() == fromAccountNumber) {
fromAccount = acc;
}
if (acc.getAccountNumber() == toAccountNumber) {
toAccount = acc;
}
}
if (fromAccount != null && toAccount != null) {
fromAccount.fundTransfer(fromAccount, toAccount, transferAmount);
} else {
System.out.println("Invalid account numbers.");
}
break;
case 11 :
System.out.println("Enter the Account Number :");
accNumber = sc.nextInt();
for(BankAccount acc : data){
if(acc.getAccountNumber() == accNumber){
if(acc instanceof PremiumAccount){
PremiumAccount premiumAccount = (PremiumAccount) acc; //type casting to Premium Account
System.out.println(premiumAccount.getBenefits());
}
else {
System.out.println("Account is not a Premium Account");
}
break;
}
}
break;
case 12 :
System.out.println("Enter the Account Number :");
accNumber = sc.nextInt();
System.out.println("Enter the number of days to calculate interest :");
int days = sc.nextInt();
for(BankAccount acc : data){
if(acc.getAccountNumber() == accNumber){
if(acc instanceof SavingsAccount){
SavingsAccount savingsAccount = (SavingsAccount) acc;
System.out.println("Interest earned: " + savingsAccount.CalculateIntrest(days));
}
else if(acc instanceof PremiumSavingsAccount){
PremiumSavingsAccount premiumSavingsAccount = (PremiumSavingsAccount) acc;
System.out.println("Interest earned: " + premiumSavingsAccount.calculateInterest(days));
}
else {
System.out.println("Account does not earn interest.");
}
break;
}
}
break;
case 13 :
System.out.println("All Accounts of the bank are : ");
for(BankAccount acc : data){
System.out.println("Bank Name : "+acc.getBankName());
System.out.println("Branch name "+acc.getBranchName());
System.out.println("Account Number: " + acc.getAccountNumber());
System.out.println("Account Holder Name: " + acc.getAccountHolderName());
System.out.println("Balance: " + acc.getBalance());
System.out.println("IFSC CODE : "+acc.getIFSC_CODE());
System.out.println();
}
break;
case 14 :
System.out.println("Exiting the application. Thank you!");
sc.close();
System.exit(0); // zero is used for normal termination
return;
default :
System.out.println("Invalid option. Please try again.");
break;
}
}
}
}