diff --git a/src/Lecture4_interfaces_abstract_classes/BankAccount.java b/src/Lecture4_interfaces_abstract_classes/BankAccount.java index 28d0d07..1ae7f64 100644 --- a/src/Lecture4_interfaces_abstract_classes/BankAccount.java +++ b/src/Lecture4_interfaces_abstract_classes/BankAccount.java @@ -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; + } +} + diff --git a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java index ed81eb8..a5caaf5 100644 --- a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java +++ b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java @@ -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(); +} + diff --git a/src/Lecture4_interfaces_abstract_classes/DepositTransaction.java b/src/Lecture4_interfaces_abstract_classes/DepositTransaction.java new file mode 100644 index 0000000..eab39ab --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/DepositTransaction.java @@ -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; + } +} + diff --git a/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java b/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java new file mode 100644 index 0000000..7b9031e --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java @@ -0,0 +1,5 @@ +public class InsufficientFundsException extends Exception { + public InsufficientFundsException(String message) { + super(message); + } +} diff --git a/src/Lecture4_interfaces_abstract_classes/Main.java b/src/Lecture4_interfaces_abstract_classes/Main.java new file mode 100644 index 0000000..993c812 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/Main.java @@ -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()); + } + } +} diff --git a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java index 5902713..a38c68f 100644 --- a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java +++ b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java @@ -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(); +} + diff --git a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java index face5b6..fb1a43d 100644 --- a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java +++ b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java @@ -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; + } +}