-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
74 lines (63 loc) · 3.54 KB
/
Copy pathMain.java
File metadata and controls
74 lines (63 loc) · 3.54 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
// Name: Fatuma Bobba
// Registration No: SCT212-0072/2024
// QUESTION 4 - Client Testing Code
public class Main {
public static void main(String[] args) {
System.out.println("=== STARTING BANK TRANSACTION SYSTEM TESTS ===\n");
// Set up test account
BankAccount myAccount = new BankAccount(500.0);
System.out.println("Initial Bank Account Balance: $" + myAccount.getBalance());
System.out.println("------------------------------------------------");
// 1. Test Deposit Transaction
System.out.println("\n--- Testing Deposit Transaction ---");
BaseTransaction deposit = new DepositTransaction(200.0); [cite: 52]
try {
deposit.apply(myAccount); [cite: 52]
deposit.printTransactionDetails();
System.out.println("New Account Balance: $" + myAccount.getBalance());
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
// 2. Test Successful Withdrawal Transaction
System.out.println("\n--- Testing Successful Withdrawal Transaction ---");
WithdrawalTransaction withdrawal1 = new WithdrawalTransaction(150.0); [cite: 52]
try {
withdrawal1.apply(myAccount); [cite: 52]
withdrawal1.printTransactionDetails();
System.out.println("New Account Balance: $" + myAccount.getBalance());
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
// 3. Test Question 2: Withdrawal Reversal
System.out.println("\n--- Testing Question 2: Reversal of Withdrawal ---");
withdrawal1.reverse();
System.out.println("Account Balance after reversal: $" + myAccount.getBalance());
// 4. Test Question 3: Exception Handling (Standard apply throwing Exception)
System.out.println("\n--- Testing Question 3: Exception Throwing (Insufficient Funds) ---");
WithdrawalTransaction expensiveWithdrawal = new WithdrawalTransaction(2000.0);
try {
expensiveWithdrawal.apply(myAccount); // This will fail and throw the exception
} catch (InsufficientFundsException e) {
System.out.println("Successfully caught expected exception: " + e.getMessage());
}
// 5. Test Question 3: Overloaded apply() with Partial Withdrawal (Try/Catch/Finally)
System.out.println("\n--- Testing Question 3: Overloaded Partial Withdrawal Method ---");
// Balance is currently $700. Attempting to withdraw $1000.
WithdrawalTransaction partialWithdrawal = new WithdrawalTransaction(1000.0);
partialWithdrawal.apply(myAccount, true); // True activates the partial withdrawal catch block logic
partialWithdrawal.printTransactionDetails();
System.out.println("Final Account Balance: $" + myAccount.getBalance());
// 6. Test Hint requirement: Polymorphism & Type Casting
System.out.println("\n--- Testing Question 4 Hint: Type Casting to Base Object ---");
WithdrawalTransaction basicW = new WithdrawalTransaction(50.0); [cite: 52]
// Polymorphically mapping subtype object to base type object via type casting
BaseTransaction baseRef = (BaseTransaction) basicW; [cite: 53]
try {
// This runs the overriding apply method due to late binding polymorphism
baseRef.apply(myAccount); [cite: 54]
baseRef.printTransactionDetails();
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
}
}