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
41 changes: 26 additions & 15 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@

import java.util.Calendar;

public abstract class BaseTransaction implements TransactionInterface {
private final int amount;
public class BaseTransaction implements TransactionInterface {
private final double amount;
private final Calendar date;
private final String transactionID;

/**
* Lecture1_adt.TransactionInterface Constructor
* @param amount in an integer
* Transaction Constructor
* @param amount the transaction amount
* @param date: Not null, and must be a Calendar object
* @return void
* Instialises the field, attributes of a transaction
* Creates a object of this
* Instialises the fields of a transaction
*/
public BaseTransaction(int amount, @NotNull Calendar date) {
public BaseTransaction(double amount, @NotNull Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone();
int uniq = (int) Math.random()*10000;
transactionID = date.toString()+uniq;
int uniq = (int) (Math.random() * 10000);
transactionID = date.toString() + uniq;
}

/**
* getAmount()
* @return integer
* @return double
*/
public double getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
Expand All @@ -37,15 +35,28 @@ public double getAmount() {
* @return Calendar Object
*/
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
}

// Method to get a unique identifier for the transaction
public String getTransactionID(){
return transactionID;
}
// Method to print a transaction receipt or details
public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);

// Method to print details of the transaction
@Override
public void printTransactionDetails() {
System.out.println("Transaction ID: " + transactionID);
System.out.println("Date: " + date.getTime());
System.out.println("Amount: $" + amount);
}

// Method to apply the transaction on a bank account
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
if (ba == null) {
throw new IllegalArgumentException("BankAccount cannot be null.");
}
System.out.println("BaseTransaction: No balance changes applied. Current balance verified: $" + ba.getBalance());
}
}
29 changes: 20 additions & 9 deletions src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@
import java.util.Calendar;

public class DepositTrasaction extends BaseTransaction {
public DepositTrasaction(int amount, @NotNull Calendar date){
public DepositTrasaction(double amount, @NotNull Calendar date){
super(amount, date);
}
private boolean checkDepositAmount(int amt){
if (amt < 0){
return false;
}
else{
return true;
}

private boolean checkDepositAmount(double amt){
return amt >= 0;
}

// Method to print a transaction receipt or details
@Override
public void printTransactionDetails(){
System.out.println("Deposit Trasaction: "+this.toString());
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("Type: Deposit (Irreversible)");
System.out.println("-----------------------------------");
}

@Override
public void apply(BankAccount ba){
if (ba == null) {
throw new IllegalArgumentException("BankAccount cannot be null.");
}
if (!checkDepositAmount(getAmount())) {
System.out.println("Error: Deposit amount cannot be negative.");
return;
}
double curr_balance = ba.getBalance();
double new_balance = curr_balance + getAmount();
ba.setBalance(new_balance);
System.out.println("Deposit applied: Credited $" + getAmount() + ". New Balance: $" + new_balance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Lecture4_interfaces_abstract_classes;

/**
* Exception thrown when a bank account does not have sufficient funds for a withdrawal.
*/
public class InsufficientFundsException extends Exception {

/**
* Constructs a new InsufficientFundsException with a null detail message.
*/
public InsufficientFundsException() {
super();
}

/**
* Constructs a new InsufficientFundsException with the specified detail message.
*
* @param message the detail message
*/
public InsufficientFundsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public interface TransactionInterface {
// Method to get a unique identifier for the transaction
String getTransactionID();

// Method to print details of the transaction
void printTransactionDetails();

// Method to apply this transaction on a Bank account object
void apply(BankAccount ba) throws InsufficientFundsException;

}


100 changes: 80 additions & 20 deletions src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,101 @@
import java.util.Calendar;

public class WithdrawalTransaction extends BaseTransaction {
public WithdrawalTransaction(int amount, @NotNull Calendar date) {
private BankAccount associatedAccount;
private boolean isApplied = false;
private double actualWithdrawnAmount = 0.0;
private double remainingAmountNotWithdrawn = 0.0;

public WithdrawalTransaction(double amount, @NotNull Calendar date) {
super(amount, date);
}

private boolean checkDepositAmount(int amt) {
if (amt < 0) {
return false;
} else {
return true;
}
private boolean checkWithdrawalAmount(double amt) {
return amt >= 0;
}

// Method to reverse the transaction
public boolean reverse() {
return true;
} // return true if reversal was successful
if (isApplied && associatedAccount != null) {
double currentBalance = associatedAccount.getBalance();
associatedAccount.setBalance(currentBalance + actualWithdrawnAmount);
System.out.println("Withdrawal reversed: Refunded $" + actualWithdrawnAmount + " to the account. New Balance: $" + associatedAccount.getBalance());
this.isApplied = false;
this.associatedAccount = null;
this.actualWithdrawnAmount = 0.0;
this.remainingAmountNotWithdrawn = 0.0;
return true;
}
System.out.println("Reversal failed: Transaction has not been applied or is already reversed.");
return false;
}

// Method to print a transaction receipt or details
@Override
public void printTransactionDetails() {
System.out.println("Deposit Trasaction: " + this.toString());
System.out.println("--- Withdrawal Transaction Receipt ---");
System.out.println("Transaction ID: " + getTransactionID());
System.out.println("Date: " + getDate().getTime());
System.out.println("Amount: $" + getAmount());
System.out.println("Actual Withdrawn Amount: $" + actualWithdrawnAmount);
System.out.println("Remaining Amount Not Withdrawn: $" + remainingAmountNotWithdrawn);
System.out.println("Applied: " + isApplied);
System.out.println("Type: Withdrawal (Reversible)");
System.out.println("--------------------------------------");
}

/*
Oportunity for assignment: implementing different form of withdrawal
*/
public void apply(BankAccount ba) {
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
if (ba == null) {
throw new IllegalArgumentException("BankAccount cannot be null.");
}
double curr_balance = ba.getBalance();
if (curr_balance > getAmount()) {
double new_balance = curr_balance - getAmount();
ba.setBalance(new_balance);
if (curr_balance < getAmount()) {
throw new InsufficientFundsException("Insufficient funds. Account balance: $" + curr_balance + ", Withdrawal amount: $" + getAmount());
}
double new_balance = curr_balance - getAmount();
ba.setBalance(new_balance);

this.associatedAccount = ba;
this.isApplied = true;
this.actualWithdrawnAmount = getAmount();
this.remainingAmountNotWithdrawn = 0.0;
System.out.println("Withdrawal applied: Debited $" + getAmount() + ". New Balance: $" + new_balance);
}

/*
Assignment 1 Q3: Write the Reverse method - a method unique to the WithdrawalTransaction Class
*/
// Overloaded apply method implementing partial withdrawal exception handling
public void apply(BankAccount ba, boolean allowPartial) {
if (ba == null) {
throw new IllegalArgumentException("BankAccount cannot be null.");
}
if (allowPartial) {
try {
this.apply(ba);
} catch (InsufficientFundsException e) {
double balance = ba.getBalance();
if (balance > 0) {
double amountNotWithdrawn = getAmount() - balance;
this.remainingAmountNotWithdrawn = amountNotWithdrawn;
this.actualWithdrawnAmount = balance;

ba.setBalance(0);
this.associatedAccount = ba;
this.isApplied = true;
System.out.println("Partial withdrawal applied. Withdrew available balance: $" + balance);
System.out.println("Amount not withdrawn (record kept): $" + amountNotWithdrawn);
} else {
System.out.println("Withdrawal failed. Balance is not greater than 0 (Balance: $" + balance + ").");
}
} finally {
System.out.println("Partial withdrawal attempt completed.");
}
} else {
try {
this.apply(ba);
} catch (InsufficientFundsException e) {
System.out.println("Withdrawal failed due to insufficient funds: " + e.getMessage());
}
}
}
}

Loading