-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
93 lines (80 loc) · 2.43 KB
/
BankAccount.java
File metadata and controls
93 lines (80 loc) · 2.43 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
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Base class for bank accounts.
*/
public class BankAccount {
// Instance variables with private access modifier (encapsulation)
private String accountNumber;
private String pin;
private double balance;
private String accountType;
private List<Transaction> transactionHistory;
// Constant minimum balance
public static final double MINIMUM_BALANCE = 100.0;
// Constructor
public BankAccount(String accountNumber, String pin, double initialBalance, String accountType) {
this.accountNumber = accountNumber;
this.pin = pin;
this.balance = initialBalance;
this.accountType = accountType;
this.transactionHistory = new ArrayList<>();
}
// PIN validation
public boolean validatePin(String enteredPin) {
return this.pin.equals(enteredPin);
}
// Method overloading for deposit
public boolean deposit(double amount) {
return deposit(amount, "Regular deposit");
}
public boolean deposit(double amount, String description) {
if (amount <= 0) {
return false;
}
this.balance += amount;
this.transactionHistory.add(new Transaction(
"Deposit",
amount,
new Date(),
description
));
return true;
}
// Withdrawal with exception handling
public boolean withdraw(double amount) {
try {
if (amount <= 0) {
throw new IllegalArgumentException("Invalid amount");
}
if (amount > this.balance) {
throw new InsufficientFundsException("Insufficient funds");
}
this.balance -= amount;
this.transactionHistory.add(new Transaction(
"Withdrawal",
amount,
new Date(),
"ATM withdrawal"
));
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
return false;
}
}
// Getters
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountType() {
return accountType;
}
public List<Transaction> getTransactionHistory() {
return new ArrayList<>(transactionHistory); // Return a copy
}
}