实现一个基于多态的职工管理系统
管理类负责内容:
void workerManger::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]->getDepNumber() << endl;
}
ofs.close();
}
在构造函数中初始化数据
workerManger::workerManger()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
//1.文件不存在情况
if (!ifs.is_open())
{
cout << "文件不存在" << endl;//测试输出
//初始化人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件为空标志
this->m_FileIsEmpty = true;
ifs.close();//关闭文件对象
return;
}
//2.文件存在但为空判断
char ch;
ifs >> ch;
//如果ch为尾,那么读完后ifs.eof函数为空
if (ifs.eof())
{
cout << "文件为空" << endl;//测试输出
//初始化人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件为空标志
this->m_FileIsEmpty = true;
ifs.close();//关闭文件对象
return;
}
//3.文件存在,并记录数据
this->get_EmpNum();
}
//统计文件中人数
int workerManger::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;
}
#include "workerManger.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
int main()
{
workerManger wm;
while (true)
{
wm.showMenu();
cout << "请输入您的选择:" << endl;
int num;
cin >> num;
switch (num)
{
case 0:
wm.exitFunction();
break;
case 1:
wm.AddEmp();
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.clear_Emp();
break;
default:
system("cls"); //清屏
break;
}
}
//Worker* worker = NULL;
//worker = new employee(1, "张三", 1);
//worker->showInfo();
//delete worker;
//worker = new manager(2, "李四", 2);
//worker->showInfo();
//delete worker;
//worker = new boss(3, "王五", 23);
//worker->showInfo();
//delete worker;
}
#pragma once
#include
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include
#define FILENAME "empFile.txt"
using namespace std;
//管理类
class workerManger
{
public:
workerManger();
~workerManger();
//菜单界面
void showMenu();
//退出功能
void exitFunction();
//添加职工功能
void AddEmp();
//写入文件功能
void save();
//统计文件中人数
int get_EmpNum();
//初始化员工
void init_Emp();
//显示员工
void show_Emp();
//删除员工
//判断员工是否存在,如果存在返回职工所在数组中的位置,
//不存在则返回-1
int IsExit(int id);
void del_Emp();
//修改员工
void mod_Emp();
//查找员工
void find_Emp();
//员工排序
void sort_Emp();
//清空数据
void clear_Emp();
//记录文件中的人数个数
int m_EmpNum;
//员工数组的指针
Worker** m_EmpArray;
//标志文件是否为空
bool m_FileIsEmpty;
};
#include "workerManger.h"
void workerManger:: 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 workerManger::exitFunction()
{
system("pause");
exit(0);
}
int workerManger::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++;
}
//关闭文件
ifs.close();
return num;
}
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;
//根据不同部门Id创建不同对象
if (dId == 1)
{
worker = new employee(id, name, dId);
}
else if (dId == 2)
{
worker = new manager(id, name, dId);
}
else
{
worker = new boss(id, name, dId);
}
//存放在数据中
this->m_EmpArray[index] = worker;
index++;
}
//关闭文件
ifs.close();
}
workerManger::workerManger()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
//1.文件不存在情况
if (!ifs.is_open())
{
//cout << "文件不存在" << endl;//测试输出
//初始化人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件为空标志
this->m_FileIsEmpty = true;
ifs.close();//关闭文件对象
return;
}
//2.文件存在但为空判断
char ch;
ifs >> ch;
//如果ch为尾,那么读完后ifs.eof函数为空
if (ifs.eof())
{
//cout << "文件为空" << endl;//测试输出
//初始化人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件为空标志
this->m_FileIsEmpty = true;
ifs.close();//关闭文件对象
return;
}
//3.文件存在,并记录数据
int num = this->get_EmpNum();
//cout << "职工人数为:" << num << endl;
this->m_EmpNum = num;
//开辟空间
this->m_EmpArray = new Worker * [this->m_EmpNum];
//将文件中的数据,存到数组中
this->init_Emp();
}
void workerManger::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_dId << endl;
}
ofs.close();
}
void workerManger:: AddEmp()
{
cout << "请输入增加员工数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
//计算新空间大小
int newSize = this->m_EmpNum + addNum;
//开辟新空间
Worker** newSpace = new Worker * [newSize];
int num = 0;
if (this->m_EmpArray != NULL)
{
for (int i = 0; i < this->m_EmpNum; ++i)
{
newSpace[i] = this->m_EmpArray[i];
num++;
}
}
for (int i = 0; i < addNum; ++i)
{
int id;//职工编号
string name;//职工名字
int dId; //部门选择
int preNum = this->m_EmpNum+i;
cout << "请输入第" << i + 1 << "个职工的编号:" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个职工的名字:" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "个职工的职位:" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> dId;
Worker* worker = NULL;
switch (dId)
{
case 1:
worker = new employee(id, name, dId);
break;
case 2:
worker = new manager(id, name, dId);
break;
case 3:
worker = new boss(id, name, dId);
break;
default:
break;
}
//将创建职工职责,添加到数组中
newSpace[this->m_EmpNum + i] = worker;
}
//释放原有空间
delete[] this->m_EmpArray;
//更改新空间的指向
this->m_EmpArray = newSpace;
//更新新的职工人数
this->m_EmpNum = newSize;
//更新职工不为空标志
this->m_FileIsEmpty = false;
//保存职工内容
this->save();
//提示添加成功
cout << "成功添加" << addNum << "名新职工" << endl;
//按任意键后,清屏回到上级目录
system("pause");
system("cls");
}
else
{
cout << "输入有误" << endl;
}
}
//显示员工
void workerManger::show_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
for (int i = 0; i < m_EmpNum; ++i)
{
//利用多态调用接口
this->m_EmpArray[i]->showInfo();
}
}
system("pause");
system("cls");
}
//删除员工
void workerManger::del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
//按照职工编号删除
cout << "请输入想要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExit(id);
if (index != -1)//找到员工
{
for (int i = index; i < this->m_EmpNum-1; ++i)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
//更新数组中记录人员数
this->m_EmpNum--;
//更新文件
this->save();
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,没有该员工!" << endl;
}
}
system("pause");
system("cls");
}
int workerManger::IsExit(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;
}
//修改员工
void workerManger::mod_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入要修改职工的编号:" << endl;
int id;
cin >> id;
int index = this->IsExit(id);
if (index != -1)
{
delete this->m_EmpArray[index];
int new_id;
string new_name;
int new_dId;
cout << "查到:"<> new_id;
cout << "请输入修改后职工的姓名:" << endl;
cin >> new_name;
cout << "请输入修改后职工的编号:" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> new_dId;
Worker* worker = NULL;
switch (new_dId)
{
case 1:
worker = new employee(new_id, new_name, new_dId);
break;
case 2:
worker = new manager(new_id, new_name, new_dId);
break;
case 3:
worker = new boss(new_id, new_name, new_dId);
break;
default:
break;
}
m_EmpArray[index] = worker;
this->save();
cout << "修改成功!" << endl;
}
else
{
cout << "没有找到该员工!" << endl;
}
}
system("pause");
system("cls");
}
//查找员工
void workerManger::find_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入查找方式:" << endl;
cout << "1. 按职工编号查找:" << endl;
cout << "2. 按职工姓名查找:" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
cout << "请输入查找的职工编号:" << endl;
int id;
cin >> id;
int f = this->IsExit(id);
if (f != -1)
{
cout << "查找成功,编号为" << id << "的信息如下:" << endl;
this->m_EmpArray[f]->showInfo();
}
else
{
cout << "没有该员工!" << endl;
}
}
else if (select == 2)
{
cout << "请输入查找的职工姓名:" << endl;
string name;
cin >> name;
bool flag = true;
for (int i = 0; i < this->m_EmpNum; ++i)
{
if (this->m_EmpArray[i]->m_Name == name)
{
cout << "查找成功,姓名为" << name << "的信息如下:" << endl;
this->m_EmpArray[i]->showInfo();
flag = false;
}
}
if (flag)
{
cout << "查无此人!" << endl;
}
}
else
{
cout << "输入错误!" << endl;
}
}
system("pause");
system("cls");
}
//员工排序
void workerManger::sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请输入:1.升序排序" << endl;
cout << "2.将序排序" << 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)
{
if (this->m_EmpArray[j]->m_Id < this->m_EmpArray[minOrmax]->m_Id)
{
minOrmax = j;
}
}
else
{
if (this->m_EmpArray[j]->m_Id > this->m_EmpArray[minOrmax]->m_Id)
{
minOrmax = j;
}
}
}
if (minOrmax != i)
{
Worker* temp = this->m_EmpArray[i];
this->m_EmpArray[i] = this->m_EmpArray[minOrmax];
this->m_EmpArray[minOrmax] = temp;
}
}
cout << "排序成功!" << endl;
this->save();
this->show_Emp();
}
}
//清空数据
void workerManger::clear_Emp()
{
cout << "确定清空数据?" << endl;
cout << "1. 确定" << endl;
cout << "2. 退出" << endl;
int select;
cin >> select;
if (select == 1)
{
ofstream ofs(FILENAME, ios::trunc);//删除文件后文件重新创建
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[] this->m_EmpArray;
this->m_EmpArray = NULL;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
workerManger:: ~workerManger()
{
if (this->m_EmpArray != NULL)
{
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
#pragma once
#include
using namespace std;
class Worker
{
public:
virtual void showInfo()=0;
virtual string getDepNumber() = 0;
int m_Id;
string m_Name;
int m_dId;
};
#pragma once
#include"worker.h"
class employee: public Worker
{
public:
employee(int id, string name, int dId);
void showInfo();
string getDepNumber();
};
#include "employee.h"
employee::employee(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_dId = dId;
}
void employee::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->getDepNumber()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
string employee::getDepNumber()
{
return string("员工");
}
#pragma once
#include"worker.h"
class manager : public Worker
{
public:
manager(int id, string name, int dId);
void showInfo();
string getDepNumber();
};
#include "manager.h"
manager::manager(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_dId = dId;
}
void manager::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->getDepNumber()
<< "\t岗位职责:完成老板交给的任务,并下发任务给员工" << endl;
}
string manager::getDepNumber()
{
return string("经理");
}
#pragma once
#include"worker.h"
class boss : public Worker
{
public:
boss(int id, string name, int dId);
void showInfo();
string getDepNumber();
};
#include "boss.h"
boss::boss(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_dId = dId;
}
void boss::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->getDepNumber()
<< "\t岗位职责:管理公司所有事务" << endl;
}
string boss::getDepNumber()
{
return string("老板");
}