-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
119 lines (89 loc) · 5.12 KB
/
Copy pathMain.java
File metadata and controls
119 lines (89 loc) · 5.12 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
package com.transactions;
import com.transactions.exceptions.InsufficientFundsException;
import com.transactions.models.BankAccount;
import com.transactions.transactions.BaseTransaction;
import com.transactions.transactions.DepositTransaction;
import com.transactions.transactions.WithdrawalTransaction;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
// create bank accounts for testing
BankAccount account1 = new BankAccount("ACC001", "Alice Wanjiku", 10000.00);
BankAccount account2 = new BankAccount("ACC002", "Bob Otieno", 500.00);
BankAccount account3 = new BankAccount("ACC003", "Carol Mwangi", 0.00);
System.out.println("===== Q1: Testing BaseTransaction =====");
BaseTransaction baseTransaction = new BaseTransaction(200.00, Calendar.getInstance());
System.out.println("Amount: " + baseTransaction.getAmount());
System.out.println("ID: " + baseTransaction.getTransactionID());
System.out.println("Date: " + baseTransaction.getDate().getTime());
try {
baseTransaction.apply(account1);
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}
System.out.println("Balance after base apply (should be unchanged): " + account1.getBalance());
System.out.println("\n===== Q2: Testing DepositTransaction =====");
DepositTransaction deposit = new DepositTransaction(5000.00, Calendar.getInstance());
try {
deposit.apply(account1);
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}
System.out.println("Balance after deposit: " + account1.getBalance());
System.out.println("\n===== Q2: Testing WithdrawalTransaction and reverse() =====");
WithdrawalTransaction withdrawal = new WithdrawalTransaction(3000.00, Calendar.getInstance());
try {
withdrawal.apply(account1);
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}
System.out.println("Balance after withdrawal: " + account1.getBalance());
boolean reversed = withdrawal.reverse();
System.out.println("Reversal successful: " + reversed);
System.out.println("Balance after reversal: " + account1.getBalance());
// try to reverse again - should fail
withdrawal.reverse();
System.out.println("\n===== Q3: Testing InsufficientFundsException with throws =====");
WithdrawalTransaction bigWithdrawal = new WithdrawalTransaction(2000.00, Calendar.getInstance());
try {
bigWithdrawal.apply(account2);
} catch (InsufficientFundsException e) {
System.out.println("Caught exception: " + e.getMessage());
}
System.out.println("Bob balance unchanged: " + account2.getBalance());
System.out.println("\n===== Q3: Testing overloaded apply() with try-catch-finally =====");
// Bob has 500, trying to withdraw 2000 - should do partial withdrawal
WithdrawalTransaction partialW = new WithdrawalTransaction(2000.00, Calendar.getInstance());
partialW.apply(account2, true);
System.out.println("Amount not withdrawn: " + partialW.getAmountNotWithdrawn());
// Carol has 0 balance - should be caught inside the method
System.out.println("\nTesting with zero balance account:");
WithdrawalTransaction zeroW = new WithdrawalTransaction(500.00, Calendar.getInstance());
zeroW.apply(account3, true);
System.out.println("\n===== Q4: Testing polymorphism with type casting =====");
BankAccount testAccount = new BankAccount("ACC004", "David Kamau", 8000.00);
// upcast DepositTransaction to BaseTransaction reference
// this demonstrates late binding - the JVM calls DepositTransaction.apply() at runtime
BaseTransaction btDeposit = new DepositTransaction(1000.00, Calendar.getInstance());
System.out.println("Runtime type: " + btDeposit.getClass().getSimpleName());
try {
btDeposit.apply(testAccount); // late binding - calls DepositTransaction.apply()
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}
// upcast WithdrawalTransaction to BaseTransaction reference
BaseTransaction btWithdrawal = new WithdrawalTransaction(2000.00, Calendar.getInstance());
System.out.println("Runtime type: " + btWithdrawal.getClass().getSimpleName());
try {
btWithdrawal.apply(testAccount); // late binding - calls WithdrawalTransaction.apply()
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}
// downcast back to WithdrawalTransaction to access reverse()
if (btWithdrawal instanceof WithdrawalTransaction) {
WithdrawalTransaction wt = (WithdrawalTransaction) btWithdrawal;
wt.reverse();
}
System.out.println("David final balance: " + testAccount.getBalance());
}
}