银行账户管理系统c++
时间: 2023-11-18 13:43:48 浏览: 57
银行账户管理系统一般包括以下功能:
1. 用户注册和登录
2. 存款和取款
3. 转账和查询余额
4. 修改密码和个人信息
下面是一个简单的C++代码示例,实现了以上功能:
```c++
#include <iostream>
#include <string>
using namespace std;
// 银行账户类
class BankAccount {
private:
string accountNo; // 账号
string password; // 密码
double balance; // 余额
string name; // 姓名
public:
BankAccount(string accountNo, string password, double balance, string name) {
this->accountNo = accountNo;
this->password = password;
this->balance = balance;
this->name = name;
}
// 存款
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 transfer(BankAccount& other, double amount) {
if (balance >= amount) {
balance -= amount;
other.balance += amount;
cout << "转账成功,当前余额为:" << balance << endl;
} else {
cout << "余额不足,转账失败!" << endl;
}
}
// 查询余额
void checkBalance() {
cout << "当前余额为:" << balance << endl;
}
// 修改密码
void changePassword(string newPassword) {
password = newPassword;
cout << "密码修改成功!" << endl;
}
// 修改个人信息
void changeName(string newName) {
name = newName;
cout << "个人信息修改成功!" << endl;
}
};
// 用户类
class User {
private:
string username; // 用户名
string password; // 密码
BankAccount* account; // 银行账户
public:
User(string username, string password, BankAccount* account) {
this->username = username;
this->password = password;
this->account = account;
}
// 登录
bool login(string username, string password) {
if (this->username == username && this->password == password) {
cout << "登录成功!" << endl;
return true;
} else {
cout << "用户名或密码错误!" << endl;
return false;
}
}
// 存款
void deposit(double amount) {
account->deposit(amount);
}
// 取款
void withdraw(double amount) {
account->withdraw(amount);
}
// 转账
void transfer(BankAccount& other, double amount) {
account->transfer(other, amount);
}
// 查询余额
void checkBalance() {
account->checkBalance();
}
// 修改密码
void changePassword(string newPassword) {
account->changePassword(newPassword);
}
// 修改个人信息
void changeName(string newName) {
account->changeName(newName);
}
};
int main() {
// 初始化账户
BankAccount account1("123456", "123456", 1000, "张三");
BankAccount account2("654321", "654321", 2000, "李四");
// 初始化用户
User user1("user1", "123456", &account1);
User user2("user2", "654321", &account2);
// 用户登录
user1.login("user1", "123456");
user2.login("user2", "123456");
// 存款
user1.deposit(500);
// 取款
user2.withdraw(1000);
// 转账
user1.transfer(account2, 300);
// 查询余额
user2.checkBalance();
// 修改密码
user1.changePassword("111111");
// 修改个人信息
user2.changeName("王五");
return 0;
}
```
以上代码只是一个简单的示例,实际应用中还需要添加更多的功能和安全措施。
阅读全文