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
16 changes: 16 additions & 0 deletions Assignment_I_Advanced_Programming/Q1/BankAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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;
}
}
124 changes: 124 additions & 0 deletions Assignment_I_Advanced_Programming/Q1/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package Lecture4_interfaces_abstract_classes;

//import org.jetbrains.annotations.NotNull;
import java.util. UUID;
import java.util.Calendar;


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

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

public BaseTransaction(double amount, Calendar date){
this.amount = amount;
this.date = date;
// int uniq = (int) (Math.random() * 1000000);
this.transactionID = UUID.randomUUID().toString();//Generates a unique ID
}

@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 void apply(BankAccount ba){
System.out.println("Applying transaction to bank account...");
}
}

public class BankAccount{
private double balance;

public BankAccount(double initialBalance){
this.balance = initialBalance;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
public void withdraw(double amount){
if(amount <= balance){
balance -= amount;
}else{
System.out.println("Insufficient funds!");
}
}
@Override
public String toString(){
return "BankAccount balance: " + balance;
}
}

public class DepositTransaction extends BaseTransaction{
public DepositTransaction(double amount, Calendar date){
super(amount, date);
}

@Override
public void apply(BankAccount ba){
ba.deposit(getAmount());
System.out.println("Deposit of" + getAmount() + "applied to bank account.");
}
}

public class WithdrawalTransaction extends BaseTransaction{
public WithdrawalTransaction(double amount, Calendar date){
super(amount, date);
}

@Override
public void apply(BankAccount ba){
ba.withdraw(getAmount());
System.out.println("Withdraw of " + getAmount() + "applied to bank account.");
}
}

public class TransactionTest{

public static void main(String[]args){
BankAccount account = new BankAccount(2000);

Calendar depositDate = Calendar.getInstance();
DepositTransaction deposit = new DepositTransaction(1000, depositDate);

Calendar withdrawalDate = Calendar.getInstance();
WithdrawalTransaction withdrawal = new WithdrawalTransaction(500, withdrawalDate);

//Apply Transactions
System.out.println("Before transactions: " + account);
deposit.apply(account);
withdrawal.apply(account);
System.out.println("After transactions: " + account);

//Print transaction details
deposit.printTransactionDetails();
withdrawal.printTransactionDetails();
}
}
30 changes: 30 additions & 0 deletions Assignment_I_Advanced_Programming/Q1/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

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

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

public void apply(BankAccount ba){
double curr_balance = ba.getBalance();
double new_balance = curr_balance + getAmount();
ba.setBalance(new_balance);
}
}
21 changes: 21 additions & 0 deletions Assignment_I_Advanced_Programming/Q1/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 printTransactionDetails();
void apply(BankAccount ba);
}
37 changes: 37 additions & 0 deletions Assignment_I_Advanced_Programming/Q1/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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());
}

public void apply(BankAccount ba) {
double curr_balance = ba.getBalance();
if (curr_balance > getAmount()) {
double new_balance = curr_balance - getAmount();
ba.setBalance(new_balance);
}
}
}
63 changes: 63 additions & 0 deletions Assignment_I_Advanced_Programming/Q2/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.Calendar;

public class WithdrawalTransaction extends BaseTransaction {
private boolean reversed = false; // Track if the transaction has been reversed
private BankAccount appliedAccount; // Keep track of the account this transaction was applied to

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

@Override
public void apply(BankAccount ba) {
if (getAmount() <= ba.getBalance()) {
ba.withdraw(getAmount());
appliedAccount = ba; // Store the account for potential reversal
System.out.println("Withdrawal of " + getAmount() + " applied.");
} else {
System.out.println("Insufficient funds for withdrawal.");
}
}

public boolean reverse() {
if (reversed) {
System.out.println("Transaction has already been reversed.");
return false;
}

if (appliedAccount != null) {
appliedAccount.deposit(getAmount());
reversed = true;
System.out.println("Withdrawal transaction reversed. Amount " + getAmount() + " restored.");
return true;
} else {
System.out.println("Transaction was not applied to any account; cannot reverse.");
return false;
}
}

public class TransactionTest {
public static void main(String[] args) {
// Create a bank account with an initial balance
BankAccount account = new BankAccount(5000);

// Create a withdrawal transaction
Calendar withdrawalDate = Calendar.getInstance();
WithdrawalTransaction withdrawal = new WithdrawalTransaction(1500, withdrawalDate);

// Apply withdrawal transaction
System.out.println("Before transaction: " + account);
withdrawal.apply(account);
System.out.println("After withdrawal: " + account);

// Reverse the withdrawal
boolean success = withdrawal.reverse();
if (success) {
System.out.println("After reversal: " + account);
}

// Attempt to reverse again
withdrawal.reverse();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package Lecture4_interfaces_abstract_classes;
import org.jetbrains.annotations.NotNull;
import java.util.Calendar;

public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}

public class WithdrawalTransaction extends BaseTransaction {
private boolean reversed = false;
private BankAccount appliedAccount;
private double remainingAmount = 0.0; // Keeps track of the amount not withdrawn

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

@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
if (getAmount() > ba.getBalance()) {
throw new InsufficientFundsException("Insufficient funds. Transaction failed.");
} else if (ba.getBalance() <= 0) {
throw new InsufficientFundsException("Account balance is zero or negative. Cannot proceed.");
} else {
ba.withdraw(getAmount());
appliedAccount = ba;
System.out.println("Withdrawal of " + getAmount() + " applied.");
}
}

// Overloaded apply() method
public void apply(BankAccount ba, boolean partialWithdrawal) {
try {
if (partialWithdrawal && ba.getBalance() > 0 && ba.getBalance() < getAmount()) {
remainingAmount = getAmount() - ba.getBalance();
System.out.println("Partial withdrawal applied. Remaining amount: " + remainingAmount);
ba.withdraw(ba.getBalance());
appliedAccount = ba;
} else {
apply(ba); // Call the original apply method
}
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
} finally {
System.out.println("Transaction process completed.");
}
}

public boolean reverse() {
if (reversed) {
System.out.println("Transaction has already been reversed.");
return false;
}

if (appliedAccount != null) {
appliedAccount.deposit(getAmount() - remainingAmount);
reversed = true;
System.out.println("Withdrawal transaction reversed. Amount " + (getAmount() - remainingAmount) + " restored.");
return true;
} else {
System.out.println("Transaction was not applied to any account; cannot reverse.");
return false;
}
}
}
Loading