C++机房预约系统

机房预约系统

1、系统简介

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

2、身份简介:

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

3、机房简介

机房一共有3间:

  • 1号机房——最大容量20人
  • 2号机房——最大容量50人
  • 3号机房——最大容量100人

4、申请简介

  • 申请的订单每周由管理员负责清空
  • 学生可以预约未来一周内的机房使用,预约的日期为周一至周五,预约时需要选择预约时段(上午、下午)
  • 教师来审核预约,根据实际情况审核通过或者不通过

5、系统具体需求

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

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

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

  • 学生需要输入:学号、姓名、登录密码
  • 老师需要输入:职工号、姓名、登录密码
  • 管理员需要输入:管理员姓名、登录密码

学生具体功能

  • 申请预约——预约机房
  • 查看自身的预约——查看自己的预约状态
  • 查看所有预约——查看全部预约信息以及预约状态
  • 取消预约—— 取消自身的预约,预约成功或审核中的预约均可取消

教师具体功能

  • 查看所有预约——查看全部预约信息以及预约状态
  • 审核预约——对学生的预约进行审核
  • 注销登录——退出登录

管理员具体功能

  • 添加账号——添加学生或教师的账号,需要检测学生编号或教师职工号是否重复
  • 查看账号——可以选择查看学生或教师的全部信息
  • 查看机房——查看所有机房的信息
  • 清空预约——清空所有预约记录
  • 注销登录——退出登录

c4d601db2f3e40c48e4b0769ab392163.pngcaf1882a802947bfa00011ef6df161bf.png

 

在文本文件中添加机房信息(房间号和最大容量)和管理员信息(姓名和登录密码),

老师和学生的账号可以通过登录管理员,选择添加账号进行添加,也可直接在文本文件中添加。

337346d2b0ac4a97a18c9813eaf93c37.pngdee7515ffc6c4e02ac9286c5b841f3ac.png

 管理员登录菜单:

96746ddad37f49a586d00a491d2142ff.png

管理员添加账号:

9e41024e1c6a498db81a5e23cbe3a585.png

 

学生代表登录菜单:

dbf0a8a8174b4981bef274e8b08792eb.png

老师登录菜单:

fc1157d8cc874f7fa11e696e78b16e62.png

 代码:

 computer_room.h

#pragma once
#include <iostream>

using namespace std;

//机房类
class computer_room
{
public:
	int com_id;  //机房id号
	int max_num;  //机房最大容量
};

 identity.h

#pragma once  //防止头文件重复包含
#include <iostream>

using namespace std;

//身份抽象基类
class identity
{
public:
	virtual void opermenu() = 0;  //操作菜单  纯虚函数
	string name;  //用户名
	string pwd;  //密码
};

manager.h

#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include "student.h"
#include "teacher.h"
#include "identity.h"
#include "computer_room.h"

using namespace std;

#define ADMIN_FILE "admin.txt"  //管理员文件 
#define STUDENT_FILE "student.txt"  //学生文件
#define TEACHER_FILE "teacher.txt"  //教师文件
#define COMPUTER_FILE "computer.txt"  //机房信息文件
#define ORDER_FILE "order.txt"  //订单文件

class manager :public identity
{
public:
	manager();
	manager(string name, string pwd);
	virtual void opermenu();  //选择菜单
	void add_person();  //添加账号
	void show_person();  //查看账号
	void show_computer();  //查看机房信息
	void clean_file();  //清空预约记录
	void init_vector();  //初始化容器
	bool check_repeat(int id, int type);  //检测重复
	vector<student>vstu;  //学生容器
	vector<teacher>vtea;  //教师容器
	vector<computer_room>vcom;  //机房信息
};

 order_file.h

#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

#define ADMIN_FILE "admin.txt"  //管理员文件 
#define STUDENT_FILE "student.txt"  //学生文件
#define TEACHER_FILE "teacher.txt"  //教师文件
#define COMPUTER_FILE "computer.txt"  //机房信息文件
#define ORDER_FILE "order.txt"  //订单文件

class order_file
{
public:

	//构造函数
	order_file();

	//更新预约记录
	void update_order();

	//记录预约条数
	int size;

	//记录所有预约信息的容器
	map<int, map<string, string>> order_data;
};

 student.h

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "identity.h"
#include "computer_room.h"
#include "order_file.h"

using namespace std;

#define ADMIN_FILE "admin.txt"  //管理员文件 
#define STUDENT_FILE "student.txt"  //学生文件
#define TEACHER_FILE "teacher.txt"  //教师文件
#define COMPUTER_FILE "computer.txt"  //机房信息文件
#define ORDER_FILE "order.txt"  //订单文件

class student :public identity
{
public:
	student();
	student(int id, string name, string pwd);
	virtual void opermenu();  //菜单界面
	void apply_order();  //申请预约
	void show_my_order();  //查看我的预约
	void show_all_order();  //查看所有预约
	void cancel_order();  //取消预约
	int id;  //学号
	vector<computer_room> vcom;  //机房容器
};

teacher.h

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "identity.h"
#include "order_file.h"

using namespace std;

class teacher :public identity
{
public:
	teacher();  //默认构造
	teacher(int id, string name, string pwd);  //有参构造
	virtual void opermenu();  //菜单界面
	void show_all_order();  //查看所有预约
	void valid_order();  //审核预约
	int empid;  //职工号
};

manager.cpp

#include "manager.h"

manager::manager()
{
}

manager::manager(string name, string pwd)
{
	//初始化管理员信息
	this->name = name;
	this->pwd = pwd;

	//初始化容器 获取到所有文件中 学生、老师信息
	init_vector();

	//初始化机房信息
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);

	computer_room com;
	while (ifs >> com.com_id && ifs >> com.max_num)
	{
		vcom.push_back(com);
	}
	ifs.close();
}

//选择菜单
void manager::opermenu()
{
	cout << "欢迎管理员:" << this->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 << "请输入您的选择: ";
}

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

	string file_name;  //操作的文件名
	string tip;  //提示id号
	string error_tip;  //重复错误提示

	ofstream ofs;  //文件操作对象

	int select;
	cin >> select;

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

		//利用追加的方式 写文件
		ofs.open(file_name, ios::out | ios::app);

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

		cout << tip << endl;

		while (true)
		{
			cin >> id;
			bool ret = check_repeat(id, select);
			if (ret)
				cout << error_tip << endl;
			else
				break;
		}

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

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

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

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

		ofs.close();

		//调用初始化容易接口,重新获取文件中的数据
		init_vector();
}

void print_student(student &s)
{
	cout << "学号: " << s.id << "   姓名: " << s.name << "   密码: " << s.pwd << endl;
}

void print_teacher(teacher& t)
{
	cout << "职工号: " << t.empid << "   姓名: " << t.empid << "   密码: " << t.pwd << endl;
}

//查看账号
void manager::show_person()
{
	cout << "请选择查看内容:" << endl;
	cout << "1、查看所有学生" << endl;
	cout << "2、查看所有老师" << endl;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		//查看学生
		cout << "所有学生信息如下:" << endl;
		for_each(vstu.begin(), vstu.end(), print_student);
	}
	else
	{
		//查看老师
		cout << "所有老师信息如下:" << endl;
		for_each(vtea.begin(), vtea.end(), print_teacher);
	}
	system("pause");
	system("cls");
}

//查看机房信息
void manager::show_computer()
{
	cout << "机房信息如下: " << endl;
	for (vector<computer_room>::iterator it = vcom.begin(); it != vcom.end(); it++)
	{
		cout << "机房编号: " << it->com_id << " 机房最大容量: " << it->max_num << endl;
	}
	system("pause");
	system("cls");
}

//清空预约记录
void manager::clean_file()
{
	ofstream ofs(ORDER_FILE, ios::trunc);
	ofs.close();

	cout << "清空成功!" << endl;
	system("pause");
	system("cls");
}

//初始化容器
void manager::init_vector()
{
	//确保容器清空状态
	vstu.clear();
	vtea.clear();

	//读取信息  老师、学生
	ifstream ifs;
	ifs.open(STUDENT_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件读取失败" << endl;
		return;
	}
	student s;
	while (ifs >> s.id && ifs >> s.name && ifs >> s.pwd)
	{
		vstu.push_back(s);
	}
	ifs.close();

	ifs.open(TEACHER_FILE, ios::in);
	teacher t;
	while (ifs >> t.empid && ifs >> t.name && ifs >> t.pwd)
	{
		vtea.push_back(t);
	}
	ifs.close();
}

//检测重复
bool manager::check_repeat(int id, int type)
{
	if (type == 1)
	{
		//检测学生
		for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); it++)
		{
			if (id == it->id) return true;
		}
	}
	else
	{
		//检测老师
		for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end(); it++)
		{
			if (id == it->empid) return true;
		}
	}

	return false;
}

order_file.cpp

#include "order_file.h"

order_file::order_file()
{
	ifstream ifs;
	ifs.open(ORDER_FILE, ios::in);

	string date;  //日期
	string interval;  //时间段
	string stu_id;  //学生编号
	string stu_name;  //学生姓名
	string room_id;  //机房编号
	string status;  //预约状态

	this->size = 0;  //记录条数

	while (ifs >> date && ifs >> interval && ifs
		>> stu_id && ifs >> stu_name && ifs >> room_id && ifs >> status)
	{
		/*cout << data << endl;
		cout << interval << endl;
		cout << stu_id << endl;
		cout << stu_name << endl;
		cout << room_id << endl;
		cout << status << endl;
		cout << endl;*/

		string key;
		string value;
		map<string, string>m;

		int pos = date.find(":");  
		if (pos != -1)
		{
			key = date.substr(0, pos);
			value = date.substr(pos + 1, date.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取时间段
		pos = interval.find(":");
		if (pos != -1)
		{
			key = interval.substr(0, pos);
			value = interval.substr(pos + 1, interval.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取学号
		pos = stu_id.find(":");
		if (pos != -1)
		{
			key = stu_id.substr(0, pos);
			value = stu_id.substr(pos + 1, stu_id.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取姓名
		pos = stu_name.find(":");
		if (pos != -1)
		{
			key = stu_name.substr(0, pos);
			value = stu_name.substr(pos + 1, stu_name.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取机房编号
		pos = room_id.find(":");
		if (pos != -1)
		{
			key = room_id.substr(0, pos);
			value = room_id.substr(pos + 1, room_id.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取预约状态
		pos = status.find(":");
		if (pos != -1)
		{
			pos = status.find(":");
			key = status.substr(0, pos);
			value = status.substr(pos + 1, status.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//将小map容器放入到大的map容器中
		this->order_data.insert(make_pair(this->size, m));
		this->size++;
	}
	ifs.close();
}

//更新预约系统
void order_file::update_order()
{
	if (this->size == 0) return;
	ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
	for (int i = 0; i < this->size; i++)
	{
		ofs << "date:" << this->order_data[i]["date"] << " ";
		ofs << "interval:" << this->order_data[i]["interval"] << " ";
		ofs << "stu_id:" << this->order_data[i]["stu_id"] << " ";
		ofs << "stu_name:" << this->order_data[i]["stu_name"] << " ";
		ofs << "room_id:" << this->order_data[i]["room_id"] << " ";
		ofs << "status:" << this->order_data[i]["status"] << endl;
	}
	ofs.close();
}

student.cpp

#include "student.h"

//默认构造
student::student()
{
}

//有参构造
student::student(int id, string name, string pwd)
{
	//初始化属性
	this->id = id;
	this->name = name;
	this->pwd = pwd;

	//初始化机房信息
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);

	computer_room com;
	while (ifs >> com.com_id && ifs >> com.max_num)
	{
		//将读取的信息放入到容器中
		vcom.push_back(com);
	}
	ifs.close();
}

//菜单界面
void student::opermenu()
{
	cout << "欢迎学生代表:" << this->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 << "请输入您的选择: ";
}

//申请预约
void student::apply_order()
{
	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 >= 1 && date <= 5) 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;
	for (int i = 0; i < vcom.size(); i++)
		cout << vcom[i].com_id << "号机房的容量为: " << vcom[i].max_num << endl;

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

	cout << "预约成功!审核中" << endl;

	ofstream ofs;
	ofs.open(ORDER_FILE, ios::app);

	ofs << "date:" << date << " ";
	ofs << "interval:" << interval << " ";
	ofs << "stu_id:" << this->id << " ";
	ofs << "stu_name:" << this->name << " ";
	ofs << "room_id:" << room << " ";
	ofs << "status:" << 1 << " ";

	ofs.close();

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

//查看我的预约
void student::show_my_order()
{
	order_file of;
	if (of.size == 0)
	{
		cout << "无预约记录!" << endl;
		system("pause");
		system("cls");
		return;
	}

	for (int i = 0; i < of.size; i++)
	{
		//string 转 int
		//string 利用 .c_str() 转 const char *
		//利用 atoi (const char *) 转 int
		if (this->id == atoi(of.order_data[i]["stu_id"].c_str()))  //找到自身预约
		{
			cout << "预约日期: 周" << of.order_data[i]["date"];
			cout << "  时间段:" << (of.order_data[i]["interval"] == "1" ? "上午" : "下午");
			cout << "  机房号: " << of.order_data[i]["room_id"];
			string status = "  状态:";
			//1、审核中  2、已预约成功  -1、预约失败  0、取消预约
			if (of.order_data[i]["status"] == "1")
				status += "审核中";
			else if (of.order_data[i]["status"] == "2")
				status += "预约成功";
			else if (of.order_data[i]["status"] == "-1")
				status += "预约失败,审核未通过";
			else
				status += "预约已取消";
			cout << status << endl;
		}
	}
	system("pause");
	system("cls");
}

//查看所有预约
void student::show_all_order()
{
	order_file of;
	if (of.size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}
	for (int i = 0; i < of.size; i++)
	{
		cout << i + 1 << "、";
		cout << "预约日期: 周" << of.order_data[i]["date"];
		cout << "  时间段:" << (of.order_data[i]["interval"] == "1" ? "上午" : "下午");
		cout << "  学号:" << of.order_data[i]["stu_id"];
		cout << "  姓名:" << of.order_data[i]["stu_name"];
		cout << "  机房编号: " << of.order_data[i]["room_id"];
		string status = "  状态:";
		//1、审核中  2、已预约成功  -1、预约失败  0、取消预约
		if (of.order_data[i]["status"] == "1")
			status += "审核中";
		else if (of.order_data[i]["status"] == "2")
			status += "预约成功";
		else if (of.order_data[i]["status"] == "-1")
			status += "预约失败,审核未通过";
		else
			status += "预约已取消";
		cout << status << endl;
	}

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

//取消预约
void student::cancel_order()
{
	order_file of;
	if (of.size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}

	cout << "审核中或预约成功的记录是可以取消,请输入取消的记录" << endl;
	vector<int>v;  //存放在最大容器中的下标编号
	int index = 1;
	for (int i = 0; i < of.size; i++)
	{
		//先判断是自身学号
		if (this->id == atoi(of.order_data[i]["stu_id"].c_str()))
		{
			//再筛选状态
			if (of.order_data[i]["status"] == "1" || of.order_data[i]["status"] == "2")
			{
				v.push_back(i);
				cout << index++ << "、";
				cout << "预约日期:周" << of.order_data[i]["date"];
				cout << "  时间段:" << (of.order_data[i]["interval"] == "1" ? "下午" : "上午");
				cout << "  机房编号: " << of.order_data[i]["room_id"];
				string status = "  状态:";
				//1、审核中  2、已预约成功  -1、预约失败  0、取消预约
				if (of.order_data[i]["status"] == "1")
					status += "审核中";
				else if (of.order_data[i]["status"] == "2")
					status += "预约成功";
				cout << status << endl;
			}
		}
	}

	cout << "请输入取消的记录,0代表返回" << endl;
	int select = 0;
	if (v.size() == 0)
	{
		cout <<  "暂无可取消的预约!" << endl;
	}
	else
	{
		while (true)
		{
			cin >> select;
			if (select >= 0 && select <= v.size())
			{
				if (select == 0) break;
				else
				{
					of.order_data[v[select - 1]]["status"] = "0";

					of.update_order();

					cout << "已取消成功" << endl;
					break;
				}
			}
			cout << "输入有误, 请重新输入" << endl;
		}
	}
	system("pause");
	system("cls");
}

teacher.cpp

#include "teacher.h"

//默认构造
teacher::teacher()
{
}

//有参构造
teacher::teacher(int empid, string name, string pwd)
{
	//初始化属性
	this->empid = empid;
	this->name = name;
	this->pwd = pwd;
}

//菜单界面
void teacher::opermenu()
{
	cout << "欢迎教师:" << this->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 << "请输入您的选择: ";
}

//查看所有预约
void teacher::show_all_order()
{
	order_file of;
	if (of.size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}
	for (int i = 0; i < of.size; i++)
	{
		cout << i + 1 << "、";
		cout << "预约日期: 周" << of.order_data[i]["date"];
		cout << "  时间段:" << (of.order_data[i]["interval"] == "1" ? "上午" : "下午");
		cout << "  学号:" << of.order_data[i]["stu_id"];
		cout << "  姓名:" << of.order_data[i]["stu_name"];
		cout << "  机房编号: " << of.order_data[i]["room_id"];
		string status = "  状态:";
		//1、审核中  2、已预约成功  -1、预约失败  0、取消预约
		if (of.order_data[i]["status"] == "1")
			status += "审核中";
		else if (of.order_data[i]["status"] == "2")
			status += "预约成功";
		else if (of.order_data[i]["status"] == "-1")
			status += "预约失败,审核未通过";
		else
			status += "预约已取消";
		cout << status << endl;
	}
	system("pause");
	system("cls");
}

//审核预约
void teacher::valid_order()
{
	order_file of;
	if (of.size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}
	vector<int>v;
	int index = 0;
	cout << "待审核的预约记录如下:" << endl;

	for (int i = 0; i < of.size; i++)
	{
		if (of.order_data[i]["status"] == "1")
		{
			v.push_back(i);
			cout << ++index << "、 ";
			cout << "预约日期: 周" << of.order_data[i]["date"];
			cout << "  时间段:" << (of.order_data[i]["interval"] == "1" ? "上午" : "下午");
			cout << "  学生编号:" << of.order_data[i]["stu_id"];
			cout << "  学生姓名:" << of.order_data[i]["stu_name"];
			cout << "  机房编号: " << of.order_data[i]["room_id"];
			string status = "  状态:审核中 ";
			cout << status << endl;
		}
	}
	cout << "请输入审核的预约记录, 0代表返回" << endl;
	int select = 0;
	int ret = 0;  //接受预约结果记录
	if (v.size() == 0)
	{
		cout << "暂无可审核的预约!" << endl;
	}
	else
	{
		while (true)
		{
			cin >> select;
			if (select >= 0 && select <= v.size())
			{
				if (select == 0) break;
				else
				{
					cout << "请输入审核结果" << endl;
					cout << "1、通过" << endl;
					cout << "2、不通过" << endl;
					cin >> ret;
					if (ret == 1)  //通过情况
						of.order_data[v[select - 1]]["status"] = "2";
					else  //不通过情况
						of.order_data[v[select - 1]]["status"] = "-1";

					of.update_order();  //更新预约记录
					cout << "审核完毕" << endl;
					break;
				}
			}
			cout << "输入有误,请重新输入" << endl;
		}
	}
	system("pause");
	system("cla");
}

机房预约系统.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "identity.h"
#include "student.h"
#include "teacher.h"
#include "manager.h"
#include "order_file.h"

using namespace std;

#define ADMIN_FILE "admin.txt"  //管理员文件 
#define STUDENT_FILE "student.txt"  //学生文件
#define TEACHER_FILE "teacher.txt"  //教师文件
#define COMPUTER_FILE "computer.txt"  //机房信息文件
#define ORDER_FILE "order.txt"  //订单文件

//进入学生子菜单界面
void student_menu(identity*& sstudent)
{
	while (true)
	{
		//调用学生子菜单
		sstudent->opermenu();

		student* stu = (student*)sstudent;

		int select = 0;
		cin >> select;

		if (select == 1)  //申请预约
		{
			stu->apply_order();
		}
		else if (select == 2)  //查看自身预约
		{
			stu->show_my_order();
		}
		else if (select == 3)  //查看所有预约
		{
			stu->show_all_order();
		}
		else if (select == 4)  //取消预约
		{
			stu->cancel_order();
		}
		else  //注销
		{
			delete stu;
			cout << "注销成功!" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}

//进入管理员子菜单
void manager_menu(identity*& mmanager)
{
	while (true)
	{
		//调用管理员子菜单
		mmanager->opermenu();
		 
		//将父类指针 转为子类指针,调用子类里其他接口
		manager * man = (manager*)mmanager;

		int select = 0;
		cin >> select;

		if (select == 1)  //添加账号
		{
			cout << "添加账号" << endl;
			man->add_person();
		}
		else if (select == 2)  //查看账号
		{
			cout << "查看账号" << endl;
			man->show_person();
		}
		else if (select == 3)  //查看机房
		{
			cout << "查看机房" << endl;
			man->show_computer();
		}
		else if (select == 4)  //清空预约
		{
			cout << "清空预约" << endl;
			man->clean_file();
		}
		else
		{
			delete man;  //销毁掉堆区对象
			cout << "注销成功!" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}

//进入教师子菜单界面
void teacher_menu(identity*& tteacher)
{
	while (true)
	{
		//调用子菜单界面
		tteacher->opermenu();

		teacher* tea = (teacher*)tteacher;

		int select = 0;
		cin >> select;
		if (select == 1)  //查看所有预约
		{
			tea->show_all_order();
		}
		else if (select == 2)  //审核预约
		{
			tea->valid_order();
		}
		else
		{
			delete tea;
			cout << "注销成功" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}

//登录功能
void login(string file_name, int type)
{
	identity* person = NULL;  //父类指针,用于指向子类对象

	//读文件
	ifstream ifs;
	ifs.open(file_name, ios::in);

	//判断文件是否存在
	if (!ifs.is_open())
	{
		cout << "文件不存在" << endl;
		ifs.close();
		return;
	}
	
	int id = 0;
	string name;
	string pwd;
	
	//判断身份
	if (type == 1)  //学生
	{
		cout << "请输入学号:" << endl;
		cin >> id;
	}
	else if (type == 2)  //教师
	{
		cout << "请输入职工号:" << endl;
		cin >> id;
	}
	cout << "请输入用户名:" << endl;
	cin >> name;
	cout << "请输入密码:" << endl;
	cin >> pwd;

	//身份验证
	if (type == 1)
	{
		//学生身份验证
		int fid;  //从文件中获取的id号
		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);

				//进入学生身份的子菜单
				student_menu(person);

				return;
			}
		}
	}
	else if (type == 2)
	{
		//教师身份验证
		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);

				//进入教师子菜单
				teacher_menu(person);

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

				//进入管理员的子菜单
				manager_menu(person);

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

int main()
{
	int select = 0;  //用于接收用户的选择

	while (true)
	{
		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 >> select;

		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;
		}
	}
	return 0;
}

 

 

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zyh_fighting

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值