How to handle account transactions?

QuestionsQuestions4 SkillsProOmit Object KeysAug, 04 2025
0293

To handle account transactions in a banking application, you can create methods for various types of transactions such as deposits, withdrawals, and viewing transaction history. Below is an example implementation in JavaScript that includes these functionalities:

class BankAccount {
    constructor(firstName, lastName, initialDeposit) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.balance = initialDeposit;
        this.accountNumber = this.generateUniqueNumber();
        this.createdate = new Date();
        this.transactions = [];
        
        // Record the initial deposit
        this.recordTransaction('Deposit', initialDeposit);
    }

    generateUniqueNumber() {
        let uniqueNumber = ''; 
        for (let i = 0; i < 10; i++) {
            let randomDigit = (new Date().getTime() % 10 + i) % 10; 
            uniqueNumber += randomDigit;
        }
        return uniqueNumber;
    }

    deposit(amount) {
        if (amount > 0) {
            this.balance += amount;
            this.recordTransaction('Deposit', amount);
            console.log(`Successfully deposited $${amount.toFixed(2)}. New balance: $${this.balance.toFixed(2)}`);
        } else {
            console.log("Deposit amount must be positive.");
        }
    }

    withdraw(amount) {
        if (amount > 0 && amount <= this.balance) {
            this.balance -= amount;
            this.recordTransaction('Withdrawal', amount);
            console.log(`Successfully withdrew $${amount.toFixed(2)}. New balance: $${this.balance.toFixed(2)}`);
        } else if (amount > this.balance) {
            console.log("Insufficient funds for this withdrawal.");
        } else {
            console.log("Withdrawal amount must be positive.");
        }
    }

    recordTransaction(type, amount) {
        const transaction = {
            type: type,
            amount: amount,
            date: new Date()
        };
        this.transactions.push(transaction);
    }

    getBalance() {
        return this.balance;
    }

    getTransactions() {
        return this.transactions;
    }
}

// Example usage:
const account = new BankAccount("John", "Doe", 100);
account.deposit(50);
account.withdraw(30);
account.withdraw(150); // Insufficient funds
console.log("Current Balance:", account.getBalance());
console.log("Transaction History:", account.getTransactions());

Explanation:

  1. Class Structure: The BankAccount class encapsulates all account-related functionalities.
  2. Constructor: Initializes the account with the owner's name, initial deposit, and generates a unique account number.
  3. Transaction Methods:
    • deposit(amount): Adds the specified amount to the balance and records the transaction.
    • withdraw(amount): Subtracts the specified amount from the balance if sufficient funds are available and records the transaction.
  4. Transaction Recording: The recordTransaction(type, amount) method logs each transaction with its type (Deposit or Withdrawal) and the date.
  5. Balance and Transaction History: Methods to retrieve the current balance and transaction history.

Example Output:

When you run the code, it will create an account, perform deposits and withdrawals, and log the current balance and transaction history to the console.

0 Comments

no data
Be the first to share your comment!