boss.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Boss:public Worker
{
public:
Boss(int id,string name,int did);
virtual void showInfo();
virtual string getDeptName();
};
employee.h
#pragma once
#include <iostream>
using namespace std;
#include"worker.h"
class Employee:public Worker
{
public:
//构造函数
Employee(int id, string name, int did);
virtual void showInfo();
virtual string getDeptName();
};
manager.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Manager : public Worker
{
public:
//构造函数
Manager(int id,string name,int did);
virtual void showInfo();
virtual string getDeptName();
};
woker.h
#pragma once
#include<iostream>//包含输入输出流头文件
using namespace std;//使用标准的命名空间
#include<string>
//抽象职工基类
class Worker
{
public:
//显示个人信息
virtual void showInfo() = 0 ;
//获取岗位名称
virtual string getDeptName() = 0;
int m_Id;
string m_Name;
int m_DeptId;
};
WokerManager.h
#pragma once//防止头文件重复包含
#include<iostream>//包含输入输出流头文件
using namespace std;//使用标准的命名空间
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include<fstream>
#define FILENAME "empFile.txt"
class WorkManager
{
public:
//构造函数
WorkManager();
//展示菜单函数
void ShowMenu();
//退出系统
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();
//判断职工是否存在
int IsExist(int id);
//修改职工
void Mod_Emp();
//查找职工
void find_Emp();
//排序职工
void sort_Emp();
//清空数据
void Clean_File();
//析构函数
~WorkManager();
};
boss.cpp
#pragma once
#include<iostream>
using namespace std;
#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("老板");
}
employee.cpp
#pragma once
#include"employee.h"
#include<iostream>
using namespace std;
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.cpp
#pragma once
#include<iostream>
using namespace std;
#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("经理");
}
WokerManager.cpp
#include"WorkManager.h"
WorkManager::WorkManager()
{
//1.文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);//读文件
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;
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();
for (int i = 0; i < this->m_EmpNum; i++)
{
cout << "职工编号:" << this->m_EmpArray[i]->m_Id
<< "姓名:" << this->m_EmpArray[i]->m_Name
<< "部门编号:" << this->m_EmpArray[i]->m_DeptId << endl;
}
}
void WorkManager::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;
cout << endl;
}
void WorkManager::exitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
//添加职工人数
void WorkManager::Add_Emp()
{
cout << "请输入需要添加的职工数量:" << endl;
int addNum = 0;//保存输入数量
cin >> addNum;
if (addNum > 0)
{
//添加
//计算空间大小
int newsize = this->m_EmpNum + addNum; //原来加新增
//开辟新空间
Worker ** newspace = new Worker * [newsize*sizeof(Worker)];
//将原来空间下的数据拷贝到新空间下
if (this->m_EmpArray != 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;
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,dselect);
break;
case 2:
worker = new Manager(id, name, dselect);
break;
case 3:
worker = new Boss(id, name, dselect);
break;
default:
cout << "错误输入" << endl;
break;
}
//将创建的职工指针保存到数组中
newspace[this->m_EmpNum + i ] = worker;
}
//释放原有空间
delete[] this->m_EmpArray;
//更新新空间的指向
this->m_EmpArray = newspace;
//更新新的职工人数
this->m_EmpNum = newsize;
//更新职工不为空标志
this->m_FileIsEmpty = false;
//提示添加成功
cout << "成功添加" << addNum << "名新员工" << endl;
this->save();
}
else
{
cout << "输入有误!" << endl;
}
system("pause");
system("cls");
}
//保存文件
void WorkManager::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 WorkManager::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 WorkManager::init_Emp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
int didex = 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
{
worker = new Boss(id, name, did);
}
this->m_EmpArray[didex] = worker;
didex++;
}
//关闭文件
ifs.close();
}
//显示职工
void WorkManager::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 WorkManager::IsExist(int id)
{
int flag = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Id == id)
{
flag = i;
break;
}
}
return flag;
}
//删除职工
void WorkManager::Del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不能为空" << endl;
}
else {
cout << "请输入你要删除的员工编号:" << endl;
int n;
cin >> n;
int x = IsExist(n);
if (x != -1) {
for (int i = 0; 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");
}
}
//修改职工
void WorkManager::Mod_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else {
cout << "请输入要修改的职工编号" << endl;
int id;
cin >> id;
int ret = IsExist(id);
if (ret != -1)
{
delete this->m_EmpArray[ret];
int id;
string name;
int did;
cout << "请输入你要修改的新的职工号" << endl;
cin >> id;
cout << "请输入你要修改的新的姓名" << endl;
cin >> name;
cout << "请输入你要修改的新的岗位" << endl;
cout << "1.职工2.经理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;
}
this->m_EmpArray[ret] = worker;
cout << "修改成功" << endl;
this->save();
}
else
{
cout << "查无此人" << endl;
}
}
system("pause");
system("cls");
}
//查找职工
void WorkManager::find_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或为空" << endl;
}
else {
cout << "请输入您要查找的方式:1.按姓名查找 2.按员工编号查找" << endl;
int n;;
cin >> n;
if (n == 1)
{
cout << "请输入您要查找的姓名:" << endl;
string name;
cin >> name;
bool flag = false;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Name == name)
{
cout << "查找成功,信息如下:" << endl;
m_EmpArray[i]->showInfo();
flag = true;
break;
}
}
if (flag == false)
{
cout << "查无此人" << endl;
}
}
else if (n == 2)
{
cout << "请输入您要查找的编号:" << endl;
int id;
cin >> id;
bool flag = false;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Id == id)
{
cout << "查找成功,信息如下:" << endl;
this->m_EmpArray[i]->showInfo();
flag = true;
break;
}
}
if (flag == false)
{
cout << "查无此人" << endl;
}
}
else
{
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
}
//排序员工
void WorkManager::sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1.按职工号升序 2.按职工号降序" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < m_EmpNum; i++)
{
int min0rMax = i;
for (int j = i + 1; j < this->m_EmpNum; j++)
{
if (select == 1)
{
if (this->m_EmpArray[min0rMax]->m_Id > (this->m_EmpArray[j]->m_Id))
{
min0rMax = j;
}
}
else
{
if (this->m_EmpArray[min0rMax]->m_Id < (this->m_EmpArray[j]->m_Id))
{
min0rMax = j;
}
}
}
if (i != min0rMax)
{
Worker* temp = this->m_EmpArray[i];
this->m_EmpArray[i] = this->m_EmpArray[min0rMax];
this->m_EmpArray[min0rMax] = temp;
}
}
}
cout << "排序成功!结果为:" << endl;
this->save();
this->Show_Emp();
}
//清空数据
void WorkManager::Clean_File()
{
cout << "确定清空吗?" << endl;
cout << "1.确定 2.返回" << endl;
int select = 0;
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_EmpNum = NULL;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
}
cout << "清空成功" << endl;
}
system("pause");
system("cls");
}
WorkManager::~WorkManager()
{
if (this->m_EmpArray != NULL)
{
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];
}
}
}
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
职工管理系统.cpp
#include<iostream>
using namespace std;
#include"WorkManager.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
int main()
{
WorkManager wm;
int choice;
while (true)
{
wm.ShowMenu();
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;
}