From 498208d4a56ec69f3e82c8e9678509163395be3a Mon Sep 17 00:00:00 2001 From: Bridget Wanjiru Date: Wed, 4 Dec 2024 10:33:56 +0300 Subject: [PATCH 1/2] Assignment 1 code --- .../BankAccount.java | 36 +++- .../BaseTransaction.java | 63 +++++-- .../DepositTrasaction.java | 96 +++++++++-- .../InsufficientFundsException.java | 8 + .../Main.java | 48 ++++++ .../TransactionInterface.java | 44 ++++- .../TransactionManager.java | 27 +++ .../WithdrawalTransaction.java | 119 ++++++++++--- src/Main.java | 156 ------------------ 9 files changed, 387 insertions(+), 210 deletions(-) create mode 100644 src/Lecture4_interfaces_abstract_classes/InsufficientFundsException.java create mode 100644 src/Lecture4_interfaces_abstract_classes/Main.java create mode 100644 src/Lecture4_interfaces_abstract_classes/TransactionManager.java delete mode 100644 src/Main.java diff --git a/src/Lecture4_interfaces_abstract_classes/BankAccount.java b/src/Lecture4_interfaces_abstract_classes/BankAccount.java index 28d0d07..a16b50a 100644 --- a/src/Lecture4_interfaces_abstract_classes/BankAccount.java +++ b/src/Lecture4_interfaces_abstract_classes/BankAccount.java @@ -1,5 +1,39 @@ + package Lecture4_interfaces_abstract_classes; +/** + * ADT Specification for BankAccount + + * Type of Data: + * - Represents a bank account with a monetary balance + * - Balance is stored as a double-precision floating-point number + + * Operations: + * 1. Constructor: + * - Create a new bank account with an initial balance + * - Precondition: Initial balance must be non-negative + * - Postcondition: Account is created with specified initial balance + + * 2. getBalance(): + * - Retrieves the current account balance + * - Precondition: None + * - Postcondition: Returns the current balance without modifying it + + * 3. setBalance(): + * - Updates the account balance + * - Precondition: New balance must be non-negative + * - Postcondition: Account balance is set to the specified value + + * Additional Recommended Operations (not implemented): + * 4. deposit(double amount): + * - Adds money to the account + * - Precondition: Deposit amount must be positive + * - Postcondition: Account balance increases by the deposited amount + * 5. withdraw(double amount): + * - Removes money from the account + * - Precondition: Withdrawal amount must be positive and not exceed current balance + * - Postcondition: Account balance decreases by the withdrawn amount + */ public class BankAccount { private double balance; public BankAccount(double balance) { @@ -13,4 +47,4 @@ public double getBalance() { 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..2eb6802 100644 --- a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java +++ b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java @@ -3,25 +3,59 @@ import org.jetbrains.annotations.NotNull; import java.util.Calendar; +/** + * ADT Specification for BaseTransaction + + * Core Operations: + * getAmount() + * Description: Retrieves the amount associated with the transaction. + * Input: None. + * Output: double — the transaction amount. + * Precondition: None. + * Postcondition: The transaction amount is returned. + + * getDate() + * Description: Retrieves the date of the transaction. + * Input: None. + * Output: Calendar — the date of the transaction (a defensive copy). + * Precondition: None. + * Postcondition: A copy of the transaction date is returned. + + * getTransactionID() + * Description: Retrieves the unique identifier for the transaction. + * Input: None. + * Output: String — the transaction ID. + * Precondition: None. + * Postcondition: The transaction ID is returned. + + * printTransactionDetails() (Abstract) + * Description: Prints details of the transaction. + * Input: None. + * Output: None. + * Precondition: Must be implemented by a concrete subclass. + * Postcondition: Transaction details are printed. + + * apply(BankAccount ba) (Abstract) + * Description: Applies the transaction to a given bank account. + * Input: BankAccount ba — the bank account to which the transaction is applied. + * Output: None. + * Precondition: Must be implemented by a concrete subclass. + * Postcondition: The transaction is applied to the bank account. + **/ public abstract class BaseTransaction implements TransactionInterface { private final int amount; private final Calendar date; - private final String transactionID; + private 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(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 } /** @@ -43,9 +77,10 @@ public Calendar getDate() { // Method to get a unique identifier for the transaction public String getTransactionID(){ + return transactionID; } // Method to print a transaction receipt or details public abstract void printTransactionDetails(); - public abstract void apply(BankAccount ba); -} + 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..f16f413 100644 --- a/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java +++ b/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java @@ -3,28 +3,100 @@ import org.jetbrains.annotations.NotNull; import java.util.Calendar; +/** + * ADT Specification for DepositTransaction + + * Type of Data: + * - Represents a deposit transaction in a banking system + * - Extends BaseTransaction + * - Tracks and applies monetary deposits to a bank account + + * Core Operations: + * 1. Constructor: + * - Creates a new deposit transaction + * - Preconditions: + * * Amount must be a positive integer + * * Date must be a valid, non-null Calendar object + * - Postconditions: + * * Transaction is initialized with specified amount and date + * * Inherited BaseTransaction properties are set + + * 2. checkDepositAmount(double amt): + * - Validates the deposit transaction amount + * - Preconditions: None + * - Postconditions: + * * Returns true if amount is strictly greater than 0 + * * Returns false for zero or negative amounts + * - Validation Criteria: + * * Ensures only positive deposit amounts are considered valid + * * Prevents processing of zero or negative deposits + + * 3. printTransactionDetails(): + * - Displays comprehensive information about the deposit transaction + * - Preconditions: None + * - Postconditions: + * * Prints transaction details to console + * * Includes amount, date, and transaction ID + * * No changes made to transaction or account state + + * 4. apply(BankAccount ba): + * - Applies the deposit to a specified bank account + * - Preconditions: + * * Bank account must be a valid BankAccount object + * * Deposit amount must be positive + * - Postconditions: + * * If deposit is valid: + * - Bank account balance is increased by deposit amount + * - Confirmation message printed to console + * * If deposit is invalid: + * - No changes made to account balance + * - Error message printed to console + * - Transaction Processing: + * * Validates deposit amount before processing + * * Calculates and updates new account balance + * * Provides feedback on transaction outcome + + + * Error Handling: + * - Rejects zero or negative deposit amounts + * - Prevents invalid deposit transactions + * - Provides clear feedback on transaction status + */ 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 check if the deposit amount is valid + //The previous method in the original code included 0 as a valid deposit amount which is not usually the case in bank transactions + //The method below modifies the validity of the transaction using a single code amt>0 + private boolean checkDepositAmount(double amt){ + return amt > 0; } // Method to print a transaction receipt or details + // The changes made here from the initial toString() method is to improve readability of the output + @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()); } + //Method to apply the deposit transaction to a bank account + @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..d0ec2f7 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/Main.java @@ -0,0 +1,48 @@ +package Lecture4_interfaces_abstract_classes; + +import java.util.Calendar; +/** + * ADT Specification for MainClass + * Description: The main method executes the program logic. + * Demonstrates: + * - Creating a BankAccount with an initial balance. + * - Performing a deposit transaction. + * - Performing a withdrawal transaction. + * - Reversing a withdrawal transaction. + */ + +public class Main { + + public static void main(String[] args) { + // Create a BankAccount with an initial balance of 100 + BankAccount account = new BankAccount(1000); + + // Create a DepositTransaction + Calendar depositDate = Calendar.getInstance(); + DepositTrasaction deposit = new DepositTrasaction(500, depositDate); + + // 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 + + // 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..109b4c2 100644 --- a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java +++ b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java @@ -1,6 +1,46 @@ package Lecture4_interfaces_abstract_classes; import java.util.Calendar; +/** + * ADT Specification for TransactionInterface + + * Type of Data: + * - Defines a contract for transaction-related operations in a banking system + * - Serves as a blueprint for implementing different types of financial transactions + + * Core Operations: + * 1. getAmount(): + * - Retrieves the monetary amount associated with the transaction + * - Preconditions: None + * - Postconditions: + * * Returns the transaction amount as a double-precision floating-point number + * * Does not modify the transaction state + * - Expected Behavior: + * * Must return a non-negative numeric value + * * Represents the absolute value of the transaction + + * 2. getDate(): + * - Retrieves the date of the transaction + * - Preconditions: None + * - Postconditions: + * * Returns a Calendar object representing the transaction date + * * Does not modify the transaction state + * - Expected Behavior: + * * Must return a valid, non-null Calendar instance + * * Represents the exact timestamp of the transaction + + * 3. getTransactionID(): + * - Retrieves a unique identifier for the transaction + * - Preconditions: None + * - Postconditions: + * * Returns a unique string identifier + * * Does not modify the transaction state + * - Expected Behavior: + * * Must return a non-null, unique identifier + * * Identifier should be consistent throughout the transaction lifecycle + + */ + /** * Interface for Transactions * Any class that defines a transaction is expected to implement this Interface @@ -16,6 +56,4 @@ public interface TransactionInterface { // Method to get a unique identifier for the transaction 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..32a0917 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/TransactionManager.java @@ -0,0 +1,27 @@ +package Lecture4_interfaces_abstract_classes; + +import java.util.Calendar; +/** + * ADT Specification for TransactionManager + * Purpose: Provides utility methods related to transaction management, such as generating unique + * transaction IDs based on the current timestamp. + + * Operations: + * generateTransactionId() + * Description: Generates a unique shortened transaction ID based on the current time in milliseconds. + * Input: None. + * Output: String — a shortened transaction ID with a "TX-" prefix. + * Precondition: None. + * Postcondition: A unique transaction ID is returned, formatted with a "TX-" prefix. + */ + +public class TransactionManager { + + // Method to generate a unique shortened transaction ID from the current time + 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..37e6986 100644 --- a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java +++ b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java @@ -3,43 +3,114 @@ import org.jetbrains.annotations.NotNull; import java.util.Calendar; +/** + * ADT Specification for WithdrawalTransaction + * + * Type of Data: + * - Represents a withdrawal transaction in a banking system. + * - Extends BaseTransaction. + * - Tracks transaction details including amount, date, and transaction ID. + * + * Core Operations: + * 1. Constructor: + * - Creates a new withdrawal transaction. + * - Preconditions: + * * Amount must be a positive integer. + * * Date must be a valid Calendar object. + * - Postconditions: + * * Transaction is initialized with specified amount and date. + * + * 2. apply(BankAccount ba): + * - Applies the fee based withdrawal to a bank account. + * - Preconditions: + * * Bank account must be a valid BankAccount object. + * * Withdrawal amount must be positive. + * - Postconditions: + * * Bank account balance is updated based on withdrawal scenario. + * * Appropriate message is printed to console. + * 3. reverse(BankAccount ba): + * - Reverses a previously applied withdrawal transaction. + * - Preconditions: + * * Bank account must be a valid BankAccount object. + * * Transaction must have been previously applied. + * - Postconditions: + * * Bank account balance is restored to previous state. + * * Reversal message is printed to console. + * * Returns true to indicate successful reversal. + + * 4. printTransactionDetails(): + * - Prints detailed information about the transaction. + * - Preconditions: None. + * - Postconditions: + * * Transaction details printed to console. + * * No changes made to transaction or account state. + + * 5. checkDepositAmount(double amt): + * - Validates the transaction amount. + * - Preconditions: None. + * - Postconditions: + * * Returns true if amount is greater than 0. + * * Returns false otherwise. + + * Error Handling: + * - Handles insufficient funds scenarios. + * - Provides fee-based withdrawal options. + */ public class WithdrawalTransaction extends BaseTransaction { + private boolean isApplied = false; // Flag to track if the withdrawal was applied + public WithdrawalTransaction(int amount, @NotNull Calendar date) { super(amount, date); } - private boolean checkDepositAmount(int amt) { - if (amt < 0) { - return false; - } else { - return true; - } + private boolean checkDepositAmount(double amt) { + return amt > 0; } - // Method to reverse the transaction - public boolean reverse() { - return true; - } // return true if reversal was successful - - // 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 - */ -} + // Apply withdrawal with exception handling + 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 From 74014cdc57fcd4069de3eb53ec79ebd1afe20cd8 Mon Sep 17 00:00:00 2001 From: Bridget Wanjiru Date: Thu, 2 Jan 2025 22:02:18 +0300 Subject: [PATCH 2/2] Moved the specifications to the right places --- .idea/inspectionProfiles/Project_Default.xml | 11 ++ .../BankAccount.java | 83 ++++++----- .../BaseTransaction.java | 134 +++++++++++------- .../DepositTrasaction.java | 134 +++++++++--------- .../Main.java | 31 +++- .../TransactionInterface.java | 95 +++++++------ .../TransactionManager.java | 30 ++-- .../WithdrawalTransaction.java | 106 +++++++------- 8 files changed, 366 insertions(+), 258 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml 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 a16b50a..a56ed64 100644 --- a/src/Lecture4_interfaces_abstract_classes/BankAccount.java +++ b/src/Lecture4_interfaces_abstract_classes/BankAccount.java @@ -1,49 +1,68 @@ package Lecture4_interfaces_abstract_classes; /** - * ADT Specification for BankAccount - - * Type of Data: - * - Represents a bank account with a monetary balance - * - Balance is stored as a double-precision floating-point number - - * Operations: - * 1. Constructor: - * - Create a new bank account with an initial balance - * - Precondition: Initial balance must be non-negative - * - Postcondition: Account is created with specified initial balance - - * 2. getBalance(): - * - Retrieves the current account balance - * - Precondition: None - * - Postcondition: Returns the current balance without modifying it - - * 3. setBalance(): - * - Updates the account balance - * - Precondition: New balance must be non-negative - * - Postcondition: Account balance is set to the specified value - - * Additional Recommended Operations (not implemented): - * 4. deposit(double amount): - * - Adds money to the account - * - Precondition: Deposit amount must be positive - * - Postcondition: Account balance increases by the deposited amount - - * 5. withdraw(double amount): - * - Removes money from the account - * - Precondition: Withdrawal amount must be positive and not exceed current balance - * - Postcondition: Account balance decreases by the withdrawn amount + * 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; } diff --git a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java index 2eb6802..ce8b783 100644 --- a/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java +++ b/src/Lecture4_interfaces_abstract_classes/BaseTransaction.java @@ -4,54 +4,49 @@ import java.util.Calendar; /** - * ADT Specification for BaseTransaction - - * Core Operations: - * getAmount() - * Description: Retrieves the amount associated with the transaction. - * Input: None. - * Output: double — the transaction amount. - * Precondition: None. - * Postcondition: The transaction amount is returned. - - * getDate() - * Description: Retrieves the date of the transaction. - * Input: None. - * Output: Calendar — the date of the transaction (a defensive copy). - * Precondition: None. - * Postcondition: A copy of the transaction date is returned. - - * getTransactionID() - * Description: Retrieves the unique identifier for the transaction. - * Input: None. - * Output: String — the transaction ID. - * Precondition: None. - * Postcondition: The transaction ID is returned. - - * printTransactionDetails() (Abstract) - * Description: Prints details of the transaction. - * Input: None. - * Output: None. - * Precondition: Must be implemented by a concrete subclass. - * Postcondition: Transaction details are printed. - - * apply(BankAccount ba) (Abstract) - * Description: Applies the transaction to a given bank account. - * Input: BankAccount ba — the bank account to which the transaction is applied. - * Output: None. - * Precondition: Must be implemented by a concrete subclass. - * Postcondition: The transaction is applied to the bank account. - **/ + * 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; private final Calendar date; - private String transactionID; + private final String transactionID; + /** + * 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) { this.amount = amount; this.date = (Calendar) date.clone(); + // 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 @@ -59,28 +54,71 @@ public BaseTransaction(int amount, @NotNull Calendar date) { } /** - * 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 - public String getTransactionID(){ + /* + * 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(); + + /** + * 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 f16f413..1794d01 100644 --- a/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java +++ b/src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java @@ -3,81 +3,77 @@ import org.jetbrains.annotations.NotNull; import java.util.Calendar; + /** - * ADT Specification for DepositTransaction - - * Type of Data: - * - Represents a deposit transaction in a banking system - * - Extends BaseTransaction - * - Tracks and applies monetary deposits to a bank account - - * Core Operations: - * 1. Constructor: - * - Creates a new deposit transaction - * - Preconditions: - * * Amount must be a positive integer - * * Date must be a valid, non-null Calendar object - * - Postconditions: - * * Transaction is initialized with specified amount and date - * * Inherited BaseTransaction properties are set - - * 2. checkDepositAmount(double amt): - * - Validates the deposit transaction amount - * - Preconditions: None - * - Postconditions: - * * Returns true if amount is strictly greater than 0 - * * Returns false for zero or negative amounts - * - Validation Criteria: - * * Ensures only positive deposit amounts are considered valid - * * Prevents processing of zero or negative deposits - - * 3. printTransactionDetails(): - * - Displays comprehensive information about the deposit transaction - * - Preconditions: None - * - Postconditions: - * * Prints transaction details to console - * * Includes amount, date, and transaction ID - * * No changes made to transaction or account state - - * 4. apply(BankAccount ba): - * - Applies the deposit to a specified bank account - * - Preconditions: - * * Bank account must be a valid BankAccount object - * * Deposit amount must be positive - * - Postconditions: - * * If deposit is valid: - * - Bank account balance is increased by deposit amount - * - Confirmation message printed to console - * * If deposit is invalid: - * - No changes made to account balance - * - Error message printed to console - * - Transaction Processing: - * * Validates deposit amount before processing - * * Calculates and updates new account balance - * * Provides feedback on transaction outcome - - - * Error Handling: - * - Rejects zero or negative deposit amounts - * - Prevents invalid deposit transactions - * - Provides clear feedback on transaction status - */ + * 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); } - // Method to check if the deposit amount is valid - //The previous method in the original code included 0 as a valid deposit amount which is not usually the case in bank transactions - //The method below modifies the validity of the transaction using a single code amt>0 + /** + * 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 - // The changes made here from the initial toString() method is to improve readability of the output + /** + * 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(){ @@ -86,8 +82,18 @@ public void printTransactionDetails(){ 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. - //Method to apply the deposit transaction to a bank account + * Example: + * - Input: BankAccount with balance 1000, deposit amount 500. + * - Output: + * Deposit of 500 applied.New Balance: 1500 + */ @Override public void apply(BankAccount ba){ if (checkDepositAmount(getAmount())) { diff --git a/src/Lecture4_interfaces_abstract_classes/Main.java b/src/Lecture4_interfaces_abstract_classes/Main.java index d0ec2f7..d94ad8a 100644 --- a/src/Lecture4_interfaces_abstract_classes/Main.java +++ b/src/Lecture4_interfaces_abstract_classes/Main.java @@ -2,25 +2,43 @@ import java.util.Calendar; /** - * ADT Specification for MainClass - * Description: The main method executes the program logic. + * 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 100 + // 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(); @@ -32,6 +50,13 @@ public static void main(String[] args) { 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(); diff --git a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java index 109b4c2..9c22893 100644 --- a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java +++ b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java @@ -1,59 +1,60 @@ package Lecture4_interfaces_abstract_classes; import java.util.Calendar; -/** - * ADT Specification for TransactionInterface - - * Type of Data: - * - Defines a contract for transaction-related operations in a banking system - * - Serves as a blueprint for implementing different types of financial transactions - - * Core Operations: - * 1. getAmount(): - * - Retrieves the monetary amount associated with the transaction - * - Preconditions: None - * - Postconditions: - * * Returns the transaction amount as a double-precision floating-point number - * * Does not modify the transaction state - * - Expected Behavior: - * * Must return a non-negative numeric value - * * Represents the absolute value of the transaction - - * 2. getDate(): - * - Retrieves the date of the transaction - * - Preconditions: None - * - Postconditions: - * * Returns a Calendar object representing the transaction date - * * Does not modify the transaction state - * - Expected Behavior: - * * Must return a valid, non-null Calendar instance - * * Represents the exact timestamp of the transaction - - * 3. getTransactionID(): - * - Retrieves a unique identifier for the transaction - * - Preconditions: None - * - Postconditions: - * * Returns a unique string identifier - * * Does not modify the transaction state - * - Expected Behavior: - * * Must return a non-null, unique identifier - * * Identifier should be consistent throughout the transaction lifecycle - - */ - -/** - * 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 + /** + * Retrieves the date of the transaction. + + * @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(); - // Method to get a unique identifier for the transaction + /* + * 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 index 32a0917..bb2b9b4 100644 --- a/src/Lecture4_interfaces_abstract_classes/TransactionManager.java +++ b/src/Lecture4_interfaces_abstract_classes/TransactionManager.java @@ -2,22 +2,28 @@ import java.util.Calendar; /** - * ADT Specification for TransactionManager - * Purpose: Provides utility methods related to transaction management, such as generating unique - * transaction IDs based on the current timestamp. + * TransactionManager Class + * Description: Provides utility methods for managing transactions, including the generation of unique transaction IDs. - * Operations: - * generateTransactionId() - * Description: Generates a unique shortened transaction ID based on the current time in milliseconds. - * Input: None. - * Output: String — a shortened transaction ID with a "TX-" prefix. - * Precondition: None. - * Postcondition: A unique transaction ID is returned, formatted with a "TX-" prefix. - */ + * Key Features: + * - Generates unique, shortened transaction IDs based on the current timestamp. + * + * @author Bridget Wanjiru + */ public class TransactionManager { - // Method to generate a unique shortened transaction ID from the current time + /** + * 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 diff --git a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java index 37e6986..0174d78 100644 --- a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java +++ b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java @@ -4,70 +4,66 @@ import java.util.Calendar; /** - * ADT Specification for WithdrawalTransaction - * - * Type of Data: - * - Represents a withdrawal transaction in a banking system. - * - Extends BaseTransaction. - * - Tracks transaction details including amount, date, and transaction ID. - * - * Core Operations: - * 1. Constructor: - * - Creates a new withdrawal transaction. - * - Preconditions: - * * Amount must be a positive integer. - * * Date must be a valid Calendar object. - * - Postconditions: - * * Transaction is initialized with specified amount and date. - * - * 2. apply(BankAccount ba): - * - Applies the fee based withdrawal to a bank account. - * - Preconditions: - * * Bank account must be a valid BankAccount object. - * * Withdrawal amount must be positive. - * - Postconditions: - * * Bank account balance is updated based on withdrawal scenario. - * * Appropriate message is printed to console. - - * 3. reverse(BankAccount ba): - * - Reverses a previously applied withdrawal transaction. - * - Preconditions: - * * Bank account must be a valid BankAccount object. - * * Transaction must have been previously applied. - * - Postconditions: - * * Bank account balance is restored to previous state. - * * Reversal message is printed to console. - * * Returns true to indicate successful reversal. - - * 4. printTransactionDetails(): - * - Prints detailed information about the transaction. - * - Preconditions: None. - * - Postconditions: - * * Transaction details printed to console. - * * No changes made to transaction or account state. - - * 5. checkDepositAmount(double amt): - * - Validates the transaction amount. - * - Preconditions: None. - * - Postconditions: - * * Returns true if amount is greater than 0. - * * Returns false otherwise. - - * Error Handling: + * 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. - * - Provides fee-based withdrawal options. + * + * @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); } + /** + * 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; } + /** + * 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. + */ + @Override public void printTransactionDetails() { System.out.println("Withdrawal Transaction Details:"); @@ -91,7 +87,13 @@ public void apply(BankAccount ba) throws InsufficientFundsException { } } - // Apply withdrawal with exception handling + /** + * 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