1, Problem description
The employee management system can be used to manage the information of all employees in the company
This paper mainly uses C + + to realize an employee management system based on polymorphism. The employees in the company are divided into three categories: ordinary employees, managers and bosses. When displaying information, it is necessary 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 issue tasks to employees
- Boss's responsibility: manage everything in the company
The functions to be realized in the management system are as follows:
- Exit management system: 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 the employee's number or employee's 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 (reconfirm before emptying to prevent accidental deletion).
2, Implementation process
Create abstract Employee class worker h
#pragma once #include <string> #include <iostream> using namespace std; // Create abstract Employee class class Worker { public: // Display personal information virtual void showInfo() = 0; int m_IDnumber; string m_Name; string m_Post; string m_Duty; };
Use inheritance to create boss, manager and employee classes respectively, and inherit Abstract employee classes
Boss class header file (Boss.h):
#pragma once #include <string> #include <iostream> #include "Worker.h" using namespace std; // Create boss class class Boss : public Worker { public: Boss(int IDnumber,string name); void showInfo(); };
The specific implementation (Boss.cpp) is as follows:
#include "Boss.h" Boss::Boss(int IDnumber, string name) { m_IDnumber = IDnumber; m_Name = name; m_Post = "boss"; m_Duty = "Manage everything in the company"; } void Boss::showInfo() { cout << "Employee No.:" << this->m_IDnumber << "\t"; cout << "full name:" << this->m_Name << "\t"; cout << "Position:" << this->m_Post << "\t"; cout << "Job responsibilities:" << this->m_Duty << endl; }
Manager class header file (Manager.h):
#pragma once #include <string> #include <iostream> #include "Worker.h" using namespace std; // Create manager class class Manager : public Worker { public: Manager(int IDnumber, string name); void showInfo(); };
The specific implementation (Manager.cpp) is as follows:
#include "Manager.h" Manager::Manager(int IDnumber, string name) { m_IDnumber = IDnumber; m_Name = name; m_Post = "manager"; m_Duty = "Complete the tasks assigned by the boss and assign tasks to employees"; } void Manager::showInfo() { cout << "Employee No.:" << this->m_IDnumber << "\t"; cout << "full name:" << this->m_Name << "\t"; cout << "Position:" << this->m_Post << "\t"; cout << "Job responsibilities:" << this->m_Duty << endl; }
Ordinary employee class header file (Staff.h):
#pragma once #include <string> #include <iostream> #include "Worker.h" using namespace std; // Create ordinary employee class class Staff : public Worker { public: Staff(int IDnumber, string name); void showInfo(); };
The specific implementation (Staff.cpp) is as follows:
#include "Staff.h" Staff::Staff(int IDnumber, string name) { m_IDnumber = IDnumber; m_Name = name; m_Post = "Ordinary staff"; m_Duty = "Complete the tasks assigned by the manager"; } void Staff::showInfo() { cout << "Employee No.:" << this->m_IDnumber << "\t"; cout << "full name:" << this->m_Name << "\t"; cout << "Position:" << this->m_Post << "\t"; cout << "Job responsibilities:" << this->m_Duty << endl; }
In this fragment, the boss class, manager class and ordinary employee class inherit the abstract Employee class, and rewrite the pure virtual function virtual void showInfo() = 0 in the parent class to meet the conditions of dynamic polymorphism. Polymorphism can be used later.
Create employee management system class
After making preparations in the previous section, we can start to create an employee management system class. The header file (workerManager.h) is as follows:
#pragma once / / prevent header files from being included repeatedly #define FILENAME "WorkerManager.txt" #include <iostream> #include <fstream> #include "Worker.h" #include "Boss.h" #include "Manager.h" #include "Staff.h" using namespace std; class workerManager { public: // Constructor workerManager(); // Display menu void showMenu(); // Exit the system void existSystem(); // Add employee void AddWorkers(); // Save to file void save(); //Get the number of employees saved in the file int getWorkerNum(); // Initialize employee void initWorker(); // Show employees void showInfo(); // Judge whether the employee exists int isExist(); // Delete employee void deleteWorker(); // Modify employee void modifyWorker(); // Find employees void searchWorker(); // Number sorting void sortWorker(); // Empty document void delAllWorker(); // Destructor ~workerManager(); Worker ** m_wmArray; // Employee array pointer int m_size; //Record the number of employees bool m_FileIsEmpty; };
What needs attention is that Worker ** m_wmArray; // The employee array pointer is a secondary pointer. It points to the first address of an array where each element is a pointer, and the pointer in the array points to the address where an employee class exists.
Member functions are basically written according to the requirements description, and individual functions that need to be used in other functions are added, such as int getWorkerNum(); You can get the number of employees saved in the file, int isExist(); Can judge whether the employees exist, etc. The specific implementation of the employee management class member function (workerManager.cpp) is given below:
#include "workerManager.h" #include <string> // Constructor workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME, ios::in | ios::binary); // Initialize when file does not exist if (!ifs.is_open()) { this->m_size = 0; this->m_FileIsEmpty = true; this->m_wmArray = NULL; ifs.close(); return; } // Initialize when the file exists and the data is empty char ch; ifs >> ch; if (ifs.eof()) { this->m_size = 0; this->m_FileIsEmpty = true; this->m_wmArray = NULL; ifs.close(); return; } // The file exists and employee data is saved int num = this->getWorkerNum(); this->m_size = num; // Open up new space and store existing employee information this->m_wmArray = new Worker*[this->m_size]; this->initWorker(); this->m_FileIsEmpty = false; return; } // Display menu void workerManager::showMenu() { cout << "****************************" << endl; cout << "*** Welcome to the employee management system ***" << endl; cout << "***** 0,Exit management system ******" << 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; } // Exit the system void workerManager::existSystem() { cout << "Welcome to use next time!" << endl; system("pause"); exit(0); } // Add employee void workerManager::AddWorkers() { cout << "Please enter the number of employees to increase:"; int num_add = 0; cin >> num_add; if (num_add > 0) { // Calculate new space size int newSize = this->m_size + num_add; // Open up new space Worker** newspace = new Worker* [newSize]; // Store the contents of the original space under the new space if(this->m_size != 0) for (int i = 0; i < this->m_size; i++) { newspace[i] = this->m_wmArray[i]; } // Enter new data for (int i = 0; i < num_add; i++) { int IDnumber; string name; int post_choice; cout << "Please enter page " << i + 1 << " Employee number of new employees:"; cin >> IDnumber; cout << "Please enter the employee's name:"; cin >> name; cout << "Please enter the position of the employee (1. Boss; 2. Manager; 3. Ordinary employee.):"; cin >> post_choice; Worker * wk = NULL; switch (post_choice) { case 1: wk = new Boss(IDnumber, name); break; case 2: wk = new Manager(IDnumber, name); break; case 3: wk = new Staff(IDnumber, name); break; default: break; } // Save the created pointer to the pointer array newspace[this->m_size + i] = wk; } // Release the original space delete[] this->m_wmArray; // Update new space pointing this->m_wmArray = newspace; // Number of new updates this->m_size = newSize; // Save to file this->save(); // Update file not empty flag this->m_FileIsEmpty = false; // Prompt information cout << "Successfully added " << num_add << " A new employee!" << endl; } else { cout << "Wrong input!" << endl; } system("pause"); system("cls"); } // Save to file void workerManager::save() { ofstream ofs; ofs.open(FILENAME, ios::out | ios::binary); for (int i = 0; i < this->m_size; i++) { ofs << this->m_wmArray[i]->m_IDnumber << "\t" << this->m_wmArray[i]->m_Name << "\t" << this->m_wmArray[i]->m_Post << "\t" << this->m_wmArray[i]->m_Duty << endl; } ofs.close(); } // Get the number of employees saved in the file int workerManager::getWorkerNum() { ifstream ifs; ifs.open(FILENAME, ios::in); int id = 0; string name; string post; string duty; int num = 0; while (ifs >> id && ifs >> name && ifs >> post && ifs >> duty) { num++; } ifs.close(); return num; } // Initialize employee void workerManager::initWorker() { ifstream ifs; ifs.open(FILENAME, ios::in); int id = 0; string name; string post; string duty; int index = 0; while (ifs >> id && ifs >> name && ifs >> post && ifs >> duty) { Worker * worker = NULL; if (post == "Ordinary staff") { worker = new Staff(id, name); } else if (post == "manager") { worker = new Manager(id, name); } else { worker = new Boss(id, name); } this->m_wmArray[index] = worker; index++; } ifs.close(); } // Show employees void workerManager::showInfo() { if (this->m_size == 0) { cout << "File does not exist or record is empty!" << endl; } else { for (int i = 0; i < this->m_size; i++) { // Using polymorphic calling program interface this->m_wmArray[i]->showInfo(); } } system("pause"); system("cls"); } // Determine whether the employee exists and return the position in the array int workerManager::isExist() { cout << "Please enter the way to search employee information (1. Search by employee number; 2. Search by name.):"; int sermethod_choice; cin >> sermethod_choice; int index = -1; if (sermethod_choice == 1) { cout << "Please enter employee number:"; int ser_idnumber; cin >> ser_idnumber; for (int i = 0; i < this->m_size; i++) { if (this->m_wmArray[i]->m_IDnumber == ser_idnumber) { index = i; break; } } return index; } else { cout << "Please enter employee name:"; string ser_name; cin >> ser_name; for (int i = 0; i < this->m_size; i++) { if (this->m_wmArray[i]->m_Name == ser_name) { index = i; break; } } return index; } } // Delete employee void workerManager::deleteWorker() { int del_index; del_index = this->isExist(); if (del_index >= 0) { for (int i = del_index; i < this->m_size - 1; i++) { this->m_wmArray[i] = this->m_wmArray[i + 1]; } this->m_size = this->m_size - 1; this->save(); cout << "The information of the resigned employee has been deleted successfully!" << endl; } else { cout << "There is no such employee in the on-the-job employees!" << endl; } system("pause"); system("cls"); } // Modify employee void workerManager::modifyWorker() { int modify_index; modify_index = this->isExist(); if (modify_index >= 0) { cout << "The original information of the employee is:" << endl; this->m_wmArray[modify_index]->showInfo(); delete this->m_wmArray[modify_index]; int IDnumber; string name; int post_choice; cout << "Please enter the modified information of the employee:" << endl; cout << "Employee No.:"; cin >> IDnumber; cout << "full name:"; cin >> name; cout << "Position (1. Boss; 2. Manager; 3. Ordinary employee.):"; cin >> post_choice; Worker * wk = NULL; switch (post_choice) { case 1: wk = new Boss(IDnumber, name); break; case 2: wk = new Manager(IDnumber, name); break; case 3: wk = new Staff(IDnumber, name); break; default: cout << "Wrong input!" << endl; break; } m_wmArray[modify_index] = wk; this->save(); cout << "The employee information has been modified successfully!" << endl; system("pause"); system("cls"); } else { cout << "There is no such employee in the on-the-job employees!" << endl; system("pause"); system("cls"); } } // Find employees void workerManager::searchWorker() { int ser_index; ser_index = this->isExist(); if (ser_index >= 0) { cout << "The employee's information is:" << endl; this->m_wmArray[ser_index]->showInfo(); system("pause"); system("cls"); } else { cout << "There is no such employee in the on-the-job employees!" << endl; system("pause"); system("cls"); } } // Number sorting void workerManager::sortWorker() { int sortMethod_choice; cout << "Please enter the sorting method (1. Sort by employee number in ascending order; 2. Sort by employee number in descending order.):"; cin >> sortMethod_choice; if (sortMethod_choice == 1) { for (int i = 0; i < this->m_size; i++) { for (int j = 0; j < this->m_size - i - 1; j++) { if (this->m_wmArray[j]->m_IDnumber > this->m_wmArray[j + 1]->m_IDnumber) { Worker *temp = this->m_wmArray[j]; this->m_wmArray[j] = this->m_wmArray[j + 1]; this->m_wmArray[j + 1] = temp; } } } this->save(); cout << "Employees have been successfully sorted in ascending order by number." << endl; system("pause"); system("cls"); } else if (sortMethod_choice == 2) { for (int i = 0; i < this->m_size; i++) { for (int j = 0; j < this->m_size - i - 1; j++) { if (this->m_wmArray[j]->m_IDnumber < this->m_wmArray[j + 1]->m_IDnumber) { Worker *temp = this->m_wmArray[j]; this->m_wmArray[j] = this->m_wmArray[j + 1]; this->m_wmArray[j + 1] = temp; } } } this->save(); cout << "Employees have been successfully sorted in ascending order by number." << endl; system("pause"); system("cls"); } else { cout << "Wrong input!" << endl; system("pause"); system("cls"); } } // Empty document void workerManager::delAllWorker() { cout << "Are you sure you want to empty the document?(1,Confirmation; 2. Cancel.)"; int del_choice; cin >> del_choice; switch (del_choice) { case 1: { // Empty file ofstream ofs(FILENAME, ios::trunc); // Delete the file and recreate it ofs.close(); // Heap data if (this->m_wmArray != NULL) { for (int i = 0; i < this->m_size; i++) { if (this->m_wmArray[i] != NULL) { delete this->m_wmArray[i]; this->m_wmArray[i] = NULL; } } delete[] this->m_wmArray; this->m_wmArray = NULL; this->m_size = 0; this->m_FileIsEmpty = true; } cout << "The document has been emptied successfully!" << endl; break; } case 2: cout << "Cancelled!" << endl; break; default: cout << "Wrong input!" << endl; break; } system("pause"); system("cls"); } //Destructor workerManager::~workerManager() { if (this->m_wmArray != NULL) { for (int i = 0; i < this->m_size; i++) { if (this->m_wmArray[i] != NULL) { delete this->m_wmArray[i]; } } } delete[] this->m_wmArray; }
Use this employee management system class in the main function
At this point, the whole function of our employee management system has been completed. Next, we need to call this class in the main file to really run the employee management system. cpp is as follows:
#include <iostream> #include <string> #include <fstream> #include "workerManager.h" using namespace std; int main() { workerManager WM; int select; int index; while (true) { WM.showMenu(); cout << "Please enter your choice:"; cin >> select; switch (select) { case 0: // Exit the system WM.existSystem(); break; case 1: // Add employee WM.AddWorkers(); break; case 2: // Show employees WM.showInfo(); break; case 3: // Delete employee WM.deleteWorker(); break; case 4: // Modify employee WM.modifyWorker(); break; case 5: // Find employees WM.searchWorker(); break; case 6: // Number sorting WM.sortWorker(); break; case 7: // Empty document WM.delAllWorker(); break; default: system("cls"); break; } } system("pause"); return 0; }
3, Testing
After running the project, the following dialog box appears:
Enter the corresponding number and press enter to use the corresponding functions. For example, we add 2 employee information, enter the number 1, press enter, and then enter the relevant information according to the prompt:
After adding employee information, you can choose to display employee information:
Delete, modify and other functions will not be tested one by one. In addition, it is worth mentioning that the employee management system saves the employee information to a TXT file. Each time the employee management system is opened, the information in the txt file will be automatically written into the memory, so that the information will not disappear when the system is closed and opened again.
4, Summary
This is a classic case of OOP programming. The newcomers are not very skilled. I think before writing this employee management class, we must sort out the logical relationship, determine the inheritance relationship, and data management methods. In a word, we have benefited a lot~