C++ 银行账户管理系统:实现资金往来账目管理
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
// 交易记录类
class Transaction {
public:
string date;
double amount;
string type;
Transaction(string d, double a, string t) {
date = d;
amount = a;
type = t;
}
};
// 账户类
class Account {
private:
string accountNumber;
string createDate;
double balance;
vector<Transaction> transactions;
public:
Account(string number, string date, double initialBalance) {
accountNumber = number;
createDate = date;
balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
time_t now = time(0);
tm* ltm = localtime(&now);
string date = to_string(1900 + ltm->tm_year) + '-' + to_string(1 + ltm->tm_mon) + '-' + to_string(ltm->tm_mday);
Transaction transaction(date, amount, 'Deposit');
transactions.push_back(transaction);
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
time_t now = time(0);
tm* ltm = localtime(&now);
string date = to_string(1900 + ltm->tm_year) + '-' + to_string(1 + ltm->tm_mon) + '-' + to_string(ltm->tm_mday);
Transaction transaction(date, amount, 'Withdraw');
transactions.push_back(transaction);
} else {
cout << 'Insufficient balance.' << endl;
}
}
void displayTransactions() {
cout << 'Account Number: ' << accountNumber << endl;
cout << 'Create Date: ' << createDate << endl;
cout << 'Balance: ' << balance << endl;
cout << 'Transaction History: ' << endl;
time_t now = time(0);
tm* ltm = localtime(&now);
string currentDate = to_string(1900 + ltm->tm_year) + '-' + to_string(1 + ltm->tm_mon) + '-' + to_string(ltm->tm_mday);
for (int i = 0; i < transactions.size(); i++) {
if (transactions[i].date >= currentDate) {
cout << transactions[i].date << ' - ' << transactions[i].type << ': ' << transactions[i].amount << endl;
}
}
}
};
int main() {
Account account('123456789', '2022-01-01', 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
account.displayTransactions();
return 0;
}
该代码使用 C++ 语言实现了一个简单的银行账户管理系统。它包含以下功能:
- 创建账户:通过
Account类创建账户,并指定账号、创建日期和初始余额。 - 存钱:使用
deposit()方法存钱,记录存钱日期和金额。 - 取钱:使用
withdraw()方法取钱,记录取钱日期和金额。 - 查询交易明细:使用
displayTransactions()方法查询近一个月的交易记录。
在 main() 函数中,创建了一个账户并进行了一些简单的操作,最后展示了近一个月的交易记录。
该示例代码仅供参考,实际的银行账户管理系统需要更加复杂的功能和安全性。
原文地址: https://www.cveoy.top/t/topic/f3Zz 著作权归作者所有。请勿转载和采集!