Skip to content
Closed
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
Binary file added Joel nganga assignment/BankAccount.class
Binary file not shown.
22 changes: 22 additions & 0 deletions Joel nganga assignment/BankAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
this.balance = initialBalance;
}

public double getBalance() {
return balance;
}

public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Insufficient funds: Available balance is " + balance);
}
balance -= amount;
}
}
Binary file added Joel nganga assignment/BaseTransaction.class
Binary file not shown.
45 changes: 45 additions & 0 deletions Joel nganga assignment/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public abstract class BaseTransaction implements TransactionInterface {
private double amount;
private java.util.Calendar date;
private String transactionID;
protected BankAccount associatedAccount;

public BaseTransaction(double amount, java.util.Calendar date, String transactionID) {
this.amount = amount;
this.date = date;
this.transactionID = transactionID;
}

@Override
public double getAmount() {
return amount;
}

@Override
public java.util.Calendar getDate() {
return date;
}

@Override
public String getTransactionID() {
return transactionID;
}

@Override
public void printTransactionDetails() {
System.out.println("Transaction ID: " + transactionID);
System.out.println("Date: " + date.getTime());
System.out.println("Amount: " + amount);
}

@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
this.associatedAccount = ba;
}

@Override
public boolean reverse() {
System.out.println("Reverse operation not supported for this transaction.");
return false;
}
}
Binary file added Joel nganga assignment/DepositTransaction.class
Binary file not shown.
19 changes: 19 additions & 0 deletions Joel nganga assignment/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class DepositTransaction extends BaseTransaction {

public DepositTransaction(double amount, java.util.Calendar date, String transactionID) {
super(amount, date, transactionID);
}

@Override
public void apply(BankAccount ba) {
try {
super.apply(ba); // Call the apply method of BaseTransaction (if it does anything)

// Since deposits don't generally have insufficient funds, we just do the deposit
ba.deposit(getAmount());
System.out.println("Deposit of " + getAmount() + " applied to account.");
} catch (InsufficientFundsException e) {
System.out.println("Error: " + e.getMessage()); // Catch the exception if it is thrown
}
}
}
Binary file not shown.
6 changes: 6 additions & 0 deletions Joel nganga assignment/InsufficientFundsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}

Binary file added Joel nganga assignment/Main.class
Binary file not shown.
50 changes: 50 additions & 0 deletions Joel nganga assignment/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.Calendar;

public class Main {
public static void main(String[] args) throws InsufficientFundsException { // Declare the exception
// Step 1: Initialize a bank account
BankAccount account = new BankAccount(1000.0);

// Step 2: Create a date object
Calendar date = Calendar.getInstance();

// Step 3: Create transactions
DepositTransaction deposit = new DepositTransaction(500.0, date, "TXN001");
WithdrawalTransaction withdrawal = new WithdrawalTransaction(1200.0, date, "TXN002");

// Step 4: Test deposit
deposit.apply(account);
System.out.println("Balance after deposit: " + account.getBalance());

// Step 5: Test withdrawal with exception handling
try {
withdrawal.apply(account); // Attempt withdrawal
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}

// Step 6: Test partial withdrawal
withdrawal.apply(account, true); // Allow partial withdrawal
System.out.println("Unmet amount: " + withdrawal.getUnmetAmount());

// Step 7: Test reversal
System.out.println("\nReversing transactions...");
deposit.reverse(); // Irreversible
withdrawal.reverse(); // Reversible

// Step 8: Demonstrate polymorphism
BaseTransaction transaction = deposit;
transaction.printTransactionDetails();

transaction = withdrawal;
transaction.printTransactionDetails();
try {
transaction.apply(account);
} catch (InsufficientFundsException e) {
System.out.println("Exception during polymorphic test: " + e.getMessage());
}

// Final balance
System.out.println("Final balance: " + account.getBalance());
}
}
Binary file added Joel nganga assignment/TransactionInterface.class
Binary file not shown.
9 changes: 9 additions & 0 deletions Joel nganga assignment/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public interface TransactionInterface {
double getAmount();
java.util.Calendar getDate();
String getTransactionID();
void printTransactionDetails();
void apply(BankAccount ba) throws InsufficientFundsException;
boolean reverse();
}

Binary file not shown.
57 changes: 57 additions & 0 deletions Joel nganga assignment/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
public class WithdrawalTransaction extends BaseTransaction {
private double unmetAmount;

public WithdrawalTransaction(double amount, java.util.Calendar date, String transactionID) {
super(amount, date, transactionID);
this.unmetAmount = 0.0;
}

@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
super.apply(ba); // Call the base class apply method
if (ba.getBalance() < getAmount()) {
throw new InsufficientFundsException("Insufficient funds for withdrawal. Available balance: " + ba.getBalance());
}
ba.withdraw(getAmount());
System.out.println("Withdrawal of " + getAmount() + " applied to account.");
}

// Overloaded apply method that supports partial withdrawal
public void apply(BankAccount ba, boolean allowPartial) throws InsufficientFundsException {
super.apply(ba); // Call to the base class apply method
if (allowPartial) {
if (ba.getBalance() < getAmount()) {
unmetAmount = getAmount() - ba.getBalance();
ba.withdraw(ba.getBalance()); // Withdraw all available funds
System.out.println("Partial withdrawal applied. Unmet amount: " + unmetAmount);
} else {
ba.withdraw(getAmount()); // Full withdrawal
System.out.println("Full withdrawal of " + getAmount() + " applied.");
}
} else {
// No partial withdrawal allowed, proceed with full withdrawal check
if (ba.getBalance() < getAmount()) {
throw new InsufficientFundsException("Insufficient funds for withdrawal. Available balance: " + ba.getBalance());
}
ba.withdraw(getAmount());
System.out.println("Full withdrawal of " + getAmount() + " applied.");
}
System.out.println("Transaction complete. Current balance: " + ba.getBalance());
}

@Override
public boolean reverse() {
if (associatedAccount == null) {
System.out.println("Transaction not associated with an account. Cannot reverse.");
return false;
}
associatedAccount.deposit(getAmount());
System.out.println("Withdrawal of " + getAmount() + " reversed.");
return true;
}

// Getter method for unmetAmount
public double getUnmetAmount() {
return unmetAmount;
}
}