⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanage.cpp

📁 适合MFC初学者学习
💻 CPP
字号:
//UserManage.cpp: implementation of the CUserManage class.

////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LLL.h"
#include "UserManage.h"



#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

////////////////////////////////////////////////////////////////////
//Construction/Destruction
////////////////////////////////////////////////////////////////////
//构造函数,从数据库表中进行初始化
CUser::CUser(CUserSet &rs)
{
	m_uID = rs.m_ID;
	m_strPopedom = rs.m_Popedom;
	m_strUserName = rs.m_UserName;
	m_strPassword = rs.m_PassWord;
	m_strinfo = rs.m_info;

}

//复制构造函数
CUser::CUser(const CUser &other)
{
	m_uID = other.m_uID;
	m_strUserName = other.m_strUserName;
	m_strPassword = other. m_strPassword;
	m_strPopedom = other.m_strPopedom;
	m_strinfo = other.m_strinfo;
}

//默认构造函数
CUserManage::CUserManage()
{
	m_pLoginUser = NULL;//没有登录者信息
	
	m_nMaxID = -1;
	CUserSet rs;
	rs.Open();
	while (!rs.IsEOF())
	{
		CUser *pUser = new CUser(rs);
		m_userInfos[pUser->m_uID] = pUser;
		if ((int)pUser->m_uID > m_nMaxID)
		{
			m_nMaxID = pUser->m_uID;
		}
		rs.MoveNext();
	}
	rs.Close();
}

//析构函数
CUserManage::~CUserManage()
{
	POSITION pos = m_userInfos.GetStartPosition();
	while (NULL != pos)
	{
		UINT uID;
		CUser * pUser = NULL;
		m_userInfos.GetNextAssoc(pos, uID, pUser);
		delete pUser;
	}
	m_userInfos.RemoveAll();
}

//登录函数在CUserManage类中,登录成功返回true
bool CUserManage::Loading(CString strUserName, CString strPassword)
{
	POSITION pos = m_userInfos.GetStartPosition();
	while (NULL != pos)
	{
		UINT uID;
		CUser * pUser = NULL;
		m_userInfos.GetNextAssoc(pos, uID, pUser);
		if (NULL == pUser)
		{
			continue;
		}
		if (strUserName == pUser->m_strUserName && strPassword == pUser->m_strPassword)
		{
			m_pLoginUser = pUser;
			return true;
		}
	}
	return false;
}

//对添加的新用户获取新的ID
UINT CUserManage::GetNewID()
{
	return ++m_nMaxID;
}

//添加用户函数,返回ID,ID=-1表示出错
UINT CUserManage::AddUser(CString strName, CString strPassword, 
						  CString strPopedom)
{
	//检查是否用重复的用户名
	for (POSITION pos = m_userInfos.GetStartPosition(); NULL != pos; )
	{
		UINT uID;
		CUser * pUser;
		m_userInfos.GetNextAssoc(pos, uID, pUser);
		if (pUser->m_strUserName == strName)
		{
			return -1;
		}
	}

	//防止内存分配出错
	CUser * pUser = new CUser;
	if (NULL == pUser)
	{
		return -1;
	}

	pUser->m_uID = GetNewID();
	pUser->m_strUserName = strName;
	pUser->m_strPassword = strPassword;
	pUser->m_strPopedom = strPopedom;
	



	//存进数据库
	CUserSet rs;
	rs.AddUser(*pUser);
	m_userInfos[pUser->m_uID] = pUser;

	return pUser->m_uID;
}

//编辑用户信息
bool CUserManage::EditUser(CUser newUser)
{
	for (POSITION pos = m_userInfos.GetStartPosition(); NULL != pos; )
	{
		UINT uID;
		CUser * pUser;
		m_userInfos.GetNextAssoc(pos, uID, pUser);
	}

	CUser * pUser = m_userInfos[pUser->m_uID];
	if (NULL == pUser)
	{
		return false;//没有找到要修改的用户
	}


	CUserSet rs;
	if (!rs.EditUser(newUser))//执行表修改函数,错误返回fales,正确则继续进行
	{
		delete pUser;
		return false;
	}

	*pUser = newUser;
	return true;
}

bool CUserManage::DeleteUser(UINT uID)
{
	CUser * pUser = m_userInfos[uID];

	CUserSet rs;
	if (!rs.DeleteUser(uID))
	{
		return false;
	}
	delete pUser;
	return m_userInfos.RemoveKey(uID);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -