定义一个Account类来表示每个账户。Account类可以包含账号、存款余额、交易记录等信息,可以定义存款、取款、查询交易记录等操作。Bank类可以包含多个Account对象,可以定义添加、删除账户等操作,也可以增加其它操作如修改密码等。这样设计可以方便地管理银行账户信息和进行相关操作。
时间: 2023-04-05 20:03:20 浏览: 121
非常好的问题!Account类可以这样定义:
```
class Account {
private:
string accountNumber; // 账号
double balance; // 存款余额
vector<string> transactionHistory; // 交易记录
public:
Account(string accountNumber, double balance) {
this->accountNumber = accountNumber;
this->balance = balance;
}
void deposit(double amount) {
balance += amount;
transactionHistory.push_back("Deposit: +" + to_string(amount));
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
transactionHistory.push_back("Withdraw: -" + to_string(amount));
} else {
cout << "Insufficient balance!" << endl;
}
}
void printTransactionHistory() {
for (string transaction : transactionHistory) {
cout << transaction << endl;
}
}
double getBalance() {
return balance;
}
};
```
Bank类可以这样定义:
```
class Bank {
private:
vector<Account> accounts;
public:
void addAccount(Account account) {
accounts.push_back(account);
}
void removeAccount(Account account) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getAccountNumber() == account.getAccountNumber()) {
accounts.erase(accounts.begin() + i);
break;
}
}
}
void modifyPassword(Account account, string newPassword) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getAccountNumber() == account.getAccountNumber()) {
accounts[i].setPassword(newPassword);
break;
}
}
}
void printAllAccounts() {
for (Account account : accounts) {
cout << "Account number: " << account.getAccountNumber() << endl;
cout << "Balance: " << account.getBalance() << endl;
account.printTransactionHistory();
cout << endl;
}
}
};
```
这样设计可以方便地管理银行账户信息和进行相关操作。