一.Employee.cpp
//Employee 的类的实现
#include "Employee.h"
//构造函数
Employee::Employee(int id, string name, int dept_id)
{
this->W_Id = id;
this->W_Name = name;
this->W_DeptId = dept_id;
}
//显示个人信息
void Employee::Show_Info()
{
cout << "岗位编号:" << this->W_Id
<< "\t职工姓名:" << this->W_Name
<< "\t岗位:" << this->Get_DeptName()
<< "\t岗位职责: 完成经理交给的任务" << endl;
}
//获取岗位名称
string Employee::Get_DeptName()
{
return "员工";
}
二.Manager.cpp
//Manager 的类的实现
#include "Manager.h"
//构造函数
Manager::Manager(int id, string name, int dept_id)
{
this->W_Id = id;
this->W_Name = name;
this->W_DeptId = dept_id;
}
//显示个人信息
void Manager::Show_Info()
{
cout << "岗位编号:" << this->W_Id
<< "\t职工姓名:" << this->W_Name
<< "\t岗位:" << this->Get_DeptName()
<< "\t岗位职责: 完成老板交给的任务,并下发任务给员工" << endl;
}
//获取岗位名称
string Manager::Get_DeptName()
{
return "经理";
}
三.Boss.cpp
//Boss 的类的实现
#include "Boss.h"
//构造函数
Boss::Boss(int id, string name, int dept_id)
{
this->W_Id = id;
this->W_Name = name;
this->W_DeptId = dept_id;
}
//显示个人信息
void Boss::Show_Info()
{
cout << "岗位编号:" << this->W_Id
<< "\t职工姓名:" << this->W_Name
<< "\t岗位:" << this->Get_DeptName()
<< "\t岗位职责: 管理公司所有事务" << endl;
}
//获取岗位名称
string Boss::Get_DeptName()
{
return "总裁";
}
三.WorkerManager.cpp
//WorkerManager类的实现
/********************************************头文件包含域*************************************/
#include "WorkerMangers.h"
#include "Worker.h"
#include "Employee.h"
#include "Manager.h"
#include "Boss.h"
#include <fstream>
using namespace std;
/*************************************WorkerManger类的成员函数实现域******************************/
//1.构造函数
WorkerManger::WorkerManger()
{
//1.构建职工链表头指针结点和尾指针,指针初始为空,结点数初始化为0
this->LinkHeader = new Header;
this->LinkHeader->next =NULL;
this->LinkHeader->worker_num = 0;
EndPtr = NULL;
//2.判断职工数据文件是否存在
ifstream ifs;
ifs.open(EMPFILE, ios::in);
//1.文件不存在
if(!ifs.is_open())
{
cout << "文件不存在!" << endl;
//初始化标记
this->File_IsEmpty = true; //文件为空
this->File_IsExist = false; //文件不存在
return;
}
//2.文件存在
else
{
//2.1检查当前文件是否为空文件
char ch;
ifs >> ch;
if(ifs.eof()) //2.1.1文件为空的情况
{
cout << "文件为空!" << endl;
//初始化标记
this->File_IsEmpty = true; //文件为空
this->File_IsExist = true; //文件存在
ifs.close();
return;
}
else //2.1.2文件不为空的情况
{
//读取文件数据到链表中
int num = this->Get_WorkerNum();
cout << "当前职工人数:" << num << endl;
this->File_IsEmpty = false;//文件不为空
this->File_IsExist = true; //文件存在
ifs.close();
}
}
}
/********************************************************************************************/
//2.读取当前打开文件中的职工人数
int WorkerManger::Get_WorkerNum()
{
//1.打开文件
ifstream ifs;
ifs.open(EMPFILE, ios::in);//以读文件方式打开文件
//2.创建临时变量,用于读取文件
int G_id; //职工编号
string G_name; //职工姓名
int G_deptId; //职工所在部门名称编号
//3.读取文件数据
int num; //记录文件中职工人数
while(ifs >> G_id && ifs >> G_name && ifs >> G_deptId)
{
num++;
}
//关闭文件
ifs.close();
return num;
}
/********************************************************************************************/
//3.初始化职工数据链表
void WorkerManger::Init_WorkerLinkList()
{
//1.检查当前文件是否存在
if(this->File_IsExist) //存在
{
if(this->File_IsEmpty == false) //文件不为空
{
//1.1创建流对象,以读文件方式打开文件
ifstream ifs;
ifs.open(EMPFILE, ios::in);
//2.创建临时变量,临时存储文件中读取的职工数据
int I_id;
string I_name;
int I_deptId;
//2.1用于标记第一次链接(头指针指向第一个数据结点)
int first = 1;
//3.开始读取职工文件数据
while(ifs >> I_id && ifs >> I_name && ifs >> I_deptId)
{
//3.1创建文件数据结点
WorkerLinkNode* file_worker = new WorkerLinkNode;
//3.2根据职工部门编号 deptId 选择创建相对应的结点数据域
switch(I_deptId)
{
case 1:
file_worker->WorkerDataPtr = new Employee(I_id, I_name,1);
break;
case 2:
file_worker->WorkerDataPtr = new Manager(I_id, I_name,2);
break;
case 3:
file_worker->WorkerDataPtr = new Boss(I_id, I_name,3);
break;
}
//4.将数据节点依次链接到链表中
if(first)//4.1头指针链接第一个文件数据结点
{
this->LinkHeader-&g