-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankDatabase.java
More file actions
42 lines (36 loc) · 1.31 KB
/
BankDatabase.java
File metadata and controls
42 lines (36 loc) · 1.31 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
import java.util.HashMap;
import java.util.Map;
/**
* BankDatabase class - simulates a database of bank accounts.
* Demonstrates: singleton pattern, collections framework.
*/
public class BankDatabase {
private static BankDatabase instance;
private Map<String, BankAccount> accounts;
// Private constructor to implement Singleton
private BankDatabase() {
accounts = new HashMap<>();
initializeAccounts();
}
// Singleton instance accessor
public static synchronized BankDatabase getInstance() {
if (instance == null) {
instance = new BankDatabase();
}
return instance;
}
// Initialize with some dummy accounts
private void initializeAccounts() {
accounts.put("1234567890123456", new SavingsAccount("1234567890123456", "1234", 5000.0, 2.5));
accounts.put("9876543210987654", new CheckingAccount("9876543210987654", "4321", 3000.0, 500.0));
}
// Get account by account number
public BankAccount getAccount(String accountNumber) {
return accounts.get(accountNumber);
}
// Authenticate user by matching PIN
public boolean authenticateUser(String accountNumber, String pin) {
BankAccount account = getAccount(accountNumber);
return account != null && account.validatePin(pin);
}
}