Bank transaction

Sure! Here's an example implementation of the banking system functionalities you described in C++:

Code:
c++
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;

class Transaction {
public:
    enum Type { DEPOSIT, WITHDRAWAL };

    Transaction(Type type, double amount) : type(type), amount(amount) {
        time_t now = time(0);
        tm* localTime = localtime(&now);
        date = asctime(localTime);
    }

    string getType() const {
        if (type == DEPOSIT) {
            return "Deposit";
        } else {
            return "Withdrawal";
        }
    }

    double getAmount() const {
        return amount;
    }

    string getDate() const {
        return date;
    }

private:
    Type type;
    double amount;
    string date;
};

class BankAccount {
public:
    BankAccount(const string& name, double balance = 0.0, const string& accountNumber = "") :
        name(name), balance(balance), accountNumber(accountNumber) {
        if (accountNumber == "") {
            // Generate random account number
            srand(time(0));
            int randomNumber = rand() % 10000 + 1000;
            this->accountNumber = "ACC" + to_string(randomNumber);
        }
    }

    string getAccountNumber() const {
        return accountNumber;
    }

    string getName() const {
        return name;
    }

    double getBalance() const {
        return balance;
    }

    void deposit(double amount) {
        balance += amount;
        Transaction transaction(Transaction::DEPOSIT, amount);
        transactionHistory.push_back(transaction);
    }

    void withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            Transaction transaction(Transaction::WITHDRAWAL, amount);
            transactionHistory.push_back(transaction);
        } else {
            cout << "Insufficient balance to withdraw " << amount << endl;
        }
    }

    void displayTransactionHistory() const {
        cout << "Transaction History for Account " << accountNumber << ":" << endl;
        for (const Transaction& transaction : transactionHistory) {
            cout << "Date: " << transaction.getDate();
            cout << "Type: " << transaction.getType();
            cout << "Amount: " << transaction.getAmount() << endl;
        }
    }

private:
    string name;
    double balance;
    string accountNumber;
    vector<Transaction> transactionHistory;
};

int main() {
    // Create a new bank account
    BankAccount account("John Doe", 1000.0, "ACC1234");

    // Deposit funds
    account.deposit(500.0);

    // Withdraw funds
    account.withdraw(200.0);

    // Inquire balance
    cout << "Account Balance: " << account.getBalance() << endl;

    // Display transaction history
    account.displayTransactionHistory();

    // Close account
    account = BankAccount("Jane Smith", 500.0, "ACC5678");

    return 0;
}

This code defines two classes: Transaction and BankAccount. The Transaction class represents a single transaction with its type, amount, and date. The BankAccount class represents a bank account with its owner's name, balance, account number, and transaction history.

In the main function, a sample scenario is demonstrated. A bank account is created with an initial balance and account number. Funds are then deposited and withdrawn from the account. The account balance is inquired, and the transaction history is displayed. Finally, a new account is created by closing the previous account.

Please note that this is a basic implementation for understanding the concepts. In a real-world scenario, you would need to handle input validations, error handling, persistence (e.g., storing account information in a database or file), and more.
 

About this Thread

  • 3
    Replies
  • 626
    Views
  • 2
    Participants
Last reply from:
Well1AmNichtDiePerson

Trending Topics

Online now

Members online
317
Guests online
1,081
Total visitors
1,398

Forum statistics

Threads
2,273,579
Posts
28,950,444
Members
1,235,838
Latest member
Bogs0811
Back
Top