-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
719 lines (641 loc) · 24 KB
/
Main.java
File metadata and controls
719 lines (641 loc) · 24 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
import java.util.*;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class Main {
/**
* Creates a new bank account based on the specified type with the given initial deposit amount.
*
* @param scanner The scanner object used for input.
* @throws NumberFormatException If the initial deposit amount is not a valid float.
*/
public static void create(Scanner scanner) {
// Extract account type, account name, and initial deposit amount from input
String accountType = scanner.next();
String accountName = scanner.next();
float initialDeposit = Float.parseFloat(scanner.nextLine().trim());
BankingSystem system = BankingSystem.getInstance();
// Create the account based on the specified type
system.createAccount(accountType, accountName, initialDeposit);
}
/**
* Deposits a specified amount into the account with the given name.
*
* @param scanner The scanner object used for input.
* @throws NumberFormatException If the input amount is not a valid float.
*/
public static void deposit(Scanner scanner) {
String accountName = scanner.next();
float depositAmount = Float.parseFloat(scanner.nextLine().trim());
BankingSystem system = BankingSystem.getInstance();
system.deposit(accountName, depositAmount);
}
/**
* Withdraws a specified amount from the given bank account.
*
* @param scanner The scanner object used for input.
* @throws NumberFormatException If the withdrawal amount is not a valid float.
*/
public static void withdraw(Scanner scanner) {
// Extract account name and withdrawal amount from input
String accountName = scanner.next();
float withdrawalAmount = Float.parseFloat(scanner.nextLine().trim());
BankingSystem system = BankingSystem.getInstance();
system.withdraw(accountName, withdrawalAmount);
}
public static void transfer(Scanner scanner) {
String fromAccountName = scanner.next();
String toAccountName = scanner.next();
float transferAmount = Float.parseFloat(scanner.nextLine().trim());
BankingSystem system = BankingSystem.getInstance();
// Transfer the specified amount between specified accounts
system.transfer(transferAmount, fromAccountName, toAccountName);
}
/**
* Displays the details of a specified bank account.
*
* @param scanner The scanner object used for input.
*/
public static void view(Scanner scanner) {
String accountName = scanner.next();
BankingSystem system = BankingSystem.getInstance();
system.viewAccount(accountName);
}
/**
* Deactivates the specified bank account for withdraw operation.
*
* @param scanner The scanner object used for input.
*/
public static void deactivate(Scanner scanner) {
String accountName = scanner.next();
BankingSystem system = BankingSystem.getInstance();
system.deactivateAccount(accountName);
}
/**
* Activates the specified bank account.
*
* @param scanner The scanner object used for input.
*/
public static void activate(Scanner scanner) {
String accountName = scanner.next();
BankingSystem system = BankingSystem.getInstance();
system.activateAccount(accountName);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < n; i++) {
String typeOfOperation = scanner.next();
if (typeOfOperation.equals("Create")) {
String typeOfObject = scanner.next();
create(scanner);
} else if (typeOfOperation.equals("Deposit")) {
deposit(scanner);
} else if (typeOfOperation.equals("Withdraw")) {
withdraw(scanner);
} else if (typeOfOperation.equals("Transfer")) {
transfer(scanner);
} else if (typeOfOperation.equals("View")) {
view(scanner);
} else if (typeOfOperation.equals("Deactivate")) {
deactivate(scanner);
} else if (typeOfOperation.equals("Activate")) {
activate(scanner);
}
}
}
}
/**
* The BankingSystemProxy interface represents a proxy for accessing the banking system's functionality.
* It defines methods for creating accounts, depositing and withdrawing funds, transferring funds between accounts,
* viewing account details, deactivating and activating accounts.
*/
interface BankingSystemProxy {
/**
* Creates a new bank account with the specified type, name, and initial deposit amount.
*
* @param accountType The type of the account (e.g., "Savings", "Checking", "Business").
* @param accountName The name of the account.
* @param initialDeposit The initial deposit amount for the account.
*/
void createAccount(String accountType, String accountName, float initialDeposit);
/**
* Deposits a specified amount into the specified account.
*
* @param accountName The name of the account to deposit funds into.
* @param amount The amount to deposit.
*/
void deposit(String accountName, float amount);
/**
* Withdraws a specified amount from the specified account.
*
* @param accountName The name of the account to withdraw funds from.
* @param amount The amount to withdraw.
*/
void withdraw(String accountName, float amount);
/**
* Transfers funds from one account to another.
*
* @param amount The amount to transfer.
* @param fromAccountName The name of the account to transfer funds from.
* @param toAccountName The name of the account to transfer funds to.
*/
void transfer(float amount, String fromAccountName, String toAccountName);
/**
* Displays the details of the specified account.
*
* @param accountName The name of the account to view.
*/
void viewAccount(String accountName);
/**
* Deactivates the specified account.
*
* @param accountName The name of the account to deactivate.
*/
void deactivateAccount(String accountName);
/**
* Activates the specified account.
*
* @param accountName The name of the account to activate.
*/
void activateAccount(String accountName);
}
/**
* The BankingSystem class represents a banking system that manages bank accounts and facilitates transactions.
*/
class BankingSystem implements BankingSystemProxy {
/**
* List of accounts managed by the banking system.
*/
public List<Account> accounts;
/**
* Singleton instance of the BankingSystem class.
*/
private static BankingSystem instance;
/**
* Private constructor to prevent external instantiation.
* Initializes the accounts list.
*/
private BankingSystem() {
accounts = new ArrayList<>();
}
/**
* Retrieves the singleton instance of the BankingSystem class.
*
* @return The singleton instance of the BankingSystem class.
*/
public static BankingSystem getInstance() {
if (instance == null) {
instance = new BankingSystem();
}
return instance;
}
/**
* Creates a new bank account with the specified type, name, and initial deposit amount.
*
* @param accountType The type of the account (e.g., "Savings", "Checking", "Business").
* @param accountName The name of the account.
* @param initialDeposit The initial deposit amount for the account.
*/
@Override
public void createAccount(String accountType, String accountName, float initialDeposit) {
switch (accountType) {
case "Savings" -> {
SavingsAccount account = new SavingsAccount(accountName, initialDeposit);
accounts.add(account);
}
case "Checking" -> {
CheckingAccount account = new CheckingAccount(accountName, initialDeposit);
accounts.add(account);
}
case "Business" -> {
BusinessAccount account = new BusinessAccount(accountName, initialDeposit);
accounts.add(account);
}
}
}
/**
* Deposits a specified amount into the specified account.
*
* @param accountName The name of the account to deposit funds into.
* @param amount The amount to deposit.
*/
@Override
public void deposit(String accountName, float amount) {
if (!findAccount(accountName)) {
System.out.println("Error: Account " + accountName + " does not exist.");
return;
}
Account account = getAccount(accountName);
account.deposit(amount);
}
/**
* Withdraws a specified amount from the specified account.
*
* @param accountName The name of the account to withdraw funds from.
* @param amount The amount to withdraw.
*/
@Override
public void withdraw(String accountName, float amount) {
if (!findAccount(accountName)) {
System.out.println("Error: Account " + accountName + " does not exist.");
return;
}
Account account = getAccount(accountName);
account.withdraw(amount);
}
/**
* Transfers funds from one account to another.
*
* @param amount The amount to transfer.
* @param fromAccountName The name of the account to transfer funds from.
* @param toAccountName The name of the account to transfer funds to.
*/
@Override
public void transfer(float amount, String fromAccountName, String toAccountName) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
if (!findAccount(fromAccountName)) {
System.out.println("Error: Account " + fromAccountName + " does not exist.");
return;
} else if (!findAccount(toAccountName)) {
System.out.println("Error: Account " + toAccountName + " does not exist.");
return;
}
Account from = getAccount(fromAccountName);
Account to = getAccount(toAccountName);
if (from.getState().equals("Inactive")) {
System.out.println("Error: Account " + fromAccountName + " is inactive.");
return;
}
if (from.getBalance() < amount) {
System.out.println("Error: Insufficient funds for " + fromAccountName + ".");
return;
}
float feeAmount = amount * from.transactionFeeRate;
float amountAfterFee = amount - feeAmount;
from.setBalance(from.getBalance() - amount);
to.setBalance(to.getBalance() + amountAfterFee);
System.out.println(fromAccountName + " successfully transferred $" + df.format(amountAfterFee) + " to " + toAccountName +
". New Balance: $" + df.format(from.getBalance()) + ". Transaction Fee: $" + df.format(feeAmount) +
" (" + from.transactionFeeRate*100 + "%) in the system.");
from.addOperation("Transfer $" + df.format(amount));
}
@Override
public void viewAccount(String accountName) {
if (!findAccount(accountName)) {
System.out.println("Error: Account " + accountName + " does not exist.");
return;
}
Account account = getAccount(accountName);
account.view();
}
@Override
public void deactivateAccount(String accountName) {
if (!findAccount(accountName)) {
System.out.println("Error: Account " + accountName + " does not exist.");
return;
}
Account account = getAccount(accountName);
account.deactivate();
}
@Override
public void activateAccount(String accountName) {
if (!findAccount(accountName)) {
System.out.println("Error: Account " + accountName + " does not exist.");
return;
}
Account account = getAccount(accountName);
account.activate();
}
/**
* Checks if an account with the specified name exists in the system.
*
* @param accountName The name of the account to check.
* @return True if the account exists, false otherwise.
*/
public boolean findAccount(String accountName) {
for (Account acc : accounts) {
if (acc.getAccountName().equals(accountName)) {
return true;
}
}
return false;
}
/**
* Retrieves the account with the specified name from the system.
*
* @param accountName The name of the account to retrieve.
* @return The account object if found, null otherwise.
*/
public Account getAccount(String accountName) {
for (Account acc : accounts) {
if (acc.getAccountName().equals(accountName)) {
return acc;
}
}
return null;
}
/**
* Adds a new account to the banking system.
*
* @param acc The account to add.
*/
public void addAccount(Account acc) {
accounts.add(acc);
}
}
/**
* The AccountState interface defines methods for managing the state of an account.
*/
interface AccountState {
void activate();
void deactivate();
}
/**
* The Account class represents a bank account.
*/
abstract class Account {
/**
* List of operations performed on the account.
*/
protected List<String> operations;
/**
* The balance of the account.
*/
private float balance;
/**
* The transaction fee rate for the account.
*/
protected float transactionFeeRate;
/**
* The name of the account.
*/
private String accountName;
/**
* The state of the account.
*/
protected String state;
/**
* Constructs an Account object with the specified account name and initial deposit.
*
* @param accountName The name of the account.
* @param initialDeposit The initial deposit amount.
*/
protected Account(String accountName, float initialDeposit) {
this.accountName = accountName;
this.balance = initialDeposit;
this.state = "Active";
this.operations = new ArrayList<>();
}
/**
* Deposits funds into the account.
*
* @param amount The amount to deposit.
*/
public void deposit(float amount) {
balance += amount;
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
System.out.println(accountName + " successfully deposited $" + df.format(amount) +
". New Balance: $" + df.format(balance) + ".");
operations.add("Deposit $" + df.format(amount));
}
/**
* Withdraws funds from the account.
*
* @param amount The amount to withdraw.
*/
public void withdraw(float amount) {
if (getState().equals("Inactive")) {
System.out.println("Error: Account " + accountName + " is inactive.");
return;
}
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
float amountOfFee = amount * transactionFeeRate;
float amountAfterFee = amount - amountOfFee;
if (amount > balance) {
System.out.println("Error: Insufficient funds for " + accountName + ".");
return;
}
balance -= amount;
System.out.println(accountName + " successfully withdrew $" + df.format(amountAfterFee) +
". New Balance: $" + df.format(balance) + ". Transaction Fee: $" + df.format(amountOfFee) +
" (" + transactionFeeRate*100 + "%) in the system.");
operations.add("Withdrawal $" + df.format(amount));
}
/**
* Adds an operation to the list of operations performed on the account.
*
* @param operation The operation to add.
*/
public void addOperation(String operation) {
operations.add(operation);
}
public String getAccountName() {
return accountName;
}
public float getBalance() {
return balance;
}
public void setBalance(float newBalance) {
this.balance = newBalance;
}
public String getState() {
return state;
}
public void view() {}
public void activate() {}
public void deactivate() {}
}
/**
* The SavingsAccount class represents a savings account.
*/
class SavingsAccount extends Account implements AccountState {
/**
* Constructs a SavingsAccount object with the specified account name and initial balance.
*
* @param accountName The name of the account.
* @param initialBalance The initial balance of the account.
*/
public SavingsAccount(String accountName, float initialBalance) {
super(accountName, initialBalance);
this.transactionFeeRate = 0.015F;
creationPrint();
}
/**
* Prints a message indicating the creation of the savings account.
*/
private void creationPrint() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
System.out.println("A new Savings account created for " + getAccountName() +
" with an initial balance of $" + df.format(getBalance()) + ".");
operations.add("Initial Deposit $" + df.format(getBalance()));
}
/**
* Displays information about the savings account.
*/
@Override
public void view() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
System.out.print(getAccountName() + "'s Account: Type: Savings, Balance: $" +
df.format(getBalance()) + ", State: " + getState() + ", Transactions: [");
for (int i = 0; i < operations.size(); i++) {
if (i == operations.size() - 1) {
System.out.print(operations.get(i) + "].");
} else {
System.out.print(operations.get(i) + ", ");
}
}
System.out.println();
}
@Override
public void activate() {
if (state.equals("Active")) {
System.out.println("Error: Account " + getAccountName() + " is already activated.");
return;
}
state = "Active";
System.out.println(getAccountName() + "'s account is now activated.");
}
@Override
public void deactivate() {
if (state.equals("Inactive")) {
System.out.println("Error: Account " + getAccountName() + " is already deactivated.");
return;
}
state = "Inactive";
System.out.println(getAccountName() + "'s account is now deactivated.");
}
}
/**
* The CheckingAccount class represents a checking account.
*/
class CheckingAccount extends Account implements AccountState {
/**
* Constructs a CheckingAccount object with the specified account name and initial balance.
*
* @param accountName The name of the account.
* @param initialBalance The initial balance of the account.
*/
public CheckingAccount(String accountName, float initialBalance) {
super(accountName, initialBalance);
this.transactionFeeRate = 0.02F;
creationPrint();
}
/**
* Prints a message indicating the creation of the checking account.
*/
private void creationPrint() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
System.out.println("A new Checking account created for " + getAccountName() +
" with an initial balance of $" + df.format(getBalance()) + ".");
operations.add("Initial Deposit $" + df.format(getBalance()));
}
@Override
public void view() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
System.out.print(getAccountName() + "'s Account: Type: Checking, Balance: $" +
df.format(getBalance()) + ", State: " + getState() + ", Transactions: [");
for (int i = 0; i < operations.size(); i++) {
if (i == operations.size() - 1) {
System.out.print(operations.get(i) + "].");
} else {
System.out.print(operations.get(i) + ", ");
}
}
System.out.println();
}
@Override
public void activate() {
if (state.equals("Active")) {
System.out.println("Error: Account " + getAccountName() + " is already activated.");
return;
}
state = "Active";
System.out.println(getAccountName() + "'s account is now activated.");
}
@Override
public void deactivate() {
if (state.equals("Inactive")) {
System.out.println("Error: Account " + getAccountName() + " is already deactivated.");
return;
}
state = "Inactive";
System.out.println(getAccountName() + "'s account is now deactivated.");
}
}
/**
* The BusinessAccount class represents a business account.
*/
class BusinessAccount extends Account implements AccountState {
/**
* Constructs a BusinessAccount object with the specified account name and initial balance.
*
* @param accountName The name of the account.
* @param initialBalance The initial balance of the account.
*/
public BusinessAccount(String accountName, float initialBalance) {
super(accountName, initialBalance);
this.transactionFeeRate = 0.025F;
creationPrint();
}
/**
* Prints a message indicating the creation of the business account.
*/
private void creationPrint() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
System.out.println("A new Business account created for " + getAccountName() +
" with an initial balance of $" + df.format(getBalance()) + ".");
operations.add("Initial Deposit $" + df.format(getBalance()));
}
@Override
public void view() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("0.000", symbols);
System.out.print(getAccountName() + "'s Account: Type: Business, Balance: $" +
df.format(getBalance()) + ", State: " + getState() + ", Transactions: [");
for (int i = 0; i < operations.size(); i++) {
if (i == operations.size() - 1) {
System.out.print(operations.get(i) + "].");
} else {
System.out.print(operations.get(i) + ", ");
}
}
System.out.println();
}
@Override
public void activate() {
if (state.equals("Active")) {
System.out.println("Error: Account " + getAccountName() + " is already activated.");
return;
}
state = "Active";
System.out.println(getAccountName() + "'s account is now activated.");
}
@Override
public void deactivate() {
if (state.equals("Inactive")) {
System.out.println("Error: Account " + getAccountName() + " is already deactivated.");
return;
}
state = "Inactive";
System.out.println(getAccountName() + "'s account is now deactivated.");
}
}