C++学习笔记_基础完结篇_机房预约管理项目

一个结课级别的C++项目,模拟学校机房的运行和管理。

项目需求如下:

使用者身份

机房简介

申请规则

系统按身份登录,每个身份通过账号密码的验证之后进入子菜单:

1. 学生菜单

2. 教师菜单

3. 管理员菜单

1. 主页面

int main() {
	while (true)
	{
		int select = 0;
		cout << "================== Welcome to Computer Room Reserving System ==================" << endl;
		cout << endl << "Please Enter Your Identity" << endl; 
		cout << "\t\t ----------------------------------------------\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t|                 1. Student                  |\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t|                 2. Teacher                  |\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t|               3. Administrator              |\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t|                  0. Quit                    |\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t|                                             |\n";
		cout << "\t\t ----------------------------------------------\n";
		cout << "Please enter: ";
		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 << "Thanks! Bye" << endl;
			system("pause");
			return 0;
			break;
		default:
			cout << "Wrong input! Please re-enter !" << endl; 
			system("cls");
			break;
		}
		
	}
	system("pause");
	return 0; 
}

 死循环+switch实现界面选择,算是基操了

身份类创建

基类

#pragma once
#include <iostream>

// 身份抽象类
class Identity {
public:
	// 操作菜单  纯虚函数
	virtual void openMenu() = 0;
	
	// 用户名
	std::string m_name;
	// 密码
	std::string m_password;

};

 学生类

#pragma once
#include <iostream>
#include "identity.h"

class Student : public Identity {
public:
	int student_id;

	// 默认构造
	Student();
	// 有参构造 学号、姓名、密码
	Student(int student_id, std::string name, std::string pwd);

	// 菜单界面
	virtual void openMenu();

	// 申请预约
	void applyOrder();

	// 查看自身预约
	void showMyOrder();

	// 查看所有预约
	void showAllOrder();

	// 取消预约
	void cancelOrder();

};

教师类

#pragma once
#include <iostream>
#include "identity.h"

class Teacher : public Identity {
public:
	int teacher_id;

	// 默认构造
	Teacher();

	// 有参构造 工号、姓名、密码
	Teacher(int teacher_id, std::string name, std::string pwd);

	// 菜单界面
	virtual void openMenu();

	// 查看所有预约
	void showAllOrder();

	// 审核预约
	void validOrder();

};

管理员类

#pragma once
#include <iostream>
#include "identity.h"

class Administrator : public Identity {
public:
	// 默认构造
	Administrator();

	// 有参构造 账号、密码
	Administrator(std::string name, std::string pwd);

	// 菜单界面
	virtual void openMenu();

	// 添加账号
	void addAccount();

	// 查看账号
	void showAccount();

	// 查看机房
	void showRoom();

	// 清空预约
	void clearOrder();

};

登录模块

在头文件中定义所有的使用到的文件名方便不同文件的文件操作

#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"

注:显示中文的话,文件格式采用ansi编码格式保存,utf-8会读出乱码

三种身份的登录及其验证

// 登录功能
void LogIn(string fileName, int type) {

	// 父类指针,用于指向子类对象
	Identity* person = NULL;
	ifstream ifs;
	ifs.open(fileName, ios::in);

	if (!ifs.is_open()) {
		cout << "No File!" << endl;
		ifs.close();
		return; 
	}

	// 初始化id、名字和密码
	int id = 0;
	string name;
	string pwd;

	// 身份判断
	if (type == 1) {
		cout << "Please enter student_ID: " << endl;
		cin >> id;
	}
	else if (type == 2) {
		cout << "Please enter staff_ID: " << endl;
		cin >> id;
	}
	
	cout << "Please enter your name: " << endl;
	cin >> name;

	cout << "Please enter your password: " << endl;
	cin >> pwd;
	
	if (type == 1) {
		// 学生身份验证
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
		/*	cout << fId << endl; 
			cout << fName << endl;
			cout << fPwd << endl; */
			if (fId == id && fName == name && fPwd == pwd) {
				cout << "student log in successfully!" << endl;
				system("pause");
				system("cls");
                // 创建学生对象
				person = new Student(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 == id && fName == name && fPwd == pwd) {
				cout << "teacher log in successfully!" << endl;
				system("pause");
				system("cls");
                // 创建教师对象
				person = new Teacher(id, name, pwd);
				teacherMenu(person);
				return;
			}
		}
	}
	else {
		// 管理员身份验证
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fName && ifs >> fPwd){
			if (fName == name && fPwd == pwd) {
				cout << "admin log in successfully!" << endl;
				system("pause");
				system("cls");
				// 创建管理员对象
				person = new Administrator(name, pwd);
				adminMenu(person);
				return;
			}
		}
	}
	cout << "Login Fail!" << endl;
	system("pause");
	system("cls");
	ifs.close();
}

管理员身份登录,功能模块:

void adminMenu(Identity*& admin) {
	while (true) {
		// 父类指针调用子类共同接口
		admin->openMenu();
		// 将父类指针转为子类指针,调用子类其他接口
		Administrator* ad = (Administrator*)admin; 
		int select = 0; 
		cin >> select;
		// 添加账号
		if (select == 1)  {
			cout << " add account " << endl;
			ad->addAccount();
		}
		// 查看账号
		else if (select == 2) {
			cout << " show account " << endl;
			ad->showAccount();
		}
		// 查看机房
		else if (select == 3) {
			cout << " show room " << endl;
			ad->showRoom();
		}
		// 清空预约
		else if (select == 4) {
			cout << " clear order " << endl;
			ad->clearOrder();
		}
		// 注销登录
		else {
			cout << "log out" << endl;
			return;
			// delete admin;
		}

	}
}

先写管理员部分,因为学生和老师的账号都由其创建,其职能如下:

头文件定义如下:

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

void printStudent(Student& s);
void printTeacher(Teacher& t);

class Administrator : public Identity {
public:
	// 默认构造
	Administrator();

	// 有参构造 账号、密码
	Administrator(std::string name, std::string pwd);

	// 菜单界面
	virtual void openMenu();

	// 添加账号
	void addAccount();

	// 查看账号
	void showAccount();

	// 查看机房
	void showRoom();

	// 清空预约
	void clearOrder();

	// 初始化容器
	void initVector();

	// 检测重复,参数1是学号/工号,参数2是检测类型
	bool checkRepeat(int id, int type);

	// 学生容器
	std::vector<Student> vStu;

	// 老师容器
	std::vector<Teacher> vTea;

	// 机房容器
	std::vector<computerRoom> vCom;
};

administrator.cpp 管理员各功能函数实现:

#include "administrator.h"
#include <iostream>
using namespace std;
// 默认构造
Administrator::Administrator() {

}

// 有参构造 账号、姓名、密码
Administrator::Administrator(string name, string pwd) {
	this->m_name = name;
	this->m_password = pwd;
	
	//初始化容器, 获取文件中所有学生和老师的账号信息
	this->initVector();

	//初始化机房信息
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);
	if (!ifs.is_open()) {
		cout << "fail to open " << endl;
		return; 
	}
	computerRoom com; 
	while (ifs >> com.m_ID && ifs >> com.m_Volume) {
		vCom.push_back(com); 
	}
	ifs.close();
	// cout << "Room num: " << vCom.size() << endl;
}

// 菜单界面
void Administrator::openMenu() {
	cout << "================== Welcome Administrator==================" << endl;
	cout << "\t\t ----------------------------------------------\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               1. Add Acounts                |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               2. Show Accounts              |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|                3. Show Room                 |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               4. Clear Order                |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               Other. Log Out                |\n";
	cout << "\t\t ----------------------------------------------\n";
	cout << "Please enter: ";
}

// 添加账号
void Administrator::addAccount() {
	cout << "Please enter the type of account you want to add:" << endl;
	cout << "1. student" << endl;
	cout << "2. teacher" << endl;
	string fileName; // 操作文件名
	string tip; // 协助显示信息
	ofstream ofs; 
	int select = 0;
	cin >> select; 
	if (select == 1) {
		fileName = STUDENT_FILE;
		tip = "Please enter student_id: ";
	}
	else {
		fileName = TEACHER_FILE;
		tip = "Please enter staff_id: "; 
	}

	ofs.open(fileName, ios::out | ios::app);
	int id; //学号/工号
	string name; // 姓名
	string pwd; // 密码
	
	while(true){
		cout << tip << endl;
		cin >> id;
		bool is_repeat = checkRepeat(id, select);
		if (is_repeat) {
			cout << "repeat id input, please re-enter! " << endl;
		}
		else {
			break; 
		}
	}
	
	cout << "Please enter your name: " << endl;
	cin >> name;
	 
	cout << "Please enter your password: " << endl;
	cin >> pwd;

	ofs << id << " " << name << " " << pwd << " " << endl; 
	cout << "Add Successfully! " << endl;
	system("pause");
	system("cls");
	ofs.close();
	// 添加完毕后重新初始化维持重复检测状态
	this->initVector();
}

// 查看账号
void Administrator::showAccount() {
	cout << "Please choose what to show: " << endl;
	cout << "1. show all students " << endl;
	cout << "2. show all teachers" << endl; 
	int select = 0;
	cin >> select;
	if (select == 1) {
		cout << "message of all students is below: " << endl; 
		for_each(vStu.begin(), vStu.end(), printStudent);
	}
	else {
		cout << "message of all teachers is below:" << endl; 
		for_each(vTea.begin(), vTea.end(), printTeacher); 
	}
	system("pause");
	system("cls");
}

// 查看机房
void Administrator::showRoom() {
	cout << "The message of computer room is below: " << endl;
	for (vector<computerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++) {
		cout << "Room number: " << it->m_ID << "Room volume: " << it->m_Volume << endl;
	}
	system("pause");
	system("cls");
}

// 清空预约
void Administrator::clearOrder() {
	ofstream ofs(ORDER_FILE, ios::trunc);
	ofs.close();
	cout << "Clear successfully! " << endl; 
	system("pause");
	system("cls"); 
}

// 初始化操作
void Administrator::initVector() {
	vStu.clear();
	vTea.clear();
	ifstream ifs;

	// 读取学生信息和老师信息存入容器,用于去重操作
	// 读取学生
	ifs.open(STUDENT_FILE, ios::in);
	if (!ifs.is_open()) {
		cout << "read file fail! " << endl;
		return; 
	}
	Student s;
	while (ifs >> s.student_id && ifs >> s.m_name && ifs >> s.m_password) {
		vStu.push_back(s);
	}
	cout << "The amount of students is: " << vStu.size() << endl;
	ifs.close();

	// 读取老师
	ifs.open(TEACHER_FILE, ios::in);
	if (!ifs.is_open()) {
		cout << "read file fail! " << endl;
		return;
	}
	Teacher t;
	while (ifs >> t.teacher_id && ifs >> t.m_name && ifs >> t.m_password) {
		vTea.push_back(t);
	}
	cout << "The amount of teachers is: " << vTea.size() << endl;
	ifs.close();
}

// 检测重复
bool Administrator::checkRepeat(int id, int type) {
	// 检测学生
	if (type == 1) {
		for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++) {
			if (id == it->student_id) {
				return true;
			}
		}
	}
	// 检测老师
	else {
		for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++) {
			if (id == it->teacher_id) {
				return true;
			}
		}
	}
	return false; 
}

// 打印账号信息
void printStudent(Student &s) {
	cout << "student_id: " << s.student_id << " student_name: " << s.m_name << endl;
}

void printTeacher(Teacher &t) {
	cout << "teacher_id: " << t.teacher_id << " teacher_name: " << t.m_name << endl;
}

对于每次添加账号的查重,需要及时保持管理员类中负责学生和老师账号容器的重新初始化,使得重复检测有效。

然后完成学生部分,因为老师是用来审核预约的,因此需要学生主体先创建预约,学生功能如下:

学生登录部分和管理员类似:

// 学生身份登录
void studentMenu(Identity*& student) {
	while (true) {
		// 父类指针调用子类共同接口
		student->openMenu();
		// 将父类指针转为子类指针,调用子类其他接口
		Student* stu = (Student*)student;
		int select = 0;
		cin >> select;
		// 申请预约
		if (select == 1) {
			cout << "add order " << endl;
			stu->applyOrder();
		}
		// 查看自身预约
		else if (select == 2) {
			cout << "show order " << endl;
			stu->showMyOrder();
		}
		// 查看所有预约
		else if (select == 3) {
			cout << "show all orders " << endl;
			stu->showAllOrder();
		}
		// 取消预约
		else if (select == 4) {
			cout << "clear order " << endl;
			stu->cancelOrder();
		}
		// 注销登录
		else {
			cout << "log out" << endl;
			return;
			//delete admin;
		}

	}
}

学生部分的功能涉及到预约的显示操作,因此定义orderFile类用于处理预约记录和更新。

orderFile.h:

#pragma once
#include <iostream>
using namespace std; 
#include "globalFile.h"
#include <fstream>
#include <string>
#include <map>

class orderFile {
public:
	orderFile();
	
	// 预约数量
	int m_Size;

	// 预约记录,使用嵌套map存储
	map<int, map<string, string>> m_Data; 

	// 更新预约记录
	void updateOrder();
};

orderFile.cpp:

#include "orderFile.h"

orderFile::orderFile() {
	ifstream ifs;
	ifs.open(ORDER_FILE, ios::in);
	string date; // 日期
	string interval; // 时间段
	string stuID; // 学号
	string stuName; // 学生姓名
	string roomID; // 机房号码
	string status;  // 审核状态

	this->m_Size = 0;
	while (ifs >> date && ifs >> interval && ifs >> stuID && ifs >> stuName && ifs >> roomID && ifs >> status ){
	/*	cout << date << endl; 
		cout << interval << endl;
		cout << stuID << endl;
		cout << stuName << endl;
		cout << roomID << 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));
			//cout << value << endl; 
			//switch(stoi(value)){
			//	case 1:
			//		m.insert(make_pair(key, "Monday"));
			//		break; 
			//	case 2:
			//		m.insert(make_pair(key, "Tuesday"));
			//		break;
			//	case 3:
			//		m.insert(make_pair(key, "Wednesday"));
			//		break;
			//	case 4:
			//		m.insert(make_pair(key, "Thursday"));
			//		break;
			//	case 5:
			//		m.insert(make_pair(key, "Friday"));
			//		break; 
			//	default:
			//		break;
			//}
		}

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

		// 截取学号
		// cout << "stuID " << stuID << endl;
		pos = stuID.find(":");
		if (pos != -1) {
			key = stuID.substr(0, pos);
			value = stuID.substr(pos + 1, stuID.size() - pos - 1);
			m.insert(make_pair(key, value));
		}

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

		// 截取机房号
		pos = roomID.find(":");
		if (pos != -1) {
			key = roomID.substr(0, pos);
			value = roomID.substr(pos + 1, roomID.size() - pos - 1);
			m.insert(make_pair(key, value));
		}

		// 截取审核状态
		pos = status.find(":");
		if (pos != -1) {
			key = status.substr(0, pos);
			value = status.substr(pos + 1, status.size() - pos - 1);
			m.insert(make_pair(key, value));
		}

		// 将小map容器放到大map容器中
		this->m_Data.insert(make_pair(this->m_Size, m));
		this->m_Size++;
	}
	ifs.close();
	
	// test 
	/*for (map<int, map<string, string>>::iterator it = m_Data.begin(); it != m_Data.end(); it++) {
		cout << "No." << it->first << " value: " << endl; 
		for (map<string, string>::iterator mit = it->second.begin(); mit != it->second.end(); mit++) {
			cout << "key = " << mit->first << " value = " << mit->second << " "; 
		}
		cout << endl; 
	}*/
}

void orderFile::updateOrder() {
	if (this->m_Size == 0) {
		return; // 无预约记录直接返回
	}
	ofstream ofs(ORDER_FILE, ios::out | ios::trunc); // 删除并重新写入
	for (int i = 0; i < this->m_Size; i++) {
		ofs << "date:" << this->m_Data[i]["date"] << " ";
		ofs << "interval:" << this->m_Data[i]["interval"] << " ";
		ofs << "stuID:" << this->m_Data[i]["stuID"] << " ";
		ofs << "stuName:" << this->m_Data[i]["stuName"] << " ";
		ofs << "roomID:" << this->m_Data[i]["roomID"] << " ";
		ofs << "status:" << this->m_Data[i]["status"] << " ";
	}
	ofs.close();
}


关键点在于字符串的切分以及重新的存取,更新

student.cpp:

#include <iostream>
#include <fstream>
#include <vector>
#include "student.h"
#include "globalFile.h"
#include "computerRoom.h"
using namespace std;
// 将数字转换为星期
string day[5] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

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

// 有参构造 学号、姓名、密码
Student::Student(int student_id, std::string name, std::string pwd) {
	this->student_id = student_id;
	this->m_name = name;
	this->m_password = pwd; 

	//初始化机房信息
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);
	if (!ifs.is_open()) {
		cout << "fail to open " << endl;
		return;
	}
	computerRoom com;
	while (ifs >> com.m_ID && ifs >> com.m_Volume) {
		vCom.push_back(com);
	}
	ifs.close();
	// cout << "Room num: " << vCom.size() << endl;
}

// 菜单界面
void Student::openMenu() {
	cout << "\t\t================== Welcome " << this->m_name << "==================" <<endl;
	cout << "\t\t ----------------------------------------------\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               1. Add Orders                 |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               2. Show Orders                |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|              3. Show All Orders             |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               4. Cancel Order               |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               Other. Log Out                |\n";
	cout << "\t\t ----------------------------------------------\n";
	cout << "Please enter: ";
}

// 申请预约
void Student::applyOrder() {
	cout << "The open time of the computer room is from Monday to Friday" << endl; 
	cout << "Please enter the time of your order: " << endl; 
	cout << "1. Monday " << endl; 
	cout << "2. Tuesday " << endl;
	cout << "3. Wednesday " << endl;
	cout << "4. Thursday " << endl;
	cout << "5. Friday " << endl;

	int date = 0; // 日期
	int interval = 0; // 时间段
	int room = 0; // 机房编号

	while (true) {
		cin >> date; 
		if (date >= 1 && date <= 5) {
			break;
		}
		cout << "date out of range, please re-enter" << endl; 
	}
	cout << "please enter the period of the order: " << endl; 
	cout << "1. morning " << endl; 
	cout << "2. afternoon " << endl; 
	while (true) {
		cin >> interval;
		if (interval >= 1 && interval <= 2) {
			break;
		}
		cout << "interval out of range, please re-enter" << endl;
	}
	
	cout << "please choose the room: " << endl;
	// 改进点:机房剩余容量的计算
	while(true) {
		cin >> room;
		if (room >= 1 && room <= 3) {
			break; 
		}
		cout << "wrong enter, please re-enter: " << endl; 
	}
	
	cout << "order successfully! under the validation...." << endl; 

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

	ofs << "date:" << date << " ";
	ofs << "interval:" << interval << " ";
	ofs << "stuID:" << this->student_id << " ";
	ofs << "stuName:" << this->m_name << " "; 
	ofs << "roomID:" << room << " ";
	ofs << "status:" << 1 << endl;
	ofs.close();
	std::system("pause");
	std::system("cls");
}

// 查看自身预约
void Student::showMyOrder() {
	// cout << "********************************" << endl; 
	orderFile od; 
	if (od.m_Size == 0) {
		cout << "no order record!" << endl;
		std::system("pause");
		std::system("cls");
		return;
	}

	//cout << od.m_Data[0]["stuID"] << endl; 
	//cout << od.m_Data[0]["date"] << endl;
	//cout << od.m_Data[0]["interval"] << endl;
	//cout << od.m_Data[0]["roomID"] << endl;
	for (int i = 0; i < od.m_Size; i++) {
		// string to int 
		// string -> .c_str() -> const char*  -> atoi -> int
		if (this->student_id == atoi(od.m_Data[i]["stuID"].c_str())) {
			cout << "date: " << day[(stoi(od.m_Data[i]["date"])-1)];
			cout << " time: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
			cout << " roomID: " << od.m_Data[i]["roomID"];
			// 1. under validation 2. success -1. fail 0. cancel 
			string status = " status: ";
			if (od.m_Data[i]["status"] == "1") {
				status += "under validation";
			}
			else if (od.m_Data[i]["status"] == "2") {
				status += "order success";
			}
			else if (od.m_Data[i]["status"] == "-1") {
				status += "order fail";
			}
			else {
				status += "order cancel"; 
			}
			cout << status << endl;
		}
	}
	std::system("pause");
	std::system("cls");
}

// 查看所有预约
void Student::showAllOrder() {
	orderFile od; 
	if (od.m_Size == 0) {
		cout << "No order recently " << endl; 
		std::system("pause");
		std::system("cls"); 
		return;
	}

	for (int i = 0; i < od.m_Size; i++) {
		cout << i + 1 << ">> ";
		cout << "stuID: " << od.m_Data[i]["stuID"];
		cout << " name: " << od.m_Data[i]["stuName"];
		cout << " date: " << day[(stoi(od.m_Data[i]["date"]) - 1)];
		cout << " interval: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
		cout << " roomID: " << od.m_Data[i]["roomID"];
		string status = " status: ";
		if (od.m_Data[i]["status"] == "1") {
			status += "under validation";
		}
		else if (od.m_Data[i]["status"] == "2") {
			status += "order success";
		}
		else if (od.m_Data[i]["status"] == "-1") {
			status += "order fail";
		}
		else {
			status += "order cancel";
		}
		cout << status << endl;
	}
	std::system("pause");
	std::system("cls");
}

// 取消预约
void Student::cancelOrder() {
	orderFile od;
	// 取消的预约,其状态需为审核中或预约成功
	if (od.m_Size == 0) {
		cout << "No order recently " << endl;
		std::system("pause");
		std::system("cls");
		return;
	}
	cout << "Your can cancel your orders finished or under validation, please choose: " << endl; 
	
	vector<int> v; 
	int index = 1;
	int j = 0; 
	for (int i = 0; i < od.m_Size; i++) {
		// 先判断自身学号
		if (this->student_id == stoi(od.m_Data[i]["stuID"])) {
			// 筛选状态
			if (od.m_Data[i]["status"] == "1" || od.m_Data[i]["status"] == "2") {
				v.push_back(i);	
				j++; 
				cout << j << ">> ";
				cout << "stuID: " << od.m_Data[i]["stuID"];
				cout << " name: " << od.m_Data[i]["stuName"];
				cout << " date: " << day[(stoi(od.m_Data[i]["date"]) - 1)];
				cout << " interval: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
				cout << " roomID: " << od.m_Data[i]["roomID"];
				string status = " status: ";
				if (od.m_Data[i]["status"] == "1") {
					status += "under validation";
				}
				else if (od.m_Data[i]["status"] == "2") {
					status += "order success";
				}
				else if (od.m_Data[i]["status"] == "-1") {
					status += "order fail";
				}
				else {
					status += "order cancel";
				}
				cout << status << endl;
			}
		}
	}

	// 
	cout << "Please enter the No. of order you want to cancel, 0 to back " << endl;
	int select = 0; 
	while (true) {
		cin >> select; 
		if (select >= 0 && select <= v.size()) {
			if (select == 0) {
				break;
			}
			else {
				od.m_Data[v[select - 1]]["status"] = "0"; 
				od.updateOrder();
				cout << "cancel successfully " << endl;
				break; 
			}
		}
		cout << "wrong input, please re-enter" << endl;
	}
	std::system("pause");
	std::system("cls");
}

最后来编写教师模块,教师身份的职能如下:

教师身份登录:

// 老师身份登录
void teacherMenu(Identity*& teacher) {
	while (true) {
		// 父类指针调用子类共同接口
		teacher->openMenu();
		// 将父类指针转为子类指针,调用子类其他接口
		Teacher* tea = (Teacher*)teacher;
		int select = 0;
		cin >> select;
		// 查看所有预约
		if (select == 1) {
			cout << "show all orders " << endl;
			tea->showAllOrder();
		}
		// 审核预约
		else if (select == 2) {
			cout << "show order " << endl;
			tea->validOrder();
		}
		// 注销登录
		else {
			cout << "log out" << endl;
			return;
			//delete admin;
		}

	}
}

老师模块的功能可以大量复用学生部分的代码。

teacher.cpp:

#include "teacher.h"
#include "student.h"
#include "orderFile.h"
#include <vector>
using namespace std; 
// 将数字转换为星期
string daylist[5] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

// 默认构造
Teacher::Teacher() {

}

// 有参构造 学号、姓名、密码
Teacher::Teacher(int id, std::string name, std::string pwd) {
	this->teacher_id = id;
	this->m_name = name;
	this->m_password = pwd; 

}

// 菜单界面
void Teacher::openMenu() {
	cout << "\t\t================= Welcome " << this->m_name << "! ==================" << endl;
	cout << "\t\t ----------------------------------------------\n";
	cout << "\t\t|              1. Show All Orders             |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               2. Valid Order                |\n";
	cout << "\t\t|                                             |\n";
	cout << "\t\t|               Other. Log Out                |\n";
	cout << "\t\t ----------------------------------------------\n";
	cout << "Please enter: ";
}

// 查看所有预约
void Teacher::showAllOrder() {
	orderFile od; 
	if (od.m_Size == 0) {
		cout << "no order record!" << endl;
		std::system("pause");
		std::system("cls");
		return;
	}

	for (int i = 0; i < od.m_Size; i++) {
		cout << i + 1 << ">> ";
		cout << "stuID: " << od.m_Data[i]["stuID"];
		cout << " name: " << od.m_Data[i]["stuName"];
		cout << " date: " << daylist[(stoi(od.m_Data[i]["date"]) - 1)];
		cout << " interval: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
		cout << " roomID: " << od.m_Data[i]["roomID"];
		string status = " status: ";
		if (od.m_Data[i]["status"] == "1") {
			status += "under validation";
		}
		else if (od.m_Data[i]["status"] == "2") {
			status += "order success";
		}
		else if (od.m_Data[i]["status"] == "-1") {
			status += "order fail";
		}
		else {
			status += "order cancel";
		}
		cout << status << endl;
	}
	system("pause");
	system("cls");
}

// 审核预约
void Teacher::validOrder() {
	orderFile od;
	if (od.m_Size == 0) {
		cout << "no order record!" << endl;
		std::system("pause");
		std::system("cls");
		return;
	}
	
	cout << "Here are orders to be valided: " << endl; 
	vector<int> v; 
	int j = 0; 
	for (int i = 0; i < od.m_Size; i++) {
		if (od.m_Data[i]["status"] == "1") {
			v.push_back(i);
			j++;
			cout << j << ">> ";
			cout << "stuID: " << od.m_Data[i]["stuID"];
			cout << " name: " << od.m_Data[i]["stuName"];
			cout << " date: " << daylist[(stoi(od.m_Data[i]["date"]) - 1)];
			cout << " interval: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
			cout << " roomID: " << od.m_Data[i]["roomID"];
			cout << " under validation" << endl; 
		}
	}
	cout << "Please enter the num of the order you want to do validation, 0 to back" << endl;
	int select = 0;
	int ret = 0; 
	while (true) {
		cin >> select;
		if (select >= 0 && select <= v.size()) {
			if (select == 0) {
				break;
			}
			else {
				cout << "Please enter the result of validation " << endl;
				cout << "1. pass " << endl;
				cout << "2. no pass " << endl;
				cin >> ret; 
				if (ret == 1) {
					od.m_Data[v[select - 1]]["status"] = "2";
				}
				else {
					od.m_Data[v[select - 1]]["status"] = "-1";
				}
				od.updateOrder();
				break; 
			}
		}
		cout << "wrong input, please re-enter" << endl;
	}
	system("pause");
	system("cls");
}

项目整体文件视图如下:

运行后登录菜单页面如下:

完整代码见下方链接

Github链接:https://github.com/Hakureiremu/ComputerRoom.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值