From 7326ab78acd362a43d8d7e820b8cb41a2d9e61f9 Mon Sep 17 00:00:00 2001 From: Emmanuel Ludwaro Date: Tue, 9 Jun 2026 21:48:10 +0300 Subject: [PATCH 1/4] emmanuelludwaro Question one --- emmanuelludwaro | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 emmanuelludwaro diff --git a/emmanuelludwaro b/emmanuelludwaro new file mode 100644 index 0000000..37bbccb --- /dev/null +++ b/emmanuelludwaro @@ -0,0 +1,47 @@ +import java.util.Calendar; + +// The core interface required by Question 1 +interface TransactionInterface { + double getAmount(); + Calendar getDate(); + String getTransactionID(); + void printTransactionDetails(); + void apply(BankAccount ba) throws InsufficientFundsException; +} + +// Simulated BankAccount class to allow transactions to function +class BankAccount { + private double balance; + private final String accountNumber; + + public BankAccount(String accountNumber, double initialBalance) { + this.accountNumber = accountNumber; + this.balance = initialBalance; + } + + public double getBalance() { + return balance; + } + + public String getAccountNumber() { + return accountNumber; + } + + public void deposit(double amount) { + if (amount > 0) { + this.balance += amount; + } + } + + public void withdraw(double amount) throws InsufficientFundsException { + if (amount > this.balance) { + throw new InsufficientFundsException("Cannot withdraw " + amount + ". Insufficient funds."); + } + this.balance -= amount; + } + + // Direct setter used exclusively for reversing transactions + public void setBalance(double balance) { + this.balance = balance; + } +} From fe417a1cad6dafab28b9fc935251ab19e00e00d5 Mon Sep 17 00:00:00 2001 From: Emmanuel Ludwaro Date: Tue, 9 Jun 2026 21:53:02 +0300 Subject: [PATCH 2/4] Question two SCT212-0258/2024 --- Question two | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Question two diff --git a/Question two b/Question two new file mode 100644 index 0000000..c75b7d8 --- /dev/null +++ b/Question two @@ -0,0 +1,113 @@ +// Deposit Transaction - Irreversible +class DepositTransaction extends BaseTransaction { + + public DepositTransaction(double amount) { + super(amount); + } + + @Override + public void apply(BankAccount ba) { + ba.deposit(this.amount); + System.out.println("[DEPOSIT] Successfully credited $" + amount + " to account " + ba.getAccountNumber()); + } + + @Override + public void printTransactionDetails() { + super.printTransactionDetails(); + System.out.println("Type: DEPOSIT (Irreversible)"); + System.out.println("---------------------------"); + } +} + +// Withdrawal Transaction - Reversible & Custom Overloaded Logic +class WithdrawalTransaction extends BaseTransaction { + private boolean isReversed = false; + private BankAccount associatedAccount; // Kept track of to facilitate the reverse() function + private double partialUnpaidAmount = 0.0; // Tracks amount not withdrawn if balance was partial + + public WithdrawalTransaction(double amount) { + super(amount); + } + + // Standard apply() using 'throws' keyword + @Override + public void apply(BankAccount ba) throws InsufficientFundsException { + this.associatedAccount = ba; + ba.withdraw(this.amount); // Will throw InsufficientFundsException if balance is low + System.out.println("[WITHDRAWAL] Successfully debited $" + amount + " from account " + ba.getAccountNumber()); + } + + /** + * Overloaded apply method: Checks if balance > 0. + * If balance < withdrawal amount, cleans out the account and records the deficit. + * Uses internal try-catch-finally blocks as instructed. + */ + public void apply(BankAccount ba, boolean allowPartialWithdrawal) { + this.associatedAccount = ba; + + if (!allowPartialWithdrawal) { + try { + apply(ba); + } catch (InsufficientFundsException e) { + System.out.println("[TRY-CATCH] Standard handling: " + e.getMessage()); + } + return; + } + + // Logic for partial withdrawal + double currentBalance = ba.getBalance(); + + try { + if (currentBalance <= 0) { + throw new InsufficientFundsException("Account empty. Cannot perform partial withdrawal."); + } else if (currentBalance < this.amount) { + // Scenario: 0 < currentBalance < withdrawal amount + this.partialUnpaidAmount = this.amount - currentBalance; + System.out.println("[PARTIAL] Insufficient for total. Attempting to withdraw remaining balance: $" + currentBalance); + + // Withdraw everything available + ba.withdraw(currentBalance); + } else { + // Balance is completely fine + apply(ba); + } + } catch (InsufficientFundsException e) { + System.err.println("[CATCH] Error during execution: " + e.getMessage()); + } finally { + System.out.println("[FINALLY] Post-Transaction Assessment -> Current Bank Balance: $" + ba.getBalance()); + if (this.partialUnpaidAmount > 0) { + System.out.println("[FINALLY] Alert: Record of unpaid deficit kept: $" + this.partialUnpaidAmount); + } + } + } + + // Reverse mechanism required by Question 2 + public boolean reverse() { + if (isReversed) { + System.out.println("[REVERSAL] Transaction was already reversed."); + return false; + } + if (associatedAccount == null) { + System.out.println("[REVERSAL] Failure: This transaction was never applied to an account."); + return false; + } + + // Calculate what was originally taken out + double amountToRestore = this.amount - this.partialUnpaidAmount; + associatedAccount.setBalance(associatedAccount.getBalance() + amountToRestore); + this.isReversed = true; + System.out.println("[REVERSAL] Success! Restored $" + amountToRestore + " back to account."); + return true; + } + + @Override + public void printTransactionDetails() { + super.printTransactionDetails(); + System.out.println("Type: WITHDRAWAL"); + System.out.println("Status: " + (isReversed ? "REVERSED" : "ACTIVE")); + if (partialUnpaidAmount > 0) { + System.out.println("Note: Partial Shortfall of $" + partialUnpaidAmount + " recorded."); + } + System.out.println("---------------------------"); + } +} From 1ddef89674eea3853a79d6a4709444cba73a2a08 Mon Sep 17 00:00:00 2001 From: Emmanuel Ludwaro Date: Tue, 9 Jun 2026 21:54:20 +0300 Subject: [PATCH 3/4] Question 4 --- Question 4 | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Question 4 diff --git a/Question 4 b/Question 4 new file mode 100644 index 0000000..a2eafe1 --- /dev/null +++ b/Question 4 @@ -0,0 +1,65 @@ +public class Main { + public static void main(String[] args) { + System.out.println("=== INITIALIZING TRANSACTION TEST CLIENT ===\n"); + + // 1. Setup a Test Account + BankAccount account = new BankAccount("987654321", 500.00); + System.out.println("Initial Account Balance: $" + account.getBalance()); + System.out.println("============================================\n"); + + // 2. Test standard Polymorphism via base reference types + BaseTransaction baseDeposit = new DepositTransaction(250.00); + BaseTransaction baseWithdraw = new WithdrawalTransaction(100.00); + + try { + // Processing transactions via polymorphism + baseDeposit.apply(account); + baseWithdraw.apply(account); + } catch (InsufficientFundsException e) { + System.out.println("Exception caught: " + e.getMessage()); + } + + baseDeposit.printTransactionDetails(); + baseWithdraw.printTransactionDetails(); + System.out.println("Balance after regular operations: $" + account.getBalance()); + System.out.println("============================================\n"); + + // 3. Testing Question 4 specific Hint: Type Casting back to Base class object + System.out.println("--- Testing Hint: Subtype Type Casting to Base Class ---"); + WithdrawalTransaction concreteWithdrawal = new WithdrawalTransaction(50.00); + + // Explicitly casting subtype up to base type + BaseTransaction castedBase = (BaseTransaction) concreteWithdrawal; + + try { + // Testing the apply method of the base class object pointer + castedBase.apply(account); + } catch (InsufficientFundsException e) { + System.out.println(e.getMessage()); + } + castedBase.printTransactionDetails(); + System.out.println("============================================\n"); + + // 4. Testing Question 2: Reversal Logic + System.out.println("--- Testing Reversal functionality ---"); + // Reversing the withdrawal via the concrete reference object + concreteWithdrawal.reverse(); + System.out.println("Account Balance after Reversal: $" + account.getBalance()); + System.out.println("============================================\n"); + + // 5. Testing Question 3: Overloaded Partial Withdrawal with try-catch-finally + System.out.println("--- Testing Overloaded Partial Withdrawal Logic ---"); + System.out.println("Current Balance is: $" + account.getBalance()); // Should be ~650 + + WithdrawalTransaction highWithdrawal = new WithdrawalTransaction(1000.00); + + // Execute the overloaded function instructing a partial wipeout handling + System.out.println("Executing $1000 withdrawal with partial capability enabled..."); + highWithdrawal.apply(account, true); + + System.out.println("\nPrinting details of partial withdrawal:"); + highWithdrawal.printTransactionDetails(); + + System.out.println("Final checking account balance: $" + account.getBalance()); + } +} From 6821f3b41c05194ad8e38c12f5f733ac641edb7a Mon Sep 17 00:00:00 2001 From: Emmanuel Ludwaro Date: Tue, 9 Jun 2026 21:54:49 +0300 Subject: [PATCH 4/4] Create Question 3 --- Question 3 | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Question 3 diff --git a/Question 3 b/Question 3 new file mode 100644 index 0000000..6b0eb1b --- /dev/null +++ b/Question 3 @@ -0,0 +1,9 @@ +/** + * Custom Java Exception class to handle NSF scenarios. + * Inheriting from Exception makes it a checked exception. + */ +class InsufficientFundsException extends Exception { + public InsufficientFundsException(String message) { + super(message); + } +}