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
36 changes: 36 additions & 0 deletions src/Bank account
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Represents a simple bank account with a balance.
*/
public class BankAccount {

private double balance;
private String accountID;

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

public double getBalance() {
return balance;
}

public String getAccountID() {
return accountID;
}


public void credit(double amount) {
this.balance += amount;
}


public void debit(double amount) {
this.balance -= amount;
}

@Override
public String toString() {
return String.format("BankAccount[ID=%s, Balance=%.2f]", accountID, balance);
}
}
86 changes: 86 additions & 0 deletions src/Base Transaction
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import java.util.Calendar;
import java.util.UUID;

/**
* BaseTransaction is a concrete class implementing TransactionInterface.
*/
public class BaseTransaction implements TransactionInterface {

// ----- Fields -----
private final double amount;
private final Calendar date;
private final String transactionID;

// ----- Constructor -----


public BaseTransaction(double amount, Calendar date) {
this.amount = amount;
this.date = date;
this.transactionID = UUID.randomUUID().toString();
}

// ----- Getters (Q1 required methods) -----

/**
* Returns the transaction amount.
* Early binding: resolved at compile time for BaseTransaction references.
*/
@Override
public double getAmount() {
return amount;
}

/**
* Returns the transaction date.
*/
@Override
public Calendar getDate() {
return date;
}

/**
* Returns the unique transaction ID.
*/
@Override
public String getTransactionID() {
return transactionID;
}

// ----- Common transaction methods -----

/**
* Prints the details of this transaction to standard output.
*/
@Override
public void printTransactionDetails() {
System.out.println("=== Transaction Details ===");
System.out.println("Transaction ID : " + transactionID);
System.out.printf ("Amount : %.2f%n", amount);
System.out.printf ("Date : %04d-%02d-%02d%n",
date.get(Calendar.YEAR),
date.get(Calendar.MONTH) + 1, // Calendar months are 0-indexed
date.get(Calendar.DAY_OF_MONTH));
System.out.println("Type : Base Transaction");
System.out.println("===========================");
}

/**
* Base implementation of apply().
*
* This implementation DIFFERS SUBSTANTIALLY from DepositTransaction and
* WithdrawalTransaction. Here we only log the transaction without modifying
* the bank account — simulating a "pending" or "record-only" transaction.
*
* Demonstrates EARLY BINDING: when called on a BaseTransaction reference,
* this method is resolved at compile time.
*
* @param ba the BankAccount on which to (not yet) apply the transaction
*/
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
System.out.println("[BaseTransaction] Recording transaction " + transactionID
+ " for account " + ba.getAccountID()
+ " — no balance change (base behaviour).");
}
}
70 changes: 70 additions & 0 deletions src/Deposit Transaction
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.Calendar;

/**
* DepositTransaction represents an irreversible credit to a BankAccount.
*
* Design assumption (Q2): deposits are IRREVERSIBLE — no reverse() method.
*
* Demonstrates LATE BINDING / POLYMORPHISM:
* When apply() is called on a TransactionInterface or BaseTransaction reference
* that actually holds a DepositTransaction object, the JVM resolves the call
* to THIS override at runtime — not the base-class version.
*/
public class DepositTransaction extends BaseTransaction {

// ----- Constructor -----

/**
* Creates a DepositTransaction.
*
* @param amount the amount to deposit (should be > 0)
* @param date the date of the deposit
*/
public DepositTransaction(double amount, Calendar date) {
super(amount, date);
}

// ----- Overridden methods -----

/**
* Prints deposit-specific details, then delegates to the superclass
* for common fields.
*/
@Override
public void printTransactionDetails() {
System.out.println("=== Deposit Transaction Details ===");
System.out.println("Transaction ID : " + getTransactionID());
System.out.printf ("Deposit Amount : %.2f%n", getAmount());
System.out.printf ("Date : %04d-%02d-%02d%n",
getDate().get(Calendar.YEAR),
getDate().get(Calendar.MONTH) + 1,
getDate().get(Calendar.DAY_OF_MONTH));
System.out.println("Reversible : No");
System.out.println("===================================");
}

/**
* Applies the deposit by crediting the given BankAccount.
*
* LATE BINDING: this override is selected at runtime when the actual
* object is a DepositTransaction, regardless of the reference type used.
*
* Differs from BaseTransaction.apply() (which makes no balance change)
* and WithdrawalTransaction.apply() (which debits and may throw exceptions).
*
* @param ba the BankAccount to credit
*/
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
System.out.printf("[DepositTransaction] Crediting %.2f to account %s.%n",
getAmount(), ba.getAccountID());

double balanceBefore = ba.getBalance();
ba.credit(getAmount());

System.out.printf(" Balance before : %.2f%n", balanceBefore);
System.out.printf(" Balance after : %.2f%n", ba.getBalance());

printTransactionDetails();
}
}
32 changes: 32 additions & 0 deletions src/InsufficientFundsException
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Custom exception thrown when a withdrawal exceeds the available balance.
* Extends Exception to make it a checked exception, requiring explicit handling.
*/
public class InsufficientFundsException extends Exception {

private double amountRequested;
private double amountAvailable;


public InsufficientFundsException(double amountRequested, double amountAvailable) {
super(String.format(
"Insufficient funds: attempted to withdraw %.2f but only %.2f is available.",
amountRequested, amountAvailable
));
this.amountRequested = amountRequested;
this.amountAvailable = amountAvailable;
}

public double getAmountRequested() {
return amountRequested;
}

public double getAmountAvailable() {
return amountAvailable;
}


public double getShortfall() {
return amountRequested - amountAvailable;
}
}
36 changes: 30 additions & 6 deletions src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
package Lecture4_interfaces_abstract_classes;

/**
* Represents a simple bank account with a balance.
*/
public class BankAccount {

private double balance;
public BankAccount(double balance) {
this.balance = balance;
private String accountID;

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

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
public String getAccountID() {
return accountID;
}

/**
* Credits (adds) an amount to the account balance.
*/
public void credit(double amount) {
this.balance += amount;
}

/**
* Debits (subtracts) an amount from the account balance.
*/
public void debit(double amount) {
this.balance -= amount;
}

@Override
public String toString() {
return String.format("BankAccount[ID=%s, Balance=%.2f]", accountID, balance);
}
}
Loading