头文件
worker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Worker
{
public:
virtual void showInfo() = 0;
virtual string getDeptName() = 0;
int m_Id;//职工编号
string m_Name;
int m_DeptId;//职工所在部门
private:
};
employee.h
#pragma once
#include<iostream>
#include "worker.h"
using namespace std;
class Employee:public Worker
{
public:
Employee(int id,string name,int dId);
~Employee();
virtual void showInfo();
virtual string getDeptName();
};
class Manager :public Worker
{
public:
Manager(int id, string name, int dId);
void showInfo();
string getDeptName();
};
class Boss :public Worker
{
public:
Boss(int, string, int);
void showInfo();
string getDeptName();
};
workerManager.h
#pragma once
#include<iostream>
#include<fstream>
using namespace std;
#include "worker.h"
#include "employee.h"
#define FILENAME "empfile.txt"
class WorkerManager
{
public:
WorkerManager();
~WorkerManager();
int get_EmpNum();
void init_Emp();
int m_EmpNum;
Worker** EmpArray;
bool m_FileEmpty;
void ShowMenu();
void save();
int IsExist(int);
void exitSystem();
void Add_Emp();
void Show_Emp();
void Del_Emp();
void Mod_Emp();
void Find_Emp();
void Sort_Emp();
void Clean_File();
};
源文件
employee.cpp
#include "employee.h"
Employee::Employee(int id,string name,int dId)
{
m_Id = id;
m_Name = name;
m_DeptId = dId;
}
Employee::~Employee()
{
}
void Employee::showInfo()
{
cout << "职工编号:" << this->m_Id;
cout << "\t职工姓名:" << this->m_Name;
cout << "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
string Employee::getDeptName()
{
return string("员工");
}
Manager::Manager(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dId;
}
void Manager::showInfo()
{
cout << "职工编号:" << this->m_Id;
cout << "\t职工姓名:" << this->m_Name;
cout << "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成老板交给的任务并分配任务给普通员工" << endl;
}
string Manager::getDeptName()
{
return string("经理");
}
Boss::Boss(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dId;
}
void Boss::showInfo()
{
cout << "职工编号:" << this->m_Id;
cout << "\t职工姓名:" << this->m_Name;
cout << "\t岗位:" << this->getDeptName()
<< "\t岗位职责:管理公司所有事务" << endl;
}
string Boss::getDeptName()
{
return string("总裁");
}
workerManager.cpp
#include"workerManager.h"
WorkerManager::WorkerManager()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "The file is not exist" << endl;
this->m_EmpNum = 0;
this->EmpArray = NULL;
this->m_FileEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "The file is empty" << endl;
this->m_EmpNum = 0;
this->EmpArray = NULL;
this->m_FileEmpty = true;
ifs.close();
return;
}
//文件存在且记录数据
int num = this->get_EmpNum();
this->m_EmpNum = num;
this->EmpArray = new Worker * [num];//开辟空间
this->init_Emp();
}
WorkerManager::~WorkerManager()
{
if (this->EmpArray)
{
for (int i = 0; i < m_EmpNum; i++)
{
if (this->EmpArray[i])
{
delete this->EmpArray[i];
this->EmpArray[i] = NULL;
}
}
delete[] this->EmpArray;
this->EmpArray = NULL;
}
}
void WorkerManager::ShowMenu()
{
cout << "*****************************" << endl;
cout << "*****欢迎使用职工管理系统*****" << endl;
cout << "******* 0 退出管理系统 *******" << endl;
cout << "******* 1 添加职工信息 *******" << endl;
cout << "******* 2 显示职工信息 *******" << endl;
cout << "******* 3 删除离职职工 *******" << endl;
cout << "******* 4 修改职工信息 *******" << endl;
cout << "******* 5 查找职工信息 *******" << endl;
cout << "******* 6 按照编号排序 *******" << endl;
cout << "******* 7 清空所有文档 *******" << endl;
cout << "*****************************" << endl;
}
void WorkerManager::exitSystem()
{
cout << "Welcome to use the system next time" << endl;
exit(0);
}
void WorkerManager::Add_Emp()
{
cout << "请输入需要添加的职工人数" << endl;
int addnum;
cin >> addnum;
if (addnum > 0)
{
int newSize = this->m_EmpNum + addnum;
Worker** newSpace = new Worker * [newSize];
if (this->EmpArray)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
newSpace[i] = this->EmpArray[i];
}
}
//添加新信息
for (int i = 0; i < addnum; i++)
{
int id, dId;
string name;
cout << "请输入第" << i + 1 << "个新职工的编号:" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个新职工的姓名:" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "个新职工的岗位:" << endl;
cout << "1、普通职工" << endl
<< "2、经理" << endl << "3、总裁" << endl;
cin >> dId;
Worker* wk = NULL;
switch (dId)
{
case 1:
wk = new Employee(id,name,1);
break;
case 2:
wk = new Manager(id, name, 2);
break;
case 3:
wk = new Boss(id, name, 3);
break;
default:
break;
}
newSpace[this->m_EmpNum + i] = wk;
}
delete[] this->EmpArray;
this->EmpArray = newSpace;
this->m_EmpNum = newSize;
cout << "成功添加" << addnum << "名职工" << endl;
this->save();
this->m_FileEmpty = false;
}
else
{
cout << "Error" << endl;
}
}
void WorkerManager::save()
{
ofstream ofs;
ofs.open(FILENAME,ios::out);
for (int i = 0; i < this->m_EmpNum; i++)
{
ofs << this->EmpArray[i]->m_Id << " "
<< this->EmpArray[i]->m_Name << " "
<< this->EmpArray[i]->m_DeptId << endl;
}
ofs.close();
}
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)
{
num++;
}
return num;
}
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* wk = NULL;
if (dId == 1)
wk = new Employee(id, name, dId);
else if (dId == 2)
wk = new Manager(id, name, dId);
else if (dId == 3)
wk = new Boss(id, name, dId);
this->EmpArray[index++] = wk;
}
ifs.close();
}
void WorkerManager::Show_Emp()
{
if (this->m_FileEmpty)
{
cout << "The file is not exist or empty" << endl;
return;
}
else
{
for (int i = 0; i < this->m_EmpNum; i++)
{//利用多态调用程序接口
this->EmpArray[i]->showInfo();
}
}
}
int WorkerManager::IsExist(int id)
{
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->EmpArray[i]->m_Id == id)
{
return i;
}
}
return index;
}
void WorkerManager::Del_Emp()
{
if (this->m_FileEmpty)
{
cout << "The file is not exist or empty" << endl;
return;
}
cout << "请输入您要删除的职工编号" << endl;
int id;
cin >> id;
int index = this->IsExist(id);
if (index != -1)
{
for (int i = index; i < this->m_EmpNum - 1; i++)
{
this->EmpArray[i] = this->EmpArray[i + 1];
}
this->m_EmpNum--;
this->save();
}
else
{
cout << "The worker is not found" << endl;
}
}
void WorkerManager::Mod_Emp()
{
if (this->m_FileEmpty)
{
cout << "The file is not exist or empty" << endl;
return;
}
cout << "请输入您要修改的职工编号" << endl;
int id;
cin >> id;
int index = this->IsExist(id);
if (index == -1)
{
cout << "The worker is not found" << endl;
}
else
{
delete this->EmpArray[index];
int newId = 0;
string name = "";
int dSelect = 0;
cout << "请输入新的职工编号:" << endl;
cin >> newId;
cout << "请输入新的职工姓名:" << endl;
cin >> name;
cout << "请输入新的职工岗位:" << endl<< "1、普通职工"
<< endl << "2、经理"<< endl << "3、总裁" << endl;
cin >> dSelect;
Worker* wk = NULL;
switch (dSelect)
{
case 1:
wk = new Employee(newId, name, dSelect);
break;
case 2:
wk = new Manager(newId, name, dSelect);
break;
case 3:
wk = new Boss(newId, name, dSelect);
break;
default:
break;
}
this->EmpArray[index] = wk;
this->save();
cout << "Modified successfully" << endl;
}
}
void WorkerManager::Find_Emp()
{
if (this->m_FileEmpty)
{
cout << "The file is not exist or empty" << endl;
return;
}
cout << "请输入查找方式:" << endl;
cout << "1 By employee ID" << endl
<< "2 By employee name" << endl;
int select, ret = -1;
cin >> select;
if (select == 1)
{
cout << "请输入查找职工的编号:" << endl;
int id;
cin >> id;
ret = this->IsExist(id);
if (ret != -1)
{
cout << "Search successful!informations as follows:" << endl;
this->EmpArray[ret]->showInfo();
}
else
{
cout << "The worker is not found" << endl;
}
}
else if (select == 2)
{
string name;
cout << "请输入查找的姓名:" << endl;
cin >> name;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->EmpArray[i]->m_Name == name)
{
ret = i;
cout << "查找成功,信息如下:" << endl;
this->EmpArray[i]->showInfo();
}
}
if (ret == -1)
{
cout << "The worker is not found" << endl;
}
}
else
{
cout << "Input Error!" << endl;
return;
}
}
void WorkerManager::Sort_Emp()
{
if (this->m_FileEmpty)
{
cout << "The file is not exist or empty" << endl;
return;
}
cout << "请选择排序方式:(By employee ID)" << endl;
cout << "1 Ascending order" << endl << "2 Descending order" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < m_EmpNum; i++)
{
int peak = i;
for (int j = i + 1; j < m_EmpNum; j++)
{
if (select == 1)
{
if (this->EmpArray[j]->m_Id < this->EmpArray[peak]->m_Id)
{
peak = j;
}
}
else
{//输入有误也按降序
if (this->EmpArray[j]->m_Id > this->EmpArray[peak]->m_Id)
{
peak = j;
}
}
}
if (peak != i)
{
Worker* temp = this->EmpArray[i];
this->EmpArray[i] = this->EmpArray[peak];
this->EmpArray[peak] = temp;
}
}
cout << "Sort Successful:" << endl;
this->save();
this->Show_Emp();
}
void WorkerManager::Clean_File()
{
cout << "Confirm clearing:" << endl;
cout << "1 OK" << endl << "2 Cancel" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
ofstream ofs;
ofs.open(FILENAME, ios::trunc);//删除文件后重新创建
ofs.close();
if (this->EmpArray != NULL)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
delete this->EmpArray[i];
this->EmpArray[i] = NULL;
}
delete[] this->EmpArray;
this->EmpArray = NULL;
this->m_EmpNum = 0;
this->m_FileEmpty = true;
}
cout << "Clear successful" << endl;
}
}
main.cpp
#include<iostream>
using namespace std;
#include "workerManager.h"
int main()
{
WorkerManager wm;
int choice = 0;
while (true)
{
wm.ShowMenu();
cout << "Please enter your choice" << endl;
cin >> choice;
switch (choice)
{
case 0:
wm.exitSystem();
break;
case 1:
wm.Add_Emp();
break;
case 2:
wm.Show_Emp();
break;
case 3:
wm.Del_Emp();
break;
case 4:
wm.Mod_Emp();
break;
case 5:
wm.Find_Emp();
break;
case 6:
wm.Sort_Emp();
break;
case 7:
wm.Clean_File();
break;
default:
system("cls");
break;
}
system("pause");
system("cls");
}
return 0;
}