Repository navigation
Expand file tree
/
Copy pathAccount.java
More file actions
62 lines (49 loc) · 1.72 KB
/
Copy pathAccount.java
File metadata and controls
62 lines (49 loc) · 1.72 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
import java.util.ArrayList;
import java.util.List;
public class Account {
private String accountHolder;
private double balance;
private List<Double> transactionHistory = new ArrayList<>();
public Account(String accountHolder, double initialBalance) {
this.accountHolder = accountHolder;
this.balance = initialBalance;
}
public void processTransaction(double amount) throws InSufficientFundsException {
if (amount <= 0) {
throw new IllegalArgumentException("Transaction amount must be positive.");
}
if (amount > balance) {
throw new InSufficientFundsException("Insufficient Funds! Available: $" + balance);
}
balance -= amount;
transactionHistory.add(amount);
if (transactionHistory.size() > 5) {
transactionHistory.remove(0);
}
System.out.println("Withdrew: $" + amount);
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: $" + amount);
}
}
public void printMiniStatement() {
System.out.println("\n Mini Statement for " + accountHolder);
if (transactionHistory.isEmpty()) {
System.out.println("No transactions yet.");
}
else {
for (int i = 0; i < transactionHistory.size(); i++) {
System.out.println((i + 1) + ". Withdrawal: -$" + transactionHistory.get(i));
}
}
System.out.println("Current Balance: $" + getBalance());
}
public double getBalance() {
return balance;
}
public String getAccountHolder() {
return accountHolder;
}
}