c++实现机房预约系统,可使用学生代表、老师以及管理员身份登陆该系统。
1.菜单页面
1.系统主菜单:
2.学生代表子菜单:
2.教师子菜单:
3.管理员子菜单:
2.搭建主菜单以及完登录
在主菜单中将注销登录功能实现。
将登陆功能封装起来,留出接口,让主函数调用。
菜单的实现:
int main()
{
while (true)
{
cout << "********************欢迎来到机房预约系统****************************" << endl;
cout << endl;
cout << "请输入您的身份" << endl;
cout << "\t\t-------------------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.学生代表 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管 理 员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.退 出 |\n";
cout << "\t\t| |\n";
cout << "\t\t-------------------------------------------------------\n";
cout << "请输入您的选择:";
int chose;
cin >> chose;
switch (chose)
{
case 1://学生
/*cout << 1;*/
loginIn(StudentFile, 1);
break;
case 2://老师
/*cout << 2;*/
loginIn(TeacherFile, 2);
break;
case 3://管理员
loginIn(AdminFile, 3);
/*cout << 3;*/
break;
case 0://退出
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "输入有误,请重新选择!" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
return 0;
}
登录功能:
void loginIn(string FileName, int type)
{
Identity* Person = NULL;
ifstream ifs;
ifs.open(FileName, ios::in);
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
ifs.close();
return ;
}
int num = 0;
string name;
string pwd;
if (type == 1)
{
cout << "请输入您的学号" << endl;
cin >> num;
}
else if (type == 2)
{
cout << "请输入您的职工号" << endl;
cin >> num;
}
cout << "请输入您的用户名" << endl;
cin >> name;
cout << "请输入您的密码" << endl;
cin >> pwd;
if (type == 1)
{
//学生身份验证
int fnum;
string fname;
string fpwd;
while (ifs >> fnum && ifs >> fname && ifs >> fpwd)
{
/*cout << fnum << endl;
cout << fname << endl;
cout << fpwd << endl;*/
if (fnum == num && fname == name && fpwd == pwd)
{
cout << "学生" << fname << "登录成功!" << endl;
system("pause");
system("cls");
Person = new Student(num, name, pwd);
StudentMenu(Person);
return;
}
}
}
else if (type == 2)
{
//老师身份验证
int fnum;
string fname;
string fpwd;
while (ifs >> fnum && ifs >> fname && ifs >> fpwd)
{
/*cout << fnum << endl;
cout << fname << endl;
cout << fpwd << endl;*/
if (fnum == num && fname == name && fpwd == pwd)
{
cout << "老师" << fname << "登录成功!" << endl;
system("pause");
system("cls");
Person=new Teacher(num, name, pwd);
TeacherMenu(Person);
return;
}
}
}
else if (type == 3)
{
//管理员身份验证
string fname;
string fpwd;
while ( ifs >> fname && ifs >> fpwd)
{
if ( fname == name && fpwd == pwd)
{
cout << "管理员" << fname << "登录成功!" << endl;
system("pause");
system("cls");
Person=new Manager(name, pwd);
ManageraMenu(Person);
return;
}
}
}
cout << "验证登录失败" << endl;
system("pause");
system("cls");
return;
}
3.使用多态的方法创建不同的身份
1.创建身份的抽象基类
菜单函数使用纯虚函数的方法。后期子类继承该纯虚函数完成不同身份的不同菜单的具体实现。
class Identity
{
public:
//子类菜单
virtual void operMenu() = 0;
//用户姓名
string m_name;
//用户密码
string m_pwd;
private:
};
2.管理员
Manager.h文件
//管理员接口
void ManageraMenu(Identity*& manager);
class Manager :public Identity
{
public:
//有参构造
Manager( string name, string pwd);
//管理员菜单实现
virtual void operMenu();
//增加账号
void AddPerson();
// 查看账号
void showPerson();
//查看机房信息
void showComputer();
//清空预约记录
void cleanFile();
//初始化容器
void InitVector();
//读取机房信息
void InitComputer();
//检测添加id是否重复 学号 类型
//返回真 重复
bool checkRepeat(int id,int type);
//学生容器
vector<Student> vStu;
//老师容器
vector<Teacher> vTea;
//机房容器
vector<Computer> vCom;
};
Manager.cpp文件
void ManageraMenu(Identity * &manager)
{
while (true)
{
manager->operMenu();
Manag