C++ Polymorphic Employee Management System A blog entry into C Duck and Duck

Posted by burhankhan on Wed, 26 Jan 2022 22:06:33 +0100

Catalog

1. Management System Requirements

2. Create a worker class

2.1 Employee abstract base class

2.2 Create Boss Class

2.3 Create Manager Class

2.4 Create a common employee class

3. Create Management Classes

3.1 Header File Implementation

3.2 Source File Implementation

4. Functional Realization

4.1 Exit the hypervisor

4.2 Increase employee information

4.3 Display employee information

4.4 Delete ex-employees

4.5 Modify employee information

4.6 Find employee information

4.7 Sort by number

4.8 Empty all documents

5. Program Effect Rendering

1. Management System Requirements

The employee management system can be used to manage information for all employees within the company

There are three categories of employees in the company: ordinary employees, managers and bosses.

When displaying information, the employee number, employee name, employee position, and duties need to be displayed

General Employee Responsibilities: Complete the tasks assigned by the manager

Manager responsibilities: Complete tasks assigned by the boss and assign tasks to employees

Boss responsibilities: Manage all business

The following functions need to be implemented in the management system:

  • Exit the management program: Exit the current management system

  • Increase employee information: realize the function of adding workers in batches, and enter the information into files. The employee information is: employee number, name, department number

  • Show employee information: Show information about all employees within the company

  • Delete the former employee: Delete the designated employee according to the number

  • Modify employee information: Modify employee personal information according to number

  • Search for employee information: Search for related personnel information according to the number of the employee or the name of the employee

  • Sort by number: Sort by employee number, with rules specified by user

  • Empty All Documents: Empty all employee information recorded in the file (need to be reconfirmed before emptying to prevent deletion by mistake)

2. Create a worker class

2.1 Employee abstract base class

The categories of employees are: general staff, managers, bosses

Abstract three types of workers into a class (worker) and use polymorphism to manage different types of workers

The attributes of an employee are: the number of the employee, the name of the employee, and the number of the Department in which the employee is located.

Employees'behavior is: job responsibility information description, get job name

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

//Employee abstract base class
class Worker {
public:

	//Display personal information
	virtual void showInfo() = 0;

	//Get job name
	virtual string getDeptName() = 0;

	int m_Id; //Employee number
	string m_Name; //Employee Name
	int m_DeptId; //Name number of the Department where the worker is located
};

2.2 Create Boss Class

Boss Abstract subclass

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

class Boss:public Worker
{
public:
    Boss(int id, string name, int dId);

    virtual void showInfo();

    virtual string getDeptName();
};

Boss Specific Implementation Class

#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 number: " << this->m_Id
		<< " \t Employee name: " << this->m_Name
		<< " \t Job:" << this->getDeptName()
		<< " \t Job responsibilities: Manage all affairs of the company" << endl;
}

string Boss::getDeptName()
{
	return string("CEO");
}

2.3 Create Manager Class

Manager Abstract subclass

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

//Manager class
class Manager :public Worker
{
public:
	Manager(int id, string name,int dId);
	virtual void showInfo();
	virtual string getDeptName();
};

Manager Specific Implementation Class

#include "manager.h"

Manager::Manager(int id, string name, int dId)
{
	this->m_Name = name;
	this->m_Id = id;
	this->m_DeptId = dId;
}

void Manager::showInfo() 
{
	cout << "Employee number: " << this->m_Id
		<< " \t Employee name: " << this->m_Name
		<< " \t Job:" << this->getDeptName()
		<< " \t Job responsibilities: Complete the tasks assigned by the boss,And assign tasks to employees" << endl;
}

string Manager::getDeptName()
{
	return string("manager");
}

2.4 Create a common employee class

General Employee Abstract Subclass

#pragma once
#include<iostream>
using namespace std;
#include "worker.h"
class Employee :public Worker {
public:

	//Constructor
	Employee(int id, string name, int dId);

	//Display personal information
	virtual void showInfo();

	//Get job name
	virtual string getDeptName();

};

General Employee Specific Implementation Class

#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 number: " << this->m_Id
		<< " \t Employee name: " << this->m_Name
		<< " \t Job:" << this->getDeptName()
		<< " \t Job responsibilities: Complete the tasks assigned by the manager" << endl;
}

string Employee::getDeptName() 
{
	return string("staff");
}

3. Create Management Classes

The management category is responsible for the following:

  • Communication menu interface with users

  • Operations to check the addition or deletion of workers

  • Read-write interaction with files

3.1 Header File Implementation

#pragma once //Prevent header file duplication
#Include <iostream> 	// Contains input and output stream header files
using namespace std; //Use standard namespace
#include "worker.h"
#include "boss.h"
#include "employee.h"
#include "manager.h"

#include <fstream>
#define FILENAME "empFile.txt"

class WorkerManager {
public:
	//Constructor
	WorkerManager();

	//Show Menu
	void Show_Menu();

	//Exit System
	void ExitSystem();

	//Record the number of employees
	int m_EmpNum;

	//Employee Array Pointer
	Worker** m_EmpArray;

	//Add workers
	void Add_Emp();

	//Save File
	void save();

	//Determine if the file is empty flag
	bool m_FileIsEmpty;

	//Number of people in the statistics file
	int get_EmpNum();

	//Initialize employee
	void init_Emp();

	//Show employees
	void Show_Emp();

	//Determine if the employee exists by employee number, and if there is a return to the employee's position in the array, there is no Return-1
	int IsExist(int id);

	//Delete workers
	void Del_Emp();

	//Modify staff and workers
	void Mod_Emp();

	//Find Employees
	void Find_Emp();

	//Sort employees
	void Sort_Emp();

	//Empty File
	void Clean_File();

	//Destructor
	~WorkerManager();

};

3.2 Source File Implementation

 

#include "workerManager.h"

//Constructor
WorkerManager::WorkerManager()
{

	//1. File does not exist
	ifstream ifs;
	ifs.open(FILENAME, ios::in);//read file

	if (!ifs.is_open())
	{
		cout << "file does not exist" << endl;
		//Initialization Properties
		//Number of Initialized Recorders
		this->m_EmpNum = 0;
		//Initialization array pointer is null
		this->m_EmpArray = NULL;
		//Is initialization file empty
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//2. File exists but is empty
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//Representation file is empty
		cout << "File is empty" << endl;
		//Number of Initialized Recorders
		this->m_EmpNum = 0;
		//Initialization array pointer is null
		this->m_EmpArray = NULL;
		//Is initialization file empty
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//3. File exists and records data
	int num = this->get_EmpNum();
	this->m_EmpNum = num;

	//Open up space
	this->m_EmpArray = new Worker * [this->m_EmpNum];
	//Store data from a file in an array
	this->init_Emp();

}

//Number of people in the statistics file
int WorkerManager::get_EmpNum()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);//Open File Read File
	int id;
	string name;
	int dId;

	int num = 0;

	while (ifs >> id && ifs >> name && ifs >> dId)
	{
		//Number of Recorded Persons
		num++;
	}
	ifs.close();

	return num;

}

//Show Menu
void WorkerManager::Show_Menu()
{
	cout << "********************************************" << endl;
	cout << "*********  Welcome to the Employee Management System! **********" << endl;
	cout << "*************  0.Exit Manager  *************" << endl;
	cout << "*************  1.Increase employee information  *************" << endl;
	cout << "*************  2.Show employee information  *************" << endl;
	cout << "*************  3.Delete retired staff  *************" << 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;
}

//Exit System
void WorkerManager::ExitSystem()
{
	cout << "Welcome to Next Use" << endl;
	system("pause");
	exit(0);	//Exit the program
}


//Add workers
void WorkerManager::Add_Emp()
{
	cout << "Please enter the number of additional employees:" << endl;
	int addNum = 0;//Save the number of user inputs
	cin >> addNum;
	if (addNum > 0)
	{
		//Calculate New Space Size
		int newsize = this->m_EmpNum + addNum;
		
		//Open up new space
		Worker** newSpace = new Worker * [newsize];

		//Put the contents of the original space under the new space
		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i < 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 #" << i + 1 << "Number of new employees:" << endl;
			cin >> id;

			cout << "Please enter # " << i + 1 << " Names of new employees:" << endl;
			cin >> name;

			cout << "Please select the position of the employee:" << endl;
			cout << "1,General staff" << endl;
			cout << "2,manager" << endl;
			cout << "3,Boss" << endl;
			cin >> dSelect;

			Worker* worker = NULL;
			switch(dSelect)
			{
			case 1:
				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 direction of the new space
		this->m_EmpArray = newSpace;

		//Update New Number
		this->m_EmpNum = newsize;

		//Update Employee Not Empty Sign
		this->m_FileIsEmpty = false;

		//Prompt message
		cout << "Added Successfully" << addNum << "New Employee" << endl;

		this->save();
	}
	else
	{
		cout << "Input Error" << endl;
	}

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

//Save File
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();
}

//Initialize employee
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;
		if (dId == 1)//General staff
		{
			worker = new Employee(id, name, dId);
		}
		else if(dId == 2)//manager
		{
			worker = new Manager(id, name, dId);
		}
		else//Boss
		{
			worker = new Boss(id, name, dId);
		}
		this->m_EmpArray[index] = worker;
		index++;
	}
	//Close File
	ifs.close();
}

//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++) 
		{
			//Utilizing the polymorphic caller interface
			this->m_EmpArray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}
//Determine if the employee exists by employee number, and if there is a return to the employee's position in the array, there is no Return-1
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;
}

//Delete workers
void WorkerManager::Del_Emp() 
{
	if (this->m_FileIsEmpty)
	{
		cout << "File does not exist or record is empty" << endl;
	}
	else
	{
		//Delete by Employee Number
		cout << "Enter the number of the employee you want to delete:" << endl;
		int id = 0;
		cin >> id;

		int index = this->IsExist(id);

		if (index != -1)  //Indicates location data on 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(); //Synchronize data to file after deletion
			cout << "Delete successful!" << endl;
		}
		else
		{
			cout << "Delete failed, the employee was not found" << endl;
		}
	}
	system("pause");
	system("cls");
}

//Modify staff and workers
void WorkerManager::Mod_Emp() 
{
	if (this->m_FileIsEmpty)
	{
		cout << "File does not exist or is empty" << endl;
	}
	else
	{
		cout << "Enter the employee number to be modified" << endl;
		int id;
		cin >> id;

		int ret = this->IsExist(id);
		if (ret != -1)
		{
			//Find Numbered Workers
			delete this->m_EmpArray[ret];
			int newId = 0;
			string newName = "";
			int dSelect = 0;
			cout << "Find:" << id << "No. Employee, please enter the new employee number:" << endl;
			cin >> newId;
			cout << "Please enter a new name: " << endl;
			cin >> newName;
			cout << "Please enter the position: " << endl;
			cout << "1,General staff" << endl;
			cout << "2,manager" << endl;
			cout << "3,Boss" << endl;
			cin >> dSelect;
			Worker* worker = NULL;
			switch (dSelect)
			{
			case 1:
				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 << "Successfully modified!" << endl;

			//Save to file
			this->save();
		}
		else
		{
			cout << "Modification failed, no person found" << endl;
		}
	}
	//Press any key to clear the screen
	system("pause");
	system("cls");
}

//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,Find by employee number" << endl;
		cout << "2,Find by name" << endl;
		int select = 0;
		cin >> select;
		if (select == 1)
		{
			int id;
			cout << "Please enter the employee number you are looking for:" << endl;
			cin >> id;
			int ret = IsExist(id);
			if (ret != -1)
			{
				cout << "Find Successful! The employee's information is as follows:" << endl;
				this->m_EmpArray[ret]->showInfo();
			}
			else
			{
				cout << "Find failed, no person 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;//Flags Found
			for (int i = 0; i < m_EmpNum; i++)
			{
				if (m_EmpArray[i]->m_Name == name)
				{
					flag = true;
					cout << "Find Successful,The worker number is:"
						<< m_EmpArray[i]->m_Id
						<< " The information of number is as follows:" << endl;
					this->m_EmpArray[i]->showInfo();
				}
			}
			if (flag == false)
			{
				//No such person found
				cout << "Find failed, no person found" << endl;
			}
		}
		else
		{
		cout << "Error in input options" << endl;
		}
	}
	system("pause");
	system("cls");
}

//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 choose the 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 << "Sort Successful,The sorted results are:" << endl;
		this->save();
		this->Show_Emp();
	}
}

//Empty File
void WorkerManager::Clean_File()
{
	cout << "Confirm empty?" << endl;
	cout << "1,confirm" << endl;
	cout << "2,Return" << endl;

	int select = 0;
	cin >> select;
	if (select == 1)
	{
		//Open mode ios::trunc Delete files and recreate them if they exist
		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 != NULL)
				{
					delete this->m_EmpArray[i];
				}
			}
			this->m_EmpNum = 0;
			delete[]this->m_EmpArray;
			this->m_EmpArray = NULL;
			this->m_FileIsEmpty = true;
		}
		cout << "Empty Success!" << endl;
	}

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

//Destructor
WorkerManager::~WorkerManager()
{
	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;
	}
}

4. Functional Realization

4.1 Exit the hypervisor

//Exit System
void WorkerManager::ExitSystem()
{
	cout << "Welcome to Next Use" << endl;
	system("pause");
	exit(0);	//Exit the program
}

4.2 Increase employee information

//Add workers
void WorkerManager::Add_Emp()
{
	cout << "Please enter the number of additional employees:" << endl;
	int addNum = 0;//Save the number of user inputs
	cin >> addNum;
	if (addNum > 0)
	{
		//Calculate New Space Size
		int newsize = this->m_EmpNum + addNum;
		
		//Open up new space
		Worker** newSpace = new Worker * [newsize];

		//Put the contents of the original space under the new space
		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i < 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 #" << i + 1 << "Number of new employees:" << endl;
			cin >> id;

			cout << "Please enter # " << i + 1 << " Names of new employees:" << endl;
			cin >> name;

			cout << "Please select the position of the employee:" << endl;
			cout << "1,General staff" << endl;
			cout << "2,manager" << endl;
			cout << "3,Boss" << endl;
			cin >> dSelect;

			Worker* worker = NULL;
			switch(dSelect)
			{
			case 1:
				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 direction of the new space
		this->m_EmpArray = newSpace;

		//Update New Number
		this->m_EmpNum = newsize;

		//Update Employee Not Empty Sign
		this->m_FileIsEmpty = false;

		//Prompt message
		cout << "Added Successfully" << addNum << "New Employee" << endl;

		this->save();
	}
	else
	{
		cout << "Input Error" << endl;
	}

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

4.3 Display employee information

//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++) 
		{
			//Utilizing the polymorphic caller interface
			this->m_EmpArray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}

4.4 Delete ex-employees

//Delete workers
void WorkerManager::Del_Emp() 
{
	if (this->m_FileIsEmpty)
	{
		cout << "File does not exist or record is empty" << endl;
	}
	else
	{
		//Delete by Employee Number
		cout << "Enter the number of the employee you want to delete:" << endl;
		int id = 0;
		cin >> id;

		int index = this->IsExist(id);

		if (index != -1)  //Indicates location data on 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(); //Synchronize data to file after deletion
			cout << "Delete successful!" << endl;
		}
		else
		{
			cout << "Delete failed, the employee was not found" << endl;
		}
	}
	system("pause");
	system("cls");
}

4.5 Modify employee information

//Modify staff and workers
void WorkerManager::Mod_Emp() 
{
	if (this->m_FileIsEmpty)
	{
		cout << "File does not exist or is empty" << endl;
	}
	else
	{
		cout << "Enter the employee number to be modified" << endl;
		int id;
		cin >> id;

		int ret = this->IsExist(id);
		if (ret != -1)
		{
			//Find Numbered Workers
			delete this->m_EmpArray[ret];
			int newId = 0;
			string newName = "";
			int dSelect = 0;
			cout << "Find:" << id << "No. Employee, please enter the new employee number:" << endl;
			cin >> newId;
			cout << "Please enter a new name: " << endl;
			cin >> newName;
			cout << "Please enter the position: " << endl;
			cout << "1,General staff" << endl;
			cout << "2,manager" << endl;
			cout << "3,Boss" << endl;
			cin >> dSelect;
			Worker* worker = NULL;
			switch (dSelect)
			{
			case 1:
				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 << "Successfully modified!" << endl;

			//Save to file
			this->save();
		}
		else
		{
			cout << "Modification failed, no person found" << endl;
		}
	}
	//Press any key to clear the screen
	system("pause");
	system("cls");
}

4.6 Find employee information

//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,Find by employee number" << endl;
		cout << "2,Find by name" << endl;
		int select = 0;
		cin >> select;
		if (select == 1)
		{
			int id;
			cout << "Please enter the employee number you are looking for:" << endl;
			cin >> id;
			int ret = IsExist(id);
			if (ret != -1)
			{
				cout << "Find Successful! The employee's information is as follows:" << endl;
				this->m_EmpArray[ret]->showInfo();
			}
			else
			{
				cout << "Find failed, no person 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;//Flags Found
			for (int i = 0; i < m_EmpNum; i++)
			{
				if (m_EmpArray[i]->m_Name == name)
				{
					flag = true;
					cout << "Find Successful,The worker number is:"
						<< m_EmpArray[i]->m_Id
						<< " The information of number is as follows:" << endl;
					this->m_EmpArray[i]->showInfo();
				}
			}
			if (flag == false)
			{
				//No such person found
				cout << "Find failed, no person found" << endl;
			}
		}
		else
		{
		cout << "Error in input options" << endl;
		}
	}
	system("pause");
	system("cls");
}

4.7 Sort by number

//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 choose the 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 << "Sort Successful,The sorted results are:" << endl;
		this->save();
		this->Show_Emp();
	}
}

4.8 Empty all documents

//Empty File
void WorkerManager::Clean_File()
{
	cout << "Confirm empty?" << endl;
	cout << "1,confirm" << endl;
	cout << "2,Return" << endl;

	int select = 0;
	cin >> select;
	if (select == 1)
	{
		//Open mode ios::trunc Delete files and recreate them if they exist
		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 != NULL)
				{
					delete this->m_EmpArray[i];
				}
			}
			this->m_EmpNum = 0;
			delete[]this->m_EmpArray;
			this->m_EmpArray = NULL;
			this->m_FileIsEmpty = true;
		}
		cout << "Empty Success!" << endl;
	}

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

5. Program Effect Rendering

Add employee information

 

Show employee information

 

File Information Display

 

Topics: C C++ Back-end