diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..0915c10 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,11 @@ + + + + \ No newline at end of file diff --git a/src/Lecture4_interfaces_abstract_classes/BankAccount.java b/src/Lecture4_interfaces_abstract_classes/BankAccount.java index 28d0d07..a56ed64 100644 --- a/src/Lecture4_interfaces_abstract_classes/BankAccount.java +++ b/src/Lecture4_interfaces_abstract_classes/BankAccount.java @@ -1,16 +1,69 @@ + package Lecture4_interfaces_abstract_classes; +/** + * BankAccount Class + * Description: Represents a simple bank account with a monetary balance. + * Provides methods to retrieve and update the account balance while enforcing non-negative values. + + * Key Features: + * - Stores and manages account balance. + * - Allows controlled updates to ensure the integrity of the balance. + + * @errorHandling: + * - Prevents negative balances during initialization or updates. + + * @author Bridget Wanjiru + */ public class BankAccount { private double balance; + /** + * Constructor for BankAccount. + * Initializes a bank account with a specified initial balance. + + * @param balance - The initial account balance (must be non-negative). + * @throws IllegalArgumentException if the initial balance is negative. + * @pre Initial balance must be non-negative. + * @post The account is created with the specified initial balance. + + * Example: + * - Input: 1000.0 + * - Output: Account created with a balance of 1000.0 + */ + public BankAccount(double balance) { this.balance = balance; } + /** + * Retrieves the current account balance. + + * @return double - The current account balance. + * @pre None. + * @post Returns the current balance without modifying it. + + * Example: + * - Input: None + * - Output: 1000.0 (current account balance) + */ public double getBalance() { return balance; } + /** + * Updates the account balance. + * + * @param balance - The new account balance (must be non-negative). + * @throws IllegalArgumentException if the new balance is negative. + * @pre New balance must be non-negative. + * @post The account balance is updated to the specified value. + + * Example: + * - Input: 1500.0 + * - Output: Account balance updated to 1500.0 + */ + public void setBalance(double balance) { this.balance = balance; } -} +} \ No newline at end of file diff --git a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java index ed81eb8..ce8b783 100644 --- a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java +++ b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java @@ -3,6 +3,29 @@ import org.jetbrains.annotations.NotNull; import java.util.Calendar; +/** + * BaseTransaction Class + * Description: Represents an abstract base class for financial transactions. + * Implements the TransactionInterface to provide basic functionality such as retrieving transaction details + * (amount, date, and ID). This class serves as a foundation for specific transaction types like deposits and withdrawals. + + * Key Features: + * - Provides common fields and methods for financial transactions. + * - Ensures that subclasses implement specific behavior for transaction application and details printing. + + * Inherits: + * - TransactionInterface + + * Abstract Methods: + * - printTransactionDetails(): Prints transaction details (to be implemented by subclasses). + * - apply(BankAccount ba): Applies the transaction to a bank account (to be implemented by subclasses). + * + * @errorHandling: + * - Defensive copying is used for date fields to ensure immutability. + * - Transaction ID is generated uniquely for each transaction. + * + * @author Bridget Wanjiru + */ public abstract class BaseTransaction implements TransactionInterface { private final int amount; @@ -10,42 +33,92 @@ public abstract class BaseTransaction implements TransactionInterface { 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 + * Constructor for BaseTransaction. + * Initializes a transaction with a specified amount and date, generating a unique transaction ID. + * + * @param amount - The transaction amount (must be positive). + * @param date - The transaction date (must be a valid, non-null Calendar object). + * @throws IllegalArgumentException if the amount is negative or the date is null. + * @pre Amount must be positive, and date must be a valid Calendar object. + * @post The transaction is initialized with the specified amount, date, and a unique ID. */ - public BaseTransaction(int amount, @NotNull Calendar date) { + + public BaseTransaction(int amount, @NotNull Calendar date) { this.amount = amount; this.date = (Calendar) date.clone(); - int uniq = (int) Math.random()*10000; - transactionID = date.toString()+uniq; + + // Generate a shortened transaction ID using a simple format + String baseID = String.valueOf(date.getTimeInMillis()); // Use the time in milliseconds for simplicity + int uniq = (int) (Math.random() * 10000); // Random value for uniqueness + transactionID = baseID.substring(0, 8) + "-" + uniq; // Shortened to the first 8 digits of the time and added random part } /** - * getAmount() - * @return integer + * Retrieves the transaction amount. + * + * @return double - The transaction amount. + * @pre None. + * @post Returns the transaction amount as a positive integer. + + * Example: + * - Input: None + * - Output: 500 (transaction amount) */ + public double getAmount() { return amount; // Because we are dealing with Value types we need not worry about what we return } /** - * getDate() - * @return Calendar Object + * Retrieves the transaction date. + * + * @return Calendar - A defensive copy of the transaction date. + * @pre None. + * @post Returns a copy of the transaction date to ensure immutability. + + * Example: + * - Input: None + * - Output: Calendar instance with 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(); } - // Method to get a unique identifier for the transaction + /* + * Retrieves the unique transaction identifier. + + * @return String - The unique transaction ID. + * @pre None. + * @post Returns a unique string identifier for the transaction. + + * Example: + * - Input: None + * - Output: TX-12345678-9876 (unique transaction ID) + */ + public String getTransactionID(){ return transactionID; } - // Method to print a transaction receipt or details + + /** + * Abstract method to print transaction details. + * Subclasses must implement this method to provide specific details. + * + * @pre Must be implemented by subclasses. + * @post Transaction details are printed to the console. + */ + public abstract void printTransactionDetails(); - public abstract void apply(BankAccount ba); -} + + /** + * Abstract method to apply the transaction to a bank account. + * Subclasses must implement this method to define specific transaction behavior. + * + * @param ba - The BankAccount object to apply the transaction to. + * @throws InsufficientFundsException if the transaction cannot be applied due to insufficient funds. + * @pre Must be implemented by subclasses. BankAccount object must be valid. + * @post The transaction is applied to the bank account. + */ + public abstract void apply(BankAccount ba) throws InsufficientFundsException; +} \ No newline at end of file diff --git a/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java b/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java index 81afab5..1794d01 100644 --- a/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java +++ b/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java @@ -4,27 +4,105 @@ import java.util.Calendar; +/** + * DepositTransaction Class + * Description: Represents a deposit transaction in the banking system. + * It extends the BaseTransaction class to provide functionality for handling monetary deposits. + + * Key Features: + * - Tracks and applies deposit transactions to bank accounts. + * - Validates deposit amounts to ensure only positive values are processed. + * - Provides detailed transaction information. + + * Inherits: + * - BaseTransaction + * + * @errorHandling: + * - Prevents processing of invalid deposit amounts (zero or negative). + * - Provides feedback on successful or failed transactions. + * + * @author Bridget Wanjiru + + */ + public class DepositTrasaction extends BaseTransaction { + /** + * Constructor for DepositTransaction. + * Initializes a deposit transaction with a specified amount and date. + * + * @param amount - The amount to deposit (must be positive). + * @param date - The date of the transaction (must be a valid, non-null Calendar object). + * @throws IllegalArgumentException if amount is negative or date is null. + * @pre Amount must be a positive integer. Date must be a valid Calendar object. + * @post The transaction is initialized with the specified amount and date. + */ + public DepositTrasaction(int amount, @NotNull Calendar date){ super(amount, date); + } - private boolean checkDepositAmount(int amt){ - if (amt < 0){ - return false; - } - else{ - return true; - } + + /** + * Validates the deposit transaction amount. + * + * @param amt - The deposit amount to validate. + * @return boolean - Returns true if the amount is strictly greater than 0, false otherwise. + * @pre None. + * @post Returns true for valid deposit amounts and false for invalid amounts. + + * Example: + * - Input: 500 + * - Output: true + */ + + private boolean checkDepositAmount(double amt){ + return amt > 0; } - // Method to print a transaction receipt or details + /** + * Prints the details of the deposit transaction. + * + * @pre None. + * @post Prints transaction details to the console, including amount, date, and transaction ID. + + * Example: + * - Input: None + * - Output: + * Deposit Transaction Details: + * Amount: 500 + * Date: Mon Jan 01 12:00:00 EAT 2024 + * Transaction ID: TX-12345678 + */ + + @Override public void printTransactionDetails(){ - System.out.println("Deposit Trasaction: "+this.toString()); + + System.out.println("Deposit Transaction Details: "); + System.out.println("Amount: " + getAmount()); + System.out.println("Date: " + getDate().getTime()); + System.out.println("Transaction ID: " + getTransactionID()); } +/** + * Applies the deposit transaction to a specified bank account. + * + * @param ba - The BankAccount object to apply the deposit to. + * @pre The bank account must be valid, and the deposit amount must be positive. + * @post The bank account balance is increased if the deposit is valid. Otherwise, no changes are made. + * Example: + * - Input: BankAccount with balance 1000, deposit amount 500. + * - Output: + * Deposit of 500 applied.New Balance: 1500 + */ + @Override public void apply(BankAccount ba){ - double curr_balance = ba.getBalance(); - double new_balance = curr_balance + getAmount(); - ba.setBalance(new_balance); + if (checkDepositAmount(getAmount())) { + double curr_balance = ba.getBalance(); + double new_balance = curr_balance + getAmount(); + ba.setBalance(new_balance); + System.out.println("Deposit of " + getAmount() + " applied. New Balance: " + ba.getBalance()); + } else { + System.out.println("Invalid deposit amount: " + getAmount()); + } } -} +} \ No newline at end of file diff --git a/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java b/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java new file mode 100644 index 0000000..943cd60 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java @@ -0,0 +1,8 @@ +package Lecture4_interfaces_abstract_classes; + +public class InsufficientFundsException extends Exception { + // Constructor to pass the error message + public InsufficientFundsException(String message) { + super(message); + } +} diff --git a/src/Lecture4_interfaces_abstract_classes/Main.java b/src/Lecture4_interfaces_abstract_classes/Main.java new file mode 100644 index 0000000..d94ad8a --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/Main.java @@ -0,0 +1,73 @@ +package Lecture4_interfaces_abstract_classes; + +import java.util.Calendar; +/** + * Main Class + * Description: The main entry point of the program, demonstrating the functionality + * of the transaction system by creating a bank account and performing various financial transactions. + + * Demonstrates: + * - Creating a BankAccount with an initial balance. + * - Performing a deposit transaction. + * - Performing a withdrawal transaction. + * - Reversing a withdrawal transaction. + * + * @author Bridget Wanjiru + + */ + +public class Main { + + /** + * Main method + * Description: Executes the program logic and demonstrates the functionality of deposit and withdrawal + * transactions along with their effects on a BankAccount. + * + * @param args - Command-line arguments (not used in this program). + */ + public static void main(String[] args) { + // Create a BankAccount with an initial balance of 1000 + BankAccount account = new BankAccount(1000); + + // Create a DepositTransaction + Calendar depositDate = Calendar.getInstance(); + DepositTrasaction deposit = new DepositTrasaction(500, depositDate); + + /* + * Apply deposit to the account. + * @see DepositTrasaction#apply(BankAccount) + * @see DepositTrasaction#printTransactionDetails() + */ + + // Print deposit transaction details + deposit.printTransactionDetails(); + + // Apply deposit to the account + deposit.apply(account); + System.out.println("Updated Account Balance after Deposit: " + account.getBalance()); + + // Create a WithdrawalTransaction with an amount that exceeds the current balance (insufficient funds) + Calendar withdrawalDate = Calendar.getInstance(); + WithdrawalTransaction withdrawal = new WithdrawalTransaction(300, withdrawalDate); // Amount exceeds balance + + /* + * Perform withdrawal and reverse if possible. + * @throws InsufficientFundsException when withdrawal amount exceeds the account balance. + * @see WithdrawalTransaction#applyWithExceptionHandling(BankAccount) + * @see WithdrawalTransaction#reverse(BankAccount) + */ + + // Print withdrawal transaction details + withdrawal.printTransactionDetails(); + + // Use the applyWithExceptionHandling method for withdrawal + withdrawal.applyWithExceptionHandling(account); // This method already handles the exception internally + + // Check if the withdrawal was successful and reverse if necessary + if (withdrawal.reverse(account)) { + System.out.println("Account Balance after Reversal: " + account.getBalance()); + } else { + System.out.println("No successful withdrawal to reverse."); + } + } +} diff --git a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java index 5902713..9c22893 100644 --- a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java +++ b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java @@ -1,21 +1,60 @@ package Lecture4_interfaces_abstract_classes; import java.util.Calendar; -/** - * Interface for Transactions - * Any class that defines a transaction is expected to implement this Interface - */ + /** + * TransactionInterface + * Description: Defines the blueprint for transaction-related operations in a banking system. + * Classes implementing this interface must provide concrete implementations for retrieving + * transaction details like amount, date, and unique identifiers. + + * Key Features: + * - Ensures a consistent contract for all financial transaction types. + * - Promotes abstraction and modularity by defining core transaction operations. + * Core Operations: + * - Retrieve transaction amount, date, and unique identifier. + + * @author Bridget Wanjiru + +*/ public interface TransactionInterface { - // Method to get the transaction amount + /** + * Retrieves the monetary amount associated with the transaction. + * + * @return double - The transaction amount as a non-negative numeric value. + * @pre None. + * @post Returns the absolute value of the transaction. Does not modify the transaction state. + + * Example: + * - Input: None + * - Output: 500.0 (transaction amount in currency units) + */ double getAmount(); - // Method to get the transaction date - Calendar getDate(); + /** + * Retrieves the date of the transaction. - // Method to get a unique identifier for the transaction - String getTransactionID(); + * @return Calendar - A valid Calendar object representing the transaction date and time. + * @pre None. + * @post Returns a non-null Calendar instance. Does not modify the transaction state. + + * Example: + * - Input: None + * - Output: Calendar instance with the transaction timestamp. + */ + Calendar getDate(); -} + /* + * Retrieves a unique identifier for the transaction. + * + * @return String - A non-null, unique string identifier for the transaction. + * @pre None. + * @post Returns a consistent identifier that remains unique across the transaction's lifecycle. + * Example: + * - Input: None + * - Output: "TX-1 + */ + String getTransactionID(); +} \ No newline at end of file diff --git a/src/Lecture4_interfaces_abstract_classes/TransactionManager.java b/src/Lecture4_interfaces_abstract_classes/TransactionManager.java new file mode 100644 index 0000000..bb2b9b4 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/TransactionManager.java @@ -0,0 +1,33 @@ +package Lecture4_interfaces_abstract_classes; + +import java.util.Calendar; +/** + * TransactionManager Class + * Description: Provides utility methods for managing transactions, including the generation of unique transaction IDs. + + * Key Features: + * - Generates unique, shortened transaction IDs based on the current timestamp. + * + * @author Bridget Wanjiru + + */ +public class TransactionManager { + + /** + * Generates a unique transaction ID based on the current timestamp. + + * @return String - A unique transaction ID prefixed with "TX-" and shortened to 8 characters for clarity. + * @pre None. + * @post A unique transaction ID is returned, formatted with a "TX-" prefix. + + * Example: + * - Input: None + * - Output: TX-12345678 + */ + public static String generateTransactionId() { + Calendar calendar = Calendar.getInstance(); + long timestamp = calendar.getTimeInMillis(); // Get current time in milliseconds + String shortenedId = String.valueOf(timestamp).substring(0, 8); // Shorten to 8 characters + return "TX-" + shortenedId; // Prefix with "TX-" for clarity + } +} \ No newline at end of file diff --git a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java index face5b6..0174d78 100644 --- a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java +++ b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java @@ -3,43 +3,116 @@ import org.jetbrains.annotations.NotNull; import java.util.Calendar; +/** + * WithdrawalTransaction Class + * Description: Represents a withdrawal transaction in the banking system, extending the functionality of BaseTransaction. + * Provides methods to apply, reverse, and validate withdrawal transactions, with appropriate exception handling. + + * Inherits: + * - BaseTransaction + + * Key Features: + * - Applies withdrawal transactions with a transaction fee. + * - Allows reversal of successful withdrawals. + * - Handles insufficient funds scenarios. + * + * @author Bridget Wanjiru + + */ public class WithdrawalTransaction extends BaseTransaction { + private boolean isApplied = false; // Flag to track if the withdrawal was applied + + /** + * WithdrawalTransaction Class + * Description: Represents a withdrawal transaction in the banking system, extending the functionality of BaseTransaction. + * Provides methods to apply, reverse, and validate withdrawal transactions, with appropriate exception handling. + + * Inherits: + * - BaseTransaction + + * Key Features: + * - Applies withdrawal transactions with a transaction fee. + * - Allows reversal of successful withdrawals. + * - Handles insufficient funds scenarios. + * + * @author Bridget Wanjiru + + */ + public WithdrawalTransaction(int amount, @NotNull Calendar date) { super(amount, date); } - private boolean checkDepositAmount(int amt) { - if (amt < 0) { - return false; - } else { - return true; - } + /** + * Validates the transaction amount. + * + * @param amt - The transaction amount to validate. + * @return boolean - Returns true if the amount is greater than 0, false otherwise. + */ + + private boolean checkDepositAmount(double amt) { + return amt > 0; } - // Method to reverse the transaction - public boolean reverse() { - return true; - } // return true if reversal was successful + /** + * Applies the withdrawal transaction to a bank account, deducting a transaction fee. + * + * @param - The BankAccount object to apply the transaction to. + * @throws InsufficientFundsException if the account balance is insufficient for the withdrawal and fee. + * @pre The BankAccount object must be valid, and the withdrawal amount must be positive. + * @post The account balance is updated, and a confirmation message is printed to the console. + */ - // Method to print a transaction receipt or details + @Override public void printTransactionDetails() { - System.out.println("Deposit Trasaction: " + this.toString()); + System.out.println("Withdrawal Transaction Details:"); + System.out.println("Amount: " + getAmount()); + System.out.println("Date: " + getDate().getTime()); + System.out.println("Transaction ID: " + getTransactionID()); } - /* - Oportunity for assignment: implementing different form of withdrawal - */ - public void apply(BankAccount ba) { + // Apply withdrawal to bank account (with fee) + public void apply(BankAccount ba) throws InsufficientFundsException { double curr_balance = ba.getBalance(); - if (curr_balance > getAmount()) { - double new_balance = curr_balance - getAmount(); - ba.setBalance(new_balance); + double transactionFee = 2; // Example fee per withdrawal + + // Check if the account has enough funds, including the transaction fee + if (curr_balance >= getAmount() + transactionFee) { + ba.setBalance(curr_balance - getAmount() - transactionFee); // Deduct the withdrawal and fee + isApplied = true; // Mark withdrawal as successful + System.out.println("Withdrawal of " + getAmount() + " applied. Fee of " + transactionFee + " deducted. New Balance: " + ba.getBalance()); + } else { + throw new InsufficientFundsException("Insufficient funds for withdrawal."); } } - /* - Assignment 1 Q3: Write the Reverse method - a method unique to the WithdrawalTransaction Class + /** + * Attempts to apply the withdrawal transaction with exception handling. + * + * @param ba - The BankAccount object to apply the transaction to. + * @pre The BankAccount object must be valid. + * @post The account balance is updated if successful, or an error message is printed if unsuccessful. */ -} + public void applyWithExceptionHandling(BankAccount ba) { + try { + apply(ba); // Attempt to apply withdrawal + } catch (InsufficientFundsException e) { + System.out.println(e.getMessage()); + } finally { + System.out.println("Attempt to apply withdrawal completed."); + } + } + // Reverse the withdrawal (only if it was successfully applied) + public boolean reverse(BankAccount ba) { + if (isApplied) { // Only reverse if the withdrawal was applied + ba.setBalance(ba.getBalance() + getAmount()); // Add the withdrawn amount back to the balance + System.out.println("Withdrawal of " + getAmount() + " reversed. New Balance: " + ba.getBalance()); + return true; // Reversal successful + } else { + System.out.println("No successful withdrawal to reverse."); + return false; // No reversal + } + } +} 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