C ++ based polymorphism Staff Management System

The main module (Staff Management System .cpp)

#include

using namespace std;

#include "workerManger.h"

#include "worker.h"

#include "employee.h"

#include "Boss.h"

#include "Manager.h"

int main ()

{

// instantiate a manager object Newell Agent Application

WorkerManger wm;

int choice = 0;

while (true)

{

// call member function to display the menu

wm.Show_Menu();

cout << "Please enter your choice:" << endl;

cin >> choice;

switch (choice)

{

case 0: // Exit

wm.ExitSystem();

break;

case 1: // increase

wm.Add_Emp();

break;

case 2: // display

wm.Show_Emp();

break;

case 3: // delete

wm.Del_Emp();

break;

case 4: // modified

wm.Mod_Emp();

break;

case 5: // Find

wm.Find_Emp();

break;

case 6: // Sort

wm.Sort_Emp();

break;

case 7: // Clear

wm.Clean_File();

break;

default:

system("cls");//清屏

break;

}

}

system("pause");

return 0;

}

workerManger.h file:

#pragma once // prevent duplicate file header contains

#include

#include

#include "worker.h"

#include "employee.h"

#include "Boss.h"

#include "Manager.h"

#define FILENAME "empFile.txt"

using namespace std;

class WorkerManger

{

public:

//Constructor

WorkerManger();

// show menu

void Show_Menu();

//Exit system

void ExitSystem();

// Record the number of employees

int m_EmpNum;

// array pointer staff

Worker ** m_EmpArray;

// add workers

void Add_Emp();

//save document

void save();

// determine whether the file is empty symbol

bool m_FileIsEmpty;

// count the number of files

int get_EmpNum();

// Initialize workers

void init_Emp();

// show workers

void Show_Emp();

// remove workers

void Del_Emp();

// determine whether the presence of workers

int IsExist(int id);

// Change in Staff

void Mod_Emp();

// Find staff

void Find_Emp();

// Sort by number

void Sort_Emp();

// empty file

void Clean_File();

// destructor

~WorkerManger();

};

workerManger.cpp file:

#include "workerManger.h"

WorkerManger::WorkerManger()

{

// 1, the file does not exist

ifstream ifs;

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

if (!ifs.is_open())

{

this-> m_EmpNum = 0; // initialization record number

this-> m_EmpArray = NULL; // initialize array pointer

this-> m_FileIsEmpty = true; // initialize whether the file is empty

ifs.close();

return;

}

// file exists, the data is empty

char ch;

ifs >> ch;

if (ifs.eof())

{

this-> m_EmpNum = 0; // initialization record number

this-> m_EmpArray = NULL; // initialize array pointer

this-> m_FileIsEmpty = true; // initialize whether the file is empty

ifs.close();

return;

}

// if file exists and data is recorded

int num = this->get_EmpNum();

this->m_EmpNum = num;

this->m_EmpArray = new Worker*[this->m_EmpNum];

this->init_Emp();

}

// show menu

void WorkerManger::Show_Menu()

{

cout << "*****" << endl;

cout << " Welcome Staff Management System! " << endl;

<< cout " ** 0: Exit Management System * ** " << endl;

<< cout " ** 1: increase employee information * ** " << endl;

<< cout " ** 2: employee information display * ** " << endl;

<< cout " ** 3: Delete leaving workers * ** " << endl;

<< cout " ** 4: Change in Staff Information * ** " << endl;

<< cout " ** 5: Finding information workers * ** " << endl;

<< cout " ** 6: Sort by number * ** " << endl;

<< cout " ** 7: Clear all documents * ** " << endl;

cout << "*****" << endl;

cout << endl;

}

// add workers

void WorkerManger::Add_Emp()

{

cout << "Please enter the number of workers added:" << endl;

int addNum = 0; // save the number input by the user

cin >> addNum;

if(addNum > 0)

{

// Calculate the size of the new space is added

int newSize = this-> m_EmpNum + addNum; new space record number = number + the number of new original //

// open up new space

Worker * newSpace = new Worker[newSize];

// The space under the original data, copied to the new space

if (this->m_EmpArray != NULL)

{

for (int i = 0; i < this->m_EmpNum; i++)

{

newSpace[i] = this->m_EmpArray[i];

}

}

// Add new data

for (int i = 0; i < addNum; i++)

{

int id; // number of workers

string name; // employee's name

int dSelect; // Select Sector

cout << "Please enter" << i + 1 << "number of new employees:" << endl;

cin >> id;

cout << "Please enter" << i + 1 << "new employee's name:" << endl;

cin >> name;

cout << "Please select the trade union posts" << endl;

cout << "1, ordinary workers" << endl;

cout << "2、经理" << endl;

cout << "3、老板" << endl;

cin >> dSelect;

Worker * worker = NULL;

switch (dSelect)

{

case 1:

worker = new Employee(id, name, 1);

break;

case 2:

worker = new Manager(id, name, 2);

break;

case 3:

worker = new Boss(id, name, 3);

break;

default:

break;

}

// staff responsibilities will be created, saved to an array

newSpace[this->m_EmpNum + i] = worker;

}

// release the original space

delete[] this->m_EmpArray;

// change to the new space

this->m_EmpArray = newSpace;

// update the number of new workers

this->m_EmpNum = newSize;

this-> m_FileIsEmpty = false; // staff is not empty

// prompted to add success

cout << "successfully added" << addNum << "name of the new workers!" << endl;

// save data to a file

this->save();

}

else

{

cout << "you make a mistake!" << endl;

}

// clear screen press any key to return to the parent directory

system("pause");

system("cls");

}

// count the number of files

int WorkerManger::get_EmpNum()

{

ifstream ifs;

ifs.open (FILENAME, ios :: in); // Open the file read file

int id;

string name;

int dId;

int num = 0;

while (ifs >> id && ifs >> name && ifs >> dId)

{

// number of statistics

a ++;

}

return num;

}

//Exit system

void WorkerManger::ExitSystem()

{

cout << "Welcome to the next use" << endl;

system("pause");

exit (0); // exit the program

}

//save document

void WorkerManger::save()

{

ofstream ofs;

ofs.open (FILENAME, ios :: out); // Open the file with the way the output - write file

// everyone's data is written to a file

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;

}

// close the file

ofs.close();

}

// Initialize workers

void WorkerManger::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) // ordinary workers

{

worker = new Employee(id, name, dId);

}

else if (dId == 2) // Manager

{

worker = new Manager(id, name, dId);

}

else

{

worker = new Boss(id, name, dId);

}

this->m_EmpArray[index] = worker;

index++;

}

ifs.close();

}

// show workers

void WorkerManger::Show_Emp()

{

// whether the file is empty

if (this->m_FileIsEmpty)

{

cout << "file does not exist or is empty record!" << endl;

}

else

{

for (int i = 0; i < m_EmpNum; i++)

{

// call using multi-state program interface

this->m_EmpArray[i]->showInfo();

}

}

system("pause");

system("cls");

}

// remove workers

void WorkerManger::Del_Emp()

{

if (this->m_FileIsEmpty)

{

cout << "file does not exist or is empty record!" << endl;

}

else

{

// delete according to number of workers

cout << "Please enter the number you want to delete employees:" << endl;

int id = 0;

cin >> id;

int index = this->IsExist(id);

if (index! = -1) // workers exist, and you remove workers at index

{

// forward data

for (int i = index; i < this->m_EmpNum - 1; i++)

{

this->m_EmpArray[i] = this->m_EmpArray[i + 1];

}

this-> m_EmpNum -; // Update the number of persons recorded in the array

// update to synchronize files

this->save();

cout << "deleted successfully!" << endl;

}

else

{

cout << "Failed to delete the employee not found!!" << endl;

}

}

system("pause");

system("cls");

}

// determine whether the presence of workers

int WorkerManger::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;

}

// Change in Staff

void WorkerManger::Mod_Emp()

{

if (this->m_FileIsEmpty)

{

cout << "record file does not exist or is empty!" << endl;

}

else

{

cout << "Please enter the number of workers to be modified:" << endl;

int id;

cin >> id;

int ret = this->IsExist(id);

if (ret! = -1) // find workers

{

delete this->m_EmpArray[ret];

int ChangE = 0;

string newName = "";

int dSelect = 0;

cout << "found:" << id << "number of workers, new workers enter the number:" << endl;

cin >> ChangE;

cout << "Please enter a new name:" << endl;

cin >> newName;

cout << "Please enter the post:" << endl;

cout << "1, ordinary workers" << endl;

cout << "2、经理" << endl;

cout << "3、老板" << endl;

cin >> dSelect;

Worker * worker = NULL;

switch (dSelect)

{

case 1:

worker = new Employee(newId, newName, dSelect);

break;

case 2:

worker = new Manager(newId, newName, dSelect);

break;

case 3:

worker = new Boss(newId, newName, dSelect);

break;

default:

break;

}

//update data

this->m_EmpArray[ret] = worker;

cout << "modified successfully!" << endl;

this-> save (); // Save to File

}

else

{

cout << "modification failed no such person!!" << endl;

}

}

system("pause");

system("cls");

}

// Find staff

void WorkerManger::Find_Emp()

{

if (this->m_FileIsEmpty)

{

cout << "file does not exist or is recorded as empty love!" << endl;

}

else

{

cout << "Please enter search methods:" << endl;

cout << "1, press the number of workers to find" << endl;

cout << "2, according to employee name search" << endl;

int select;

cin >> select;

if (select == 1)

{

// by number

int id;

cout << "Please enter the number of workers looking for:" << endl;

cin >> id;

int ret = this->IsExist(id);

if (right! = -1)

{

// find workers

cout << "to find information on the success of the trade union as follows:!" << endl;

this->m_EmpArray[ret]->showInfo();

}

else

{

cout << "Failed to find no such person!!" << endl;

}

}

else if(select == 2)

{

// by name

string name;

cout << "Please enter the name to find:";

cin >> name;

bool flag = false; // determine whether or not found, the default is not found

for (int i = 0; i < m_EmpNum; i++)

{

if (this->m_EmpArray[i]->m_Name == name)

{

cout << "to find success, employees numbered"

<< this->m_EmpArray[i]->m_ID

<< "employee information is as follows:" << endl;

this->m_EmpArray[i]->showInfo();

flag = true;

}

}

if (flag == false)

{

cout << "Failed to find no such person!!" << endl;

}

}

else

{

cout << "you enter a wrong option!" << endl;

}

}

system("pause");

system("cls");

}

// Sort

void WorkerManger::Sort_Emp()

{

if (this->m_FileIsEmpty)

{

cout << "record file does not exist or is empty!" << endl;

system("pause");

system("cls");

}

else

{

cout << "Please select Sort by:" << endl;

cout << "1, according to employee number order" << endl;

cout << "2, in descending order according to the number of workers" << endl;

int select;

cin >> select;

for (int i = 0; i < this->m_EmpNum; i++)

{

int MinOrMax = i;

for (int j = i + 1; j < this->m_EmpNum; j++)

{

if (select == 1) // ascending

{

if (this->m_EmpArray[MinOrMax]->m_ID > this->m_EmpArray[j]->m_ID)

{

MinOrMax = j;

}

}

else

{

if (this->m_EmpArray[MinOrMax]->m_ID < this->m_EmpArray[j]->m_ID)

{

MinOrMax = j;

}

}

}

Determining a minimum or maximum value // is the minimum beginning identified or not the calculated maximum value exchange if not

if (i != MinOrMax)

{

Worker * temp = this->m_EmpArray[i];

this->m_EmpArray[i] = this->m_EmpArray[MinOrMax];

this->m_EmpArray[MinOrMax] = temp;

}

}

cout << "Sort results after a successful order of:!" << endl;

this-> save (); // Sort the results saved to a file

this-> Show_Emp (); // show all employees

}

}

// empty file

void WorkerManger::Clean_File()

{

cout << "is determined to clear it?" << endl;

cout << "1、确定" << endl;

cout << "2、返回" << endl;

int select = 0;

cin >> select;

if (select == 1)

{

ofstream ofs (FILENAME, ios :: trunc); // re-created after deleting files

ofs.close();

if (this->m_EmpArray != NULL)

{

for (int i = 0; i < this->m_EmpNum; i++)

{

delete this->m_EmpArray[i];

this->m_EmpArray[i] = NULL;

}

// delete the array pointer stack area

delete[] this->m_EmpArray;

this->m_EmpArray = NULL;

this->m_EmpNum = 0;

this->m_FileIsEmpty = true;

}

cout << "Empty success!" << endl;

}

system("pause");

system("cls");

}

WorkerManger::~WorkerManger()

{

if (this->m_EmpArray != NULL)

{

delete[] this->m_EmpArray;

this->m_EmpArray = NULL;

}

}

worker.h file:

#pragma once

#include

using namespace std;

#include

// abstract class workers

class Worker

{

public:

// display personal information

virtual void showInfo() = 0;

// Get the Job Title

virtual string getDeptName() = 0;

int m_ID; // number of workers

string m_Name; // employee's name

int m_DeptID; // department number

};

employee.h file:

// general staff file

#pragma once

#include

#include

using namespace std;

#include "worker.h"

class Employee : public Worker

{

public:

//Constructor

Employee(int id, string name, int dID);

// display personal information

void showInfo();

// Get the Job Title

string getDeptName();

};

employee.cpp file:

#include "employee.h"

//Constructor

Employee::Employee(int id, string name, int dID)

{

this->m_ID = id;

this->m_Name = name;

this->m_DeptID = dID;

}

// display personal information

void Employee::showInfo()

{

cout << "职工编号: " << this->m_ID

<< "\t职工姓名: " << this->m_Name

<< "\t岗位: " << this->getDeptName()

<< "\ t Responsibilities: complete the tasks assigned by the manager" << endl;

}

// Get the Job Title

string Employee::getDeptName()

{

return string ( "employee");

}

Manager.h file:

#pragma once

#include

#include "worker.h"

using namespace std;

// class manager

class Manager : public Worker

{

public:

//Constructor

Manager(int id, string name, int dId);

// display personal information

virtual void showInfo();

// Get the Job Title

virtual string getDeptName();

};

Manager.cpp file:

#include "Manager.h"

//Constructor

Manager::Manager(int id, string name, int dId)

{

this->m_ID = id;

this->m_Name = name;

this->m_DeptID = dId;

}

// display personal information

void Manager::showInfo()

{

cout << "职工编号: " << this->m_ID

<< "\t职工姓名: " << this->m_Name

<< "\t岗位: " << this->getDeptName()

<< "\ t Responsibilities: complete the tasks assigned by the owner and issued a mandate to the general staff" << endl;

}

// Get the Job Title

string Manager::getDeptName()

{

return string ( "Manager");

}

Boss.h file:

#pragma once

#include

#include "worker.h"

using namespace std;

// class manager

class Boss : public Worker

{

public:

//Constructor

Boss(int id, string name, int dId);

// display personal information

virtual void showInfo();

// Get the Job Title

virtual string getDeptName();

};

Boss.cpp file:

#include "Boss.h"

//Constructor

Boss::Boss(int id, string name, int dId)

{

this->m_ID = id;

this->m_Name = name;

this->m_DeptID = dId;

}

// display personal information

void Boss::showInfo()

{

cout << "职工编号: " << this->m_ID

<< "\t职工姓名: " << this->m_Name

<< "\t岗位: " << this->getDeptName()

<< "\ t Responsibilities: Management of all things" << endl;

}

// Get the Job Title

string Boss::getDeptName()

{

return string ( "Boss");

}

Guess you like

Origin blog.51cto.com/14511863/2476984