diff --git a/.gitignore b/.gitignore
index f68d109..985ff6c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,4 +26,7 @@ bin/
.vscode/
### Mac OS ###
-.DS_Store
\ No newline at end of file
+.DS_Store
+
+# Ignore all .class files
+*.class
diff --git a/src/Lecture1_adt/Transaction1.class b/src/Lecture1_adt/Transaction1.class
new file mode 100644
index 0000000..25224be
Binary files /dev/null and b/src/Lecture1_adt/Transaction1.class differ
diff --git a/src/Lecture1_adt/Transaction2.class b/src/Lecture1_adt/Transaction2.class
new file mode 100644
index 0000000..036f6d6
Binary files /dev/null and b/src/Lecture1_adt/Transaction2.class differ
diff --git a/src/Lecture1_adt/Transaction2.java b/src/Lecture1_adt/Transaction2.java
index 6c7a010..274b8f7 100644
--- a/src/Lecture1_adt/Transaction2.java
+++ b/src/Lecture1_adt/Transaction2.java
@@ -1,36 +1,29 @@
package Lecture1_adt;
-import org.jetbrains.annotations.NotNull;
-
import java.util.Calendar;
-//import java.util.Date;
/**
- * This Lecture1_adt.Transaction2 Class Takes the first step to resolves the ADT design issues of Transaction1:
+ * This Lecture1_adt.Transaction2 Class takes the first step to resolve the ADT design issues of Transaction1:
* 1. Representation Independence: --- Encapsulation - Providing access methods to the internal data.
- * External client code only access via allowable operations
- * --- Changes to internal representation can still be accessed via same methods defined
- *
- * 2. Preservation of Invariants: --- Access Modifies private final makes the data Unchangeable
+ * External client code only accesses via allowable operations
+ * --- Changes to internal representation can still be accessed via the same methods defined
*
- * Lecture1_adt
+ * 2. Preservation of Invariants: --- Access modifies private final fields to make the data unchangeable
*/
-
public class Transaction2 {
private final int amount;
private final Calendar date;
- public Transaction2(int amount, @NotNull Calendar date) {
+ public Transaction2(int amount, Calendar date) {
this.amount = amount;
- this.date = date;
+ this.date = date; // Defensive copying not required for constructor arguments in this class
}
public int getAmount() {
- return amount; // Because we are dealing with Value types we need not worry about what we return
+ return amount; // Value types like integers can be returned directly
}
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
+ return (Calendar) date.clone(); // Defensive copying ensures reference types are not altered externally
}
}
diff --git a/src/Lecture1_adt/Transaction3.class b/src/Lecture1_adt/Transaction3.class
new file mode 100644
index 0000000..f24dc62
Binary files /dev/null and b/src/Lecture1_adt/Transaction3.class differ
diff --git a/src/Lecture1_adt/Transaction3.java b/src/Lecture1_adt/Transaction3.java
index 48781e7..ef47946 100644
--- a/src/Lecture1_adt/Transaction3.java
+++ b/src/Lecture1_adt/Transaction3.java
@@ -1,31 +1,25 @@
package Lecture1_adt;
-import org.jetbrains.annotations.NotNull;
-
import java.util.Calendar;
/**
- * There is still Exposure seen in Transaction2 I that if we make next payment the date of first payment is also altered
- * This Class Transaction3:Adds Code to correct this exposure:
- * Intentional review of any methods that receive produces (returns)
- * If produces interface of a method deals with objects or any reference types,
- * there is need to perform defensive copying to enhance Invariant preservation
+ * This Lecture1_adt.Transaction3 class adds code to correct potential exposure issues in Transaction2.
+ * Methods that return objects of reference types use defensive copying to enhance invariant preservation.
*/
public class Transaction3 {
private final int amount;
private final Calendar date;
- public Transaction3(int amount, @NotNull Calendar date) {
+ public Transaction3(int amount, Calendar date) {
this.amount = amount;
- this.date = date;
+ this.date = date; // Defensive copying not required for constructor arguments in this class
}
public int getAmount() {
- return amount; // Because we are dealing with Value types we need not worry about what we return
+ return amount; // Value types like integers can be returned directly
}
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
+ return (Calendar) date.clone(); // Defensive copying ensures reference types are not altered externally
}
}
diff --git a/src/Lecture1_adt/Transaction4.class b/src/Lecture1_adt/Transaction4.class
new file mode 100644
index 0000000..1533774
Binary files /dev/null and b/src/Lecture1_adt/Transaction4.class differ
diff --git a/src/Lecture1_adt/Transaction4.java b/src/Lecture1_adt/Transaction4.java
index f09b0d8..2c8fd9d 100644
--- a/src/Lecture1_adt/Transaction4.java
+++ b/src/Lecture1_adt/Transaction4.java
@@ -1,32 +1,25 @@
package Lecture1_adt;
-import org.jetbrains.annotations.NotNull;
-
import java.util.Calendar;
+
/**
- * There is still Exposure seen in Transaction3 in that if we make a list of 12 payments,
- * the date of all payments remain the same.
- * This Class Transaction4:Adds Code to correct this exposure:
- * In Making th List of 12 Transactions, we are constantly using the constructor which takes a Date Object
- * Intentional review of any methods that receives or requires (i.e. takes in as parameters)
- * If requires interface of a method deals with objects or any reference types,
- * there is need to perform defensive copying to enhance Invariant preservation
+ * This Lecture1_adt.Transaction4 class ensures that both constructor arguments and method returns use defensive copying.
+ * This prevents unintended side effects when dealing with reference types.
*/
public class Transaction4 {
private final int amount;
private final Calendar date;
- public Transaction4(int amount, @NotNull Calendar date) {
+ public Transaction4(int amount, Calendar date) {
this.amount = amount;
- this.date = (Calendar) date.clone(); // Defensive copying or Judicious Copying for Requires interfaces
+ this.date = (Calendar) date.clone(); // Defensive copying for constructor arguments
}
public int getAmount() {
- return amount; // Because we are dealing with Value types we need not worry about what we return
+ return amount; // Value types like integers can be returned directly
}
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 for produces Interfaces
+ return (Calendar) date.clone(); // Defensive copying ensures reference types are not altered externally
}
}
diff --git a/src/Lecture2_adt_specification/Transaction4.class b/src/Lecture2_adt_specification/Transaction4.class
new file mode 100644
index 0000000..d18d0c9
Binary files /dev/null and b/src/Lecture2_adt_specification/Transaction4.class differ
diff --git a/src/Lecture2_adt_specification/Transaction4.java b/src/Lecture2_adt_specification/Transaction4.java
index be8a1eb..6e35b4e 100644
--- a/src/Lecture2_adt_specification/Transaction4.java
+++ b/src/Lecture2_adt_specification/Transaction4.java
@@ -1,47 +1,38 @@
package Lecture2_adt_specification;
-import org.jetbrains.annotations.NotNull;
-
import java.util.Calendar;
-//import java.util.Date;
/**
- * In Addition to the Design considerations in Transaction4 class:
- * This class adds Specifications defining the Requires and Produces interfaces
+ * This Lecture2_adt_specification.Transaction4 class extends the design considerations of the previous Transaction4 class.
+ * It adds specifications for the requires and produces interfaces.
*/
-
public class Transaction4 {
private final int amount;
private final Calendar date;
/**
- * 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
+ * Constructor for Transaction4
+ * @param amount the transaction amount (must be an integer)
+ * @param date the transaction date (must be a Calendar object and not null)
*/
- public Transaction4(int amount, @NotNull Calendar date) {
+ public Transaction4(int amount, Calendar date) {
this.amount = amount;
- this.date = (Calendar) date.clone();
+ this.date = (Calendar) date.clone(); // Defensive copying ensures input reference is not shared
}
-
/**
- * getAmount()
- * @return integer
+ * Retrieves the transaction amount.
+ * @return an integer representing the amount
*/
public int getAmount() {
- return amount; // Because we are dealing with Value types we need not worry about what we return
+ return amount; // Value types like integers can be returned directly
}
/**
- * getDate()
- * @return Calendar Object
+ * Retrieves the transaction date.
+ * @return a Calendar object representing the transaction date
*/
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
+ return (Calendar) date.clone(); // Defensive copying ensures reference types are not altered externally
}
}
diff --git a/src/Lecture4_interfaces_abstract_classes/BankAccount.class b/src/Lecture4_interfaces_abstract_classes/BankAccount.class
new file mode 100644
index 0000000..22eca18
Binary files /dev/null and b/src/Lecture4_interfaces_abstract_classes/BankAccount.class differ
diff --git a/src/Lecture4_interfaces_abstract_classes/BankAccount.java b/src/Lecture4_interfaces_abstract_classes/BankAccount.java
index 28d0d07..e5e676e 100644
--- a/src/Lecture4_interfaces_abstract_classes/BankAccount.java
+++ b/src/Lecture4_interfaces_abstract_classes/BankAccount.java
@@ -2,6 +2,7 @@
public class BankAccount {
private double balance;
+
public BankAccount(double balance) {
this.balance = balance;
}
@@ -10,7 +11,15 @@ public double getBalance() {
return balance;
}
- public void setBalance(double balance) {
- this.balance = balance;
+ public void deposit(double amount) {
+ balance += amount;
+ }
+
+ public void withdraw(double amount) {
+ if (balance >= amount) {
+ balance -= amount;
+ } else {
+ System.out.println("Insufficient balance for withdrawal.");
+ }
}
}
diff --git a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.class b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.class
new file mode 100644
index 0000000..a380986
Binary files /dev/null and b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.class differ
diff --git a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
index ed81eb8..4f775e4 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 double 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) {
+ public BaseTransaction(double amount, Calendar date, String transactionID) {
this.amount = amount;
- this.date = (Calendar) date.clone();
- int uniq = (int) Math.random()*10000;
- transactionID = date.toString()+uniq;
+ this.date = (Calendar) date.clone(); // Defensive copying
+ this.transactionID = transactionID;
}
- /**
- * getAmount()
- * @return integer
- */
+ @Override
public double getAmount() {
- return amount; // Because we are dealing with Value types we need not worry about what we return
+ return amount;
}
- /**
- * 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
+ return (Calendar) date.clone(); // Defensive copying
+ }
+
+ @Override
+ public String getTransactionID() {
+ return transactionID;
}
- // Method to get a unique identifier for the transaction
- 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());
}
- // Method to print a transaction receipt or details
- public abstract void printTransactionDetails();
+
+ @Override
public abstract void apply(BankAccount ba);
+
+ @Override
+ public abstract boolean reverse(BankAccount ba);
}
diff --git a/src/Lecture4_interfaces_abstract_classes/DepositTransaction.class b/src/Lecture4_interfaces_abstract_classes/DepositTransaction.class
new file mode 100644
index 0000000..d4bc182
Binary files /dev/null and b/src/Lecture4_interfaces_abstract_classes/DepositTransaction.class differ
diff --git a/src/Lecture4_interfaces_abstract_classes/DepositTransaction.java b/src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
new file mode 100644
index 0000000..c585717
--- /dev/null
+++ b/src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
@@ -0,0 +1,27 @@
+package Lecture4_interfaces_abstract_classes;
+
+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(getAmount());
+ System.out.println("Deposited: " + getAmount());
+ }
+
+ @Override
+ public boolean reverse(BankAccount ba) {
+ if (ba.getBalance() >= getAmount()) {
+ ba.withdraw(getAmount());
+ System.out.println("Deposit reversed: " + getAmount());
+ return true;
+ } else {
+ System.out.println("Unable to reverse deposit: insufficient balance.");
+ return false;
+ }
+ }
+}
diff --git a/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java b/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java
deleted file mode 100644
index 81afab5..0000000
--- a/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package Lecture4_interfaces_abstract_classes;
-
-import org.jetbrains.annotations.NotNull;
-
-import java.util.Calendar;
-
-public class DepositTrasaction extends BaseTransaction {
- public DepositTrasaction(int amount, @NotNull Calendar date){
- super(amount, date);
- }
- private boolean checkDepositAmount(int amt){
- if (amt < 0){
- return false;
- }
- else{
- return true;
- }
- }
-
- // 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();
- double new_balance = curr_balance + getAmount();
- ba.setBalance(new_balance);
- }
-}
diff --git a/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.class b/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.class
new file mode 100644
index 0000000..67311d2
Binary files /dev/null and b/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.class differ
diff --git a/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java b/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java
new file mode 100644
index 0000000..97176e3
--- /dev/null
+++ b/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java
@@ -0,0 +1,6 @@
+package Lecture4_interfaces_abstract_classes;
+public class InsufficientFundsException extends Exception {
+ public InsufficientFundsException(String message) {
+ super(message);
+ }
+}
diff --git a/src/Lecture4_interfaces_abstract_classes/Main.class b/src/Lecture4_interfaces_abstract_classes/Main.class
new file mode 100644
index 0000000..6d5faf3
Binary files /dev/null and b/src/Lecture4_interfaces_abstract_classes/Main.class differ
diff --git a/src/Lecture4_interfaces_abstract_classes/Main.java b/src/Lecture4_interfaces_abstract_classes/Main.java
new file mode 100644
index 0000000..3ec0681
--- /dev/null
+++ b/src/Lecture4_interfaces_abstract_classes/Main.java
@@ -0,0 +1,41 @@
+package Lecture4_interfaces_abstract_classes;
+
+import java.util.Calendar;
+
+public class Main {
+ public static void main(String[] args) {
+
+ BankAccount account = new BankAccount(1000);
+
+ DepositTransaction deposit = new DepositTransaction(500, Calendar.getInstance(), "TX001");
+ System.out.println("\nExecuting Deposit Transaction:");
+ deposit.printTransactionDetails();
+ deposit.apply(account);
+ System.out.println("Account balance after deposit: " + account.getBalance());
+
+
+ WithdrawalTransaction withdrawal = new WithdrawalTransaction(200, Calendar.getInstance(), "TX002");
+ System.out.println("\nExecuting Withdrawal Transaction:");
+ withdrawal.printTransactionDetails();
+ withdrawal.apply(account);
+ System.out.println("Account balance after withdrawal: " + account.getBalance());
+
+
+ System.out.println("\nReversing Withdrawal Transaction:");
+ if (withdrawal.reverse(account)) {
+ System.out.println("Withdrawal reversed successfully.");
+ } else {
+ System.out.println("Reversal failed due to insufficient balance.");
+ }
+ System.out.println("Account balance after reversal: " + account.getBalance());
+
+
+ System.out.println("\nReversing Deposit Transaction:");
+ if (deposit.reverse(account)) {
+ System.out.println("Deposit reversed successfully.");
+ } else {
+ System.out.println("Reversal failed due to insufficient balance.");
+ }
+ System.out.println("Account balance after deposit reversal: " + account.getBalance());
+ }
+}
diff --git a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.class b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.class
new file mode 100644
index 0000000..d28a0ef
Binary files /dev/null and b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.class differ
diff --git a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
index 5902713..a4e591d 100644
--- a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
+++ b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
@@ -1,21 +1,12 @@
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 apply(BankAccount ba);
+ boolean reverse(BankAccount ba);
+ void printTransactionDetails();
}
-
-
diff --git a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.class b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.class
new file mode 100644
index 0000000..5e796a0
Binary files /dev/null and b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.class differ
diff --git a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
index face5b6..8c6d5ca 100644
--- a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
+++ b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
@@ -1,45 +1,26 @@
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);
+ public WithdrawalTransaction(double amount, Calendar date, String transactionID) {
+ super(amount, date, transactionID);
}
- private boolean checkDepositAmount(int amt) {
- if (amt < 0) {
- return false;
+ @Override
+ public void apply(BankAccount ba) {
+ if (ba.getBalance() >= getAmount()) {
+ ba.withdraw(getAmount());
+ System.out.println("Withdrawn: " + getAmount());
} else {
- return true;
+ System.out.println("Insufficient funds for withdrawal.");
}
}
- // Method to reverse the transaction
- public boolean reverse() {
+ @Override
+ public boolean reverse(BankAccount ba) {
+ ba.deposit(getAmount());
+ System.out.println("Withdrawal reversed: " + getAmount());
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
- */
}
-
diff --git a/src/Main.java b/src/Main.java
deleted file mode 100644
index 584a048..0000000
--- a/src/Main.java
+++ /dev/null
@@ -1,156 +0,0 @@
-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();
-
- Lecture1_adt.Transaction2 t = new Lecture1_adt.Transaction2(1000, d1);
-
- Lecture1_adt.Transaction2 modified_t = makeNextPayment(t);
-
- 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());
-
- 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());
-
- /* 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
- */
-
- }
-
-
- /** @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 {
-
- List listOfTransaction3s = new ArrayList();
- Calendar date = new GregorianCalendar(2024, Calendar.JANUARY, 3);
-
-
- for (int i = 0; i < 12; i++) {
- listOfTransaction3s.add(new Transaction3(amount, date));
- date.add(Calendar.MONTH, 1);
- }
- return listOfTransaction3s;
- }
-
- /*
- Testing Transaction3 class
- */
- public static void testTransaction3() {
-
- List allPaymentsIn2024 = makeYearOfPayments(1000);
-
- 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
- */
- }
-
-
- /** @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
- * */
-
- public static List makeYearOfPaymentsFinal (int amount) throws NullPointerException {
-
- List listOfTransaction4s = new ArrayList();
- Calendar date = new GregorianCalendar(2024, Calendar.JANUARY, 3);
-
-
- for (int i = 0; i < 12; i++) {
- listOfTransaction4s.add(new Transaction4(amount, date));
- date.add(Calendar.MONTH, 1);
- }
- 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());
- }
-
- // 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()
- }
-}
\ No newline at end of file