C++ ATM 机管理系统代码示例:实现基本功能
C++ ATM 机管理系统代码示例:实现基本功能
由于 ATM 机管理系统功能较为复杂,需要实现各种功能模块,因此这里仅列举 ATM 机管理系统的主要功能模块和对应代码,以供学习参考。
1. 登录模块
#include <iostream>
#include <string>
#include <map>
using namespace std;
// 定义用户结构体
struct User {
string name; // 用户名
string password; // 密码
double balance; // 余额
};
// 定义用户数据
map<string, User> users = {
{"张三", {"张三", "123456", 10000}},
{"李四", {"李四", "123456", 20000}},
{"王五", {"王五", "123456", 30000}},
};
// 登录函数
bool login(string name, string password) {
// 查找用户
auto user = users.find(name);
if (user == users.end()) {
cout << "用户不存在" << endl;
return false;
}
// 检查密码
if (user->second.password != password) {
cout << "密码错误" << endl;
return false;
}
// 登录成功
cout << "登录成功" << endl;
return true;
}
2. 查询余额模块
// 查询余额函数
void queryBalance(string name) {
// 查找用户
auto user = users.find(name);
if (user == users.end()) {
cout << "用户不存在" << endl;
return;
}
// 输出余额
cout << "余额为:" << user->second.balance << endl;
}
3. 存款模块
// 存款函数
void deposit(string name, double amount) {
// 查找用户
auto user = users.find(name);
if (user == users.end()) {
cout << "用户不存在" << endl;
return;
}
// 更新余额
user->second.balance += amount;
cout << "存款成功,当前余额为:" << user->second.balance << endl;
}
4. 取款模块
// 取款函数
void withdraw(string name, double amount) {
// 查找用户
auto user = users.find(name);
if (user == users.end()) {
cout << "用户不存在" << endl;
return;
}
// 检查余额
if (user->second.balance < amount) {
cout << "余额不足" << endl;
return;
}
// 更新余额
user->second.balance -= amount;
cout << "取款成功,当前余额为:" << user->second.balance << endl;
}
5. 转账模块
// 转账函数
void transfer(string from, string to, double amount) {
// 查找转出用户
auto from_user = users.find(from);
if (from_user == users.end()) {
cout << "转出用户不存在" << endl;
return;
}
// 查找转入用户
auto to_user = users.find(to);
if (to_user == users.end()) {
cout << "转入用户不存在" << endl;
return;
}
// 检查转出用户余额
if (from_user->second.balance < amount) {
cout << "余额不足" << endl;
return;
}
// 更新余额
from_user->second.balance -= amount;
to_user->second.balance += amount;
cout << "转账成功,当前余额为:" << from_user->second.balance << endl;
}
6. 修改密码模块
// 修改密码函数
void changePassword(string name, string old_password, string new_password) {
// 查找用户
auto user = users.find(name);
if (user == users.end()) {
cout << "用户不存在" << endl;
return;
}
// 检查旧密码
if (user->second.password != old_password) {
cout << "密码错误" << endl;
return;
}
// 更新密码
user->second.password = new_password;
cout << "密码修改成功" << endl;
}
以上是 ATM 机管理系统的主要功能模块和对应代码,可以根据需要进行修改和扩展。
注意:
- 此代码仅为示例,实际项目中需要考虑安全性、数据持久化等问题。
- 建议使用更安全的密码存储方式,例如哈希加密。
- 可以根据实际需求添加更多功能,例如交易记录查询、用户管理等。
原文地址: http://www.cveoy.top/t/topic/oszu 著作权归作者所有。请勿转载和采集!