编写c++代码以现实中银行卡为原型,进行需求调查,抽象出相应的类,设计类的成员函数和数据 成员,完成银行账户管理系统。编写程序,帮助工作人员有效、准确并且高校的完成存取事 件。(系统主要完成开户管理,存取钱管理,修改密码,注销等功能)
时间: 2024-06-09 08:09:06 浏览: 185
以下是我实现的代码,其中包括了银行账户管理系统的基本功能:
```cpp
#include<iostream>
#include<string>
using namespace std;
// 银行账户类
class BankAccount {
private:
string user_name; // 用户名
string account_num; // 账户号码
string password; // 密码
double balance; // 余额
public:
// 构造函数
BankAccount(string user_name, string account_num, string password, double balance = 0.0) {
this->user_name = user_name;
this->account_num = account_num;
this->password = password;
this->balance = balance;
}
// 获取用户名
string getUserName() {
return user_name;
}
// 获取账户号码
string getAccountNum() {
return account_num;
}
// 存款
void deposit(double amount) {
balance += amount;
cout << "存款成功,当前余额为:" << balance << endl;
}
// 取款
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
cout << "取款成功,当前余额为:" << balance << endl;
}
else {
cout << "余额不足,取款失败" << endl;
}
}
// 修改密码
void changePassword(string new_password) {
password = new_password;
cout << "修改密码成功" << endl;
}
// 注销账户
void closeAccount() {
balance = 0.0;
cout << "账户已注销" << endl;
}
};
// 银行账户管理系统
class BankAccountManagementSystem {
public:
// 开户
static void openAccount() {
string user_name, account_num, password;
cout << "请输入用户名:";
cin >> user_name;
cout << "请输入账户号码:";
cin >> account_num;
cout << "请输入密码:";
cin >> password;
BankAccount account(user_name, account_num, password);
accounts.push_back(account);
cout << "开户成功" << endl;
}
// 存款
static void deposit() {
string account_num;
double amount;
cout << "请输入账户号码:";
cin >> account_num;
bool found = false;
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getAccountNum() == account_num) {
found = true;
cout << "请输入存款金额:";
cin >> amount;
accounts[i].deposit(amount);
break;
}
}
if (!found) {
cout << "账户不存在" << endl;
}
}
// 取款
static void withdraw() {
string account_num;
double amount;
cout << "请输入账户号码:";
cin >> account_num;
bool found = false;
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getAccountNum() == account_num) {
found = true;
cout << "请输入取款金额:";
cin >> amount;
accounts[i].withdraw(amount);
break;
}
}
if (!found) {
cout << "账户不存在" << endl;
}
}
// 修改密码
static void changePassword() {
string account_num, password, new_password;
cout << "请输入账户号码:";
cin >> account_num;
bool found = false;
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getAccountNum() == account_num) {
found = true;
cout << "请输入原密码:";
cin >> password;
if (password == accounts[i].getPassword()) {
cout << "请输入新密码:";
cin >> new_password;
accounts[i].changePassword(new_password);
}
else {
cout << "密码错误" << endl;
}
break;
}
}
if (!found) {
cout << "账户不存在" << endl;
}
}
// 注销账户
static void closeAccount() {
string account_num, password;
cout << "请输入账户号码:";
cin >> account_num;
bool found = false;
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getAccountNum() == account_num) {
found = true;
cout << "请输入密码:";
cin >> password;
if (password == accounts[i].getPassword()) {
accounts[i].closeAccount();
accounts.erase(accounts.begin() + i);
}
else {
cout << "密码错误" << endl;
}
break;
}
}
if (!found) {
cout << "账户不存在" << endl;
}
}
private:
static vector<BankAccount> accounts; // 存储所有银行账户
};
// 初始化静态成员变量
vector<BankAccount> BankAccountManagementSystem::accounts;
// 主函数
int main() {
int choice;
do {
cout << "请选择操作:\n"
<< "1. 开户\n"
<< "2. 存款\n"
<< "3. 取款\n"
<< "4. 修改密码\n"
<< "5. 注销账户\n"
<< "6. 退出\n";
cin >> choice;
switch (choice) {
case 1:
BankAccountManagementSystem::openAccount();
break;
case 2:
BankAccountManagementSystem::deposit();
break;
case 3:
BankAccountManagementSystem::withdraw();
break;
case 4:
BankAccountManagementSystem::changePassword();
break;
case 5:
BankAccountManagementSystem::closeAccount();
break;
case 6:
cout << "退出程序" << endl;
break;
default:
cout << "无效操作" << endl;
break;
}
} while (choice != 6);
return 0;
}
```
在这个程序中,我们首先定义了一个银行账户类 `BankAccount`,包括了用户名、账户号码、密码和余额等成员变量,以及存款、取款、修改密码和注销账户等成员函数。然后我们定义了一个银行账户管理系统类 `BankAccountManagementSystem`,其中包括了开户、存款、取款、修改密码和注销账户等功能。最后我们在 `main` 函数中调用这些功能来实现整个银行账户管理系统。
在程序运行时,我们会先输出一个菜单,让用户选择要进行的操作。然后根据用户的选择,调用相应的功能函数来完成对应的操作。如果用户输入的操作无效,则输出提示信息并重新输出菜单等待用户再次选择。
阅读全文
相关推荐













