MFC中用户登录后,在一个新的窗口中能够实现以下功能:用户信息查询(在已建好的文本中查询并显示,包括姓名,账号,密码,身份证号,余额),密码的修改,存款,取款。经过一些列的操作后,修改过的信息仍保存到文本中。具体步骤及代码
时间: 2024-02-23 08:55:55 浏览: 27
这个问题涉及到很多方面,需要分步骤来完成。以下是一个基本的实现步骤及代码:
1. 创建一个新的对话框窗口,用于显示用户信息。
2. 在窗口中添加控件,包括姓名、账号、密码、身份证号、余额等显示框,以及修改密码、存款、取款等按钮。
3. 在窗口类中添加相应的变量和函数,用于读取和保存用户信息。例如:
```
struct UserInfo {
CString name;
CString account;
CString password;
CString id;
double balance;
};
class CUserInfoDialog : public CDialogEx
{
public:
CUserInfoDialog(CWnd* pParent = nullptr); // 标准构造函数
virtual ~CUserInfoDialog();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_USER_INFO_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
UserInfo m_userInfo;
BOOL ReadUserInfo();
BOOL SaveUserInfo();
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
};
```
4. 在 OnInitDialog 函数中读取用户信息并显示在相应的控件中。例如:
```
BOOL CUserInfoDialog::ReadUserInfo()
{
// 读取用户信息
CString filename = "userinfo.txt";
CStdioFile file;
if (!file.Open(filename, CFile::modeRead)) {
return FALSE;
}
CString line;
while (file.ReadString(line)) {
// 解析每一行数据
// 格式为:姓名,账号,密码,身份证号,余额
CStringArray tokens;
int count = Tokenize(line, tokens, ",");
if (count != 5) {
continue;
}
m_userInfo.name = tokens[0];
m_userInfo.account = tokens[1];
m_userInfo.password = tokens[2];
m_userInfo.id = tokens[3];
m_userInfo.balance = _tstof(tokens[4]);
if (m_userInfo.account == m_strAccount) {
// 找到当前用户的信息,退出循环
break;
}
}
file.Close();
return TRUE;
}
BOOL CUserInfoDialog::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 读取用户信息
if (!ReadUserInfo()) {
MessageBox(_T("读取用户信息失败!"));
EndDialog(IDCANCEL);
return TRUE;
}
// 将用户信息显示在相应的控件中
SetDlgItemText(IDC_NAME_EDIT, m_userInfo.name);
SetDlgItemText(IDC_ACCOUNT_EDIT, m_userInfo.account);
SetDlgItemText(IDC_PASSWORD_EDIT, m_userInfo.password);
SetDlgItemText(IDC_ID_EDIT, m_userInfo.id);
CString strBalance;
strBalance.Format(_T("%.2f"), m_userInfo.balance);
SetDlgItemText(IDC_BALANCE_EDIT, strBalance);
return TRUE;
}
```
5. 实现修改密码、存款、取款等功能,例如:
```
void CUserInfoDialog::OnOK()
{
// 保存修改后的密码
CString strPassword;
GetDlgItemText(IDC_PASSWORD_EDIT, strPassword);
if (strPassword != m_userInfo.password) {
m_userInfo.password = strPassword;
if (!SaveUserInfo()) {
MessageBox(_T("保存用户信息失败!"));
return;
}
}
CDialogEx::OnOK();
}
void CUserInfoDialog::OnDeposit()
{
// 存款
double amount = GetAmount();
if (amount <= 0) {
MessageBox(_T("请输入正确的存款金额!"));
return;
}
m_userInfo.balance += amount;
if (!SaveUserInfo()) {
MessageBox(_T("保存用户信息失败!"));
return;
}
UpdateBalance();
MessageBox(_T("存款成功!"));
}
void CUserInfoDialog::OnWithdraw()
{
// 取款
double amount = GetAmount();
if (amount <= 0 || amount > m_userInfo.balance) {
MessageBox(_T("余额不足或输入金额不正确!"));
return;
}
m_userInfo.balance -= amount;
if (!SaveUserInfo()) {
MessageBox(_T("保存用户信息失败!"));
return;
}
UpdateBalance();
MessageBox(_T("取款成功!"));
}
double CUserInfoDialog::GetAmount()
{
// 获取用户输入的金额
CString strAmount;
GetDlgItemText(IDC_AMOUNT_EDIT, strAmount);
return _tstof(strAmount);
}
void CUserInfoDialog::UpdateBalance()
{
// 更新余额显示
CString strBalance;
strBalance.Format(_T("%.2f"), m_userInfo.balance);
SetDlgItemText(IDC_BALANCE_EDIT, strBalance);
}
BOOL CUserInfoDialog::SaveUserInfo()
{
// 将用户信息保存到文件中
CString filename = "userinfo.txt";
CStdioFile file;
if (!file.Open(filename, CFile::modeReadWrite | CFile::typeText)) {
return FALSE;
}
CString line;
CStringArray lines;
while (file.ReadString(line)) {
// 解析每一行数据
// 格式为:姓名,账号,密码,身份证号,余额
CStringArray tokens;
int count = Tokenize(line, tokens, ",");
if (count != 5) {
continue;
}
if (tokens[1] == m_userInfo.account) {
// 找到当前用户的信息,替换为新的数据
line.Format(_T("%s,%s,%s,%s,%.2f"), m_userInfo.name, m_userInfo.account,
m_userInfo.password, m_userInfo.id, m_userInfo.balance);
}
lines.Add(line);
}
if (lines.IsEmpty()) {
// 文件为空,直接添加新的数据
line.Format(_T("%s,%s,%s,%s,%.2f"), m_userInfo.name, m_userInfo.account,
m_userInfo.password, m_userInfo.id, m_userInfo.balance);
lines.Add(line);
}
file.SetLength(0);
for (int i = 0; i < lines.GetCount(); i++) {
file.WriteString(lines[i]);
file.WriteString(_T("\n"));
}
file.Close();
return TRUE;
}
```
6. 在主对话框中添加一个按钮,用于打开用户信息窗口。例如:
```
void CMainFrame::OnUserInfo()
{
CUserInfoDialog dlg;
dlg.m_strAccount = m_strAccount;
dlg.DoModal();
}
```
这样就完成了一个简单的用户信息查询、修改、存取款的功能。完整代码可以参考以下链接:
https://github.com/linxinyu/MFC-Bank-System
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)