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
151 changes: 151 additions & 0 deletions .idea/IntelliLang.xml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package Lecture4_interfaces_abstract_classes;

public class BankAccount {

private double balance;

public BankAccount(double balance) {
this.balance = balance;
}
Expand All @@ -13,4 +15,4 @@ public double getBalance() {
public void setBalance(double balance) {
this.balance = balance;
}
}
}
56 changes: 25 additions & 31 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public abstract 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
*/
public BaseTransaction(int amount, @NotNull Calendar date) {
public BaseTransaction(int amount, 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);
this.transactionID = date.getTimeInMillis() + "-" + uniq;
}

/**
* getAmount()
* @return integer
*/
@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
*/
@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();
}

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

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

// Method to get a unique identifier for the transaction
public String getTransactionID(){
return transactionID;
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
System.out.println("Base transaction executed.");
}
// Method to print a transaction receipt or details
public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);
}
}
20 changes: 13 additions & 7 deletions src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public class DepositTrasaction extends BaseTransaction {

public DepositTrasaction(int amount, @NotNull Calendar date){
super(amount, date);
}

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

// Method to print a transaction receipt or details
@Override
public void printTransactionDetails(){
System.out.println("Deposit Trasaction: "+this.toString());
System.out.println("--- Deposit Transaction ---");
System.out.println("Transaction ID: " + getTransactionID());
System.out.println("Date: " + getDate().getTime());
System.out.println("Amount Deposited: " + getAmount());
}

public void apply(BankAccount ba){
// Added 'throws InsufficientFundsException' to maintain signature compatibility
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
double curr_balance = ba.getBalance();
double new_balance = curr_balance + getAmount();
ba.setBalance(new_balance);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Lecture4_interfaces_abstract_classes;

public class InsufficientFundsException extends Exception {

private double deficitAmount;

public InsufficientFundsException(String message, double deficit) {
super(message);
this.deficitAmount = deficit;
}

public InsufficientFundsException(String message) {
super(message);
this.deficitAmount = 0.0;
}

public double getDeficitAmount() {
return deficitAmount;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package Lecture4_interfaces_abstract_classes;

import java.util.Calendar;

/**
* Interface for Transactions
* Any class that defines a transaction is expected to implement this Interface
*/
public interface TransactionInterface {

// Method to get the transaction amount
double getAmount();

// Method to get the transaction date
Calendar getDate();

// Method to get a unique identifier for the transaction
String getTransactionID();

}

void apply(BankAccount ba) throws InsufficientFundsException;

void printTransactionDetails();
}
77 changes: 62 additions & 15 deletions src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public class WithdrawalTransaction extends BaseTransaction {
private BankAccount appliedAccount = null;
private boolean isApplied = false;
private double shortfallRecord = 0.0;

public WithdrawalTransaction(int amount, @NotNull Calendar date) {
super(amount, date);
}
Expand All @@ -17,29 +20,73 @@ private boolean checkDepositAmount(int amt) {
}
}

// Method to reverse the transaction
// Question 2: Reversal logic restores the account balance to its original amount
public boolean reverse() {
return true;
} // return true if reversal was successful
if (isApplied && appliedAccount != null) {
double currentBalance = appliedAccount.getBalance();
// Restore original amount (current balance + what was successfully withdrawn)
double restoredBalance = currentBalance + (getAmount() - shortfallRecord);
appliedAccount.setBalance(restoredBalance);
isApplied = false; // Reset state
System.out.println("Withdrawal transaction reversed successfully.");
return true;
}
System.out.println("Reversal failed: Transaction has not been applied yet.");
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 ---");
System.out.println("Transaction ID: " + getTransactionID());
System.out.println("Date: " + getDate().getTime());
System.out.println("Amount Requested: " + getAmount());
if (shortfallRecord > 0) {
System.out.println("Shortfall (Not Withdrawn): " + shortfallRecord);
}
}

/*
Oportunity for assignment: implementing different form of withdrawal
*/
public void apply(BankAccount ba) {
// Question 3: Standard apply using the throws keyword
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
double curr_balance = ba.getBalance();
if (curr_balance > getAmount()) {
if (curr_balance >= getAmount()) {
double new_balance = curr_balance - getAmount();
ba.setBalance(new_balance);
this.appliedAccount = ba;
this.isApplied = true;
this.shortfallRecord = 0.0;
} else {
throw new InsufficientFundsException("Insufficient funds to complete the withdrawal of " + getAmount());
}
}

/*
Assignment 1 Q3: Write the Reverse method - a method unique to the WithdrawalTransaction Class
*/
}

// Question 3: Overloaded apply handling 0 < balance < withdrawal amount using try...catch...finally
public void apply(BankAccount ba, boolean partialWithdrawalAllowed) {
try {
double curr_balance = ba.getBalance();
if (curr_balance < getAmount()) {
// Check if balance is greater than 0 for partial withdrawal
if (curr_balance > 0) {
shortfallRecord = getAmount() - curr_balance;
ba.setBalance(0.0); // Withdraw all available balance
this.appliedAccount = ba;
this.isApplied = true;
System.out.println("Partial withdrawal executed. Balance set to 0. Shortfall: " + shortfallRecord);
} else {
// Trigger exception block manually if balance is 0 or less
throw new InsufficientFundsException("Account balance is zero or negative. Cannot withdraw.");
}
} else {
// If funds are completely sufficient, proceed normally via standard apply
apply(ba);
}
} catch (InsufficientFundsException e) {
System.out.println("Caught Exception inside overloaded apply: " + e.getMessage());
this.isApplied = false;
} finally {
System.out.println("Transaction processing attempt finished for Transaction ID: " + getTransactionID());
}
}
}
Loading