C + + realize the complete source code sharing of the computer room reservation system!

Catalogue of series articles

Tip: it is estimated that the reading time will be 20 minutes. If you study carefully and start from the complete needs, it is recommended that you study and realize it in one week.

Computer room reservation system

1. Room reservation system requirements

1.1 system introduction

  • The school has several computer rooms with different specifications. Due to the frequent "crash" phenomenon in use, a set of computer room reservation system is developed to solve this problem.

1.2 profile

There are three identities to use the program

  • Student representative: apply to use the computer room
  • Teacher: review the student's reservation application
  • Administrator: create an account for students and teachers

1.3 introduction to computer room

There are 3 machine rooms in total

  • Machine room 1 - maximum capacity 20 persons
  • Machine room 2 - maximum capacity 50 persons
  • Machine room 3 - maximum capacity 100 persons

1.4 application introduction

  • The requested order is emptied by the administrator every week.
  • Students can make an appointment for the computer room in the next week. The appointment date is from Monday to Friday. When making an appointment, they need to choose the appointment time (morning and afternoon)
  • The teacher will review the appointment, and review whether the appointment passes or fails according to the actual situation

1.5 system specific requirements

  • First, enter the login interface. The optional login identities are:
    • Student representative
    • teacher
    • administrators
    • sign out
  • After each identity needs to be verified, enter the submenu
    • Students need to enter: student number, name and login password
    • The teacher needs to input: employee number, name and login password
    • Administrator needs to enter: administrator name and login password
  • Student specific functions
    • Application for reservation - reservation of computer room
    • View your own appointments - view your appointment status
    • View all appointments - view all appointment information and appointment status
    • Cancel appointment - cancel your own appointment. You can cancel the appointment successfully or in approval
    • Logout - logout
  • Specific functions of Teachers
    • View all appointments - view all appointment information and appointment status
    • Review appointments - Review student appointments
    • Logout - logout
  • Administrator specific functions
    • Add account - add the account number of a student or teacher. It is necessary to check whether the student number or teacher employee number is duplicate
    • View account - you can choose to view all the information of students or teachers
    • View machine rooms - view information for all machine rooms
    • Clear appointment - clear all appointment records
    • Logout - logout

2. Create project

The steps to create a project are as follows:

  • Create a new project
  • Add file

2.1 create project

  • After opening vs2017, click create new project to create a new C + + project

As shown in the figure:

  • Fill in the project name and select the project path, and click OK to generate the project

2.2 adding files

  • Right click the source file to add a file
  • Fill in the file name and click Add

  • The file is generated successfully. The effect is shown in the figure below

3. Create main menu

Function Description:

  • Design the main menu to interact with users

3.1 menu implementation

  • Add a menu prompt in the main function. The code is as follows:
int main() {

	cout << "======================  Welcome to the smart podcast room reservation system  =====================" 
         << endl;
	cout << endl << "Please enter your identity" << endl;
	cout << "\t\t -------------------------------\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t|          1.Student representative           |\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t|          2.old    division           |\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t|          3.administrators           |\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t|          0.retreat    Out           |\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t -------------------------------\n";
	cout << "Enter your selection: ";

	system("pause");

	return 0;
}

The operation effect is shown in the figure below:

3.2 interface construction

  • Accept the user's choice and build the interface
  • Add code in main
int main() {

	int select = 0;

	while (true)
	{

		cout << "======================  Welcome to the smart podcast room reservation system  =====================" << endl;
		cout << endl << "Please enter your identity" << endl;
		cout << "\t\t -------------------------------\n";
		cout << "\t\t|                               |\n";
		cout << "\t\t|          1.Student representative           |\n";
		cout << "\t\t|                               |\n";
		cout << "\t\t|          2.old    division           |\n";
		cout << "\t\t|                               |\n";
		cout << "\t\t|          3.administrators           |\n";
		cout << "\t\t|                               |\n";
		cout << "\t\t|          0.retreat    Out           |\n";
		cout << "\t\t|                               |\n";
		cout << "\t\t -------------------------------\n";
		cout << "Enter your selection: ";

		cin >> select; //Accept user selection

		switch (select)
		{
		case 1:  //Student identity
			break;
		case 2:  //Teacher identity
			break;
		case 3:  //Administrator identity
			break;
		case 0:  //Exit the system
			break;
		default:
             cout << "Incorrect input, please re select!" << endl;
		    system("pause");
			system("cls");
			break;
		}

	}
	system("pause");
	return 0;
}

During the test, enter 0, 1, 2 and 3 to return to the interface. Enter other prompts. If there is an error, clear the screen and select again

The effect is shown in the figure:

So far, the interface has been built

4. Exit function implementation

4.1 realization of exit function

In the main function branch 0 option, add the code to exit the program:

	cout << "Welcome to use next time"<<endl;
	system("pause");
	return 0;

4.2 test exit function

Run the program, and the effect is shown in the figure below:

So far, exit the program function

5. Create identity class

5.1 base class of identity

  • In the whole system, there are three identities: student representative, teacher and administrator
  • The three identities have their commonalities and characteristics, so we can abstract the three identities into an identity base class identity
  • Create the Identity.h file under the header file

Add the following code to Identity.h:

#pragma once
#include<iostream>
using namespace std;

//Identity abstract class
class Identity
{
public:

	//Operation menu
	virtual void operMenu() = 0;

	string m_Name; //user name
	string m_Pwd;  //password
};

The effect is shown in the figure:

5.2 students

5.2.1 function analysis

  • The main function of the student class is to reserve laboratory operations through the member functions in the class

  • The main functions of the student category are:

    • The menu interface for student operation is displayed
    • Apply for an appointment
    • View your own appointment
    • View all appointments
    • cancel reservation

5.2.2 class creation

  • Create student.h and student.cpp files under the header file and source file

Add the following code to student.h:

#pragma once
#include<iostream>
using namespace std;
#include "identity.h"

//Student class
class Student :public Identity
{
public:
	//Default construction
	Student();

	//Structure with parameters (student number, name, password)
	Student(int id, string name, string pwd);

	//Menu interface
	virtual void operMenu(); 

	//Apply for an appointment
	void applyOrder(); 

	//Check my appointments
	void showMyOrder(); 

	//View all appointments
	void showAllOrder(); 

	//cancel reservation
	void cancelOrder();
	
	//Student number
	int m_Id;

};

Add the following code to student.cpp:

#include "student.h"

//Default construction
Student::Student()
{
}

//Structure with parameters (student number, name, password)
Student::Student(int id, string name, string pwd)
{
}

//Menu interface
void Student::operMenu()
{
}

//Apply for an appointment
void Student::applyOrder()
{

}

//Check my appointments
void Student::showMyOrder()
{

}

//View all appointments
void Student::showAllOrder()
{

}

//cancel reservation
void Student::cancelOrder()
{

}

5.3 teachers

5.3.1 function analysis

  • The main function of teachers is to view and review students' appointments

  • The main functions of teachers include:

    • Display the menu interface of teacher operation

    • View all appointments

    • Audit appointment

5.3.2 class creation

  • Create the teacher.h and teacher.cpp files under the header file and the source file

Add the following code to teacher.h:

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "identity.h"

class Teacher :public Identity
{
public:

	//Default construction
	Teacher();

	//Structure with reference (employee number, name, password)
	Teacher(int empId, string name, string pwd);

	//Menu interface
	virtual void operMenu();

	//View all appointments
	void showAllOrder(); 

	//Audit appointment
	void validOrder(); 

	int m_EmpId; //Teacher number

};
  • Add the following code to teacher.cpp:
#include"teacher.h"

//Default construction
Teacher::Teacher()
{
}

//Structure with reference (employee number, name, password)
Teacher::Teacher(int empId, string name, string pwd)
{
}

//Menu interface
void Teacher::operMenu()
{
}

//View all appointments
void Teacher::showAllOrder()
{
}

//Audit appointment
void Teacher::validOrder()
{
}

5.4 administrator

5.4.1 function analysis

  • The main functions of the administrator class are to manage student and teacher accounts, view computer room information and empty reservation records

  • The main functions of the administrator class are:

    • The menu interface for administrator operation is displayed

    • Add account

    • View account

    • View machine room information

    • Clear appointment record

5.4.2 class creation

  • Create the manager.h and manager.cpp files under the header file and the source file

Add the following code to manager.h:

#pragma once
#include<iostream>
using namespace std;
#include "identity.h"

class Manager :public Identity
{
public:

	//Default construction
	Manager();

	//Name and password of administrator
	Manager(string name, string pwd);

	//Select menu
	virtual void operMenu();

	//Add account  
	void addPerson();

	//View account
	void showPerson();

	//View machine room information
	void showComputer();

	//Clear appointment record
	void cleanFile();

};
  • Add the following code to manager.cpp:
#include "manager.h"

//Default construction
Manager::Manager()
{
}

//Parametric structure
Manager::Manager(string name, string pwd)
{
}

//Select menu
void Manager::operMenu()
{
}

//Add account  
void Manager::addPerson()
{
}

//View account
void Manager::showPerson()
{
}

//View machine room information
void Manager::showComputer()
{
}

//Clear appointment record
void Manager::cleanFile()
{
}

So far, all identity classes have been created, as shown in the following figure:

6. Login module

6.1 global file addition

Function Description:

  • Different identities may use different file operations. We can define all file names into a global file
  • Add the globalFile.h file to the header file
  • And add the following code:
#pragma once 

//Administrator files
#define ADMIN_FILE     "admin.txt"
//Student documents
#define STUDENT_FILE   "student.txt"
//Teacher files
#define TEACHER_FILE   "teacher.txt"
//Computer room information file
#define COMPUTER_FILE  "computerRoom.txt"
//Order document
#define ORDER_FILE     "order.txt"

And create these files in the same level directory

6.2 login function encapsulation

Function Description:

  • According to the user's choice, enter different identities to log in

Add the global function void LoginIn(string fileName, int type) to the. cpp file of the reservation system

Parameters:

  • fileName - file name of the operation
  • type - login identity (1 for students, 2 for teachers, 3 for administrators)

Add the following code to LoginIn:

#include "globalFile.h"
#include "identity.h"
#include <fstream>
#include <string>


//Login function
void LoginIn(string fileName, int type)
{

	Identity * person = NULL;

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

	//File does not exist
	if (!ifs.is_open())
	{
		cout << "file does not exist" << endl;
		ifs.close();
		return;
	}

	int id = 0;
	string name;
	string pwd;

	if (type == 1)	//Student login
	{
		cout << "Please enter your student number" << endl;
		cin >> id;
	}
	else if (type == 2) //Teacher login
	{
		cout << "Please enter your employee number" << endl;
		cin >> id;
	}

	cout << "Please enter user name:" << endl;
	cin >> name;

	cout << "Please input a password: " << endl;
	cin >> pwd;


	if (type == 1)
	{
		//Student login verification
	}
	else if (type == 2)
	{
		//Teacher login verification
	}
	else if(type == 3)
	{
		//Administrator login authentication
	}
	
	cout << "Failed to verify login!" << endl;

	system("pause");
	system("cls");
	return;
}
  • Fill in different login interfaces in different branches of the main function

6.3 student login implementation

Add two pieces of student information to the student.txt file for testing

Add information:

1 Zhang san123
2 Li Si 123456

Of which:

  • The first column represents the student number
  • The second column represents the student's name
  • The third column represents the password

design sketch:

Add the following code to the student branch of the Login function to verify the student identity

		//Student login verification
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd)
		{
			if (id == fId && name == fName && pwd == fPwd)
			{
				cout << "Student authentication login succeeded!" << endl;
				system("pause");
				system("cls");
				person = new Student(id, name, pwd);
				
				return;
			}
		}

Add code renderings

Test:

6.4 implementation of teacher login

Add a teacher information in the teacher.txt file for testing

Add information:

1 Lao Wang 123

Of which:

  • The first column represents the number of teachers and employees
  • The second column represents the name of the teacher
  • The third column represents the password

design sketch:

Add the following code to the teacher branch of the Login function to verify the identity of the teacher

		//Teacher login verification
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd)
		{
			if (id == fId && name == fName && pwd == fPwd)
			{
				cout << "Teacher authentication login succeeded!" << endl;
				system("pause");
				system("cls");
				person = new Teacher(id, name, pwd);
				return;
			}
		}

Add code renderings

Test:

6.5 administrator login implementation

Add an administrator information in the admin.txt file. Since we have only one administrator, there is no function to add an administrator in this case

Add information:

admin 123

Where: admin represents the administrator user name and 123 represents the administrator password

design sketch:

Add the following code to the administrator branch of the Login function to verify the administrator identity

//Administrator login authentication
		string fName;
		string fPwd;
		while (ifs >> fName && ifs >> fPwd)
		{
			if (name == fName && pwd == fPwd)
			{
				cout << "Verify login succeeded!" << endl;
				//After successful login, press any key to enter the administrator interface
				system("pause");
				system("cls");
				//Create administrator object
				person = new Manager(name,pwd);
				return;
			}
		}

Add the effect as shown in the figure:

The test effect is shown in the figure:

So far, the login function of all identities has been realized!

7. Administrator module

7.1 administrator login and logout

7.1.1 constructor

  • In the constructor of the Manager class, initialize the administrator information. The code is as follows:
//Parametric structure
Manager::Manager(string name, string pwd)
{
	this->m_Name = name;
	this->m_Pwd = pwd;
}

7.1.2 administrator submenu

  • In the computer room reservation system. cpp, when the user logs in as an administrator, add the administrator menu interface
  • Provide different branches
    • Add account
    • View account
    • View machine room
    • Empty appointment
    • Logout login
  • Realize logout function

Add the global function void managermenu (identity * & Manager) with the following code:

//Administrator menu
void managerMenu(Identity * &manager)
{
	while (true)
	{
		//Administrator menu
		manager->operMenu();

		Manager* man = (Manager*)manager;
		int select = 0;

		cin >> select;
        
		if (select == 1)  //Add account
		{
			cout << "Add account" << endl;
			man->addPerson();
		}
		else if (select == 2) //View account
		{
			cout << "View account" << endl;
			man->showPerson(); 
		}
		else if (select == 3) //View machine room
		{
			cout << "View machine room" << endl;
			man->showComputer();
		}
		else if (select == 4) //Empty appointment
		{
			cout << "Empty appointment" << endl;
			man->cleanFile();
		}
		else
		{
			delete manager;
			cout << "Logout succeeded" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}

7.1.3 realization of menu function

  • When implementing the member function void Manager::operMenu(), the code is as follows:
//Select menu
void Manager::operMenu()
{
	cout << "Welcome administrator:"<<this->m_Name << "Sign in!" << endl;
	cout << "\t\t ---------------------------------\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          1.Add account            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          2.View account            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          3.View machine room            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          4.Empty appointment            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          0.Logout login            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t ---------------------------------\n";
	cout << "Please select your action: " << endl;
}

7.1.4 interface docking

  • After the administrator has successfully logged in, the administrator submenu interface is called.
  • In the administrator login authentication branch, add the code:
				//Enter the administrator submenu
				managerMenu(person);

Add effects such as:

Test docking, and the effect is shown in the figure:

Login succeeded

Logout login:

At this point, the administrator can successfully log in and log out

7.2 add account

Function Description:

  • Add a new account to a student or teacher

Functional requirements:

  • When adding, student ID and teacher employee ID cannot be repeated

7.2.1 add function realization

In the addPerson member function of the Manager, you can add a new account. The code is as follows:

//Add account  
void Manager::addPerson()
{

	cout << "Please enter the type of account you want to add" << endl;
	cout << "1,Add student" << endl;
	cout << "2,Add teacher" << endl;

	string fileName;
	string tip;
	ofstream ofs;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		fileName = STUDENT_FILE;
		tip = "Please enter student number: ";
	}
	else
	{
		fileName = TEACHER_FILE;
		tip = "Please enter employee number:";
	}

	ofs.open(fileName, ios::out | ios::app);
	int id;
	string name;
	string pwd;
	cout <<tip << endl;
	cin >> id;

	cout << "Please enter your name: " << endl;
	cin >> name;

	cout << "Please input a password: " << endl;
	cin >> pwd;

	ofs << id << " " << name << " " << pwd << " " << endl;
	cout << "Added successfully" << endl;

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

	ofs.close();
}

Test add student:

Successfully added a message to the student file

Test add teacher:

Successfully added a message to the teacher file

7.2.2 weight removal operation

Function Description: when adding a new account, if it is a duplicate student number or a duplicate teacher employee number, you will be prompted that there is an error

7.2.2.1 reading information
  • To remove duplicate accounts, first obtain the account information of students and teachers into the program before detection
  • In manager.h, add two containers to store the information of students and teachers
  • Add a new member function void initVector() to initialize the container
	//Initialize container
	void initVector();

	//Student container
	vector<Student> vStu;

	//Teacher container
	vector<Teacher> vTea;

Add location as shown in the figure:

In the parameterized constructor of Manager, get the current student and teacher information

The code is as follows:

void Manager::initVector()
{
	//Read the information in the student file
	ifstream ifs;
	ifs.open(STUDENT_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "File read failed" << endl;
		return;
	}
    
	vStu.clear();
     vTea.clear();
    
	Student s;
	while (ifs >> s.m_Id && ifs >> s.m_Name &&  ifs >> s.m_Pwd)
	{
		vStu.push_back(s);
	}
	cout << "The current number of students is: " << vStu.size() << endl;
	ifs.close(); //Student initialization

	//Read teacher file information
	ifs.open(TEACHER_FILE, ios::in);

	Teacher t;
	while (ifs >> t.m_EmpId && ifs >> t.m_Name &&  ifs >> t.m_Pwd)
	{
		vTea.push_back(t);
	}
	cout << "The current number of teachers is: " << vTea.size() << endl;

	ifs.close();
}

In the parameter constructor, the initialization container function is called.

//Parametric structure
Manager::Manager(string name, string pwd)
{
	this->m_Name = name;
	this->m_Pwd = pwd;
    
	//Initialize container
	this->initVector();
}

Test, run the code, you can see the test code, and get the current number of students and teachers

7.2.2.2 de duplication function encapsulation

Add the member function bool checkRepeat(int id, int type) in the manager.h file;

	//Duplicate detection parameters: (incoming id, incoming type) return value: (true means duplicate, false means no duplicate)
	bool checkRepeat(int id, int type);

Implement the member function bool checkRepeat(int id, int type) in the manager.cpp file;

bool Manager::checkRepeat(int id, int type)
{
	if (type == 1)
	{
		for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
		{
			if (id == it->m_Id)
			{
				return true;
			}
		}
	}
	else
	{
		for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
		{
			if (id == it->m_EmpId)
			{
				return true;
			}
		}
	}
	return false;
}
7.2.2.3 adding and removing weights

When adding student number or teacher employee number, check whether there is repetition. The code is as follows:

	string errorTip; //Repeat error prompt

	if (select == 1)
	{
		fileName = STUDENT_FILE;
		tip = "Please enter student number: ";
		errorTip = "Duplicate student number, please re-enter";
	}
	else
	{
		fileName = TEACHER_FILE;
		tip = "Please enter employee number:";
		errorTip = "Duplicate employee number, please re-enter";
	}
	ofs.open(fileName, ios::out | ios::app);
	int id;
	string name;
	string pwd;
	cout <<tip << endl;

	while (true)
	{
		cin >> id;

		bool ret = this->checkRepeat(id, 1);

		if (ret) //There is repetition
		{
			cout << errorTip << endl;
		}
		else
		{
			break;
		}
	}

The code location is shown in the figure:

Detection effect:

7.2.2.4 bug resolution

bug Description:

  • Although duplicate accounts can be detected, the newly added account will not be detected because it has not been updated to the container
  • The student number or employee number of the newly added account can still be repeated when added again

Solution:

  • Reinitialize the container each time a new account is added

After adding, add the code:

	//Initialize container
	this->initVector();

The location is shown in the figure:

Test again. The newly added account will not be added again!

7.3 display account number

Function Description: display student information or teacher information

7.3.1 realization of display function

In the showPerson member function of the Manager, you can display the account number. The code is as follows:

void printStudent(Student & s)
{
	cout << "Student No.: " << s.m_Id << " full name: " << s.m_Name << " password:" << s.m_Pwd << endl;
}
void printTeacher(Teacher & t)
{
	cout << "Employee No.: " << t.m_EmpId << " full name: " << t.m_Name << " password:" << t.m_Pwd << endl;
}

void Manager::showPerson()
{
	cout << "Please select the content to view:" << endl;
	cout << "1,View all students" << endl;
	cout << "2,View all teachers" << endl;

	int select = 0;

	cin >> select;
    
	if (select == 1)
	{
		cout << "All student information is as follows: " << endl;
		for_each(vStu.begin(), vStu.end(), printStudent);
	}
	else
	{
		cout << "All teacher information is as follows: " << endl;
		for_each(vTea.begin(), vTea.end(), printTeacher);
	}
	system("pause");
	system("cls");
}

7.3.2 testing

Test to see student effect

Test to see the effect of Teachers

So far, the display account function has been completed

7.4 viewing the machine room

7.4.1 adding machine room information

In the case requirements, there are three machine rooms, including 20 machines in room 1, 50 machines in room 2 and 100 machines in room 3

We can enter the information into computerRoom.txt

7.4.2 creation of computer room

Under the header file, create a new file computerRoom.h

And add the following code:

#pragma once
#include<iostream>
using namespace std;
//Machine room
class ComputerRoom
{
public:

	int m_ComId; //Machine room id number

	int m_MaxNum; //Maximum capacity of machine room
};

7.4.3 initialization of machine room information

Under the Manager administrator class, add a container for the machine room to save the machine room information

	//Machine room container
	vector<ComputerRoom> vCom;

In the Manager parameter constructor, add the following code to initialize the machine room information

	//Obtain the computer room information
	ifstream ifs;

	ifs.open(COMPUTER_FILE, ios::in);

	ComputerRoom c;
	while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
	{
		vCom.push_back(c);
	}
	cout << "The current number of machine rooms is: " << vCom.size() << endl;

	ifs.close();

The location is shown in the figure:

Because the current version of the computer room information will not be changed, if there is a modification function in the later stage, it is best to package it into a function to facilitate maintenance

7.4.4 display machine room information

Add the following code to the showComputer member function of the Manager class:

//View machine room information
void Manager::showComputer()
{
	cout << "The information of the machine room is as follows: " << endl;
	for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++)
	{
		cout << "Machine room No.: " << it->m_ComId << " Maximum capacity of machine room: " << it->m_MaxNum << endl;
	}
	system("pause");
	system("cls");
}

Test and display the information function of the machine room:

7.5 empty appointment

Function Description:

Empty the generated order.txt appointment file

7.5.1 realization of emptying function

Add the following code to the cleanFile member function of Manager:

//Clear appointment record
void Manager::cleanFile()
{
	ofstream ofs(ORDER_FILE, ios::trunc);
	ofs.close();

	cout << "Empty successfully!" << endl;
	system("pause");
	system("cls");
}

Test empty, you can write some information in order.txt, and then call cleanFile to empty the file interface to see if it is empty.

8. Student module

8.1 student login and logout

8.1.1 constructor

  • In the constructor of Student class, initialize Student information. The code is as follows:
//Structure with parameters (student number, name, password)
Student::Student(int id, string name, string pwd)
{
	//Initialize properties
	this->m_Id = id;
	this->m_Name = name;
	this->m_Pwd = pwd;
}

8.1.2 administrator submenu

  • In the computer room reservation system. cpp, when the user logs in as a student, add the student menu interface
  • Provide different branches
    • Apply for an appointment
    • Check my appointments
    • View all appointments
    • cancel reservation
    • Logout login
  • Realize logout function

The code for adding the global function void studentmenu (identity * & Manager) is as follows:

//Student menu
void studentMenu(Identity * &student)
{
	while (true)
	{
		//Student menu
		student->operMenu();

		Student* stu = (Student*)student;
		int select = 0;

		cin >> select;

		if (select == 1) //Apply for an appointment
		{
			stu->applyOrder();
		}
		else if (select == 2) //View your own appointment
		{
			stu->showMyOrder();
		}
		else if (select == 3) //View all appointments
		{
			stu->showAllOrder();
		}
		else if (select == 4) //cancel reservation
		{
			stu->cancelOrder();
		}
		else
		{
			delete student;
			cout << "Logout succeeded" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}

8.1.3 realization of menu function

  • When implementing the member function void Student::operMenu(), the code is as follows:
//Menu interface
void Student::operMenu()
{
	cout << "Welcome student representatives:" << this->m_Name << "Sign in!" << endl;
	cout << "\t\t ----------------------------------\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          1.Apply for an appointment              |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          2.Check my appointments          |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          3.View all appointments          |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          4.cancel reservation              |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          0.Logout login              |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t ----------------------------------\n";
	cout << "Please select your action: " << endl;
}

8.1.4 interface docking

  • After the student has successfully logged in, the student's submenu interface is called.
  • In the student login branch, add the code:
				//Enter the student submenu
				studentMenu(person);

Add the effect as shown in the figure:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-jksx9efs-163165633807) (assets / 1548659552298. PNG)]

Test docking, and the effect is shown in the figure:

Login verification passed:

Student submenu:

Logout login:

8.2 application for appointment

8.2.1 obtaining information of computer room

  • When applying for an appointment, students can see the information of the computer room, so we need to let students get the information of the computer room

Add a new member function in student.h as follows:

	//Machine room container
	vector<ComputerRoom> vCom;

Add the following code to the student's parametric constructor:

	//Obtain the computer room information
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);

	ComputerRoom c;
	while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
	{
		vCom.push_back(c);
	}

	ifs.close();

The additional position is shown in the figure:

So far, the vCom container has saved the information of all machine rooms

8.2.2 realization of reservation function

Implement the member function void Student::applyOrder() in student.cpp

//Apply for an appointment
void Student::applyOrder()
{
	cout << "The computer room is open from Monday to Friday!" << endl;
	cout << "Please enter the time to apply for an appointment:" << 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 << "Input error, please re-enter" << endl;
	}


	cout << "Please enter the time period for applying for an appointment:" << endl;
	cout << "1,morning" << endl;
	cout << "2,afternoon" << endl;

	while (true)
	{
		cin >> interval;
		if (interval >= 1 && interval <= 2)
		{
			break;
		}
		cout << "Input error, please re-enter" << endl;
	}

	cout << "Please select a machine room:" << endl;
	cout << "1 Capacity of machine room No.:" << vCom[0].m_MaxNum << endl;
	cout << "2 Capacity of machine room No.:" << vCom[1].m_MaxNum << endl;
	cout << "3 Capacity of machine room No.:" << vCom[2].m_MaxNum << endl;

	while (true)
	{
		cin >> room;
		if (room >= 1 && room <= 3)
		{
			break;
		}
		cout << "Input error, please re-enter" << endl;
	}

	cout << "Appointment succeeded! Under review" << endl;

	ofstream ofs(ORDER_FILE, 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");
}

Run the program, test code:

Generate the following in the order.txt file:

8.3 display appointment

8.3.1 create reservation class

Function Description: when displaying reservation records, you need to obtain all records from files to display and create reservation classes to manage records and update

Create orderFile.h and orderFile.cpp files under the header file and source file respectively

Add the following code to orderFile.h:

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

class OrderFile
{
public:

	//Constructor
	OrderFile();

	//Update appointment record
	void updateOrder();

	//Record container key - number of records value - key value pair information of specific records
	map<int, map<string, string>> m_orderData;

	//Number of reservation records
	int m_Size;
};

Get all the information from the constructor and store it in the container. Add the following code:

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

	string date;      //date
	string interval;  //time slot
	string stuId;     //Student number
	string stuName;   //Student name
	string roomId;    //Machine room No
	string status;    //Reservation status


	this->m_Size = 0; //Number of appointment records

	while (ifs >> date && ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId &&  ifs >> status)
	{
		//Test code
		/*
		cout << date << endl;
		cout << interval << endl;
		cout << stuId << endl;
		cout << stuName << endl;
		cout << roomId << endl;
		cout << status << 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 = 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));
		}


		this->m_orderData.insert(make_pair(this->m_Size, m));
		this->m_Size++;
	}

	//Test code
	//for (map<int, map<string, string>>::iterator it = m_orderData.begin(); it != m_orderData.end();it++)
	//{
	//	cout << "key = " << it->first << " value = " << endl;
	//	for (map<string, string>::iterator mit = it->second.begin(); mit != it->second.end(); mit++)
	//	{
	//		cout << mit->first << " " << mit->second << " ";
	//	}
	//	cout << endl;
	//}
    
    ifs.close();
}

The code of the member function updateOrder for updating appointment records is as follows:

void OrderFile::updateOrder()
{
	if (this->m_Size == 0)
	{
		return;
	}

	ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
	for (int i = 0; i < 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 << "status:" << this->m_orderData[i]["status"] << endl;
	}
    ofs.close();
}

8.3.2 display self appointment

First, we add several appointment records, which can be added by program or directly modify the order.txt file

The contents of the order.txt file are as follows: for example, three of our students have generated three appointment records respectively

In the void Student::showMyOrder() member function of Student class, add the following code

//Check my appointments
void Student::showMyOrder()
{
	OrderFile of;
	if (of.m_Size == 0)
	{
		cout << "No appointment record" << endl;
		system("pause");
		system("cls");
		return;
	}
	for (int i = 0; i < of.m_Size; i++)
	{
		if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
		{
			cout << "Appointment date: Week" << of.m_orderData[i]["date"];
			cout << " Period:" << (of.m_orderData[i]["interval"] == "1" ? "morning" : "afternoon");
			cout << " computer room:" << of.m_orderData[i]["roomId"];
			string status = " Status: ";  // 0 cancelled appointment 1 in approval 2 reserved - 1 appointment failed
			if (of.m_orderData[i]["status"] == "1")
			{
				status += "Under review";
			}
			else if (of.m_orderData[i]["status"] == "2")
			{
				status += "Appointment succeeded";
			}
			else if (of.m_orderData[i]["status"] == "-1")
			{
				status += "Failed to approve, appointment failed";
			}
			else
			{
				status += "Appointment cancelled";
			}
			cout << status << endl;

		}
	}

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

The test effect is shown in the figure:

8.3.3 display all appointments

In the void Student::showAllOrder() member function of Student class, add the following code

//View all appointments
void Student::showAllOrder()
{
	OrderFile of;
	if (of.m_Size == 0)
	{
		cout << "No appointment record" << endl;
		system("pause");
		system("cls");
		return;
	}

	for (int i = 0; i < of.m_Size; i++)
	{
		cout << i + 1 << ", ";

		cout << "Appointment date: Week" << of.m_orderData[i]["date"];
		cout << " Period:" << (of.m_orderData[i]["interval"] == "1" ? "morning" : "afternoon");
		cout << " Student No.:" << of.m_orderData[i]["stuId"];
		cout << " full name:" << of.m_orderData[i]["stuName"];
		cout << " computer room:" << of.m_orderData[i]["roomId"];
		string status = " Status: ";  // 0 cancelled appointment 1 in approval 2 reserved - 1 appointment failed
		if (of.m_orderData[i]["status"] == "1")
		{
			status += "Under review";
		}
		else if (of.m_orderData[i]["status"] == "2")
		{
			status += "Appointment succeeded";
		}
		else if (of.m_orderData[i]["status"] == "-1")
		{
			status += "Failed to approve, appointment failed";
		}
		else
		{
			status += "Appointment cancelled";
		}
		cout << status << endl;
	}

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

The test effect is shown in the figure:

8.4 cancellation of appointment

In the void Student::cancelOrder() member function of the Student class, add the following code

//cancel reservation
void Student::cancelOrder()
{
	OrderFile of;
	if (of.m_Size == 0)
	{
		cout << "No appointment record" << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "The records being approved or successfully reserved can be cancelled. Please enter the cancelled records" << endl;

	vector<int>v;
	int index = 1;
	for (int i = 0; i < of.m_Size; i++)
	{
		if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
		{
			if (of.m_orderData[i]["status"] == "1" || of.m_orderData[i]["status"] == "2")
			{
				v.push_back(i);
				cout <<  index ++  << ", ";
				cout << "Appointment date: Week" << of.m_orderData[i]["date"];
				cout << " Period:" << (of.m_orderData[i]["interval"] == "1" ? "morning" : "afternoon");
				cout << " computer room:" << of.m_orderData[i]["roomId"];
				string status = " Status: ";  // 0 cancelled appointment 1 in approval 2 reserved - 1 appointment failed
				if (of.m_orderData[i]["status"] == "1")
				{
					status += "Under review";
				}
				else if (of.m_orderData[i]["status"] == "2")
				{
					status += "Appointment succeeded";
				}
				cout << status << endl;

			}
		}
	}

	cout << "Please enter a record to cancel,0 Representative return" << endl;
	int select = 0;
	while (true)
	{
		cin >> select;
		if (select >= 0 && select <= v.size())
		{
			if (select == 0)
			{
				break;
			}
			else
			{
				//	Cout < < record location: < < v [Select - 1] < < endl;
				of.m_orderData[v[select - 1]]["status"] = "0";
				of.updateOrder();
				cout << "Appointment cancelled" << endl;
				break;
			}

		}
		cout << "Input error, please re-enter" << endl;
	}

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

Test cancellation:

View personal appointment record again:

View all appointments

View the order.txt appointment file

So far, all the functions of the student module have been realized

9. Teacher module

9.1 teacher login and logout

9.1.1 constructor

  • In the constructor of Teacher class, initialize the Teacher information. The code is as follows:
//Structure with reference (employee number, name, password)
Teacher::Teacher(int empId, string name, string pwd)
{
	//Initialize properties
	this->m_EmpId = empId;
	this->m_Name = name;
	this->m_Pwd = pwd;
}

9.1.2 teacher submenu

  • In the computer room reservation system. cpp, when the user logs in as a teacher, add the teacher menu interface
  • Provide different branches
    • View all appointments
    • Audit appointment
    • Logout login
  • Realize logout function

The code of adding the global function void teachermenu (person * & Manager) is as follows:

//Teacher menu
void TeacherMenu(Identity * &teacher)
{
	while (true)
	{
		//Teacher menu
		teacher->operMenu();

		Teacher* tea = (Teacher*)teacher;
		int select = 0;

		cin >> select;

		if (select == 1)
		{
			//View all appointments
			tea->showAllOrder();
		}
		else if (select == 2)
		{
			//Audit appointment
			tea->validOrder();
		}
		else
		{
			delete teacher;
			cout << "Logout succeeded" << endl;
			system("pause");
			system("cls");
			return;
		}

	}
}

9.1.3 realization of menu function

  • When implementing the member function void Teacher::operMenu(), the code is as follows:
//Teacher menu interface
void Teacher::operMenu()
{
	cout << "Welcome teachers:" << this->m_Name << "Sign in!" << endl;
	cout << "\t\t ----------------------------------\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          1.View all appointments          |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          2.Audit appointment              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          0.Logout login              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t ----------------------------------\n";
	cout << "Please select your action: " << endl;
}

9.1.4 interface docking

  • After the teacher has successfully logged in, call the teacher's submenu interface.
  • In the teacher login branch, add the code:
				//Enter the teacher submenu
				TeacherMenu(person);

Add the effect as shown in the figure:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-txc12uqr-163165633814) (assets / 1548670866708. PNG)]

Test docking, and the effect is shown in the figure:

Login verification passed:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-jcphluvj-163165633814) (assets / 1548670949885. PNG)]

Teacher submenu:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-qf347r0y-163165633815) (assets / 1548670958602. PNG)]

Logout login:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-q29z4qmb-163165633815) (assets / 1548670966988. PNG)]

9.2 view all appointments

9.2.1 realization of all reservation functions

This function is similar to the function of viewing all appointments as students. It is used to display all appointment records

Implement the member function void Teacher::showAllOrder() in Teacher.cpp

void Teacher::showAllOrder()
{
	OrderFile of;
	if (of.m_Size == 0)
	{
		cout << "No appointment record" << endl;
		system("pause");
		system("cls");
		return;
	}
	for (int i = 0; i < of.m_Size; i++)
	{
		cout << i + 1 << ", ";

		cout << "Appointment date: Week" << of.m_orderData[i]["date"];
		cout << " Period:" << (of.m_orderData[i]["interval"] == "1" ? "morning" : "afternoon");
		cout << " Student No.:" << of.m_orderData[i]["stuId"];
		cout << " full name:" << of.m_orderData[i]["stuName"];
		cout << " computer room:" << of.m_orderData[i]["roomId"];
		string status = " Status: ";  // 0 cancelled appointment 1 in approval 2 reserved - 1 appointment failed
		if (of.m_orderData[i]["status"] == "1")
		{
			status += "Under review";
		}
		else if (of.m_orderData[i]["status"] == "2")
		{
			status += "Appointment succeeded";
		}
		else if (of.m_orderData[i]["status"] == "-1")
		{
			status += "Failed to approve, appointment failed";
		}
		else
		{
			status += "Appointment cancelled";
		}
		cout << status << endl;
	}

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

9.2.2 test function

Run the test to see all appointments as teachers

The test effect is shown in the figure:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-m2pch0oi-163165633815) (assets / 1548676922678. PNG)]

9.3 audit appointment

9.3.1 implementation of audit function

Function Description: the teacher reviews the student's reservation and reviews the reservation according to the actual situation

Implement the member function void Teacher::validOrder() in Teacher.cpp

The code is as follows:

//Audit appointment
void Teacher::validOrder()
{
	OrderFile of;
	if (of.m_Size == 0)
	{
		cout << "No appointment record" << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "The appointment records to be reviewed are as follows:" << endl;

	vector<int>v;
	int index = 0;
	for (int i = 0; i < of.m_Size; i++)
	{
		if (of.m_orderData[i]["status"] == "1")
		{
			v.push_back(i);
			cout << ++index << ", ";
			cout << "Appointment date: Week" << of.m_orderData[i]["date"];
			cout << " Period:" << (of.m_orderData[i]["interval"] == "1" ? "morning" : "afternoon");
			cout << " computer room:" << of.m_orderData[i]["roomId"];
			string status = " Status: ";  // 0 cancelled appointment 1 in approval 2 reserved - 1 appointment failed
			if (of.m_orderData[i]["status"] == "1")
			{
				status += "Under review";
			}
			cout << status << endl;
		}
	}
	cout << "Please enter the approved appointment record,0 Representative return" << 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 audit result" << endl;
				cout << "1,adopt" << endl;
				cout << "2,Fail" << endl;
				cin >> ret;

				if (ret == 1)
				{
					of.m_orderData[v[select - 1]]["status"] = "2";
				}
				else
				{
					of.m_orderData[v[select - 1]]["status"] = "-1";
				}
				of.updateOrder();
				cout << "Audit completed!" << endl;
				break;
			}
		}
		cout << "Input error, please re-enter" << endl;
	}

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

9.3.2 test audit appointment

Test - approved

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-ab8sisgx-163165633816) (assets / 1548677286679. PNG)]

Approval

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-9hbrbybl-163165633816) (assets / 1548677383681. PNG)]

Test - Audit failed

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zbekdarz-163165633816) (assets / 1548677402705. PNG)]

Audit failure:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-w9mecthd-163165633817) (assets / 1548677632792. PNG)]

View records as a student:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG ppmtcnqy-163165633817) (assets / 1548677798815. PNG)]

Approval of appointment succeeded!

So far, the production of this case is completed^_^

Keywords: Java C++ C#

Added by FluxNYC on Wed, 15 Sep 2021 00:20:15 +0300