C++实现计算中心预约管理系统v1.0

业务需求

  • 学校计算中心目前有四个规格不同的机房,由于管理的不便,特开发一套机房预约管理系统,以供老师管理和学生预约。
  • 预约时间为下一周的周1到周5,可以选择上/下午时间段,以及具体的座位号,预约记录随时可取消。

预设身份

  • 【学生】行为:申请预约、查看/取消预约。
  • 【教师】行为:审核预约。
  • 【管理员】行为:查看/添加/删除身份账号信息、查看/清空预约、查看/修改机房布局。
  • 用户进入系统,选择身份类型,输入用户信息进行验证,通过后进入各自的子菜单。
  • 用户信息包括用户ID、姓名和密码。

机房信息

  • 一共四间机房,如下图所示。
编号容量:布局
136:6*6
296:8*12
348:6*8
424 :6*4

预约设置

  • 读取预约文件并初始化容器记录。
  • 可调取updateOrder()实时更新预约记录并存储至文件。
//orderFile.h
class OrderFile
{
	public:
		OrderFile();
		void updateOrder(); // 更新记录文件
		int m_Size; //记录预约条数
		map<int, map<string, string> > m_orderData; //预约记录表
};
//orderFile.cpp
OrderFile::OrderFile()
{
	this->m_Size = 0;
	ifstream ifs;
	ifs.open(ORDER_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "No File!" << endl;
		ifs.close();
		return;
	}
	
	string date;    // 日期
	string interval; //时间段
	string stuId;   //学生编号
	string stuName; //学生姓名
	string roomId;  //机房编号
	string deskId; //座位号
	string status;  //预约状态
	this->m_Size; //记录数
	
	while (ifs >> date && ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId && ifs >> deskId && ifs >> status) // 读取所有预约记录
	{
		string key;
		string value;
		map<string, string> m;
		//截取日期
		int pos = date.find(":");
		if (pos != -1)
		{
			value = date.substr(pos + 1, date.size() - pos - 1);
			m.insert(make_pair("date", value));
		}
		//截取时间段
		pos = interval.find(":");
		if (pos != -1)
		{
			value = interval.substr(pos + 1, interval.size() - pos - 1);
			m.insert(make_pair("interval", value));
		}
		//截取学号
		pos = stuId.find(":");
		if (pos != -1)
		{
			value = stuId.substr(pos + 1, stuId.size() - pos - 1);
			m.insert(make_pair("stuId", value));
		}
		//截取姓名
		pos = stuName.find(":");
		if (pos != -1)
		{
			value = stuName.substr(pos + 1, stuName.size() - pos - 1);
			m.insert(make_pair("stuName", value));
		}
		//截取机房号
		pos = roomId.find(":");
		if (pos != -1)
		{
			value = roomId.substr(pos + 1, roomId.size() - pos - 1);
			m.insert(make_pair("roomId", value));
		}
		//截取座位号
		pos = deskId.find(":");
		if (pos != -1)
		{
			value = deskId.substr(pos + 1, deskId.size() - pos - 1);
			m.insert(make_pair("deskId", value));
		}
		//截取预约状态
		pos = status.find(":");
		if (pos != -1)
		{
			value = status.substr(pos + 1, status.size() - pos - 1);
			m.insert(make_pair("status", value));
		}
		//添加记录
		this->m_orderData.insert(make_pair(this->m_Size, m));
		this->m_Size++;
	}
	ifs.close();
}

void OrderFile::updateOrder()
{
	ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
	if(this->m_Size == 0)
	{
		return;
	}
	for (int i = 0; i < this->m_Size; i++)
	{
		ofs << "date:" << this->m_orderData[i]["date"] << " ";
		ofs << "interval:" << this->m_orderData[i]["interval"] << " ";
		ofs << "stuId:" << this->m_orderData[i]["stuId"] << " ";
		ofs << "stuName:" << this->m_orderData[i]["stuName"] << " ";
		ofs << "roomId:" << this->m_orderData[i]["roomId"] << " ";
		ofs << "deskId:" << this->m_orderData[i]["deskId"] << " ";
		ofs << "status:" << this->m_orderData[i]["status"] << endl;
	}
	ofs.close();
}

机房设置

  • 初始化机房信息,可绘制机房布局。
    机房布局
//computerRoom.h
class ComputerRoom
{
	public:
		ComputerRoom(): m_ComId(0), m_MaxNum(0), row(0), col(0), layout("") {};
		int m_ComId; //机房Id号
		int m_MaxNum; //机房最大容量
		int row; //行
		int col; //列
		string layout; //位置状态
		void printLayout() const;//绘制机房布局
};
//computerRoom.cpp
void ComputerRoom::printLayout() const
{
	cout << "注意:显示为1的座位是占用状态!" << endl;
	cout << "\n\t ------------------------------ " << endl;
	for (int i = 0; i < this->row; i++)
	{
		cout << "\t\t| ";
		for (int j = 0; j < this->col; j++)
		{
			cout << this->layout[j + i * this->col] << " ";
		}
		cout << "|\n";
	}
	cout << "\t ------------------------------- " << endl;
}

身份基类

  • 定义纯虚函数openMenu()实现用户自定义菜单,可以在不同的身份子类中进行个性化定制。
  • 定义静态机房和预约容器,保证各用户数据一致性。
// identify.h
class Identify
{
	public:
		virtual void openMenu() = 0; // 定义纯虚函数,实现操作菜单多态性
		string m_Name; // 用户名
		string m_Pwd; // 密码
		static vector<ComputerRoom> vCom; //机房信息容器
		static OrderFile of; //预约记录
}

学生

  • 进入子菜单后,选择申请预约功能:选择下周内的时间(周1-周5),时间段(上午、下午),选择机房、选择座位号,确认预约信息。
  • 选择查看预约功能:查看已预约的记录,可以对正在审核或者审核通过的记录进行取消。
  • 选择注销功能:返回上一级登录界面。
    申请预约
    查看预约
// student.h
class Student: public Identify
{
	public:
		Student();
		Student(const int& uid,const string& name,const string& pwd);
		virtual void openMenu(); // 子菜单界面
		void applyOrder(); // 申请预约
		void showOrder(); // 查看预约
		void cancelOrder(const int& id); // 取消预约
		void updateComputer() const;//更新机房文件
	    int uid; // 学号
};
// student.cpp
Student::Student()
{
	this->uid = 0;
	this->m_Name = "";
	this->m_Pwd = "";
}
Student::Student(const int& uid, const string& name, const string& pwd)
{
	this->udi = uid;
	this->m_Name = name;
	this->m_Pwd = pwd;
}

void Student::openMenu()
{
	cout << "欢迎学生:" << this->m_Name << "登录!" << 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|          0.注销登录              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t ----------------------------------\n";
	cout << "请选择您的操作: " << endl;
}

void Student::applyOrder()
{
	int date = 0;   //日期
	int interval = 0; //时间段
	int room = 0; //机房编号
	
	cout << "计算中心开放时间为周1-周5!" << endl;
	cout << "请输入申请预约的时间:" << endl;
	cout << "1、周一" << endl;
	cout << "2、周二" << endl;
	cout << "3、周三" << endl;
	cout << "4、周四" << endl;
	cout << "5、周五" << endl;
	
	while(true)
	{
		cin>>date;
		if(date>=1 && date<=5)
		{
			break;
		}
		cout<<"Try again!"<<endl;
	}
	
	cout << "请输入申请预约时间段:" << endl;
	cout << "1、上午" << endl;
	cout << "2、下午" << endl;
	
	while(true)
	{
		cin>>interval;
		if(interval>=1 && interval<=2)
		{
			break;
		}
		cout<<"Try again!"<<endl;
	}
	
	cout << "请选择机房:" << endl;
	for (int i = 0; i < ComputerRoom::num; i++)
	{
		cout << Identity::vCom[i].m_ComId << "号机房容量为: " << Identity::vCom[i].m_MaxNum << endl;
	}
	
	while (true)
	{
		cin >> room;
		if (room >= 1 && room <= this->vCom.size())
		{
			break;
		}
		cout<<"Try again!"<<endl;
	}
	
	int T_room = (date - 1) * ComputerRoom::num * 2 + (interval - 1) * ComputerRoom::num + room; //计算真实机房编号

	Identity::vCom[T_room - 1].printLayout();
	int r, l, t = 0;
	cout << "请输入选择的行和列,例如第二排第三列输入:2 3" << endl;

	while (true)
	{
		cin >> r >> l;
		if (r <= Identity::vCom[T_room - 1].row && r >= 1 && l >= 1 && l <= Identity::vCom[T_room - 1].col)
		{
			t = l + (r - 1) * Identity::vCom[T_room - 1].col;
			if (t <= Identity::vCom[T_room - 1].m_MaxNum)
			{
				if (Identity::vCom[T_room - 1].layout[t - 1] == '1')
				{
					cout << "该位置已被占用,请重新选择" << endl;
				}
				else
				{
					break;
				}
			}
			else
			{
				cout<<"Try again!"<<endl;
			}
		}
		else
		{
			cout<<"Try again!"<<endl;
		}
	}

	bool flag = false; //冲突标识
	for (int i = 0; i < Identity::of.m_Size; i++) //防止预约冲突
	{
		// string 先利用 .c_str() 转 const char *
		// 再利用 atoi ( const char *) 转 int
		int f_date = atoi(Identity::of.m_orderData[i]["date"].c_str());
		int f_interval = atoi(Identity::of.m_orderData[i]["interval"].c_str());
		int f_uid = atoi(Identity::of.m_orderData[i]["stuId"].c_str());
		string f_name = Identity::of.m_orderData[i]["stuName"];
		int f_room = atoi(Identity::of.m_orderData[i]["roomId"].c_str());
		int f_desk = atoi(Identity::of.m_orderData[i]["deskId"].c_str());
		int f_status = atoi(Identity::of.m_orderData[i]["status"].c_str());
	
		if (f_date == date && f_interval == interval && f_room == room && f_uid == this->uid)
		{
			//1 审核中 2 已预约 -1 预约失败  0 取消预约
			if (f_status == 1 || f_status == -1 || f_status == 0)
			{
				Identity::of.m_orderData[i]["deskId"] = to_string(t);
				Identity::of.m_orderData[i]["status"] = "1";
				Identity::vCom[T_room - 1].layout[f_desk - 1] = '0';
				Identity::vCom[T_room - 1].layout[t - 1] = '1';
				cout << "预约成功!审核中!" << endl;
			}
			else if(f_status == 2)
			{
				cout << "预约失败!已完成该预约!" << endl;
			}
			flag = true;
			break;
		}
	}
	if (!flag) //未找到同一预约,插入新记录
	{
		string key;
		string value;
		map<string, string> m;
		m.insert(make_pair("date", to_string(date)));
		m.insert(make_pair("interval", to_string(interval)));
		m.insert(make_pair("stuId", to_string(this->uid)));
		m.insert(make_pair("stuName", this->m_Name));
		m.insert(make_pair("roomId", to_string(room)));
		m.insert(make_pair("deskId", to_string(t)));
		m.insert(make_pair("status", "1"));
	
		Identity::of.m_orderData.insert(make_pair(Identity::of.m_Size, m));
		Identity::of.m_Size++;
	
		Identity::vCom[T_room - 1].layout[t - 1] = '1';
	
		cout << "预约成功!审核中" << endl;
	}
	Identity::of.updateOrder();
	this->updateComputer();
	
	system("pause");
	system("cls");	
}

void Student::updateComputer() const
{
	ofstream ofs;
	ofs.open(COMPUTER_FILE, ios::out | ios::trunc);
	if (!ofs.is_open())
	{
		cout << "文件读取失败" << endl;
		return;
	}

	for (vector<ComputerRoom>::iterator it = Identity::vCom.begin(); it != Identity::vCom.end(); it++)
	{
		ofs << it->m_ComId << " " << it->m_MaxNum << " " << it->row << " " << it->col << " " << it->layout << endl;
	}
	ofs.close();
}

void Student::showOrder()
{
	if (Identity::of.m_Size == 0)
	{
		cout << "无预约记录!" << endl;
		system("pause");
		system("cls");
		return;
	}

	cout << "[编号]\t[日期]\t[时间]\t[机房]\t[座位]\t[状态]" << endl;
	int index = 1;
	for (int i = 0; i < Identity::of.m_Size; i++)
	{
		if (this->uid == atoi(Identity::of.m_orderData[i]["stuId"].c_str())) //找到自身预约
		{
			cout << "  " << index++;
			cout << "\t 周" << Identity::of.m_orderData[i]["date"];
			cout << "\t " << (Identity::of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
			cout << "\t  " << Identity::of.m_orderData[i]["roomId"];
			cout << "\t  " << Identity::of.m_orderData[i]["deskId"];
			string status = "\t";
			// 1 审核中  2 已预约  -1 预约失败 0 取消预约
			if (Identity::of.m_orderData[i]["status"] == "1")
			{
				status += "审核中";
			}
			else if (Identity::of.m_orderData[i]["status"] == "2")
			{
				status += "预约成功";
			}
			else if (Identity::of.m_orderData[i]["status"] == "-1")
			{
				status += "预约失败,审核未通过";
			}
			else
			{
				status += "预约已取消";
			}
			cout << status << endl;
		}
	}
	if (index == 1)
	{
		cout << "无预约记录!" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		int orderId;
		cout << "输入记录序号取消预约。输入0重新选择" << endl;
		cin >> orderId;

		if (orderId)
		{
			cout << "确认取消?输入1确认,输入0重新选择" << endl;
			int flag = 0;
			cin >> flag;
			if (flag)
			{
				this->cancelOrder(orderId);
			}
			else
			{
				system("pause");
				system("cls");
			}
		}
		else
		{
			system("pause");
			system("cls");
		}
	}
}

void Student::cancelOrder(const int& orderId)
{
	if (Identity::of.m_Size == 0 )
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}

	int index = 1;
	for (int i = 0; i < Identity::of.m_Size; i++)
	{
		if (this->uid == atoi(Identity::of.m_orderData[i]["stuId"].c_str())) //判断自身学号
		{
			if (index == orderId)
			{
				//再筛选状态、审核中或预约成功
				if (Identity::of.m_orderData[i]["status"] == "1" || Identity::of.m_orderData[i]["status"] == "2")
				{
					Identity::of.m_orderData[i]["status"] = "0";
					int f_room = atoi(Identity::of.m_orderData[i]["roomId"].c_str());
					int f_desk = atoi(Identity::of.m_orderData[i]["deskId"].c_str());
					Identity::vCom[f_room - 1].layout[f_desk - 1] = '0';
					cout << "取消成功" << endl;
				}
				else
				{
					cout << "不能取消" <<endl;
				}
				break;
			}
			else
			{
				index++;
			}	
		}
	}
	Identity::of.updateOrder();
	this->updateComputer();
	system("pause");
	system("cls");
}

教师

  • 进入子菜单后,选择查看预约功能:可以查看所有学生的预约信息,可选择未完成审核的记录进行审核,可按照实际情况选择通过或不通过。
  • 选择注销功能:返回上一级登录界面。
    审核预约
// teacher.h
class Teacher: public Identify
{
	public:
		Teacher();
	    Teacher(const int& tid, const string& name, const string& pwd);
		virtual void openMenu();
		void showOrder();//查看预约
		void auditOrder(const int& id);//审核预约
		int tid;
};
//teacher.cpp
Teacher::Teacher
{
	this->tid = 0;
	this->m_Name = "";
	this->m_Pwd = "";
}
Teacher::Teacher(const int& tid, const string& name, const string& pwd)
{
	this->tid = tid;
	this->m_Name = name;
	this->m_Pwd = pwd;
}

void Teacher::openMenu()
{
	cout << "欢迎教师:" << this->m_Name << "登录!" << endl;
	cout << "\t\t ----------------------------------\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          1.查看预约              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          0.注销登录              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t ----------------------------------\n";
	cout << "请选择您的操作: " << endl;
}

void Teacher::showOrder()
{
	if (Identity::of.m_Size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}

	cout << "[编号]\t[日期]\t[时间]\t[学号]\t[姓名]\t[机房]\t[座位]\t[状态]" << endl;
	for (int i = 0; i < Identity::of.m_Size; i++)
	{
		cout << "  " << i + 1;
		cout << "\t 周" << Identity::of.m_orderData[i]["date"];
		cout << "\t " << (Identity::of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
		cout << "\t " << Identity::of.m_orderData[i]["stuId"];
		cout << "\t " << Identity::of.m_orderData[i]["stuName"];
		cout << "\t  " << Identity::of.m_orderData[i]["roomId"];
		cout << "\t  " << Identity::of.m_orderData[i]["deskId"];
		string status = "\t";
		//1 审核中 2 已预约 -1 预约失败  0 取消预约
		if (Identity::of.m_orderData[i]["status"] == "1")
		{
			status += "审核中";
		}
		else if (Identity::of.m_orderData[i]["status"] == "2")
		{
			status += "预约成功";
		}
		else if (Identity::of.m_orderData[i]["status"] == "-1")
		{
			status += "预约失败,审核未通过";
		}
		else
		{
			status += "预约已取消";
		}
		cout << status << endl;

	}

	int id;
	cout << "输入记录编号进行审核,输入0重新选择" << endl;
	cin >> id;
	if (id > 0)
	{
		this->auditOrder(id);
	}
	else
	{
		system("pause");
		system("cls");
	}
}

void Teacher::auditOrder(const int& orderId)
{
	if (Identity::of.m_Size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}

	if (orderId > Identity::of.m_Size && orderId < 1)
	{
		cout << "编号有误" << endl;
		system("pause");
		system("cls");
		return;
	}

	for (int i = 0; i < Identity::of.m_Size; i++)
	{
		if (i + 1 == orderId)
		{
			if (Identity::of.m_orderData[i]["status"] == "1")
			{
				int res;
				cout << "请输入审核结果" << endl;
				cout << "1、通过" << endl;
				cout << "2、不通过" << endl;
				cin >> res;
				if (res == 1)
				{
					Identity::of.m_orderData[i]["status"] = "2";
				}
				else
				{
					Identity::of.m_orderData[i]["status"] = "-1";
				}
				Identity::of.updateOrder(); 
				cout << "审核完毕" << endl;
			}
			else
			{
				cout<<"该记录不能审核" << endl;
			}
			break;
		}
	}

	system("pause");
	system("cls");
}

管理员

  • 进入子菜单后,选择查看账号功能:可以查看所有学生和教师的账号信息,可删除或者添加账号。
  • 选择查看预约功能:可以查看所有的预约信息,可选择是否清空所有的预约信息。
  • 选择查看机房功能:可以查看所有的机房信息,可选择相应的机房信息及进行布局修改。
  • 选择注销功能:返回上一级登录界面。
    查看预约
    查看账号
//manager.h
class Manager: public Identify
{
	public:
		Manager();
		Manager(const string& name, const string& pwd);
		virtual void openMenu();
		void initVector(); //初始化容器
		void addPerson(); //添加账号
		bool checkRepeat(const int& id, const int& type) const;//判断账号重复
		void showPerson(); //查看账号
		void cleanPerson(const string& fileName) const; //清空账号文件
		void updatePerson(const string& fileName, const int& type)  const; //更新账号文件
		void delPerson(); //删除账号
		void showOrder() const; //查看预约记录
		void showComputer(); //查看机房
		void alterRoom(const int& n); //改变布局
		void updateComputer() const;//更新机房记录
		
	private:	
		static vector<Student> vStu; //学生容器
		static vector<Teacher> vTea; //教师容器
};
//manager.cpp
//初始化静态变量
vector<Student> Manager::vStu;
vector<Teacher> Manager::vTea;
Manager::Manager()
{
	this->m_Name = "";
	this->m_Pwd = "";
}

Manager::Manager(const string& name, const string& pwd)
{
	this->m_Name = name;
	this->m_Pwd= pwd;
	this->initVector();
}

void Manager::openMenu()
{
	cout << "欢迎管理员:" << this->m_Name << "登录!" << 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 << "请选择您的操作: " << endl;
}

void Manager::initVector()
{
	//清空容器
	this->vStu.clear();
	this->vTea.clear();

	//读取学生信息   
	ifstream ifs;
	ifs.open(STUDENT_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件读取失败" << endl;
		return;
	}

	Student s;
	while (ifs >> s.uid && ifs >> s.m_Name && ifs >> s.m_Pwd)
	{
		this->vStu.push_back(s);
	}
	ifs.close();

	//读取老师信息   
	ifs.open(TEACHER_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件读取失败" << endl;
		return;
	}
	Teacher t;
	while (ifs >> t.tid && ifs >> t.m_Name && ifs >> t.m_Pwd)
	{
		this->vTea.push_back(t);
	}

	ifs.close();
}
void Manager::cleanPerson(const string& fileName) const
{
	ofstream ofs(fileName, ios::trunc);
	ofs.close();

	cout << "清空成功!" << endl;
	system("pause");
	system("cls");
}
void Manager::updatePerson(const string& fileName, const int& type) const
{
	//读取信息
	ofstream ofs;
	ofs.open(fileName, ios::out | ios::app);
	if (!ofs.is_open())
	{
		cout << "文件读取失败" << endl;
		return;
	}

	if (type == 1)
	{
		for (vector<Student>::iterator it = this->vStu.begin(); it != this->vStu.end(); it++)
		{
			ofs << it->uid << " " << it->m_Name << " " << it->m_Pwd << " " << endl;
		}
	}
	else
	{
		for (vector<Teacher>::iterator it = this->vTea.begin(); it != this->vTea.end(); it++)
		{
			ofs << it->tid << " " << it->m_Name << " " << it->m_Pwd << " " << endl;
		}

	}
	cout << "文件更新成功" << endl;
	ofs.close();
}
void Manager::showPerson()
{
	cout << "所有学生信息如下:" << endl;
	cout << "[学号]\t[姓名]\t[密码]"<<endl;
	for (vector<Student>::iterator i = this->vStu.begin(); i != this->vStu.end(); i++)
	{
		cout <<" " << i->uid << "\t " << i->m_Name << "\t" << i->m_Pwd << endl;
	}
	cout << "\n所有老师信息如下:" << endl;
	cout << "[职工号]\t[姓名]\t[密码]" << endl;
	for (vector<Teacher>::iterator i = this->vTea.begin(); i != this->vTea.end(); i++)
	{
		cout <<"  "<< i->tid << "\t\t " << i->m_Name << "\t" << i->m_Pwd << endl;
	}

	cout << "请选择1或2进行操作:" << endl;
	cout << "1、添加账号" << endl;
	cout << "2、删除账号" << endl;
	cout << "0、重新选择" << endl;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		this->addPerson();
	}
	else if (select == 2)
	{
		this->delPerson();
	}
	else
	{
		system("pause");
		system("cls");
	}

}
void Manager::addPerson()
{
	cout << "请输入添加账号类型" << endl;
	cout << "1、学生" << endl;
	cout << "2、老师" << endl;

	string fileName; //操作文件名
	string tip;  //提示id号
	string errorTip; //重复错误提示

	int select = 0;
	cin >> select; //接受用户的选项

	if (select == 1)
	{
		//添加的是学生
		fileName = STUDENT_FILE;
		tip = "请输入学号:";
		errorTip = "学号重复,请重新输入";
	}
	else
	{
		fileName = TEACHER_FILE;
		tip = "请输入职工编号:";
		errorTip = "职工号重复,请重新输入";
	}

	ofstream ofs;//文件操作对象
	//利用追加的方式 写文件
	ofs.open(fileName, ios::out | ios::app);

	int id; //学号 /职工号
	string name; //姓名
	string pwd; //密码

	cout << tip << endl;

	while (true)
	{
		cin >> id;
		if (this->checkRepeat(id, select)) //有重复
		{
			cout << errorTip << endl;
		}
		else
		{
			break;
		}
	}

	cout << "请输入姓名: " << endl;
	cin >> name;

	cout << "请输入密码: " << endl;
	cin >> pwd;

	//向文件中添加数据
	ofs << id << " " << name << " " << pwd << " " << endl;
	cout << "添加成功" << endl;

	system("pause");
	system("cls");

	ofs.close();

	//调用初始化容器接口,重新获取文件中的数据
	this->initVector();
}
bool Manager::checkRepeat(const int& id, const int& type) const
{
	if (type == 1)
	{
		//检测学生
		for (vector<Student>::iterator it = this->vStu.begin(); it != this->vStu.end(); it++)
		{
			if (id == it->uid)
			{
				return true;
			}
		}
	}
	else
	{
		//检测老师
		for (vector<Teacher>::iterator it = this->vTea.begin(); it != this->vTea.end(); it++)
		{
			if (id == it->tid)
			{
				return true;
			}
		}
	}

	return false;
}
void Manager::delPerson()
{
	cout << "请输入删除账号类型" << endl;
	cout << "1、学生" << endl;
	cout << "2、老师" << endl;

	string fileName;
	string tip;
	string errorTip;

	int type = 0;
	cin >> type;

	if (type == 1)
	{
		fileName = STUDENT_FILE;
		tip = "请输入学号:";
		errorTip = "学号错误,请重新输入";
	}
	else
	{
		fileName = TEACHER_FILE;
		tip = "请输入职工编号:";
		errorTip = "职工号错误,请重新输入";
	}

	int id; //学号、职工号
	cout << tip << endl;

	bool flag = false;
	while (!flag)
	{
		cin >> id;
		if (type == 1)
		{
			for (vector<Student>::iterator it = this->vStu.begin(); it != this->vStu.end(); it++)
			{
				if (id == it->uid)
				{
					this->vStu.erase(it);
					flag = true;
					break;
				}
			}
			cout << errorTip << endl;
		}
		else
		{
			for (vector<Teacher>::iterator it = this->vTea.begin(); it != this->vTea.end(); it++)
			{
				if (id == it->tid)
				{
					this->vTea.erase(it);
					flag = true;
					break;
				}
			}
			cout << errorTip << endl;
		}
	}

	cout << "删除成功" << endl;
	this->cleanPerson(fileName);
	this->updatePerson(fileName, type);
	system("pause");
	system("cls");

}
void Manager::updateComputer() const
{
	ofstream ofs;
	ofs.open(COMPUTER_FILE, ios::out | ios::trunc);
	if (!ofs.is_open())
	{
		cout << "文件读取失败" << endl;
		return;
	}

	for (vector<ComputerRoom>::iterator it = Identity::vCom.begin(); it != Identity::vCom.end(); it++)
	{
		ofs << it->m_ComId << " " << it->m_MaxNum << " " << it->row << " "<<it->col << " "<<it->layout << endl;
	}

	ofs.close();
}
void Manager::showComputer()
{
	cout << "机房信息如下:" << endl;
	cout << "[编号]\t[容量]\t[行数]\t[列数]" << endl;
	int m = 0;
	for (vector<ComputerRoom>::iterator it = Identity::vCom.begin(); m < ComputerRoom::num; it++, m++)
	{
		cout <<"   "<< it->m_ComId << "\t  " << it->m_MaxNum << "\t  " << it->row<<"\t  " << it->col <<endl;
	}

	cout << "输入编号1-" << ComputerRoom::num << ",可以进行机房的布局修改" << endl;
	cout << "输入0,重新选择" << endl;
	int n = 0;
	cin >> n;
	if (n)
	{
		Identity::vCom[n - 1].printLayout();
		this->alterRoom(n);
	}
	else
	{
		system("pause");
		system("cls");
	}
}
void Manager::alterRoom(const int& n)
{
	cout << "请输入房间容量,行数,列数" << endl;
	int new_size, new_col, new_row;
	cin >> new_size >> new_row >> new_col;
	if (new_size <= new_row * new_col && new_size > 0 && new_row > 0 && new_col > 0)
	{
		for (int i = 0; i < ComputerRoom::num * 10; i++)
		{
			if (i % ComputerRoom::num == n - 1)
			{
				Identity::vCom[i].row = new_row;
				Identity::vCom[i].col = new_col;
				Identity::vCom[i].m_MaxNum = new_size;
				string t(new_size, '0');
				Identity::vCom[i].layout.assign(t);
			}
		}

		cout << "修改成功,返回上一级界面" << endl;
		this->updateComputer();
		this->showComputer();
	}
	else
	{
		cout << "修改失败,返回上一级界面" << endl;
		system("pause");
		system("cls");
	}
}
void Manager::showOrder() const
{
	if (Identity::of.m_Size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "[编号]\t[日期]\t[时间]\t[学号]\t[姓名]\t[机房]\t[座位]\t[状态]" << endl;
	for (int i = 0; i < Identity::of.m_Size; i++)
	{
		cout <<"  "<< i + 1;
		cout << "\t 周" << Identity::of.m_orderData[i]["date"];
		cout << "\t " << (Identity::of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
		cout << "\t " << Identity::of.m_orderData[i]["stuId"];
		cout << "\t "<< Identity::of.m_orderData[i]["stuName"];
		cout << "\t  " << Identity::of.m_orderData[i]["roomId"];
		cout << "\t  " << Identity::of.m_orderData[i]["deskId"];
		string status = "\t";
		// 1 审核中 2 已预约 -1 预约失败  0 取消预约
		if (Identity::of.m_orderData[i]["status"] == "1")
		{
			status += "审核中";
		}
		else if (Identity::of.m_orderData[i]["status"] == "2")
		{
			status += "预约成功";
		}
		else if (Identity::of.m_orderData[i]["status"] == "0")
		{
			status += "预约失败";
		}
		else
		{
			status += "预约已取消";
		}
		cout << status << endl;
	}

	cout << "\n输入1清空所有记录,输入0重新选择" << endl;
	int f = 1;
	cin >> f;
	if (f)
	{
		cout << "\n操作完成!" << endl;
		Identity::of.m_orderData.clear();
		Identity::of.m_Size = 0;
		Identity::of.updateOrder();

		for (vector<ComputerRoom>::iterator it = Identity::vCom.begin(); it != Identity::vCom.end(); it++)
		{
			string t(it->m_MaxNum, '0');
			it->layout.assign(t);
		}
		this->updateComputer();
	}
	system("pause");
	system("cls");
}

重点问题

  • 用户信息、机房信息和预约信息以txt形式保存于本地。
  • 管理员需要在进入系统后初始化教师和学生容器。

全局模块

  • 保存全局文件信息
//globalFile.h
#define ADMIN_FILE     "admin.txt" //管理员信息
#define STUDENT_FILE   "student.txt" //学生信息
#define TEACHER_FILE   "teacher.txt" //教师信息
#define COMPUTER_FILE  "computerRoom.txt" //机房信息
#define ORDER_FILE     "order.txt" //预约记录

登录模块

  • 选择用户类型后进入到验证信息环节,读取相应的txt文件进行信息比对,成功则进入子菜单。
  • 期间需要先初始化基类指针,而后指向不同子类的实例,调用重写后的虚函数。
  • 在进入每个子菜单后,需要定义子类指针接管基类指针指向的对象。
  • 在进入系统后初始化机房和预约容器。
    选择登录
// ComputerOrder.cpp
//初始化静态变量
vector<ComputerRoom> Identity::vCom; 
int ComputerRoom::num = 4; // 4间教室
OrderFile Identity::of;
void initComputerRoom()
{
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "No File!" << endl;
		ifs.close();
		return;
	}
	ComputerRoom com;
	Identity::vCom.clear();
	while (ifs >> com.m_ComId && ifs >> com.m_MaxNum && ifs >> com.row && ifs >> com.col && ifs >> com.layout)
	{
		Identity::vCom.push_back(com);
	}
	ifs.close();
}
void Login(const string& fileName, const int& type)//选择符合身份类型type的身份文件进行信息验证
{
	Identity * person = NULL;//父类指针,用于指向子类对象

	ifstream ifs;
	ifs.open(fileName, ios::in);

	if (!ifs.is_open())
	{
		cout << "文件不存在" << endl;
		ifs.close();
		return;
	}

	int u_id = 0;
	string name;
	string pwd;

	//判断身份
	if (type == 1)
	{
		cout << "请输入你的学号:" << endl;
		cin >> u_id;
	}
	else if (type == 2)
	{
		cout << "请输入您的职工号: " << endl;
		cin >> u_id;
	}

	cout << "请输入用户名:" << endl;
	cin >> name;

	cout << "请输入密码:" << endl;
	cin >> pwd;

	if (type == 1)//学生身份验证
	{
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd )
		{
			if (fId == u_id && fName == name && fPwd == pwd)
			{
				cout << "学生验证登录成功!" << endl;
				system("pause");
				system("cls");
				person = new Student(u_id, name, pwd);
				studentMenu(person); //进入学生身份的子菜单
				return;
			}
		}
	}
	else if (type == 2)//教师身份验证
	{
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd)
		{
			if (fId == u_id && fName == name && fPwd == pwd)
			{
				cout << "教师验证登录成功!" << endl;
				system("pause");
				system("cls");
				person = new Teacher(u_id, name, pwd);
				teacherMenu(person);
				return;
			}
		}

	}
	else if (type == 3)//管理员身份验证
	{
		string fName;
		string fPwd;
		while (ifs >> fName && ifs >> fPwd)
		{
			if (name == fName && pwd == fPwd)
			{
				cout << "管理员验证登录成功!" << endl;
				system("pause");
				system("cls");
				person = new Manager(name, pwd);
				managerMenu(person);
				return;
			}
		}
	}

	cout << "验证登录失败!" << endl;
	system("pause");
	system("cls");

	return;
}
void studentMenu(Identify *student) //学生子菜单
{
	while(true)
	{
		student->openMenu(); // 调用菜单界面,实现多态
		Student *p= (Student *)student; // 定义学生指针接管基类对象
		
		int select=0;
		cin>>select;

		if(select == 1) // 选择预约
		{
			p->applyOrder();
		}
		else if(select == 2) // 查看预约
		{
			p->showOrder();
		}
		else
		{
			delete student;
			cout << "注销成功" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}
void teacherMenu(Identify *teacher) //教师子菜单
{
	while(true)
	{
		teacher->openMenu();
		Teacher *p= (Teacher *)teacher;
		
		int select;
		cin>>select;

		if(select == 1) // 查看预约
		{
			p->showOrder();
		}
		else
		{
			delete teacher;
			cout << "注销成功" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}
void managerMenu(Identify *manager) //管理员子菜单
{
	while(true)
	{
		manager->openMenu();
		Manager *p= (Manager *)manager;
		
		int select;
		cin>>select;

		if(select == 1) // 查看人员信息
		{
			p->showPerson();
		}
		else if(select == 2) // 查看机房信息
		{
			p->showComputer()
		}
		else if(select == 3) // 查看预约信息
		{
			p->showOrder()
		}
		else
		{
			delete manager;
			cout << "注销成功" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}
int main()
{
	int select = 0;

	while (true)
	{
		cout << "======================  欢迎来到计算中心预约管理系统v1.0(XTU)  =====================" << endl;
		cout << endl << "请选择您的身份" << 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 << "输入您的选择: ";

		cin >> select;
		initComputerRoom();
		switch (select) //根据用户选择 实现不同接口
		{
			case 1: //学生身份
				Login(STUDENT_FILE, 1);
				break;
			case 2: //老师身份
				Login(TEACHER_FILE, 2);
				break;
			case 3:  //管理员身份
				Login(ADMIN_FILE, 3);
				break;
			case 0:  //退出系统
				cout << "欢迎下一次使用" << endl;
				system("pause");
				return 0;
				break;
			default:
				cout << "输入有误,请重新选择!" << endl;
				system("pause");
				system("cls");
				break;
		}
	}

	system("pause");
	return 0;
}

代码文件

  • 完整代码文件如:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值