mico manuel
Addict
- Joined
- Aug 5, 2015
- Posts
- 171
- Reaction
- 104
- Points
- 116
- Age
- 34
close
Last edited:
c++
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
class Transaction {
public:
enum Type { DEPOSIT, wí†hdráwAL };
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 "wí†hdráwal";
}
}
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 wí†hdráw(double amount) {
if (balance >= amount) {
balance -= amount;
Transaction transaction(Transaction::wí†hdráwAL, amount);
transactionHistory.push_back(transaction);
} else {
cout << "Insufficient balance to wí†hdráw " << 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);
// wí†hdráw funds
account.wí†hdráw(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 wí†hdráwn 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.