-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawalTransaction.java
More file actions
93 lines (63 loc) · 1.97 KB
/
Copy pathWithdrawalTransaction.java
File metadata and controls
93 lines (63 loc) · 1.97 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
public class WithdrawalTransaction extends BaseTransaction {
private BankAccount accountUsed;
private boolean reversed = false;
private double amountNotWithdrawn;
public WithdrawalTransaction(double amount,
String transactionID) {
super(amount, transactionID);
}
@Override
public void apply(BankAccount ba) {
try {
ba.withdraw(amount);
accountUsed = ba;
System.out.println(
"Withdrawal of " +
amount +
" completed.");
} catch (InsufficientFundsException e) {
System.out.println(
e.getMessage());
}
}
public void apply(BankAccount ba,
boolean useAvailableBalance) {
try {
double balance =
ba.getBalance();
if (balance <= 0) {
throw new
InsufficientFundsException(
"Account balance is zero.");
}
if (balance < amount) {
amountNotWithdrawn =
amount - balance;
ba.withdraw(balance);
System.out.println(
"Partial withdrawal completed.");
} else {
ba.withdraw(amount);
amountNotWithdrawn = 0;
}
} catch (InsufficientFundsException e) {
System.out.println(
e.getMessage());
} finally {
System.out.println(
"Transaction processing finished.");
}
}
public boolean reverse() {
if (accountUsed != null &&
!reversed) {
accountUsed.deposit(amount);
reversed = true;
return true;
}
return false;
}
public double getAmountNotWithdrawn() {
return amountNotWithdrawn;
}
}