以现实中银行卡为原型,进行需求调查,抽象出相应的类,设计类的成员函数和数据 成员,完成银行账户管理系统。编写程序,帮助工作人员有效、准确并且高校的完成存取事 件。(系统主要完成开户管理,存取钱管理,修改密码,注销等功能)C++
时间: 2024-06-09 07:08:46 浏览: 144
以下是银行账户管理系统的设计:
1. Account类
- 数据成员:账户号(accountNumber)、账户余额(balance)、账户密码(password)
- 成员函数:构造函数(Account)、获取账户号(getAccountNumber)、获取账户余额(getBalance)、修改账户余额(modifyBalance)、获取账户密码(getPassword)、修改账户密码(modifyPassword)
2. Bank类
- 数据成员:账户列表(accountsList)
- 成员函数:开户(createAccount)、存款(deposit)、取款(withdraw)、修改密码(modifyPassword)、注销账户(closeAccount)
下面是C++代码实现:
```
#include <iostream>
#include <vector>
using namespace std;
class Account {
private:
string accountNumber;
double balance;
string password;
public:
Account(string number, double initialBalance, string pass) {
accountNumber = number;
balance = initialBalance;
password = pass;
}
string getAccountNumber() {
return accountNumber;
}
double getBalance() {
return balance;
}
void modifyBalance(double amount) {
balance += amount;
}
string getPassword() {
return password;
}
void modifyPassword(string pass) {
password = pass;
}
};
class Bank {
private:
vector<Account> accountsList;
public:
void createAccount(string accountNumber, double initialBalance, string password) {
Account newAccount(accountNumber, initialBalance, password);
accountsList.push_back(newAccount);
cout << "开户成功!账户号:" << accountNumber << endl;
}
void deposit(string accountNumber, double amount) {
for (int i = 0; i < accountsList.size(); i++) {
if (accountsList[i].getAccountNumber() == accountNumber) {
accountsList[i].modifyBalance(amount);
cout << "存款成功!账户号:" << accountNumber << ",存入金额:" << amount << ",余额:" << accountsList[i].getBalance() << endl;
return;
}
}
cout << "账户不存在!" << endl;
}
void withdraw(string accountNumber, double amount) {
for (int i = 0; i < accountsList.size(); i++) {
if (accountsList[i].getAccountNumber() == accountNumber) {
if (accountsList[i].getBalance() < amount) {
cout << "余额不足!" << endl;
return;
}
accountsList[i].modifyBalance(-amount);
cout << "取款成功!账户号:" << accountNumber << ",取出金额:" << amount << ",余额:" << accountsList[i].getBalance() << endl;
return;
}
}
cout << "账户不存在!" << endl;
}
void modifyPassword(string accountNumber, string password) {
for (int i = 0; i < accountsList.size(); i++) {
if (accountsList[i].getAccountNumber() == accountNumber) {
accountsList[i].modifyPassword(password);
cout << "密码修改成功!账户号:" << accountNumber << endl;
return;
}
}
cout << "账户不存在!" << endl;
}
void closeAccount(string accountNumber) {
for (int i = 0; i < accountsList.size(); i++) {
if (accountsList[i].getAccountNumber() == accountNumber) {
accountsList.erase(accountsList.begin() + i);
cout << "账户注销成功!账户号:" << accountNumber << endl;
return;
}
}
cout << "账户不存在!" << endl;
}
};
int main() {
Bank bank;
bank.createAccount("1001", 1000, "123456");
bank.deposit("1001", 500);
bank.withdraw("1001", 200);
bank.modifyPassword("1001", "654321");
bank.closeAccount("1001");
return 0;
}
```
阅读全文