-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
166 lines (136 loc) · 4.66 KB
/
Copy pathAccount.java
File metadata and controls
166 lines (136 loc) · 4.66 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
package model.entity;
import java.util.ArrayList;
import java.util.List;
import exceptions.DailyLimitExceededException;
import exceptions.InsufficientFundsException;
import exceptions.InvalidAmountException;
import exceptions.InvalidIFSCException;
import exceptions.MinimumBalanceException;
import model.enums.AccountType;
import model.enums.TransactionType;
/**
* Abstract base class for all bank accounts.
* Provides common functionality: deposit, withdraw, transactions, interest.
*/
public abstract class Account implements BankAccount {
// 1. Fields (private, final first)
private final int accNo;
private final String ifscCode;
private double balance;
private final AccountType accType;
private final List<Transaction> transactions = new ArrayList<>();
private double todayDebitTotal = 0.0;
private java.time.LocalDate lastDebitDate = java.time.LocalDate.now();
private static final double DAILY_LIMIT = 50000.0;
// 2. Constructor
public Account(int accNo, String ifscCode, double balance, AccountType accType)
throws InvalidIFSCException, MinimumBalanceException{
if (!ifscCode.toUpperCase().matches("[A-Z]{4}0[A-Z]{6}")){
throw new InvalidIFSCException(ifscCode);
}
this.accNo = accNo;
this.ifscCode = ifscCode.toUpperCase();
if (balance < util.BankConstants.MIN_BALANCE) {
throw new MinimumBalanceException(balance);
}
this.balance = Math.max(0, balance);
this.accType = accType;
}
// 3. Interface Methods (BankAccount)
@Override
public int getAccNo() {
return accNo;
}
@Override
public double getBalance() {
return balance;
}
@Override
public void deposit(double amount) throws InvalidAmountException {
if (amount <= 0) throw new InvalidAmountException(amount);
balance += amount;
transactions.add(new Transaction(TransactionType.DEPOSIT, amount, balance,
"Deposit to A/c " + accNo));
System.out.println("✅ Deposited ₹" + String.format("%.2f", amount));
}
@Override
public void withdraw(double amount) throws InvalidAmountException,
InsufficientFundsException, DailyLimitExceededException, MinimumBalanceException {
if (amount <= 0)
throw new InvalidAmountException(amount);
if (balance < amount)
throw new InsufficientFundsException(amount, balance);
double newBalance = balance - amount;
if (newBalance < util.BankConstants.MIN_BALANCE) {
throw new MinimumBalanceException(newBalance);
}
checkDailyLimit(amount);
// ✅ Apply withdrawal
balance = newBalance; // Use calculated value
todayDebitTotal += amount;
transactions.add(new Transaction(TransactionType.TRANSFER_OUT, amount, balance,
"Withdrawal from A/c " + accNo));
System.out.println("✅ Withdrew ₹" + String.format("%.2f", amount));
}
// 4. Getters/Setters (Business fields)
public String getIfscCode() {
return ifscCode;
}
public AccountType getAccType() {
return accType;
}
// 5. Transaction Management
public List<Transaction> getTransactions() {
return new ArrayList<>(transactions); // Defensive copy
}
/**
* Prints last N transactions in table format
*/
public void printStatement(int count) {
System.out.println("\n=== LAST " + count + " TRANSACTIONS ===");
System.out.println("Date | Type | Amount | Balance | Desc");
System.out.println("------------------------------------------------");
if (transactions.isEmpty()) {
System.out.println("No transactions yet.");
return;
}
int start = Math.max(0, transactions.size() - count);
for (int i = start; i < transactions.size(); i++) {
System.out.println(transactions.get(i));
}
}
// 6. Interest Management (Abstract method + utility)
/**
* Subclasses must implement interest calculation logic
*/
public abstract double calculateInterest();
/**
* Adds calculated interest to balance and records transaction
*/
public final void addInterestToBalance() {
double interest = calculateInterest();
if (interest > 0) {
balance += interest;
transactions.add(new Transaction(TransactionType.DEPOSIT, interest, balance,
"Interest credited to A/c " + accNo));
System.out.println("✅ Interest added: ₹" + String.format("%.2f", interest));
}
}
// 7. toString() - Professional format
@Override
public String toString() {
return String.format("AccNo=%d, IFSC=%s, Bal=₹%.2f, Type=%s",
accNo, ifscCode, balance, accType);
}
private void checkDailyLimit(double amount) throws DailyLimitExceededException {
java.time.LocalDate today = java.time.LocalDate.now();
// reset if new day
if (!today.equals(lastDebitDate)) {
todayDebitTotal = 0.0;
lastDebitDate = today;
}
if (todayDebitTotal + amount > DAILY_LIMIT) {
throw new DailyLimitExceededException();
}
}
}