以下是一个复杂的 C++ 代码示例,用于实现一个简单的银行账户管理系统:
#include <iostream>
#include <vector>
#include <algorithm>
class Transaction {
private:
double amount;
std::string type;
public:
Transaction(double amount, std::string type) {
this->amount = amount;
this->type = type;
}
double getAmount() {
return amount;
}
std::string getType() {
return type;
}
};
class BankAccount {
private:
std::string accountNumber;
double balance;
std::vector<Transaction> transactions;
public:
BankAccount(std::string accountNumber) {
this->accountNumber = accountNumber;
balance = 0.0;
}
std::string getAccountNumber() {
return accountNumber;
}
double getBalance() {
return balance;
}
void deposit(double amount) {
balance += amount;
transactions.push_back(Transaction(amount, "Deposit"));
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
transactions.push_back(Transaction(amount, "Withdrawal"));
}
}
void printTransactions() {
for (const Transaction& t : transactions) {
std::cout << "Amount: " << t.getAmount() << std::endl;
std::cout << "Type: " << t.getType() << std::endl;
std::cout << "---------------------" << std::endl;
}
}
};
int main() {
BankAccount account("1234567890");
account.deposit(1000.0);
account.withdraw(500.0);
account.deposit(200.0);
std::cout << "Account Number: " << account.getAccountNumber() << std::endl;
std::cout << "Balance: " << account.getBalance() << std::endl;
std::cout << "Transactions: " << std::endl;
account.printTransactions();
return 0;
}
这个代码示例实现了一个简单的银行账户管理系统,包括账户类 BankAccount
和交易类 Transaction
。账户类具有账户号码、余额和交易记录等属性,并提供存款、取款和打印交易记录的方法。交易类具有金额和类型等属性。
在 main
函数中,示例创建了一个银行账户对象 account
,然后进行了一些存款和取款操作。最后,打印账户号码、余额和交易记录。