diff --git a/src/Main.java b/src/Main.java
index 584a048..aaffd52 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,156 +1,69 @@
-import Lecture1_adt.*; // Import all classes from Lecture1_adt package to be used in this client code
-
import java.util.Calendar;
-import java.util.GregorianCalendar;
-import java.util.ArrayList;
-import java.util.List;
-
-//TIP To Run code, press or
-// click the icon in the gutter.
-/*
-* Client Code for accessing the Lecture1_adt.TransactionInterface.java module
- */
-public class Main {
-
- public static void testTransaction1() {
- Calendar d1 = new GregorianCalendar(); // d1 is an Object [Objects are Reference types]
- Lecture1_adt.Transaction1 t1 = new Lecture1_adt.Transaction1(1000, d1); // amount and d1 are arguments
-
- System.out.println(t1.toString());
- System.out.println("Lecture1_adt.TransactionInterface Amount: \t " + t1.amount);
- System.out.println("Lecture1_adt.TransactionInterface Date: \t " + t1.date);
-
- // Please note that the Client Codes can access the data in the class directly through the dot operator
- // This kind of exposure is a threat to both the Representation Independence and Preservation of Invariants
- }
-
-
- /** @return a transaction of same amount as t, one month later
- * This is a PRODUCER of the class Lecture1_adt.Transaction2
- * This code will help demostrate the Design exposures still present in transaction2 class
- * */
- public static Transaction2 makeNextPayment(Transaction2 t) {
- Calendar d = t.getDate();
- d.add(Calendar.MONTH, 1);
- return new Transaction2(t.getAmount(), d);
- }
-
- /*
- Testing Transaction2 class
- */
- public static void testTransaction2() {
-
- Calendar d1 = new GregorianCalendar();
+public class Main {
- Lecture1_adt.Transaction2 t = new Lecture1_adt.Transaction2(1000, d1);
+ public static void main(String[] args) {
- Lecture1_adt.Transaction2 modified_t = makeNextPayment(t);
+ BankAccount acc =
+ new BankAccount("ACC001", 1000);
- System.out.println("\n\nState of the Object T1 After Client Code Tried to Change the Amount");
- System.out.println("Lecture1_adt.TransactionInterface Amount: \t "+modified_t.getAmount());
- System.out.println("Lecture1_adt.TransactionInterface Date: \t "+modified_t.getDate().getTime());
+ Calendar today = Calendar.getInstance();
- System.out.println("\n\nHow does T2 Look Like?????");
- System.out.println("Lecture1_adt.TransactionInterface Amount: \t "+modified_t.getAmount());
- System.out.println("Lecture1_adt.TransactionInterface Date: \t "+modified_t.getDate().getTime());
+ DepositTransaction deposit =
+ new DepositTransaction(
+ 500,
+ today,
+ "D001");
- /* Please note that Although we have solved the problem of Transaction1
- * And client code can no longer use the dot (.) operator to directly access the data
- * There is still some exposure especially if we pass an object of a previous Transaction2 to create a new Transaction2 object
- */
+ WithdrawalTransaction withdrawal =
+ new WithdrawalTransaction(
+ 300,
+ today,
+ "W001");
- }
+ // Test Deposit
+ deposit.printTransactionDetails();
+ deposit.apply(acc);
+ acc.printAccount();
+ System.out.println();
- /** @return a list of 12 monthly payments of identical amounts
- * This code will help demostrate the Design exposures still present in transaction3 class
- * */
- public static List makeYearOfPayments (int amount) throws NullPointerException {
+ // Test Withdrawal
+ try {
- List listOfTransaction3s = new ArrayList();
- Calendar date = new GregorianCalendar(2024, Calendar.JANUARY, 3);
+ withdrawal.printTransactionDetails();
+ withdrawal.apply(acc);
+ } catch (InsufficientFundsException e) {
- for (int i = 0; i < 12; i++) {
- listOfTransaction3s.add(new Transaction3(amount, date));
- date.add(Calendar.MONTH, 1);
+ System.out.println(e.getMessage());
}
- return listOfTransaction3s;
- }
- /*
- Testing Transaction3 class
- */
- public static void testTransaction3() {
+ acc.printAccount();
- List allPaymentsIn2024 = makeYearOfPayments(1000);
+ System.out.println();
- for (Transaction3 t3 : allPaymentsIn2024) {
-
- // Display all the 12 Transactions
- for (Transaction3 transact : allPaymentsIn2024) {
- System.out.println("\n\n ::::::::::::::::::::::::::::::::::::::::::::\n");
- System.out.println("Lecture1_adt.TransactionInterface Amount: \t "+transact.getAmount());
- System.out.println("Lecture1_adt.TransactionInterface Date: \t "+transact.getDate().getTime());
- }
- }
-
- /* Please Check all the 12 transactions displayed and hwo their dates look like
- * Note that Although Transaction3 class resolves to an extent the exposure in Transaction2 class
- * There is still some exposure especially if we pass an object of a previous Transaction3 to create a
- * new Transaction3 object
- */
- }
+ // Reverse Withdrawal
+ withdrawal.reverse();
+ acc.printAccount();
+ System.out.println();
- /** @return a list of 12 monthly payments of identical amounts
- * This code Show that by judicious copying and defensive programming we eliminate the exposure in Transaction3
- * As defined in the constructor of Transaction4 class
- * */
+ // Polymorphism
+ BaseTransaction bt;
- public static List makeYearOfPaymentsFinal (int amount) throws NullPointerException {
+ bt = deposit;
+ bt.apply(acc);
- List listOfTransaction4s = new ArrayList();
- Calendar date = new GregorianCalendar(2024, Calendar.JANUARY, 3);
+ bt = withdrawal;
-
- for (int i = 0; i < 12; i++) {
- listOfTransaction4s.add(new Transaction4(amount, date));
- date.add(Calendar.MONTH, 1);
+ try {
+ bt.apply(acc);
}
- return listOfTransaction4s;
- }
-
- /*
- Testing Transaction3 class
- */
- public static void testTransaction4() {
-
- /*
- * Call the function to make all the Twelve transaction in a year of our business
- */
-
- List transactionsIn2024 = makeYearOfPaymentsFinal(1200);
-
- // Display all the 12 Transactions
- for (Transaction4 transact : transactionsIn2024) {
- System.out.println("\n\n ::::::::::::::::::::::::::::::::::::::::::::\n");
- System.out.println("Lecture1_adt.TransactionInterface Amount: \t "+transact.getAmount());
- System.out.println("Lecture1_adt.TransactionInterface Date: \t "+transact.getDate().getTime());
+ catch (Exception e) {
+ System.out.println(e.getMessage());
}
- // Please Take a look at all the 12 transaction now and compare with the outputs of the Transaction3 class
- }
-
-
- 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
-
- // testTransaction1()
- // testTransaction2()
- // testTransaction3()
- // testTransaction4()
+ acc.printAccount();
}
-}
\ No newline at end of file
+}
diff --git a/src/basetransactionclass b/src/basetransactionclass
new file mode 100644
index 0000000..63ac7b5
--- /dev/null
+++ b/src/basetransactionclass
@@ -0,0 +1,41 @@
+import java.util.Calendar;
+
+public class BaseTransaction implements TransactionInterface {
+
+ protected double amount;
+ protected Calendar date;
+ protected String transactionID;
+
+ public BaseTransaction(double amount, Calendar date, String transactionID) {
+ this.amount = amount;
+ this.date = date;
+ this.transactionID = transactionID;
+ }
+
+ @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("Generic transaction applied.");
+ }
+}
diff --git a/src/deposittransactionclass b/src/deposittransactionclass
new file mode 100644
index 0000000..5084a7b
--- /dev/null
+++ b/src/deposittransactionclass
@@ -0,0 +1,20 @@
+import java.util.Calendar;
+
+public class DepositTransaction extends BaseTransaction {
+
+ public DepositTransaction(double amount, Calendar date, String transactionID) {
+ super(amount, date, transactionID);
+ }
+
+ @Override
+ public void apply(BankAccount ba) {
+ ba.deposit(amount);
+ System.out.println("Deposit successful.");
+ }
+
+ @Override
+ public void printTransactionDetails() {
+ System.out.println("=== Deposit Transaction ===");
+ super.printTransactionDetails();
+ }
+}
diff --git a/src/insufficientfundsexception b/src/insufficientfundsexception
new file mode 100644
index 0000000..ad4c526
--- /dev/null
+++ b/src/insufficientfundsexception
@@ -0,0 +1,7 @@
+public class InsufficientFundsException
+ extends Exception {
+
+ public InsufficientFundsException(String message) {
+ super(message);
+ }
+}
diff --git a/src/reversewithdrawal b/src/reversewithdrawal
new file mode 100644
index 0000000..1c076ba
--- /dev/null
+++ b/src/reversewithdrawal
@@ -0,0 +1,13 @@
+public boolean reverse() {
+
+ if (accountUsed == null) {
+ return false;
+ }
+
+ accountUsed.deposit(amount);
+
+ System.out.println(
+ "Withdrawal reversed successfully.");
+
+ return true;
+}
diff --git a/src/withdrawaltransactionclass b/src/withdrawaltransactionclass
new file mode 100644
index 0000000..b0f4836
--- /dev/null
+++ b/src/withdrawaltransactionclass
@@ -0,0 +1,90 @@
+import java.util.Calendar;
+
+public class WithdrawalTransaction extends BaseTransaction {
+
+ private BankAccount accountUsed;
+ private double amountNotWithdrawn = 0;
+
+ public WithdrawalTransaction(double amount,
+ Calendar date,
+ String transactionID) {
+ super(amount, date, transactionID);
+ }
+
+ @Override
+ public void apply(BankAccount ba)
+ throws InsufficientFundsException {
+
+ if (ba.getBalance() < amount) {
+ throw new InsufficientFundsException(
+ "Insufficient funds.");
+ }
+
+ ba.withdraw(amount);
+ accountUsed = ba;
+
+ System.out.println("Withdrawal successful.");
+ }
+
+ // Overloaded apply method
+ public void apply(BankAccount ba, boolean availableBalanceMode) {
+
+ try {
+
+ if (ba.getBalance() <= 0) {
+ throw new InsufficientFundsException(
+ "Account balance is zero.");
+ }
+
+ if (ba.getBalance() < amount) {
+
+ amountNotWithdrawn =
+ amount - ba.getBalance();
+
+ ba.withdraw(ba.getBalance());
+
+ System.out.println(
+ "Partial withdrawal completed.");
+ }
+ else {
+ ba.withdraw(amount);
+ }
+
+ accountUsed = ba;
+ }
+
+ catch (InsufficientFundsException e) {
+ System.out.println(e.getMessage());
+ }
+
+ finally {
+ System.out.println(
+ "Withdrawal process completed.");
+ }
+ }
+
+ public double getAmountNotWithdrawn() {
+ return amountNotWithdrawn;
+ }
+
+ public boolean reverse() {
+
+ if (accountUsed == null) {
+ return false;
+ }
+
+ accountUsed.deposit(amount);
+
+ System.out.println(
+ "Withdrawal reversed successfully.");
+
+ return true;
+ }
+
+ @Override
+ public void printTransactionDetails() {
+ System.out.println(
+ "=== Withdrawal Transaction ===");
+ super.printTransactionDetails();
+ }
+}