-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
205 lines (183 loc) · 8.08 KB
/
Copy pathAccount.java
File metadata and controls
205 lines (183 loc) · 8.08 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.util.ArrayList;
/**
* The Account class represents a basic bank account.
* It demonstrates the OOP concept of Encapsulation by keeping fields private
* and providing public getter and setter methods, as well as controlled transactional methods.
*/
public class Account {
// Private variables (Encapsulation)
private String accountNumber;
private String accountHolderName;
private double balance;
private ArrayList<String> transactionHistory;
// Overloaded Constructor 1: Account with initial balance
public Account(String accountNumber, String accountHolderName, double initialBalance) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
if (initialBalance >= 0) {
this.balance = initialBalance;
} else {
this.balance = 0;
System.out.println("Initial balance cannot be negative. Setting balance to 0.0");
}
this.transactionHistory = new ArrayList<>();
// Record initial deposit in transaction history
recordTransaction("Account created with initial balance: Rs. " + this.balance);
}
// Overloaded Constructor 2: Account with zero initial balance (Constructor Overloading)
public Account(String accountNumber, String accountHolderName) {
this(accountNumber, accountHolderName, 0.0);
}
// Getter for Account Number
public String getAccountNumber() {
return accountNumber;
}
// Getter for Account Holder Name
public String getAccountHolderName() {
return accountHolderName;
}
// Setter for Account Holder Name (Encapsulation)
public void setAccountHolderName(String accountHolderName) {
this.accountHolderName = accountHolderName;
}
// Getter for Balance
public double getBalance() {
return balance;
}
// Internal helper to update balance and record transaction
protected void setBalance(double balance) {
this.balance = balance;
}
// Helper method to record transactions in ArrayList
protected void recordTransaction(String details) {
transactionHistory.add(details);
}
/**
* Deposits a specified amount into the account.
* @param amount The amount to deposit.
*/
public void deposit(double amount) {
if (amount <= 0) {
System.out.println("Error: Deposit amount must be positive.");
return;
}
this.balance += amount;
String transactionDetails = "Deposited: Rs. " + amount + " | Current Balance: Rs. " + this.balance;
recordTransaction(transactionDetails);
System.out.println("Successfully deposited Rs. " + amount);
}
/**
* Withdraws a specified amount from the account.
* Performs validation on the amount and checks for sufficient balance.
* @param amount The amount to withdraw.
*/
public void withdraw(double amount) {
if (amount <= 0) {
System.out.println("Error: Withdrawal amount must be positive.");
return;
}
if (amount > this.balance) {
System.out.println("Error: Insufficient Balance. Current balance: Rs. " + this.balance);
recordTransaction("Failed Withdrawal: Rs. " + amount + " (Insufficient Balance)");
return;
}
this.balance -= amount;
String transactionDetails = "Withdrew: Rs. " + amount + " | Current Balance: Rs. " + this.balance;
recordTransaction(transactionDetails);
System.out.println("Successfully withdrew Rs. " + amount);
}
/**
* Displays the current account details and balance.
*/
public void displayBalance() {
System.out.println("\n--- Account Balance ---");
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Holder: " + accountHolderName);
System.out.println("Current Balance: Rs. " + balance);
System.out.println("-----------------------");
}
/**
* Displays all transaction logs stored in the ArrayList.
*/
public void showTransactionHistory() {
System.out.println("\n--- Transaction History for Account " + accountNumber + " ---");
if (transactionHistory.isEmpty()) {
System.out.println("No transactions recorded yet.");
} else {
for (int i = 0; i < transactionHistory.size(); i++) {
System.out.println((i + 1) + ". " + transactionHistory.get(i));
}
}
System.out.println("-------------------------------------------------------");
}
}
/**
* SavingsAccount subclass demonstrating Inheritance and Method Overriding.
* It extends the base class Account and adds a minimum balance requirement
* and interest calculation functionality.
*/
class SavingsAccount extends Account {
private double interestRate; // e.g., 0.03 for 3%
private static final double MINIMUM_BALANCE = 500.0; // Savings accounts must maintain Rs. 500
public SavingsAccount(String accountNumber, String accountHolderName, double initialBalance, double interestRate) {
// Calling base class constructor
super(accountNumber, accountHolderName, initialBalance);
this.interestRate = interestRate;
// Verify minimum balance constraint on creation
if (initialBalance < MINIMUM_BALANCE) {
System.out.println("Warning: Savings accounts should maintain a minimum balance of Rs. " + MINIMUM_BALANCE);
}
}
// Overloaded constructor for SavingsAccount with default 3.5% interest rate
public SavingsAccount(String accountNumber, String accountHolderName, double initialBalance) {
this(accountNumber, accountHolderName, initialBalance, 0.035);
}
// Getter for interest rate
public double getInterestRate() {
return interestRate;
}
// Method Overriding: Overrides withdraw() to enforce minimum balance rule
@Override
public void withdraw(double amount) {
if (amount <= 0) {
System.out.println("Error: Withdrawal amount must be positive.");
return;
}
// Enforce minimum balance after withdrawal
if (getBalance() - amount < MINIMUM_BALANCE) {
System.out.println("Error: Insufficient Balance. A minimum balance of Rs. " + MINIMUM_BALANCE + " must be maintained.");
System.out.println("Current Balance: Rs. " + getBalance() + " | Maximum allowed withdrawal: Rs. " + (getBalance() - MINIMUM_BALANCE));
recordTransaction("Failed Withdrawal: Rs. " + amount + " (Enforced Minimum Balance constraint)");
return;
}
// Use base class setBalance & recordTransaction methods
setBalance(getBalance() - amount);
String transactionDetails = "Withdrew (Savings): Rs. " + amount + " | Current Balance: Rs. " + getBalance();
recordTransaction(transactionDetails);
System.out.println("Successfully withdrew Rs. " + amount + " from Savings Account.");
}
/**
* Calculates and applies interest to the balance.
* Demonstrates an operation specific to Savings Accounts.
*/
public void applyInterest() {
double interest = getBalance() * interestRate;
if (interest > 0) {
setBalance(getBalance() + interest);
String transactionDetails = "Interest Applied (" + (interestRate * 100) + "%): Rs. " + interest + " | Current Balance: Rs. " + getBalance();
recordTransaction(transactionDetails);
System.out.println("Successfully applied interest of Rs. " + interest);
} else {
System.out.println("No interest applied (balance is 0).");
}
}
// Method Overriding: Overrides displayBalance() to show additional details
@Override
public void displayBalance() {
super.displayBalance();
System.out.println("Account Type: Savings Account");
System.out.println("Interest Rate: " + (interestRate * 100) + "% per annum");
System.out.println("Minimum Balance Required: Rs. " + MINIMUM_BALANCE);
System.out.println("-----------------------");
}
}