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
75 changes: 52 additions & 23 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,80 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public abstract class BaseTransaction implements TransactionInterface {
/**
* BaseTransaction represents a concrete transaction implementing TransactionInterface.
* It provides base fields, getters, and a default implementation of common transaction methods.
*/
public class BaseTransaction implements TransactionInterface {
private final int amount;
private final Calendar date;
private final String transactionID;

/**
* Lecture1_adt.TransactionInterface Constructor
* @param amount in an integer
* @param date: Not null, and must be a Calendar object
* @return void
* Instialises the field, attributes of a transaction
* Creates a object of this
* Constructs a BaseTransaction with a specified amount and date.
*
* @param amount the transaction amount
* @param date the transaction date (must not be null)
*/
public BaseTransaction(int amount, @NotNull Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone();
int uniq = (int) Math.random()*10000;
transactionID = date.toString()+uniq;
// Fixed: Parentheses added around Math.random() * 10000 to cast the result instead of casting Math.random() to 0
int uniq = (int) (Math.random() * 10000);
this.transactionID = date.getTimeInMillis() + "_" + uniq;
}

/**
* getAmount()
* @return integer
* Returns the transaction amount.
*
* @return the transaction amount
*/
@Override
public double getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
return amount;
}

/**
* getDate()
* @return Calendar Object
* Returns a copy of the transaction date.
*
* @return the transaction date
*/
@Override
public Calendar getDate() {
// return date; // Because we are dealing with Reference types we need to judiciously copy what our getters return
return (Calendar) date.clone(); // Defensive copying or Judicious Copying
return (Calendar) date.clone();
}

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

/**
* Prints the details of this transaction to the standard output.
*/
public void printTransactionDetails() {
System.out.println("Transaction ID: " + getTransactionID());
System.out.println("Date: " + getDate().getTime());
System.out.println("Amount: $" + getAmount());
}

// Method to get a unique identifier for the transaction
public String getTransactionID(){
return transactionID;
/**
* Applies the transaction to the specified bank account.
* The base implementation is a generic operation that does not modify the account balance.
*
* @param ba the BankAccount to apply the transaction to
* @throws InsufficientFundsException if the transaction cannot be applied due to insufficient funds
*/
public void apply(BankAccount ba) throws InsufficientFundsException {
System.out.println("Warning: Applying a generic BaseTransaction of amount "
+ getAmount() + " on the account. Balance remains unchanged.");
}
// Method to print a transaction receipt or details
public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);
}

61 changes: 61 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;
import java.util.Calendar;

/**
* DepositTransaction represents a transaction that deposits funds into a BankAccount.
* All deposits are irreversible.
*/
public class DepositTransaction extends BaseTransaction {

/**
* Constructs a DepositTransaction with a specified amount and date.
*
* @param amount the deposit amount
* @param date the transaction date (must not be null)
*/
public DepositTransaction(int amount, @NotNull Calendar date) {
super(amount, date);
}

/**
* Validates that the deposit amount is non-negative.
*
* @param amt the amount to validate
* @return true if the amount is non-negative, false otherwise
*/
private boolean checkDepositAmount(int amt) {
return amt >= 0;
}

/**
* Prints a transaction receipt for this deposit transaction.
*/
@Override
public void printTransactionDetails() {
System.out.println("--- Deposit Transaction Receipt ---");
System.out.println("Transaction ID: " + getTransactionID());
System.out.println("Date: " + getDate().getTime());
System.out.println("Amount: $" + getAmount());
System.out.println("Status: Completed (Irreversible)");
System.out.println("-----------------------------------");
}

/**
* Applies this deposit transaction to the specified bank account by increasing its balance.
*
* @param ba the BankAccount to apply the deposit to
*/
@Override
public void apply(BankAccount ba) {
if (!checkDepositAmount((int) getAmount())) {
System.out.println("Error: Cannot deposit a negative amount.");
return;
}
double curr_balance = ba.getBalance();
double new_balance = curr_balance + getAmount();
ba.setBalance(new_balance);
System.out.println("Deposit applied successfully. New balance: $" + new_balance);
}
}
30 changes: 0 additions & 30 deletions src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package Lecture4_interfaces_abstract_classes;

/**
* Custom checked exception thrown when a bank account does not have
* sufficient funds to cover a withdrawal transaction.
*/
public class InsufficientFundsException extends Exception {
private final double amount;
private final double balance;

/**
* Constructs a new InsufficientFundsException with a custom message.
*
* @param message the detail message
*/
public InsufficientFundsException(String message) {
super(message);
this.amount = 0.0;
this.balance = 0.0;
}

/**
* Constructs a new InsufficientFundsException with detailed transaction information.
*
* @param message the detail message
* @param amount the requested withdrawal amount
* @param balance the current available balance
*/
public InsufficientFundsException(String message, double amount, double balance) {
super(message + " (Requested: " + amount + ", Available: " + balance + ")");
this.amount = amount;
this.balance = balance;
}

/**
* Gets the requested amount that caused the exception.
*
* @return the requested amount
*/
public double getAmount() {
return amount;
}

/**
* Gets the available balance at the time of the exception.
*
* @return the available balance
*/
public double getBalance() {
return balance;
}
}
Loading