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
51 changes: 35 additions & 16 deletions src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
package Lecture4_interfaces_abstract_classes;

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

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}
}
public class BankAccount {
private String accountNumber;
private double balance;

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

public String getAccountNumber() {
return accountNumber;
}

public double getBalance() {
return balance;
}

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

public void withdraw(double amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Insufficient funds for withdrawal.");
}
balance -= amount;
}

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

94 changes: 43 additions & 51 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
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) {
this.amount = amount;
this.date = (Calendar) date.clone();
int uniq = (int) Math.random()*10000;
transactionID = date.toString()+uniq;
}

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

/**
* getDate()
* @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);
}
import java.util.Calendar;
import java.util.UUID;

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

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

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

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

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

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

@Override
public abstract void apply(BankAccount ba) throws InsufficientFundsException;

@Override
public abstract boolean reverse();
}

18 changes: 18 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class DepositTransaction extends BaseTransaction {

public DepositTransaction(double amount) {
super(amount);
}

@Override
public void apply(BankAccount ba) {
ba.deposit(getAmount());
}

@Override
public boolean reverse() {
System.out.println("Reversing deposit transaction...");
return true;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
27 changes: 27 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount("456", 0);

DepositTransaction deposit = new DepositTransaction(0);
WithdrawalTransaction withdrawal = new WithdrawalTransaction(0);

try {
// Testing Deposit
deposit.apply(account);
deposit.printTransactionDetails();
System.out.println("Balance after deposit: " + account.getBalance());

// Testing Withdrawal
withdrawal.apply(account);
withdrawal.printTransactionDetails();
System.out.println("Balance after withdrawal: " + account.getBalance());

// Reversing Transactions
deposit.reverse();
withdrawal.reverse();

} catch (InsufficientFundsException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
32 changes: 11 additions & 21 deletions src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
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();

}


import java.util.Calendar;

public interface TransactionInterface {
double getAmount();
Calendar getDate();
String getTransactionID();
void printTransactionDetails();
void apply(BankAccount ba) throws InsufficientFundsException;
boolean reverse();
}

75 changes: 30 additions & 45 deletions src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,30 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public class WithdrawalTransaction extends BaseTransaction {
public WithdrawalTransaction(int amount, @NotNull Calendar date) {
super(amount, date);
}

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

// Method to reverse the transaction
public boolean reverse() {
return true;
} // return true if reversal was successful

// Method to print a transaction receipt or details
public void printTransactionDetails() {
System.out.println("Deposit Trasaction: " + this.toString());
}

/*
Oportunity for assignment: implementing different form of withdrawal
*/
public void apply(BankAccount ba) {
double curr_balance = ba.getBalance();
if (curr_balance > getAmount()) {
double new_balance = curr_balance - getAmount();
ba.setBalance(new_balance);
}
}

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

public class WithdrawalTransaction extends BaseTransaction {
private boolean isReversed;

public WithdrawalTransaction(double amount) {
super(amount);
}

@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
if (ba.getBalance() < getAmount() && ba.getBalance() > 0) {
double remainingBalance = ba.getBalance();
ba.withdraw(remainingBalance);
System.out.println("Partial withdrawal made: " + remainingBalance);
} else if (ba.getBalance() >= getAmount()) {
ba.withdraw(getAmount());
} else {
throw new InsufficientFundsException("Insufficient balance for withdrawal.");
}
}

@Override
public boolean reverse() {
if (!isReversed) {
System.out.println("Reversing withdrawal transaction...");
isReversed = true;
return true;
}
return false;
}
}