mico manuel
Established
close
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;
}
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.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.