-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawalTransaction.java
More file actions
116 lines (96 loc) · 4.28 KB
/
Copy pathWithdrawalTransaction.java
File metadata and controls
116 lines (96 loc) · 4.28 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
package com.transactions.transactions;
import com.transactions.exceptions.InsufficientFundsException;
import com.transactions.models.BankAccount;
import java.util.Calendar;
public class WithdrawalTransaction extends BaseTransaction {
private BankAccount appliedAccount;
private boolean applied;
private double amountNotWithdrawn;
private double amountDebited;
public WithdrawalTransaction(double amount, Calendar date) {
super(amount, date);
this.applied = false;
this.amountNotWithdrawn = 0;
this.amountDebited = 0;
}
@Override
public void printTransactionDetails() {
System.out.println("--- Withdrawal Transaction ---");
System.out.println("Transaction ID: " + getTransactionID());
System.out.println("Amount: " + getAmount());
System.out.println("Date: " + getDate().getTime());
System.out.println("Type: WITHDRAWAL (reversible)");
System.out.println("Applied: " + applied);
if (amountNotWithdrawn > 0) {
System.out.println("Amount not withdrawn: " + amountNotWithdrawn);
}
}
// apply method uses throws to propagate InsufficientFundsException to the caller
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
if (ba.getBalance() < getAmount()) {
throw new InsufficientFundsException(getAmount(), ba.getBalance());
}
double balanceBefore = ba.getBalance();
ba.debit(getAmount());
this.appliedAccount = ba;
this.applied = true;
this.amountDebited = getAmount();
System.out.println("Withdrawal applied to account: " + ba.getAccountHolder());
System.out.println("Balance before: " + balanceBefore);
System.out.println("Balance after: " + ba.getBalance());
printTransactionDetails();
}
// overloaded apply - handles partial withdrawal using try catch finally
public void apply(BankAccount ba, boolean allowPartial) {
try {
if (ba.getBalance() <= 0) {
throw new InsufficientFundsException(getAmount(), ba.getBalance());
}
if (allowPartial && ba.getBalance() < getAmount()) {
// balance is between 0 and the withdrawal amount so withdraw whatever is available
double available = ba.getBalance();
this.amountNotWithdrawn = getAmount() - available;
this.amountDebited = available;
ba.debit(available);
this.appliedAccount = ba;
this.applied = true;
System.out.println("Partial withdrawal: only " + available + " was available");
System.out.println("Amount not withdrawn: " + amountNotWithdrawn);
System.out.println("Balance after: " + ba.getBalance());
} else {
if (ba.getBalance() < getAmount()) {
throw new InsufficientFundsException(getAmount(), ba.getBalance());
}
double balanceBefore = ba.getBalance();
ba.debit(getAmount());
this.appliedAccount = ba;
this.applied = true;
this.amountDebited = getAmount();
System.out.println("Withdrawal applied: " + getAmount());
System.out.println("Balance before: " + balanceBefore);
System.out.println("Balance after: " + ba.getBalance());
}
} catch (InsufficientFundsException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Transaction complete.");
printTransactionDetails();
}
}
public boolean reverse() {
if (!applied || appliedAccount == null) {
System.out.println("Cannot reverse: transaction was not applied.");
return false;
}
appliedAccount.credit(amountDebited);
this.applied = false;
System.out.println("Withdrawal reversed for: " + appliedAccount.getAccountHolder());
System.out.println("Amount restored: " + amountDebited);
System.out.println("New balance: " + appliedAccount.getBalance());
return true;
}
public double getAmountNotWithdrawn() {
return amountNotWithdrawn;
}
}