# Employee management system ## 1. Management system requirements The employee management system can be used to manage the information of all employees in the company This tutorial mainly uses C++To realize a staff management system based on polymorphism Employees in the company are divided into three categories: ordinary employees, managers and bosses. When displaying information, you need to display employee number, employee name, employee position and responsibility Responsibilities of ordinary employees: complete the tasks assigned by the manager Manager Responsibilities: complete the tasks assigned by the boss and distribute the tasks to employees Responsibilities of the boss: manage all affairs of the company The functions to be realized in the management system are as follows: * Exit management program: exit the current management system - Add employee information: realize the function of adding employees in batch, and enter the information into the file. The employee information is: employee number, name and department number - Display employee information: displays the information of all employees within the company - Delete resigned employee: delete the specified employee by number - Modify employee information: modify employee personal information according to the number - Find employee information: find relevant personnel information according to employee number or employee name - Sort by number: sort by employee number. The sorting rules are specified by the user - Empty all documents: empty all employee information recorded in the file (it needs to be confirmed again before emptying to prevent accidental deletion) The system interface effect diagram is as follows: ![1546511409198](assets/1546511409198.png) Different functions need to be completed according to different choices of users! ## 2. Create project The steps to create a project are as follows: - Create a new project - Add file ### 2.1 create project open vs2017 Click create new project to create a new project C++project ![1544151201465](assets/1544151201465.png) Fill in the project name and project path, and click OK ![1546349209805](assets/1546349209805.png) ### 2.2 adding files Right click the source file to add a file ![1546349360960](assets/1546349360960.png) ![1546349421496](assets/1546349421496.png) ![1546349488752](assets/1546349488752.png) So far, the project has been created ## 3. Create management class The management is responsible for the following: * Menu interface for communication with users * Adding, deleting, modifying and querying employees * Read write interaction with files ### 3.1 creating files Create in the folder of header file and source file respectively workerManager.h and workerManager.cpp file ![1546349904944](assets/1546349904944.png) ### 3.2 header file implementation stay workerManager.h Design management class in The code is as follows: ```C++ #pragma once #include<iostream> using namespace std; class WorkerManager { public: //Constructor WorkerManager(); //Destructor ~WorkerManager(); };
3.3 source file implementation
In the workermanager In CPP, empty constructors and destructors are complemented
#include "workerManager.h" WorkerManager::WorkerManager() { } WorkerManager::~WorkerManager() { }
At this point, the employee management class has been created
4. Menu function
Function Description: communication interface with users
4.1 adding member functions
In the management class workermanager Add member function void show in H_ Menu();
[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-3Hf2ohtW-1641181311948)(assets/1546351543942.png)]
4.2 realization of menu function
In the management class workermanager Show in CPP_ Menu() function
void WorkerManager::Show_Menu() { cout << "********************************************" << endl; cout << "********* Welcome to the employee management system! **********" << endl; cout << "************* 0.Exit management program *************" << endl; cout << "************* 1.Add employee information *************" << endl; cout << "************* 2.Display employee information *************" << endl; cout << "************* 3.Delete resigned employee *************" << endl; cout << "************* 4.Modify employee information *************" << endl; cout << "************* 5.Find employee information *************" << endl; cout << "************* 6.Sort by number *************" << endl; cout << "************* 7.Empty all documents *************" << endl; cout << "********************************************" << endl; cout << endl; }
4.3 test menu function
In the employee management system Test menu function in cpp
code:
#include<iostream> using namespace std; #include "workerManager.h" int main() { WorkerManager wm; wm.Show_Menu(); system("pause"); return 0; }
The operation effect is shown in the figure below:
[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-lbi7quan-164181311948) (assets / 1546352771191. PNG)]
5. Exit function
5.1 provide functional interfaces
Branch selection is provided in the main function, and each function interface is provided
code:
int main() { WorkerManager wm; int choice = 0; while (true) { //Display menu wm.Show_Menu(); cout << "Please enter your choice:" << endl; cin >> choice; switch (choice) { case 0: //Exit the system break; case 1: //Add employee break; case 2: //Show employees break; case 3: //Delete employee break; case 4: //Modify employee break; case 5: //Find employees break; case 6: //Sort employees break; case 7: //Empty file break; default: system("cls"); break; } } system("pause"); return 0; }
5.2 realize exit function
In the workermanager H provides the member function void exitSystem(), which exits the system;
In the workermanager CPP provides specific function implementation
void WorkerManager::exitSystem() { cout << "Welcome to use next time" << endl; system("pause"); exit(0); }
5.3 test function
In the 0 option of the main function branch, the interface of the exit program is called.
[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-denunplb-164181311949) (assets / 1546353199424. PNG)]
The running test effect is shown in the figure below:
[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-senbxhlz-164181311949) (assets / 1546353155490. PNG)]
6. Create employee class
6.1 employee Abstract creation
Employees are classified as ordinary employees, managers and bosses
Three kinds of employees are abstracted into a class (worker), and different types of employees are managed by polymorphism
The attributes of employees are: employee number, employee name and employee department number
The behaviors of employees are: job responsibility information description and obtaining job name
Create a file worker under the header file folder H file and add the following code:
#pragma once #include<iostream> #include<string> using namespace std; //Employee abstract base class class Worker { public: //Display personal information virtual void showInfo() = 0; //Get position name virtual string getDeptName() = 0; int m_Id; //Employee number string m_Name; //Employee name int m_DeptId; //Employee's department name and number };
6.2 create ordinary employees
The ordinary employee class inherits the employee abstract class and overrides the pure virtual function in the parent class
Create employee. In the folder of header file and source file respectively H and employee Cpp file
employee. The codes in H are as follows:
#pragma once #include<iostream> using namespace std; #include "worker.h" //Employee category class Employee :public Worker { public: //Constructor Employee(int id, string name, int dId); //Display personal information virtual void showInfo(); //Get employee position name virtual string getDeptName(); };
employee. The code in CPP is as follows:
#include "employee.h" Employee::Employee(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } void Employee::showInfo() { cout << "Employee No.: " << this->m_Id << " \t Employee name: " << this->m_Name << " \t Position:" << this->getDeptName() << " \t Job responsibilities: complete the tasks assigned by the manager" << endl; } string Employee::getDeptName() { return string("staff"); }
6.3 create manager class
The manager class inherits the employee abstract class and overrides the pure virtual function in the parent class, which is similar to ordinary employees
Create a manager. In the folder of the header file and the source file, respectively H and manager Cpp file
manager. The codes in H are as follows:
#pragma once #include<iostream> using namespace std; #include "worker.h" //Manager class class Manager :public Worker { public: Manager(int id, string name, int dId); //Display personal information virtual void showInfo(); //Get employee position name virtual string getDeptName(); };
manager. The code in CPP is as follows:
#include "manager.h" Manager::Manager(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } void Manager::showInfo() { cout << "Employee No.: " << this->m_Id << " \t Employee name: " << this->m_Name << " \t Position:" << this->getDeptName() << " \t Job responsibilities: complete the tasks assigned by the boss,And issue tasks to employees" << endl; } string Manager::getDeptName() { return string("manager"); }
6.4 create boss class
The boss class inherits the employee abstract class and overrides the pure virtual function in the parent class, which is similar to ordinary employees
Create boss under the folder of header file and source file respectively H and boss Cpp file
boss. The codes in H are as follows:
#pragma once #include<iostream> using namespace std; #include "worker.h" //Boss class class Boss :public Worker { public: Boss(int id, string name, int dId); //Display personal information virtual void showInfo(); //Get employee position name virtual string getDeptName(); };
boss. The code in CPP is as follows:
#include "boss.h" Boss::Boss(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } void Boss::showInfo() { cout << "Employee No.: " << this->m_Id << " \t Employee name: " << this->m_Name << " \t Position:" << this->getDeptName() << " \t Job responsibilities: manage all affairs of the company" << endl; } string Boss::getDeptName() { return string("CEO"); }
6.5 testing polymorphism
In the employee management system cpp add test functions, and run can produce polymorphism
The test code is as follows:
#include "worker.h" #include "employee.h" #include "manager.h" #include "boss.h" void test() { Worker * worker = NULL; worker = new Employee(1, "Zhang San", 1); worker->showInfo(); delete worker; worker = new Manager(2, "Li Si", 2); worker->showInfo(); delete worker; worker = new Boss(3, "Wang Wu", 3); worker->showInfo(); delete worker; }
The operation effect is shown in the figure below:
[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-yuiskl0r-1641811311950) (assets / 1546398236081. PNG)]
After the test is successful, the test code comments can be retained or deleted
7. Add employee
Function Description: add employees in batches and save them to files
7.1 functional analysis
analysis:
When creating in batch, users may create different types of employees
If you want to put all different types of employees into an array, you can maintain the pointers of all employees in an array
If you want to maintain this indefinite length array in the program, you can create the array to the heap and maintain it by using the pointer of Worker * *
[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 ijnfs4xs-1641811311950) (assets / 1546399491099. PNG)]
7.2 function realization
At wokermanager Add a member attribute code to the H header file:
//Number of people in the record file int m_EmpNum; //Pointer to employee array Worker ** m_EmpArray;
Initialize properties in the WorkerManager constructor
WorkerManager::WorkerManager() { //Initialization number this->m_EmpNum = 0; //Initialize array pointer this->m_EmpArray = NULL; }
In the workermanager Add member function in H
//Increase staff void Add_Emp();
workerManager. The function is implemented in CPP
//Increase staff void WorkerManager::Add_Emp() { cout << "Please enter the number of employees to be added: " << endl; int addNum = 0; cin >> addNum; if (addNum > 0) { //Calculate new space size int newSize = this->m_EmpNum + addNum; //Open up new space Worker ** newSpace = new Worker*[newSize]; //Store the contents of the original space under the new space if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { newSpace[i] = this->m_EmpArray[i]; } } //Enter new data for (int i = 0; i < addNum; i++) { int id; string name; int dSelect; cout << "Please enter page " << i + 1 << " New employee No.:" << endl; cin >> id; cout << "Please enter page " << i + 1 << " Names of new employees:" << endl; cin >> name; cout << "Please select the position of the employee:" << endl; cout << "1,Ordinary workers" << endl; cout << "2,manager" << endl; cout << "3,boss" << endl; cin >> dSelect; Worker * worker = NULL; switch (dSelect) { case 1: //Ordinary staff worker = new Employee(id, name, 1); break; case 2: //manager worker = new Manager(id, name, 2); break; case 3: //boss worker = new Boss(id, name, 3); break; default: break; } newSpace[this->m_EmpNum + i] = worker; } //Release the original space delete[] this->m_EmpArray; //Change the orientation of the new space this->m_EmpArray = newSpace; //Number of new updates this->m_EmpNum = newSize; //Prompt information cout << "Successfully added" << addNum << "A new employee!" << endl; } else { cout << "Incorrect input" << endl; } system("pause"); system("cls"); }
In workermanager In the destructor of CPP, the heap data is released
WorkerManager::~WorkerManager() { if (this->m_EmpArray != NULL) { delete[] this->m_EmpArray; } }
7.3 test addition
In the 1 option of the main function branch, call the worker interface.
[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-lPix2y7A-1641181311951)(assets/1546401705277.png)]
The 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-Bg4Ezpxf-1641181311951)(assets/1546401763461.png)]
At this point, the function of adding employees to the program is completed
8. File interaction - write file
Function Description: read and write files
In the last add function, we just added all the data to the memory. Once the program is finished, we can't save it
Therefore, the file management class needs a function to interact with files to read and write files
8.1 setting file path
First, we put the file path in workermanager H and contains the header file fstream
#include <fstream> #define FILENAME "empFile.txt"
8.2 member function declaration
In the workermanager Add the member function void save() to the class in H
//Save file void save();
8.3 realization of file saving function
void WorkerManager::save() { ofstream ofs; ofs.open(FILENAME, ios::out); for (int i = 0; i < this->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->m_Id << " " << this->m_EmpArray[i]->m_Name << " " << this->m_EmpArray[i]->m_DeptId << endl; } ofs.close(); }
8.4 save file function test
Add the save file function after adding successfully in the add employee function
[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-wq1ythry-164181311951) (assets / 1546432469465. PNG)]
Run the code again and add the employee
[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-jmaofnvnn-1641811311952) (assets / 1546401763461. PNG)]
There are more files in the same level directory, and the added information is saved
[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-NmAwNRgL-1641181311952)(assets/1546432343078.png)]
9. File interaction - read file
Function Description: read the contents of the file into the program
Although we realized the operation of saving to the file after adding employees, we did not read the data in the file into the program every time we started running the program
There is also a need to empty files in our program functions
Therefore, there are three situations in which the constructor initializes data
- First use, file not created
- The file exists, but the data is cleared by the user
- The file exists and all data of employees are saved
9.1 file not created
In the workermanager Add a new member attribute m to H_ Fileisempty flag whether the file is empty
//Flag whether the file is empty bool m_FileIsEmpty;
Modify workermanager Constructor code in CPP
WorkerManager::WorkerManager() { ifstream ifs; ifs.open(FILENAME, ios::in); //File does not exist if (!ifs.is_open()) { cout << "file does not exist" << endl; //Test output this->m_EmpNum = 0; //Initialization number this->m_FileIsEmpty = true; //Initialization file empty flag this->m_EmpArray = NULL; //Initialize array ifs.close(); //Close file return; } }
After deleting the file, initialize the data function when the test file does not exist
9.2 the file exists and the data is empty
In the workermanager Constructor append code in CPP:
//The file exists and is not recorded char ch; ifs >> ch; if (ifs.eof()) { cout << "The file is empty!" << endl; this->m_EmpNum = 0; this->m_FileIsEmpty = true; this->m_EmpArray = NULL; ifs.close(); return; }
The location of the additional code 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-0smbbemn-164181311952) (assets / 1546435197575. PNG)]
After creating the file, empty the file content and test the initialization function in this case
We found that the file does not exist or is empty m_FileIsEmpty flag to judge whether the file is empty is true. When is it false?
After the employee is successfully added, the flag that the file is not empty should be changed
In void workermanager:: add_ Add to the emp() member function:
//Update employee not null flag this->m_FileIsEmpty = false;
[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-lgwwja5h-164181311952) (assets / 1546656256176. PNG)]
9.3 documents exist and employee data are saved
9.3.1 number of employees obtaining records
In the workermanager Add member function int get in H_ EmpNum();
//Number of Statistics int get_EmpNum();
workerManager. Implementation in CPP
int WorkerManager::get_EmpNum() { ifstream ifs; ifs.open(FILENAME, ios::in); int id; string name; int dId; int num = 0; while (ifs >> id && ifs >> name && ifs >> dId) { //Record number of people num++; } ifs.close(); return num; }
In the workermanager Continue to append code in CPP constructor:
int num = this->get_EmpNum(); cout << "Number of employees:" << num << endl; //Test code this->m_EmpNum = num; //Update member properties
Manually add some employee data and test the function of obtaining employee quantity
[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-80p72xou-16411811953) (assets / 1546436429055. PNG)]
[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-pbd2pbvw-16411811953) (assets / 1546436385793. PNG)]
9.3.2 initializing arrays
Initialize worker * * m in workerManager according to employee data and employee data_ Emparray pointer
In workermanager Add the member function void init in H_ Emp();
//Initialize employee void init_Emp();
In workermanager Implementation in CPP
void WorkerManager::init_Emp() { ifstream ifs; ifs.open(FILENAME, ios::in); int id; string name; int dId; int index = 0; while (ifs >> id && ifs >> name && ifs >> dId) { Worker * worker = NULL; //Create different objects according to different department IDs if (dId == 1) // 1 ordinary employees { worker = new Employee(id, name, dId); } else if (dId == 2) //2 Manager { worker = new Manager(id, name, dId); } else //CEO { worker = new Boss(id, name, dId); } //Stored in an array this->m_EmpArray[index] = worker; index++; } }
In the workermanager Append code to CPP constructor
//Create an array based on the number of employees this->m_EmpArray = new Worker *[this->m_EmpNum]; //Initialize employee init_Emp(); //Test code for (int i = 0; i < m_EmpNum; i++) { cout << "Employee No.: " << this->m_EmpArray[i]->m_Id << " Employee name: " << this->m_EmpArray[i]->m_Name << " Department No.: " << this->m_EmpArray[i]->m_DeptId << endl; }
Run the program to test the data obtained from the file
[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-1wzfxaad-1641811311953) (assets / 1546436938152. PNG)]
So far, the initialization data function is completed, and the test code can be commented or deleted!
10. Show employees
Function Description: displays the information of all current employees
10.1 display employee function declaration
In the workermanager Add member function void show in H_ Emp();
//Show employees void Show_Emp();
10.2 implementation of display function
In the workermanager Implementing member function void show in CPP_ Emp();
//Show employees void WorkerManager::Show_Emp() { if (this->m_FileIsEmpty) { cout << "File does not exist or record is empty!" << endl; } else { for (int i = 0; i < m_EmpNum; i++) { //Using polymorphic call interface this->m_EmpArray[i]->showInfo(); } } system("pause"); system("cls"); }
10.3 test display
In the 2 option of the main function branch, call the display worker interface.
[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-hhmv9npf-16411811954) (assets / 1546497336465. PNG)]
When testing, test the case that the file is empty and the case that the file is not empty
Test effect:
Test 1 - file does not exist or is empty
[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-zhptsizt-16411811954) (assets / 1546497082135. PNG)]
Test 2 - documentation exists and documented
[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-cdldumkc-164181311954) (assets / 1546496947671. PNG)]
After the test, the function of displaying all employee information has been realized
11. Delete employee
Function Description: delete an employee according to the employee number
11.1 delete employee function declaration
In the workermanager Add member function void del in H_ Emp();
//Delete employee void Del_Emp();
11.2 does the employee have a function declaration
Many functions need to be operated according to the existence of employees, such as deleting employees, modifying employees and finding employees
Therefore, the announcement function is added for subsequent calls
In the workermanager Add the member function int IsExist(int id) to h;
//Judge whether the employee exists according to the employee number. If it exists, return the position of the employee in the array, and if it does not exist, return - 1 int IsExist(int id);
11.3 does the employee have function implementation
In the workermanager Implement the member function int IsExist(int id) in CPP;
int WorkerManager::IsExist(int id) { int index = -1; for (int i = 0; i < this->m_EmpNum; i++) { if (this->m_EmpArray[i]->m_Id == id) { index = i; break; } } return index; }
11.4 delete employee function implementation
In the workermanager Implementing member function void del in CPP_ Emp();
//Delete employee void WorkerManager::Del_Emp() { if (this->m_FileIsEmpty) { cout << "File does not exist or record is empty!" << endl; } else { //Delete by employee number cout << "Please enter the employee number you want to delete:" << endl; int id = 0; cin >> id; int index = this->IsExist(id); if (index != -1) //Description: the location data on the index needs to be deleted { for (int i = index; i < this->m_EmpNum - 1; i++) { this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum--; this->save(); //After deletion, the data is synchronized to the file cout << "Delete succeeded!" << endl; } else { cout << "Deletion failed. The employee was not found" << endl; } } system("pause"); system("cls"); }
11.5 test deletion
In the 3 option of the main function branch, the worker interface is called.
[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-0o3hf2k2-16411811955) (assets / 1546502698622. PNG)]
Test 1 - delete non-existent employees
[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 dpccfliu-1641811311955) (assets / 1546500324196. PNG)]
Test 2 - delete existing employees
Prompt diagram for successful deletion:
[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-yvyehtqu-16411811955) (assets / 1546500350526. PNG)]
Display all employee information again to ensure that it has been deleted
[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-vnfhzhr0-16411811955) (assets / 1546500361889. PNG)]
Check the information in the file and verify again that the employee has been completely deleted
[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-aWA6cgrE-1641181311956)(assets/1546500383570.png)]
So far, the employee deletion function is completed!
12. Modify employee
Function Description: it can modify and save employee information according to employee number
12.1 modify employee function statement
In the workermanager Add member function void mod in H_ Emp();
//Modify employee void Mod_Emp();
12.2 modifying employee function implementation
In the workermanager Implementing member function void mod in CPP_ Emp();
//Modify employee void WorkerManager::Mod_Emp() { if (this->m_FileIsEmpty) { cout << "File does not exist or record is empty!" << endl; } else { cout << "Please enter the number of the modified employee:" << endl; int id; cin >> id; int ret = this->IsExist(id); if (ret != -1) { //Employee with No. found delete this->m_EmpArray[ret]; int newId = 0; string newName = ""; int dSelect = 0; cout << "Found: " << id << "Employee No., please enter the new employee No.: " << endl; cin >> newId; cout << "Please enter a new name: " << endl; cin >> newName; cout << "Please enter the position: " << endl; cout << "1,Ordinary workers" << endl; cout << "2,manager" << endl; cout << "3,boss" << endl; cin >> dSelect; Worker * worker = NULL; switch (dSelect) { case1: worker = new Employee(newId, newName, dSelect); break; case2: worker = new Manager(newId, newName, dSelect); break; case 3: worker = new Boss(newId, newName, dSelect); break; default: break; } //Change data to array this->m_EmpArray[ret]= worker; cout << "Modification succeeded!" << endl; //Save to file this->save(); } else { cout << "Modification failed, no one found" << endl; } } //Press any key to clear the screen system("pause"); system("cls"); }
12.3 test modification
In the 4 option of the main function branch, call modify the worker interface.
[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-mxDvCLGa-1641181311956)(assets/1546502651922.png)]
Test 1 - modify no employee situation
[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-8x16xXvB-1641181311956)(assets/1546502759643.png)]
Test 2 - modify the existing employee situation, for example, change the employee "Li Si" to "Zhao Si"
[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-pnalviq4-1641811311956) (assets / 1546502830350. PNG)]
After modification, view all employee information again and confirm that the modification is successful
[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-zvdwynvf-164181311957) (assets / 1546502865443. PNG)]
Confirm again that the information in the file is also updated synchronously
[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-cqwMPq7E-1641181311957)(assets/1546502898653.png)]
So far, the function of modifying employees has been realized!
13. Find employees
Function Description: there are two ways to find employees: one is by employee number and the other is by employee name
13.1 find employee function declaration
In the workermanager Add the member function void find in H_ Emp();
//Find employees void Find_Emp();
13.2 implementation of employee search function
In the workermanager Implementing member function void find in CPP_ Emp();
//Find employees void WorkerManager::Find_Emp() { if (this->m_FileIsEmpty) { cout << "File does not exist or record is empty!" << endl; } else { cout << "Please enter how to find:" << endl; cout << "1,Search by employee number" << endl; cout << "2,Find by name" << endl; int select = 0; cin >> select; if (select == 1) //Search by employee number { int id; cout << "Please enter the employee number you are looking for:" << endl; cin >> id; int ret = IsExist(id); if (ret != -1) { cout << "Search succeeded! The employee information is as follows:" << endl; this->m_EmpArray[ret]->showInfo(); } else { cout << "Search failed, no one found" << endl; } } else if(select == 2) //Find by name { string name; cout << "Please enter the name you are looking for:" << endl; cin >> name; bool flag = false; //Flag found for (int i = 0; i < m_EmpNum; i++) { if (m_EmpArray[i]->m_Name == name) { cout << "Search succeeded,Employee No.:" << m_EmpArray[i]->m_Id << " The information of the number is as follows:" << endl; flag = true; this->m_EmpArray[i]->showInfo(); } } if (flag == false) { //No one was found cout << "Search failed, no one found" << endl; } } else { cout << "Incorrect input options" << endl; } } system("pause"); system("cls"); }
13.3 test results
In the 5 option of the main function branch, call to find the worker interface.
[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-koybqkvd-164118131957) (assets / 1546504714318. PNG)]
Test 1 - find by employee number - find no employee
[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-ancx8u4v-164181311957) (assets / 1546504767229. PNG)]
Test 2 - find employees by employee number - find existing employees
[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-U63Lp88K-1641181311958)(assets/1546505046521.png)]
Test 3 - find by employee name - find no employee
[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-3wDDGyaz-1641181311958)(assets/1546505115610.png)]
Test 4 - Search by employee name - search for existing employees (if there are duplicate names, they are also displayed, and duplicate employees can be added to the file)
For example, add two employees of Wang Wu, and then find Wang Wu by name
[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-O1U4uQuZ-1641181311958)(assets/1546507850441.png)]
[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-ydDle0H2-1641181311958)(assets/1546507760284.png)]
So far, the employee search function is completed!
14. Sort
Function Description: sort by employee number. The sorting order is specified by the user
14.1 sorting function declaration
In the workermanager Add member function void sort in H_ Emp();
//Sort employees void Sort_Emp();
14.2 implementation of sorting function
In the workermanager Implementing member function void sort in CPP_ Emp();
//Sort employees void WorkerManager::Sort_Emp() { if (this->m_FileIsEmpty) { cout << "File does not exist or record is empty!" << endl; system("pause"); system("cls"); } else { cout << "Please select sorting method: " << endl; cout << "1,Ascending by employee number" << endl; cout << "2,Descending by employee number" << endl; int select = 0; cin >> select; for (int i = 0; i < m_EmpNum; i++) { int minOrMax = i; for (int j = i + 1; j < m_EmpNum; j++) { if (select == 1) //Ascending order { if (m_EmpArray[minOrMax]->m_Id > m_EmpArray[j]->m_Id) { minOrMax = j; } } else //Descending order { if (m_EmpArray[minOrMax]->m_Id < m_EmpArray[j]->m_Id) { minOrMax = j; } } } if (i != minOrMax) { Worker * temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[minOrMax]; m_EmpArray[minOrMax] = temp; } } cout << "Sorting succeeded,After sorting, the result is:" << endl; this->save(); this->Show_Emp(); } }
14.3 test sorting function
In the 6 option of the main function branch, call the sorting worker interface.
[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-QeSbiMwh-1641181311959)(assets/1546510145181.png)]
Test:
First, we add some employees whose serial numbers are out of order, for example:
[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-nqzrooff-16411811959) (assets / 1546658169987. PNG)]
Test - ascending sort
[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-ueixyxnm-164181311959) (assets / 1546658190479. PNG)]
File synchronization update
[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-KUccvQ4u-1641181311959)(assets/1546658273581.png)]
Test - descending sort
[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-ge9u9w0b-1641811311960) (assets / 1546658288936. PNG)]
File synchronization update
[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-1TQzpbx6-1641181311960)(assets/1546658313704.png)]
So far, the function of sorting employees by number has been realized!
15. Empty file
Function Description: clear the data recorded in the file
15.1 empty function declaration
In the workermanager Add member function void clean in H_ File();
//Empty file void Clean_File();
15.2 implementation of emptying function
In the workermanager Implement void clean in CPP_ File();
//Empty file void WorkerManager::Clean_File() { cout << "Confirm emptying?" << endl; cout << "1,confirm" << endl; cout << "2,return" << endl; int select = 0; cin >> select; if (select == 1) { //Open the mode ios::trunc. If it exists, delete the file and re create it ofstream ofs(FILENAME, ios::trunc); ofs.close(); if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { if (this->m_EmpArray[i] != NULL) { delete this->m_EmpArray[i]; } } this->m_EmpNum = 0; delete[] this->m_EmpArray; this->m_EmpArray = NULL; this->m_FileIsEmpty = true; } cout << "Empty successfully!" << endl; } system("pause"); system("cls"); }
15.3 test empty file
In the 7 option of the main function branch, the empty file interface is called.
[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-icjvah3s-1641811311960) (assets / 1546511085541. PNG)]
Test: confirm to empty the file
[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-VaM4cdMn-1641181311960)(assets/1546510976745.png)]
Check the data in the file again. The record is empty
[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-ufaxhrfu-1641811311961) (assets / 1546510994196. PNG)]
Open the file and ensure that the data in it is empty. This function needs to be used with caution!
[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-vkymzek6-164181311961) (assets / 1546511018517. PNG)]
With the realization of the empty file function, this case is completed ^^