文章目录
基于多态的职工管理系统
职工管理系统.cpp
主体框架
#include<iostream>
using namespace std;
#include"WorkerManager.h"
#include"Worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
int main()
{
WorkerManager wm;//实例化管理者对象
int choice = 0; //储存用户的选项
while (true)
{
wm.Show_Menu();//展示菜单成员函数
cout << "输入你的选择: " << 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");
return 0;
}
WorkerManager.h
管理系统功能头文件
#pragma once//防止头文件重复
#include<iostream>
#include"Worker.h"
#include<fstream>
#define FILENAME "empFile.txt"
using namespace std;
class WorkerManager
{
public:
WorkerManager();//构造函数
void Show_Menu();//菜单展示
void ExitSystem();//退出系统
int m_EmpNum;//记录职工人数
Worker** m_EmpArray;//职工数组指针
void Add_Emp();//添加职工的功能
void save();//保存数据到文件
bool m_FileIsEmpty;//判断文件是否为空
int get_EmpNum();//统计人数
void init_Emp();//初始化员工
void Show_Emp();//显示职工
void Del_Emp();//删除职工
void Mod_Emp();//修改员工
void Find_Emp();//查找职工信息
int Dnum(int id);//判断编号重复
void Sort_Emp();//按编号顺寻排序职工
void Clean_File();//清空文档
int IsExist(int id);//判断职工是否存在,存在返回位置,不存在返回-1
~WorkerManager();//析构函数
};
WorkerManager.cpp
管理系统的具体功能实现
#include<iostream>
#include<fstream>
#define FILENAME "empFile.txt"
using namespace std;
#include"WorkerManager.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
WorkerManager::WorkerManager()
{
//1.文件不存在的初始化
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
this->m_EmpNum = 0; //初始化记录人数
this->m_EmpArray = NULL;//初始化数组指针为空
this->m_FileIsEmpty = true;//文件为空
ifs.close();
return;
}
//2.文件存在 但数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
this->m_EmpNum = 0; //初始化记录人数
this->m_EmpArray = NULL;//初始化数组指针为空
this->m_FileIsEmpty = true;//文件为空
ifs.close();
return;
}
//3.文件存在且有数据
int num = this->get_EmpNum();
this->m_EmpNum = num;
this->m_EmpArray = new Worker * [this->m_EmpNum]; //开辟空间
this->init_Emp();//数据存入数组
}
void WorkerManager::Show_Menu()
{
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;
cout << endl;
}
void WorkerManager::ExitSystem()
{
cout << "感谢你的使用" << endl;
system("pause");
exit(0);
}
void WorkerManager::Add_Emp()
{
cout << "请输入添加职工的数量:" << endl;
int addNum = 0;//保存用户的输入数量
cin >> addNum;
if(addNum > 0) // 添加
{
int newSize = this->m_EmpNum + addNum; //计算新的空间
Worker ** newSpace = new Worker * [newSize];//开辟相空间
if (this->m_EmpNum != NULL )//将原空间数据考入新空间
{
for (int i = 0; i < this->m_EmpNum; i++)
{
newSpace[i] = this->m_EmpArray[i];
}
}
for (int i = 0; i < addNum; i++)
{
int id;
string name;
int dSelect;
cout << "请输入第" << i + 1 << "个新员工的编号" << endl;
cin >> id;
id = Dnum(id);
cout << "请输入第" << i + 1 << "个新员工的姓名" << endl;
cin >> name;
cout << "请选择该职工的职位" << endl;
cout << "1.普通职工" << 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;
}
newSpace[this->m_EmpNum + i] = worker;//将创建的职工职责,保存到数组中
this->save();
}
delete[] this->m_EmpArray;//释放原有空间
this->m_EmpArray = newSpace;//更新新空间指向
this->m_EmpNum = newSize;//更新新职工人数
this->m_FileIsEmpty = false;//更新职工不为空
cout << "成功添加" << addNum << "名新职工" << endl;
}
else
{
cout << "输入数据有误" << endl;
}
system("pause");
system("cls");
}
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();
}
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* worker = NULL;
if (dId == 1)//普通员工
{
worker = new Employee(id, name, dId);
}
else if (dId == 2)//经理
{
worker = new Manager(id, name, dId);
}
else if (dId == 3)//老板
{
worker = new Boss(id, name, dId);
}
this->m_EmpArray[index] = worker;
index++;
}
ifs.close();
}
void WorkerManager::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");
}
int WorkerManager::Dnum(int id)//判断编号重复
{
int num = id;
int flag = 0;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (num == this->m_EmpArray[i]->m_Id)
{
cout << "已存在编号为" << num << "的职工,请重新输入" << endl;
cin >> num;
flag = 1;
break;
}
}
if (flag == 1) Dnum(num);
else return num;
}
void WorkerManager::Sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序的方式" << endl;
cout << "1.按照升序进行排列" << endl;
cout << "2.按照降序进行排列" << endl;
int select;
cin >> select;
if (select == 1)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
for (int j = i+1; j < this->m_EmpNum; j++)
{
if (this->m_EmpArray[i]->m_Id > this->m_EmpArray[j]->m_Id)
{
swap(this->m_EmpArray[i], this->m_EmpArray[j]);
}
}
}
cout << "已按照升序排列,结果如下:" << endl;
this->save();
Show_Emp();
}
else if (select == 2)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
for (int j = i + 1; j < this->m_EmpNum; j++)
{
if (this->m_EmpArray[i]->m_Id < this->m_EmpArray[j]->m_Id)
{
swap(this->m_EmpArray[i], this->m_EmpArray[j]);
}
}
}
cout << "已按照降序排列,结果如下:" << endl;
this->save();
Show_Emp();
}
else
{
cout << "输入选项错误" << endl;
}
}
}
void WorkerManager::Clean_File()
{
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++)
{
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;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
int WorkerManager::IsExist(int id)
{
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (id == this->m_EmpArray[i]->m_Id)
{
index = i;
break;
}
}
return index;
}
void WorkerManager::Del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
int id = 0;
cout << "请输入要删除的职工的编号" << endl;
cin >> id;
int index = this->IsExist(id);
if (index != -1)//职工存在,删除index上的职工,并前移后续职工
{
for (int i = index; i < this->m_EmpNum - 1; i++)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
this->m_EmpNum--;//总人数 -1
this->save();//写入文件中
if (this->m_EmpNum == 0)
{
this->m_FileIsEmpty = true;//判断文件是否为空
}
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,未找到该职工" << endl;
}
}
system("pause");
system("cls");
}
void WorkerManager::Mod_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在" << endl;
}
else
{
cout << "请输入修改职工的编号:" << endl;
int id;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)//查找到该员工
{
delete this->m_EmpArray[ret];
int newId = 0;
string newName = "";
int dSelect = 0;
cout << "查找到id为: " << id << " 的员工,请输入新的编号" << endl;
cin >> newId;
id = Dnum(newId);
cout << "请输入新的姓名" << endl;
cin >> newName;
cout << "请输入新的职位" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect)
{
case 1:
worker = new Employee(newId, newName, 1);
break;
case 2:
worker = new Manager(newId, newName, 2);
break;
case 3:
worker = new Boss(newId, newName, 3);
break;
default:
break;
}
this->m_EmpArray[ret] = worker;//更新数据到数组中
this->save();
cout << "修改成功!" << endl;
}
else
{
cout << "修改失败,未查询到该员工" << endl;
}
}
system("pause");
system("cls");
}
void WorkerManager::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 ret = IsExist(id);
if (ret != -1)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Id == id)
{
cout << "查找到该员工!信息如下:" << endl;
this->m_EmpArray[i]->showInfo();
}
}
}
else
{
cout << "查找失败,查无此人" << endl;
}
}
else if (select == 2)
{
cout << "请输入要查询职工的姓名:" << endl;
string name = "";
cin >> name;
bool flag = false;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (m_EmpArray[i]->m_Name == name)
{
cout << "查找到编号为"<< m_EmpArray[i]->m_Id <<"的员工,信息如下:" << endl;
flag = true;
this->m_EmpArray[i]->showInfo();
}
}
if(flag == false) cout << "查找失败,查无此人" << endl;
}
else
{
cout << "输入选项有误!" << endl;
}
}
system("pause");
system("cls");
}
WorkerManager::~WorkerManager()
{
if (this->m_EmpArray != NULL)//手动释放
{
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
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;//部门编号
};
employee.h
职工类的头文件
//普通员工文件
#pragma once
#include<iostream>
#include<string>
#include"Worker.h"
using namespace std;
class Employee :public Worker
{
public:
Employee(int id,string name,int dId);//构造函数
virtual void showInfo();//显示个人信息
virtual string GetDeptName();//获取岗位名称
};
employee.cpp
职工类的封装
#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 << "职工编号:" << this->m_Id
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->GetDeptName()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
string Employee::GetDeptName()
{
return string("员工");
}
manager.h
经理类的头文件
#pragma once
#include<iostream>
#include<string>
#include"Worker.h"
using namespace std;
class Manager :public Worker
{
public:
Manager(int id, string name, int dId);//构造函数
virtual void showInfo();//显示个人信息
virtual string GetDeptName();//获取岗位名称
};
manager.cpp
经理类的封装
#include "manager.h"
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
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->GetDeptName()
<< "\t岗位职责:完成老板交代的任务,并下发老板交给的任务" << endl;
}
string Manager::GetDeptName()
{
return string("经理");
}
boss.h
老板类的头文件
#pragma once
#include<iostream>
#include<string>
#include"Worker.h"
using namespace std;
class Boss :public Worker
{
public:
Boss(int id, string name, int dId);
virtual void showInfo() ;//显示个人信息
virtual string GetDeptName() ;//获取岗位名称
};
boss.cpp
老板类的封装
#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 << "职工编号:" << this->m_Id
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->GetDeptName()
<< "\t岗位职责:管理公司的所有事务" << endl;
}
string Boss::GetDeptName()
{
return string("老板");
}
尝试写这个小项目时,对于封装多态的理解进一步加深