![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/79c23e507dfebf6353a4acd07ce5bdfd.png)
#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 WManager
{
public:
WManager();
void show_menu();
void Exitsystem();
int m_EmpNum;
Worker ** m_EmpAray;
void Add_Emp();
void savef();
bool 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();
~WManager();
};
#include"worker manager.h"
WManager::WManager()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "文件不存在!" << endl;
this->m_EmpNum = 0;
this->m_EmpAray = NULL;
this->FileIsEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件存在为空!" << endl;
this->m_EmpNum = 0;
this->m_EmpAray = NULL;
this->FileIsEmpty = true;
ifs.close();
return;
}
int num = this->get_EmpNum();
cout << "职工的人数为: " << num << endl;
this->m_EmpNum = num;
this->m_EmpAray = new Worker * [this->m_EmpNum];
this->init_emp() ;
}
void WManager::show_menu()
{
cout << "-------------------------------------------------" << endl;
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;
cout << endl;
}
void WManager::Exitsystem()
{
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
}
void WManager::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_EmpAray != NULL)
{
for (int i = 0;i < this->m_EmpNum;i++)
{
newSpace[i] = this->m_EmpAray[i];
}
}
for (int i = 0; i < addNum; i++)
{
int id;
string name;
int depId;
cout << "请输入第 " << i + 1 << " 人的职工编号" << endl;
cin >> id;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpAray[i]->m_ID == id)
{
cout << "请勿重复输入同一个职工编号!请重新输入编号" << endl;
cin >> id;
}
}
cout << "请输入第 " << i + 1 << " 人的职工姓名" << endl;
cin >> name;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpAray[i]->m_Name == name)
{
cout << "请勿重复输入同一个姓名!请重新输入姓名" << endl;
cin >> name;
}
}
cout << "请选择职工岗位编号 :" << endl;
cout << "1 - 普通职工" << endl;
cout << "2 - 经理" << endl;
cout << "3 - 老板" << endl;
cin >> depId;
Worker* worker01 = NULL;
switch (depId)
{
case 1:
worker01 = new Employee(id, name, 1);
break;
case 2:
worker01 = new Manager(id, name, 2);
break;
case 3:
worker01 = new Boss(id, name, 3);
break;
default:
break;
}
newSpace[this->m_EmpNum + i] = worker01;
}
delete[] this->m_EmpAray;
this->m_EmpAray = newSpace;
this->m_EmpNum = newSize;
this->FileIsEmpty = false;
cout << "成功添加 " << addNum << "名员工!!!" << endl;
this->savef();
}
else
{
cout << "输入有误!" << endl;
}
system("pause");
system("cls");
}
void WManager::savef()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < m_EmpNum; i++)
{
ofs << this->m_EmpAray[i]->m_ID << " "
<< this->m_EmpAray[i]->m_Name << " "
<< this->m_EmpAray[i]->m_DeptID << " " << endl;
}
ofs.close();
}
int WManager::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 WManager::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 == 0)
{
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_EmpAray[index] = worker;
index++;
}
ifs.close();
}
void WManager::show_emp()
{
if (this->FileIsEmpty)
{
cout << "文件不存在或者文件记录为空" << endl;
}
else
{
for (int i = 0; i < this->m_EmpNum; i++)
{
this->m_EmpAray[i]->showInfor();
}
}
system("pause");
system("cls");
}
void WManager::Del_emp()
{
if (this->FileIsEmpty)
{
cout << "文件为空或者不存在!!!" << endl;
}
else
{
cout << "请输入删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExist(id);
if (index != -1)
{
for (int i = index; i < this->m_EmpNum - 1; i++)
{
this->m_EmpAray[i] = this->m_EmpAray[i + 1];
}
this->m_EmpNum--;
this->savef();
cout << "删除成功!!!" << endl;
}
else
{
cout << "无此员工!" << endl;
}
}
system("pause");
system("cls");
}
int WManager::IsExist(int id)
{
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpAray[i]->m_ID ==id)
{
index = i;
break;
}
}
return index;
}
void WManager::Mod_Emp()
{
if (this->FileIsEmpty)
{
cout << "文件为空或者不存在!!!" << endl;
}
else
{
cout << "请输入要修改的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExist(id);
if (index != -1)
{
delete this->m_EmpAray[index];
int newID = 0;
string newName = "";
int newDid = 0;
cout << "查找到ID号为: " << id << " 的职工,请输入职工新编号: " << endl;
cin >> newID;
cout << "请输入新的名字: " << endl;
cin >> newName;
cout << "请输入新的部门编号: " << endl;
cout << "请选择职工岗位编号 :" << endl;
cout << "1 - 普通职工" << endl;
cout << "2 - 经理" << endl;
cout << "3 - 老板" << endl;
cin >> newDid;
Worker* worker01 = NULL;
switch (newDid)
{
case 1:
worker01 = new Employee(newID, newName, newDid);
break;
case 2:
worker01 = new Manager(newID, newName, newDid);
break;
case 3:
worker01 = new Boss(newID, newName, newDid);
break;
default:
break;
}
this->m_EmpAray[index] = worker01;
this->savef();
cout << "修改成功!!!" << endl;
}
else
{
cout << "无此员工!" << endl;
}
}
system("pause");
system("cls");
}
void WManager::Find_Emp()
{
if (this->FileIsEmpty)
{
cout << "文件为空或者不存在!!!" << endl;
}
else
{
cout << "请输入查找的方式:" << endl;
cout << "1. 按职工编号查找 " << endl;
cout << "2. 按职工姓名查找 " << endl;
int select = 0;
cin >> select;
if (select == 1)
{
int id = 0;
cout << "请输入查找职工的编号: " << endl;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
cout << "找到这个人! 该职工的信息如下: " << endl;
this->m_EmpAray[ret]->showInfor();
}
else
{
cout << "查无此人!!" << endl;
}
}
else if (select == 2)
{
string name;
cout << "请输入要查找到姓名: " << endl;
cin >> name;
bool flag = false;
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpAray[i]->m_Name == name)
{
cout << "找到此人!这个人的编号为:"<<
this->m_EmpAray[i]->m_ID<<" 此人的信息如下:" << endl;
flag = true;
this->m_EmpAray[i]->showInfor() ;
}
}
if (flag == false)
{
cout << "查找失败,无此人!!" << endl;
}
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
system("pause");
system("cls");
}
void WManager::Sort_Emp()
{
if (this->FileIsEmpty)
{
cout << "文件为空或者不存在!!!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请输入排序方式: " << endl;
cout << "1.升序" << endl;
cout << "2.降序" << endl;
int select = 0;
cin >> select;
for (int i=0;i<m_EmpNum;i++)
{
int minORmax = i;
for (int j = i + 1; j < this->m_EmpNum; j++)
{
if (select == 1)
{
if (this->m_EmpAray[minORmax]->m_ID > this->m_EmpAray[j]->m_ID)
{
minORmax = j;
}
}
else
{
if (this->m_EmpAray[minORmax]->m_ID < this->m_EmpAray[j]->m_ID)
{
minORmax = j;
}
}
}
if (i != minORmax)
{
Worker* temp = this->m_EmpAray[i];
this->m_EmpAray[i] = this->m_EmpAray[minORmax];
this->m_EmpAray[minORmax] = temp;
}
}
cout << "排序成功,排序后的结果为:" << endl;
this->savef();
this->show_emp();
}
}
void WManager::clean_File()
{
cout << "确定清空吗?" << endl;
cout << "1.YES" << endl;
cout << "2.NO" << endl;
int select = 0;
cin >>select;
if (select == 1)
{
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->m_EmpAray != NULL)
{
for (int i = 0; i < m_EmpNum; i++)
{
delete this->m_EmpAray[i];
this->m_EmpAray[i] = NULL;
}
delete[]this->m_EmpAray;
this->m_EmpAray = NULL;
this->m_EmpNum = 0;
this->FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
WManager::~WManager()
{
if (this->m_EmpAray != NULL)
{
delete[] this->m_EmpAray;
this->m_EmpAray = NULL;
}
}
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Worker
{
public:
virtual void showInfor() = 0;
virtual string getDeptName() = 0;
int m_ID;
string m_Name;
int m_DeptID;
};
#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class Employee:public Worker
{
public:
Employee(int ID,string name,int deID);
virtual void showInfor();
virtual string getDeptName();
};
#include"employee.h"
Employee::Employee(int ID, string name, int deID)
{
this->m_ID = ID;
this->m_Name = name;
this->m_DeptID = deID;
}
void Employee::showInfor()
{
cout << "职工编号:" << this->m_ID
<< "\t职工姓名:" << this->m_Name
<< "\t所在部门:" << this->getDeptName()
<<"\t岗位职责:完成经理交给的任务!"<< endl;
}
string Employee::getDeptName()
{
return string("普通员工");
}
#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class Manager :public Worker
{
public:
Manager(int ID, string name, int deID);
virtual void showInfor();
virtual string getDeptName();
};
#include"manager.h"
Manager::Manager(int ID, string name, int deID)
{
this->m_ID = ID;
this->m_Name = name;
this->m_DeptID = deID;
}
void Manager::showInfor()
{
cout << "职工编号:" << this->m_ID
<< "\t职工姓名:" << this->m_Name
<< "\t所在部门:" << this->getDeptName()
<< "\t 岗位职责:完成老板交给的任务,传达老板的命令!" << endl;
}
string Manager::getDeptName()
{
return string("经理");
}
#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class Boss :public Worker
{
public:
Boss(int ID, string name, int deID);
virtual void showInfor();
virtual string getDeptName();
};
#include"boss.h"
Boss::Boss(int ID, string name, int deID)
{
this->m_ID = ID;
this->m_Name = name;
this->m_DeptID = deID;
}
void Boss::showInfor()
{
cout << "职工编号:" << this->m_ID
<< "\t职工姓名:" << this->m_Name
<< "\t所在部门:" << this->getDeptName()
<< "\t 岗位职责:下达命令,监督经理和员工完成任务!" << endl;
}
string Boss::getDeptName()
{
return string("老板");
}