Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Question 3
Original file line number Diff line number Diff line change
@@ -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);
}
}
65 changes: 65 additions & 0 deletions Question 4
Original file line number Diff line number Diff line change
@@ -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());
}
}
113 changes: 113 additions & 0 deletions Question two
Original file line number Diff line number Diff line change
@@ -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("---------------------------");
}
}
47 changes: 47 additions & 0 deletions emmanuelludwaro
Original file line number Diff line number Diff line change
@@ -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;
}
}