diff --git a/Assignment_I_Advanced_Programming/Q1/BankAccount.java b/Assignment_I_Advanced_Programming/Q1/BankAccount.java new file mode 100644 index 0000000..74f3877 --- /dev/null +++ b/Assignment_I_Advanced_Programming/Q1/BankAccount.java @@ -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; + } +} \ No newline at end of file diff --git a/Assignment_I_Advanced_Programming/Q1/BaseTransaction.java b/Assignment_I_Advanced_Programming/Q1/BaseTransaction.java new file mode 100644 index 0000000..0c1f61d --- /dev/null +++ b/Assignment_I_Advanced_Programming/Q1/BaseTransaction.java @@ -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(); + } +} \ No newline at end of file diff --git a/Assignment_I_Advanced_Programming/Q1/DepositTransaction.java b/Assignment_I_Advanced_Programming/Q1/DepositTransaction.java new file mode 100644 index 0000000..ff98d51 --- /dev/null +++ b/Assignment_I_Advanced_Programming/Q1/DepositTransaction.java @@ -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); + } +} \ No newline at end of file diff --git a/Assignment_I_Advanced_Programming/Q1/TransactionInterface.java b/Assignment_I_Advanced_Programming/Q1/TransactionInterface.java new file mode 100644 index 0000000..887e565 --- /dev/null +++ b/Assignment_I_Advanced_Programming/Q1/TransactionInterface.java @@ -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); +} \ No newline at end of file diff --git a/Assignment_I_Advanced_Programming/Q1/WithdrawalTransaction.java b/Assignment_I_Advanced_Programming/Q1/WithdrawalTransaction.java new file mode 100644 index 0000000..49a31e9 --- /dev/null +++ b/Assignment_I_Advanced_Programming/Q1/WithdrawalTransaction.java @@ -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); + } + } +} diff --git a/Assignment_I_Advanced_Programming/Q2/WithdrawalTransaction.java b/Assignment_I_Advanced_Programming/Q2/WithdrawalTransaction.java new file mode 100644 index 0000000..e929ce4 --- /dev/null +++ b/Assignment_I_Advanced_Programming/Q2/WithdrawalTransaction.java @@ -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(); + } +} +} \ No newline at end of file diff --git a/Assignment_I_Advanced_Programming/Q3/InsufficientFundsException.java b/Assignment_I_Advanced_Programming/Q3/InsufficientFundsException.java new file mode 100644 index 0000000..24d04c7 --- /dev/null +++ b/Assignment_I_Advanced_Programming/Q3/InsufficientFundsException.java @@ -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; + } + } +} diff --git a/Assignment_I_Advanced_Programming/Q4/Main.java b/Assignment_I_Advanced_Programming/Q4/Main.java new file mode 100644 index 0000000..7d66406 --- /dev/null +++ b/Assignment_I_Advanced_Programming/Q4/Main.java @@ -0,0 +1,63 @@ +package Lecture4_interfaces_abstract_classes; + +import org.jetbrains.annotations.NotNull; + +import java.util.Calendar; + +public class Main { + public static void main(String[] args) { + // Create a BankAccount with an initial balance + BankAccount account = new BankAccount(5000); + + // Create a DepositTransaction + Calendar depositDate = Calendar.getInstance(); + DepositTransaction deposit = new DepositTransaction(2000, depositDate); + + // Create a WithdrawalTransaction + Calendar withdrawalDate = Calendar.getInstance(); + WithdrawalTransaction withdrawal = new WithdrawalTransaction(3000, withdrawalDate); + + // Test DepositTransaction (Directly) + System.out.println("Testing DepositTransaction:"); + System.out.println("Before deposit: " + account); + deposit.apply(account); + System.out.println("After deposit: " + account); + deposit.printTransactionDetails(); + + // Test WithdrawalTransaction (Directly) + System.out.println("\nTesting WithdrawalTransaction:"); + System.out.println("Before withdrawal: " + account); + try { + withdrawal.apply(account); + } catch (InsufficientFundsException e) { + System.out.println("Exception caught: " + e.getMessage()); + } + System.out.println("After withdrawal: " + account); + withdrawal.printTransactionDetails(); + + // Test BaseTransaction behavior (Type casting) + System.out.println("\nTesting BaseTransaction behavior using type casting:"); + BaseTransaction baseDeposit = (BaseTransaction) deposit; // Upcasting + BaseTransaction baseWithdrawal = (BaseTransaction) withdrawal; // Upcasting + + // Using BaseTransaction references + System.out.println("Testing apply() on BaseTransaction reference:"); + System.out.println("Before base deposit: " + account); + baseDeposit.apply(account); // Polymorphic call + System.out.println("After base deposit: " + account); + + System.out.println("Before base withdrawal: " + account); + try { + baseWithdrawal.apply(account); // Polymorphic call + } catch (InsufficientFundsException e) { + System.out.println("Exception caught: " + e.getMessage()); + } + System.out.println("After base withdrawal: " + account); + + // Reversal Test + System.out.println("\nTesting reversal functionality:"); + withdrawal.reverse(); + System.out.println("After reversal: " + account); + } +} + diff --git a/README.md b/README.md index 99b7168..15342bd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ +# VICTOR KURIA THUITA +# SCT212-0708/2022 # Advanced-Programming +## Assignment_1_Advanced_Programming is the assignment folder A Repository for Programming Exercises and Assignments in Advanced Programming