forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvancedBankingSystem.java
More file actions
157 lines (136 loc) · 4.73 KB
/
AdvancedBankingSystem.java
File metadata and controls
157 lines (136 loc) · 4.73 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
import java.util.*;
import java.util.concurrent.locks.*;
class BankAccount {
private int accountId;
private String ownerName;
private double balance;
private boolean hasLoan;
private double loanAmount;
private Lock lock = new ReentrantLock();
public BankAccount(int accountId, String ownerName, double balance) {
this.accountId = accountId;
this.ownerName = ownerName;
this.balance = balance;
this.hasLoan = false;
this.loanAmount = 0;
}
// Deposit money (thread-safe)
public void deposit(double amount) {
lock.lock();
try {
balance += amount;
System.out.println(ownerName + " deposited $" + amount + ", New Balance: $" + balance);
} finally {
lock.unlock();
}
}
// Withdraw money (thread-safe)
public boolean withdraw(double amount) {
lock.lock();
try {
if (amount <= balance) {
balance -= amount;
System.out.println(ownerName + " withdrew $" + amount + ", Remaining Balance: $" + balance);
return true;
} else {
System.out.println(ownerName + " attempted to withdraw $" + amount + " but insufficient funds!");
return false;
}
} finally {
lock.unlock();
}
}
// Transfer money to another account
public boolean transfer(BankAccount target, double amount) {
if (this.withdraw(amount)) {
target.deposit(amount);
System.out.println(ownerName + " transferred $" + amount + " to " + target.ownerName);
return true;
}
return false;
}
// Request a loan
public boolean requestLoan(double amount) {
lock.lock();
try {
if (!hasLoan && balance >= amount * 0.1) { // simple approval: balance >= 10% of loan
hasLoan = true;
loanAmount = amount;
balance += amount;
System.out.println(ownerName + " got loan of $" + amount);
return true;
} else {
System.out.println(ownerName + " loan request denied!");
return false;
}
} finally {
lock.unlock();
}
}
public double getBalance() { return balance; }
public boolean hasLoan() { return hasLoan; }
public double getLoanAmount() { return loanAmount; }
public String getOwnerName() { return ownerName; }
}
class Bank {
private List<BankAccount> accounts = new ArrayList<>();
public void addAccount(BankAccount account) {
accounts.add(account);
}
// Report top 3 richest accounts
public void topRichest() {
accounts.sort((a, b) -> Double.compare(b.getBalance(), a.getBalance()));
System.out.println("\nTop 3 richest accounts:");
for (int i = 0; i < Math.min(3, accounts.size()); i++) {
BankAccount a = accounts.get(i);
System.out.println(a.getOwnerName() + " - Balance: $" + a.getBalance());
}
}
// Report accounts with active loans
public void activeLoans() {
System.out.println("\nAccounts with active loans:");
for (BankAccount a : accounts) {
if (a.hasLoan()) {
System.out.println(a.getOwnerName() + " - Loan Amount: $" + a.getLoanAmount());
}
}
}
}
// Simulate concurrent transactions
class TransactionThread extends Thread {
private BankAccount account;
private BankAccount target;
private double amount;
public TransactionThread(BankAccount account, BankAccount target, double amount) {
this.account = account;
this.target = target;
this.amount = amount;
}
public void run() {
account.transfer(target, amount);
}
}
public class AdvancedBankingSystem {
public static void main(String[] args) throws InterruptedException {
Bank bank = new Bank();
BankAccount alice = new BankAccount(101, "Alice", 1000);
BankAccount bob = new BankAccount(102, "Bob", 1500);
BankAccount charlie = new BankAccount(103, "Charlie", 2000);
bank.addAccount(alice);
bank.addAccount(bob);
bank.addAccount(charlie);
// Concurrent transfers
Thread t1 = new TransactionThread(alice, bob, 200);
Thread t2 = new TransactionThread(bob, charlie, 300);
Thread t3 = new TransactionThread(charlie, alice, 500);
t1.start(); t2.start(); t3.start();
t1.join(); t2.join(); t3.join();
// Loans
alice.requestLoan(1000);
bob.requestLoan(500);
charlie.requestLoan(3000);
// Reports
bank.topRichest();
bank.activeLoans();
}
}