C++练习题2:机房预约系统

学校现有几个规格不同的机房,由于使用时经常出现"撞车"现象,现开发一套机房预约系统,解决这一问题。

分别有三种身份使用该程序

学生代表:申请使用机房;教师:审核学生的预约申请;管理员:给学生、教师创建账号

机房总共有3间

* 1号机房   --- 最大容量20人

* 2号机房   --- 最多容量50人

* 3号机房   --- 最多容量100人

申请简介

* 申请的订单每周由管理员负责清空。

* 学生可以预约未来一周内的机房使用,预约的日期为周一至周五,预约时需要选择预约时段(上午、下午)

* 教师来审核预约,依据实际情况审核预约通过或者不通过

系统具体需求

* 首先进入登录界面,可选登录身份有:

  * 学生代表* 老师* 管理员* 退出

*每个身份都需要进行验证后,进入子菜单

  * 学生需要输入 :学号、姓名、登录密码

  * 老师需要输入:职工号、姓名、登录密码

  * 管理员需要输入:管理员姓名、登录密码

* 学生具体功能

  * 申请预约    ---   预约机房

  * 查看自身的预约    ---  查看自己的预约状态

  * 查看所有预约   ---   查看全部预约信息以及预约状态

  * 取消预约    ---   取消自身的预约,预约成功或审核中的预约均可取消

  * 注销登录    ---   退出登录

* 教师具体功能

  * 查看所有预约   ---   查看全部预约信息以及预约状态

  * 审核预约    ---   对学生的预约进行审核

  * 注销登录    ---   退出登录

* 管理员具体功能

  * 添加账号    ---   添加学生或教师的账号,需要检测学生编号或教师职工号是否重复

  * 查看账号    ---   可以选择查看学生或教师的全部信息

  * 查看机房    ---   查看所有机房的信息

  * 清空预约    ---   清空所有预约记录

  * 注销登录    ---   退出登录

具体的视频链接可参考 :

黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili

程序附:和视频中的不太一样,可不用。

 机房预约系统.cpp

#include"functional.h"


int main()
{
	functional f;
	while (true)
	{
		int choice;
		cout << "======================  机房预约系统  ====================="
			<< 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 >> choice;
		if (cin.good())//判断输入类型是否匹配,如果这里是123lin,还会存在流关闭的情况,这里就不再详细说了
		{
			switch (choice)
			{
			case 1://学生代表
				f.LoginIn(STUDENT_FILE, 1);
				break;
			case 2://老师代表
				f.LoginIn(TEACHER_FILE, 2);
				break;
			case 3://管理员代表
				f.LoginIn(ADMIN_FILE, 3);
				break;
			case 0://退出
				cout << "欢迎下一次使用" << endl;
				system("pause");
				return 0;
				break;
			default:
				cout << "输入有误,重新输入!" << endl;
				system("pause");
				system("cls");
				break;
			}
		}
		else
		{
			cout << "输入非数字,重新输入!" << endl;//本来一个int型choice,但却输入了一个char型,此时你再输入根本输入不进去,char型一直占着位置,failbit置1,你可以输出cin.fail()查看当前的failbit值,
		//因此首先要讲流标志复位cin.clear()。然后,并且取走刚才流中的字符cin.ignore()
			cin.clear();
			cin.ignore();
			system("pause");
			system("cls");
		}
	}
	

	system("pause");
	return 0;
}

 globalFile.h

#pragma once


//管理员文件
#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"

 ComputerRoom.h

#pragma once
#include
#include
#include
#include"globalFile.h"
using namespace std;

class ComputerRoom
{
public:
	ComputerRoom() {  }

	int c_num;
	int c_Max;
};

class OrderFile
{
public:
	OrderFile();
	void updateOrder();
	void parseOrder();
	map> m_OrderDate;
	int m_size;

};

OrderFile::OrderFile()
{
	m_OrderDate.clear();
	this->m_size = 0;
	ifstream ifs;
	ifs.open(ORDER_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件不存在!" << endl;
		return;
	}
	char c;
	ifs >> c;
	while (ifs.eof())
	{
		cout << "文件为空!" << endl;
		ifs.close();
		return;
	}
	ifs.putback(c);
	string date;      //日期
	string interval;  //时间段
	string stuId;     //学生编号
	string stuName;   //学生姓名
	string roomId;    //机房编号
	string status;    //预约状态
	while (ifs>>date&& ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId && ifs>>status)
	{
		string key;
		string value;
		map temp;
		int pos = -1;
		pos=date.find(":");
		key = date.substr(0, pos);
		value = date.substr(pos + 1, date.size() - 1);
		temp.insert(make_pair(key, value));

		pos = interval.find(":");
		key = interval.substr(0, pos);
		value = interval.substr(pos + 1, date.size() - 1);
		temp.insert(make_pair(key, value));

		pos = stuId.find(":");
		key = stuId.substr(0, pos);
		value = stuId.substr(pos + 1, date.size() - 1);
		temp.insert(make_pair(key, value));

		pos = stuName.find(":");
		key = stuName.substr(0, pos);
		value = stuName.substr(pos + 1, date.size() - 1);
		temp.insert(make_pair(key, value));

		pos = roomId.find(":");
		key = roomId.substr(0, pos);
		value = roomId.substr(pos + 1, date.size() - 1);
		temp.insert(make_pair(key, value));

		pos = status.find(":");
		key = status.substr(0, pos);
		value = status.substr(pos + 1, date.size() - 1);
		
		temp.insert(make_pair(key, value));
		m_OrderDate.insert(make_pair(m_size, temp));
		this->m_size++;
		temp.clear();
	}
	ifs.close();	
	//测试代码
	/*for (map>::iterator it = m_OrderDate.begin(); it != m_OrderDate.end();it++)
	{
		cout << "key = " << it->first << " value = " << endl;
		for (map::iterator mit = it->second.begin(); mit != it->second.end(); mit++)
		{
			cout << mit->first << " " << mit->second << " ";
		}
		cout << endl;
	}*/

}

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

void OrderFile::parseOrder()
{
	if (this->m_size == 0)
	{
		cout << "无记录" << endl;
		return;
	}
	for (map>::iterator mit = this->m_OrderDate.begin(); mit != this->m_OrderDate.end(); mit++)
	{
		cout << "预约日期: 周" << (*mit).second["date"];
		cout << " 时段:" << ((*mit).second["interval"] == "1" ? "上午" : "下午");
		cout << " 学生学号:" << (*mit).second["stuId"];
		cout << " 学生姓名:" << (*mit).second["stuName"];
		cout << " 机房:" << (*mit).second["roomId"];
		string status = " 状态: ";  // 0 取消的预约    1 审核中    2 已预约    -1 预约失败
		if ((*mit).second["status"] == "1")
		{
			status += "审核中";
		}
		else if ((*mit).second["status"] == "2")
		{
			status += "预约成功";
		}
		else if ((*mit).second["status"] == "-1")
		{
			status += "审核未通过,预约失败";
		}
		else
		{
			status += "预约已取消";
		}
		cout << status << endl;
	}
	system("pause");
	system("cls");
}

 functional.h

#pragma once
#include
#include
#include
#include"ComputerRoom.h"
using namespace std;
#include"globalFile.h"
#include"Identity AND student.hpp"
#include"Teacher AND manger.hpp"


class functional
{
public:
	void LoginIn(string filename, int type);
	void mangerMenu(Identity* &m);
	void studentMenu(Identity*& m);
	void teacherMenu(Identity*& m);
};


void functional::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 id = 0;
	string name;
	string pwd;

	if (type == 1)//学生代表
	{
		cout << "请输入学号:"; cin >> id;
		cout << "请输入用户名:"; cin >> name;
		cout << "请输入密码:"; cin >> pwd;
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd)
		{
			if (fId == id && fName == name && fPwd == pwd)
			{
				cout << "登陆成功!" << endl;
				system("pause");
				system("cls");
				Person = new Student(id, name, pwd);
				//进入子类同学的单元序列
				this->studentMenu(Person);
				return;
			}
		}
	}
	else if (type == 2)//老师
	{
		cout << "请输入工号:"; cin >> id;
		cout << "请输入用户名:"; cin >> name;
		cout << "请输入密码:"; cin >> pwd;
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd)
		{
			if (fId == id && fName == name && fPwd == pwd)
			{
				cout << "登陆成功!" << endl;
				system("pause");
				system("cls");
				Person = new Teacher(id, name, pwd);
				//进入子类教师的单元序列
				this->teacherMenu(Person);
				return;
			}
		}


	}
	else//管理员
	{
		cout << "请输入用户名:"; cin >> name;
		cout << "请输入密码:"; cin >> pwd;
		string fName;
		string fPwd;
		while (ifs >> fName && ifs >> fPwd)
		{
			if (fName == name && fPwd == pwd)
			{
				cout << "登陆成功!" << endl;
				system("pause");
				system("cls");
				Person = new Manger(name, pwd);
				//进入子类管理员的单元序列
				this->mangerMenu(Person);
				return;
			}
		}
	}
	cout << "登陆失败!" << endl;
	system("pause");
	system("cls");
	return;
}

void functional::mangerMenu(Identity * &m)
{
	while (true)
	{
		m->operMenu();
		Manger* admin = (Manger*) m;
		int select;
		cin >> select;
		switch (select)
		{
		case 1:admin->addPerson(); break;
		case 2:admin->showPerson(); break;
		case 3:admin->showComputer(); break;
		case 4:admin->clearFile(); break;
		case 0:
			delete m;
			cout << "注销登录成功!" << endl;
			system("pause");
			system("cls");
			return;
			//注销操作!
		
		default:
			cout << "输入有误,请重新输入!" << endl;
			system("pause");
			system("cls");
			break;
		}
	}
}

void functional::studentMenu(Identity*& m)
{
	while (true)
	{
		m->operMenu();
		Student* stu = (Student*)m;//强转至Student类
		int select;
		cin >> select;
		switch (select)
		{
		case 1:stu->applyOrder(); break;
		case 2:stu->showMyOrder(); break;
		case 3:stu->showAllOrder(); break;
		case 4:stu->cancelOrder(); break;
		case 0:
			delete m;
			cout << "注销登录成功!" << endl;
			system("pause");
			system("cls");
			return;
			//注销操作!

		default:
			cout << "输入有误,请重新输入!" << endl;
			system("pause");
			system("cls");
			break;
		}
	}
}

void functional::teacherMenu(Identity*& m)
{
	while (true)
	{
		m->operMenu();
		Teacher* t = (Teacher*)m;//强转至Student类
		int select;
		cin >> select;
		switch (select)
		{
		case 1:t->showAllOrder(); break;
		case 2:t->validOrder(); break;
		case 0:
			delete m;
			cout << "注销登录成功!" << endl;
			system("pause");
			system("cls");
			return;
			//注销操作!

		default:
			cout << "输入有误,请重新输入!" << endl;
			system("pause");
			system("cls");
			break;
		}
	}
}

Teacher AND manger.hpp

#pragma once
#include 
#include"Identity AND student.hpp"
#include
#include
#include
using namespace std;

class Teacher:public Identity
{
public:
	Teacher() {  }
	Teacher(int id,string name, string pwd );
	void operMenu();
	//查看所有预约
	void showAllOrder();
	// 审核预约
	void validOrder();
	
	
	int m_empId;
};


Teacher::Teacher(int id,string name, string pwd)
{
	this->m_Name = name; this->m_Pwd = pwd; this->m_empId = id;
}


void Teacher::operMenu()
{
	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 Teacher::showAllOrder()
{
	OrderFile of;
	of.parseOrder();
}

void Teacher::validOrder()
{
	OrderFile of;
	if (of.m_size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}
	int index = 0;
	map query;
	//显示可取消预约的信息
	cout << "可审核的预约信息!" << endl;
	for (int i = 0; i < of.m_size; i++)
	{
			if (atoi(of.m_OrderDate[i]["status"].c_str()) == 1)//1是预约待审核、2是预约成功
			{
				index++;
				cout << index;
				cout << "预约日期: 周" << of.m_OrderDate[i]["date"];
				cout << " 时段:" << (of.m_OrderDate[i]["interval"] == "1" ? "上午" : "下午");
				cout << " 机房:" << of.m_OrderDate[i]["roomId"];
				string status = " 状态: ";  // 0 取消的预约    1 审核中    2 已预约    -1 预约失败
				status += "审核中";
				cout << status << endl;
				query.insert(make_pair(index, i));
			}
	}
	if (index==0)
	{
		cout << "无符合条件的预约信息!" << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "请输入通过审核的编号:" << endl;
	int choice = 0; cin >> choice;
	if (choice > 0 && choice <= index)//判断输入的choice在index之间
	{
		of.m_OrderDate[query[choice]]["status"] = "2";
		of.updateOrder();
		cout << "已通过该预约" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "输入非法!默认重新输入!" << endl;
		system("pause");
		system("cls");
	}
}



void printStudent(Student& s)//要知道algorithm中for_each的底层,才知道第三个参数是规定打印格式,这时候定义函数的时候一定要接受一个操作对象
{
	cout << "学生学号:" << s.m_Id << "\t学生姓名:" << s.m_Name << "\t学生密码:" << s.m_Pwd << endl;
}
void printTeacher(Teacher& s)
{
	cout << "教师工号:" << s.m_empId << "\t教师姓名:" << s.m_Name << "\t教师密码:" << s.m_Pwd << endl;
}

void PrintComputer(ComputerRoom & s)
{
	cout << "教室编号:" << s.c_num << "\t教室容量:" << s.c_Max << endl;
}





class Manger :public Identity
{
public:
	Manger(string name, string pwd);
	
	void operMenu();
	//添加账号
	void addPerson();

	//查看现有账号
	void showPerson();
	//查看机房使用情况
	void showComputer();
	// 清空预约记录
	void clearFile();
	

	vector vStu;
	vector vTea;
	vector vCom;
	void initVector();
};

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

void Manger::operMenu()
{
	cout << "欢迎管理员:" <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|          4.清空预约            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          0.注销登录            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t ---------------------------------\n";
	cout << "请选择您的操作: " << endl;

}
void Manger::addPerson()
{
	cout << "请选择添加的对象:" << endl << "1.添加学生" << endl << "2.添加老师" << endl;
	int choice; cin >> choice;
	string name; string psd; int id;
	
	if (choice == 1)
	{
		//添加学生
		ofstream ofs;
		ofs.open(STUDENT_FILE, ios::out | ios::app);
		while (true)
		{
			cout << "请输入学生学号:"; cin >> id;
			bool flag = true;
			for (vector::iterator it = vStu.begin(); it != vStu.end(); it++)
			{
				if ((*it).m_Id == id)
				{
					cout << "输入学号有重复!请重新输入!" << endl;
					system("pause");
					flag = false;
					break;
				}
			}
			if (flag)
			{
				cout << "输入有效!" << endl;
				break;
			}
		}
		cout << "请输入学生姓名:"; cin >> name;
		cout << "请输入学生登陆密码:"; cin >> psd;
		Student temp(id, name, psd);
		vStu.push_back(temp);
		ofs << id << " " << name << " " << psd << endl;
		ofs.close();
	}
	else
	{
		//添加老师
		ofstream ofs;
		ofs.open(TEACHER_FILE, ios::out | ios::app);
		while (true)
		{
			cout << "请输入教师工号:"; cin >> id;
			bool flag = true;
			for (vector::iterator it = vTea.begin(); it != vTea.end(); it++)
			{
				if ((*it).m_empId== id)
				{
					cout << "输入工号有重复!请重新输入!" << endl;
					system("pause");
					flag = false;
					break;
				}
			}
			if (flag)
			{
				cout << "输入有效!" << endl;
				break;
			}
		}
		cout << "请输入教师姓名:"; cin >> name;
		cout << "请输入教师登陆密码:"; cin >> psd;
		Teacher temp(id, name, psd);
		vTea.push_back(temp);
		ofs << id << " " << name << " " << psd<> choice;
	if (choice == 1)
	{
		for_each(vStu.begin(), vStu.end(), printStudent);
	}
	else 
	{
		for_each(vTea.begin(), vTea.end(), printTeacher);//printTeacher函数接收(*it)的类型
	}
	system("pause");
	system("cls");
}
//for_each中的打印业务


void Manger::showComputer()
{
	for_each(vCom.begin(), vCom.end(), PrintComputer);
	system("pause");
	system("cls");
}
void Manger::clearFile()
{
	ofstream ofs;
	ofs.open(ORDER_FILE, ios::trunc);
	cout << "记录已清空!" << endl;
	ofs.close();
	system("pause");
	system("cls");
}

void Manger::initVector()
{
	this->vStu.clear();
	this->vTea.clear();
	this->vCom.clear();
	
	ifstream ifs;
	ifs.open(STUDENT_FILE, ios::in);
	Student stu;
	while (ifs>>stu.m_Id&&ifs>>stu.m_Name&&ifs>>stu.m_Pwd)
	{
		vStu.push_back(stu);
	}
	ifs.close();


	ifs.open(TEACHER_FILE, ios::in);
	Teacher tea;
	while (ifs >> tea.m_empId && ifs >> tea.m_Name && ifs >> tea.m_Pwd)
	{
		vTea.push_back(tea);
	}
	ifs.close();

	ifs.open(COMPUTER_FILE, ios::in);
	ComputerRoom com;
	while (ifs >> com.c_num&& ifs>>com.c_Max)
	{
		vCom.push_back(com);
	}
	ifs.close();
}

 Teacher AND manger.hpp

#pragma once
#include 
#include
#include
#include"ComputerRoom.h"

using namespace std;

class Identity {
public:
	virtual void operMenu() = 0;
	string m_Name;
	string m_Pwd;
};

class Student:public Identity
{
public:
	Student(){  }
	Student(int id,string name,string pwd);
	void operMenu();
	//申请预约
	void applyOrder();
	//查看自身预约
	void showMyOrder();
	//查看所有预约
	void showAllOrder();
	//取消预约
	void cancelOrder();

	vector vCom;
	int m_Id;
};

Student::Student(int id,string name, string pwd)
{
	this->m_Name = name; this->m_Pwd = pwd; this->m_Id = id;
	
	this->vCom.clear();
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);
	ComputerRoom com;
	while (ifs >> com.c_num && ifs >> com.c_Max)
	{
		vCom.push_back(com);
	}
	ifs.close();
}

void Student::operMenu()
{
	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|          4.取消预约              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          0.注销登录              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t ----------------------------------\n";
	cout << "请选择您的操作: " << endl;
}
void Student::applyOrder()
{
	cout << "-----机房开放时间为周一至周五!-----" << endl;
	cout << "请输入申请预约的时间:" << endl;
	cout << "1、周一" << endl;
	cout << "2、周二" << endl;
	cout << "3、周三" << endl;
	cout << "4、周四" << endl;
	cout << "5、周五" << endl;
	int date = 0;
	int interval = 0;
	int room = 0;
	while (true)
	{
		cin >> date;
		if (date > 0 && date < 6)
		{
			break;
		}
		cout << "输入有误,重新输入!" << endl;
	}
	cout << "-----请输入申请预约的时间段:-----" << endl;
	cout << "1、上午" << endl;
	cout << "2、下午" << endl;
	while (true)
	{
		cin >> interval;
		if (interval >= 1 && interval <= 2)
		{
			break;
		}
		cout << "输入有误,请重新输入" << endl;
	}
	cout << "-----请选择机房:-----" << endl;
	cout << "1号机房容量:" << vCom[0].c_Max<< endl;
	cout << "2号机房容量:" << vCom[1].c_Max<< endl;
	cout << "3号机房容量:" << vCom[2].c_Max << endl;

	while (true)
	{
		cin >> room;
		if (room >= 1 && room <= 3)
		{
			break;
		}
		cout << "输入有误,请重新输入" << endl;
	}

	cout << "预约成功!审核中" << endl;
	ofstream ofs;
	ofs.open(ORDER_FILE, ios::out | ios::app);
	ofs << "date:" << date << " ";
	ofs << "interval:" << interval << " ";
	ofs << "stuId:" << this->m_Id << " ";
	ofs << "stuName:" << this->m_Name << " ";
	ofs << "roomId:" << room << " ";
	ofs << "status:" << 1 << endl;//状态为申请中

	ofs.close();

	system("pause");
	system("cls");//是不是要考虑实时更新的问题?具体见老师里


}
void  Student::showMyOrder()
{
	OrderFile of;
	if (of.m_size == 0)
	{
		cout << "无记录" << endl;
		return;
	}
	int flag = 0;
	for (map>::iterator mit = of.m_OrderDate.begin(); mit != of.m_OrderDate.end(); mit++)
	{
		for (int j=0;j<5;j++)
		{
			if (atoi((*mit).second["stuId"].c_str())== this->m_Id)//已知字符串是string类的数字类,然后将他转为int型,前有atoi后有c_str()
			{
				cout << "预约日期: 周" << (*mit).second["date"];
				cout << " 时段:" << ((*mit).second["interval"] == "1" ? "上午" : "下午");
				cout << " 机房:" << (*mit).second["roomId"];
				string status = " 状态: ";  // 0 取消的预约    1 审核中    2 已预约    -1 预约失败
				if ((*mit).second["status"] == "1")
				{
					status += "审核中";
				}
				else if ((*mit).second["status"] == "2")
				{
					status += "预约成功";
				}
				else if ((*mit).second["status"] == "-1")
				{
					status += "审核未通过,预约失败";
				}
				else
				{
					status += "预约已取消";
				}
				cout << status << endl;
				flag = 1;
				break;
			}
		}
	}
	if (flag == 0)
	{
		cout << "未找到预约申请" << endl;
	}
	system("pause");
	system("cls");
}
void Student::showAllOrder()
{
	OrderFile of;
	of.parseOrder();
}
void Student::cancelOrder()
{
	OrderFile of;
	int index=0;
	map query;
	//显示可取消预约的信息
	cout << "可取消的预约信息!" << endl;
	for (int i = 0; i m_Id)
		{
			if (atoi(of.m_OrderDate[i]["status"].c_str()) == 1 || atoi(of.m_OrderDate[i]["status"].c_str()) == 2)//1是预约待审核、2是预约成功
			{
				index++;
				cout << index;
				cout << "预约日期: 周" << of.m_OrderDate[i]["date"];
				cout << " 时段:" << (of.m_OrderDate[i]["interval"] == "1" ? "上午" : "下午");
				cout << " 机房:" << of.m_OrderDate[i]["roomId"];
				string status = " 状态: ";  // 0 取消的预约    1 审核中    2 已预约    -1 预约失败
				if (of.m_OrderDate[i]["status"] == "1")
				{
					status += "审核中";
				}
				else if (of.m_OrderDate[i]["status"] == "2")
				{
					status += "预约成功";
				}
				cout << status << endl;
				query.insert(make_pair(index, i));
			}
		}
	}
	cout << "请输入欲取消的记录编号:" << endl;
	int choice = 0; cin >> choice;
	if ( choice>0&&choice<=index)//判断输入的choice在index之间
	{
		of.m_OrderDate[query[choice]]["status"] ="0";
		of.updateOrder();
		cout << "已取消预约" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "输入非法!默认重新输入!" << endl;
		system("pause");
		system("cls");
	}
}

———2021.11.04

你可能感兴趣的:(STL,C++,c++,开发语言)