-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicATM.java
More file actions
68 lines (55 loc) · 1.83 KB
/
BasicATM.java
File metadata and controls
68 lines (55 loc) · 1.83 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
import java.util.HashMap;
import java.util.Map;
/**
* BasicATM class - implements ATM interface
* Demonstrates: interface implementation, singleton pattern.
*/
public class BasicATM implements ATM {
// Singleton instance
private static BasicATM instance;
// Map to track daily withdrawals by account number
private Map<String, Double> dailyWithdrawals;
// Private constructor for singleton
private BasicATM() {
dailyWithdrawals = new HashMap<>();
}
// Singleton access method
public static synchronized BasicATM getInstance() {
if (instance == null) {
instance = new BasicATM();
}
return instance;
}
@Override
public boolean validateCard(String cardNumber, String pin) {
// Basic dummy validation
return cardNumber.length() == 16 && pin.length() == 4;
}
@Override
public double checkBalance(BankAccount account) {
return account.getBalance();
}
@Override
public boolean withdrawCash(BankAccount account, double amount) {
String accountNumber = account.getAccountNumber();
// Enforce daily limit
double todayWithdrawal = dailyWithdrawals.getOrDefault(accountNumber, 0.0);
if (todayWithdrawal + amount > DAILY_WITHDRAWAL_LIMIT) {
System.out.println("Exceeds daily withdrawal limit.");
return false;
}
boolean success = account.withdraw(amount);
if (success) {
dailyWithdrawals.put(accountNumber, todayWithdrawal + amount);
}
return success;
}
@Override
public boolean depositFunds(BankAccount account, double amount) {
return account.deposit(amount);
}
// Call this at the end of day to reset limits
public void resetDailyLimits() {
dailyWithdrawals.clear();
}
}