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
28 changes: 26 additions & 2 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public BaseTransaction(int amount, @NotNull Calendar date) {
* getAmount()
* @return integer
*/

@Override
public double getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
}
Expand All @@ -36,16 +38,38 @@ public double getAmount() {
* 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
}

// Method to get a unique identifier for the transaction
@Override
public String getTransactionID(){
return transactionID;
}
// Method to print a transaction receipt or details
public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);
@Override
public abstract void printTransactionDetails(){
System.out.println("Transaction ID: " + transactionID);
System.out.println("Date: " + date.getTime());
System.out.println("Amount: " + amount);
}
@Override
public abstract void apply(BankAccount ba){
// Default implementation: Just prints a message
System.out.println("Applying base transaction...");
}
@Override
public boolean reverse(BankAccount ba) {
System.out.println("Reverse not supported for base transaction.");
return false;
}
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
// Default implementation: Just prints a message
System.out.println("Applying base transaction...");
}
}
11 changes: 11 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ public void printTransactionDetails(){
System.out.println("Deposit Trasaction: "+this.toString());
}

@Override
public void apply(BankAccount ba){
double curr_balance = ba.getBalance();
double new_balance = curr_balance + getAmount();
ba.setBalance(new_balance);
}
@Override
public boolean reverse(BankAccount ba) {
if (ba.getBalance() >= amount) {
ba.withdraw(amount);
System.out.println("Deposit of " + amount + " reversed.");
return true;
}
System.out.println("Insufficient balance to reverse deposit.");
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// InsufficientFundsException class

public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,28 @@ public void printTransactionDetails() {
/*
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);
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
if (ba.getBalance() >= amount) {
ba.withdraw(amount);
System.out.println("Withdrawal of " + amount + " applied.");
} else if (ba.getBalance() > 0) {
double availableAmount = ba.getBalance();
ba.withdraw(availableAmount);
System.out.println("Partial withdrawal of " + availableAmount + " applied. Shortfall: " + (amount - availableAmount));
} else {
throw new InsufficientFundsException("Insufficient balance for withdrawal.");
}
}

}
@Override
public boolean reverse(BankAccount ba) {
ba.deposit(amount);
System.out.println("Withdrawal of " + amount + " reversed.");
return true;
}

/*
Assignment 1 Q3: Write the Reverse method - a method unique to the WithdrawalTransaction Class
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ public static void main(String[] args) {
// This is the client code
// Uncomment the following lines to test the class which you would like to test

BankAccount account = new BankAccount(500);

BaseTransaction deposit = new DepositTransaction(200);
BaseTransaction withdrawal = new WithdrawalTransaction(300);
BaseTransaction largeWithdrawal = new WithdrawalTransaction(700);

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

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

largeWithdrawal.apply(account);
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}

System.out.println("Final Balance: " + account.getBalance());

// testTransaction1()
// testTransaction2()
// testTransaction3()
Expand Down