本文已参与「新人创作礼」活动,一起开启掘金创作之路。
先看一下整体框架
//boss.h
#pragma once
#include <iostream>
#include "worker.h"
using namespace std;
class Boss :public Worker
{
public:
//声明构造函数
Boss(int id, string name, int dep);
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 dep);
//显示个人信息
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 dep);
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
};
//worker.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
//员工抽象基类(运用多态弄出三个小类)
class Worker
{
public:
//显示个人信息(用到多态)
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = NULL;
int p_ID; //员工编号
string p_Name;//员工姓名
int p_DeptId;//员工所在部门名称编号
};
//workManager.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 p_EmpNum;
//员工数组指针
Worker ** p_EmpArray;
//添加员工
void add_Emp();
//保存文件
void save();
//判断文件是否为空的标志
bool p_FileIsEmpty;
//统计人数
int getEmpNum();
//初始化员工
void init_Emp();
//显示员工
void show_Emp();
//删除员工
void del_Emp();
//判断员工是否存在
int isExist(int id);
//修改员工
void modify_Emp();
//查找员工
void find_Emp();
//员工排序
void sort_Emp();
//清空文件
void clean_Emp();
//析构函数
~WorkManager();
};
//boss.cpp
#include "boss.h"
Boss::Boss(int id, string name, int dep)
{
this->p_ID = id;
this->p_Name = name;
this->p_DeptId = dep;
}
void Boss::showInfo()
{
cout << "员工编号:" << this->p_ID
<< "\t员工姓名:" << this->p_Name
<< "\t员工岗位:" << this->getDeptName()
<< "\t岗位职责:管理公司所有事情" << endl;
}
string Boss::getDeptName()
{
return string("老板");
}
//employee.cpp
#include "employee.h"
Employee::Employee(int id, string name, int dep)
{
this->p_ID = id;
this->p_Name = name;
this->p_DeptId = dep;
}
void Employee::showInfo()
{
cout << "员工编号:" << this->p_ID
<< "\t员工姓名:" << this->p_Name
<< "\t员工岗位:" << this->getDeptName()
<< "\t岗位职责:完成经理交付的任务" << endl;
}
string Employee::getDeptName()
{
return string("普通员工");
}
//main.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 = 0;
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.modify_Emp();
break;
case 5://查找员工
wm.find_Emp();
break;
case 6://员工排序
wm.sort_Emp();
break;
case 7://清空员工
wm.clean_Emp();
break;
default:
system("cls");//清空再回来
break;
}
}
system("pause");
return 0;
}
//manager.cpp
#include "manager.h"
Manager::Manager(int id, string name, int dep)
{
this->p_ID = id;
this->p_Name = name;
this->p_DeptId = dep;
}
void Manager::showInfo()
{
cout << "员工编号:" << this->p_ID
<< "\t员工姓名:" << this->p_Name
<< "\t员工岗位:" << this->getDeptName()
<< "\t岗位职责:完成老板布置的任务并下发任务给普通员工" << endl;
}
string Manager::getDeptName()
{
return string("经理");
}
//workManager.cpp
/*
1、与用户的沟通菜单界面
2、对职工增删改查的操作
3、与文件的读写交互
*/
#include "workManager.h"
WorkManager::WorkManager()
{
//1、文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);//读文件
//判断文件是否打开成功(bool类型)
if (!ifs.is_open())
{
//cout << "文件不存在!" << endl;
//初始化属性
//初始化记录人数
this->p_EmpNum = 0;
//初始化数组指针为空
this->p_EmpArray = NULL;
//初始化文件是否为空
this->p_FileIsEmpty = true;
ifs.close();
return;
}
//2、文件存在,但数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
//文件为空
//cout << "文件为空" << endl;
//初始化属性
//初始化记录人数
this->p_EmpNum = 0;
//初始化数组指针为空
this->p_EmpArray = NULL;
//初始化文件是否为空
this->p_FileIsEmpty = true;
ifs.close();
return;
}
//3、文件存在,且有数据存在
int num = this->getEmpNum();
//cout << "职工人数为:" << num << endl;
this->p_EmpNum = num;
//开辟空间
this->p_EmpArray = new Worker*[this->p_EmpNum];
this->init_Emp();//初始化,将文件中的数据存到数组中
}
void WorkManager::showMenu()
{
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 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->p_EmpNum + addNum;//新空间大小 = 原纪录人数+新增人数
//开辟新空间
Worker ** newSpace = new Worker*[newSize];
//将原空间下的数据拷贝到新空间下
//判断
if (this->p_EmpArray != NULL) {
for (int i = 0; i < this->p_EmpNum; i++) {
newSpace[i] = this->p_EmpArray[i];
}
}
//批量添加新数据
for (int i = 0; i < addNum; i++)
{
int id;
string name;
int dep;
cout << "请输入第 "<< i + 1 << "个新职工编号:" << endl;
cin >> id;
cout << "请输入第 " << i + 1 << "个新职工姓名:" << endl;
cin >> name;
cout << "请输入新职工岗位:" << endl;
cout << "1、普通员工" << endl;
cout << "2、产品经理" << endl;
cout << "3、老板" << endl;
cin >> dep;
Worker * worker = NULL;
switch (dep)
{
case 1://普通员工
worker = new Employee(id, name, 1);
break;
case 2://产品经理
worker = new Manager(id, name, 1);
break;
case 3://老板
worker = new Boss(id, name, 3);
break;
default:
break;
}
//将创建职工职责保存到数组中
newSpace[this->p_EmpNum + i] = worker;
}
//释放原有空间
delete[] this->p_EmpArray;
//更改新空间指向
this->p_EmpArray = newSpace;
//更新新的职工人数
this->p_EmpNum = newSize;
//更新职工不为空的标志
this->p_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->p_EmpNum; i++)
{
ofs << this->p_EmpArray[i]->p_ID
<< "\t"<< this->p_EmpArray[i]->p_Name
<< "\t"<< this->p_EmpArray[i]->p_DeptId << endl;
}
ofs.close();//关闭文件
}
//统计文件中的人数
int WorkManager::getEmpNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int dep;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> dep)
{
//记录人数
num++;
}
ifs.close();
return num;
}
void WorkManager::init_Emp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int dep;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> dep)
{
Worker * worker = NULL;
if (dep == 1)//普通员工
{
worker = new Employee(id, name, dep);
}
else if (dep == 2)
{
worker = new Manager(id, name, dep);
}
else
{
worker = new Boss(id, name, dep);
}
this->p_EmpArray[index] = worker;
index++;
}
//关闭文件
ifs.close();
}
void WorkManager::show_Emp()
{
if (this->p_FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
for (int i = 0; i < p_EmpNum; i++)
{
//利用多态调用接口
this->p_EmpArray[i]->showInfo();
}
}
system("pause");
system("cls");
}
void WorkManager::del_Emp()
{
if (this->p_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
//按照员工的编号来删除员工
cout << "请输入想要删除的员工编号:" << endl;
int id = 0;
cin >> id;
int index = this->isExist(id);
if (index != -1)//说明员工存在,并且要删除掉index位置上的员工
{
//数据前移
for (int i = index; i < this->p_EmpNum-1; i++)
{
this->p_EmpArray[i] = this->p_EmpArray[i + 1];
}
this->p_EmpNum--;//更新数组中记录人员的个数
//同步更新到文件中
this->save();
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,未找到此员工!" << endl;
}
}
system("pause");
system("cls");
}
int WorkManager::isExist(int id)
{
int index = -1;
for (int i = 0; i < p_EmpNum; i++)
{
if (this->p_EmpArray[i]->p_ID == id)
{
index = i;
break;
}
}
return index;
}
//修改员工
void WorkManager::modify_Emp()
{
if (this->p_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入你要修改的员工编号:" << endl;
int id = 0;
cin >> id;
int num = this->isExist(id);
if (num != -1)
{
//查到该员工的编号
delete this->p_EmpArray[num];
int newID = 0;
string newName = "";
int depSelect = 0;
cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
cin >> newID;
cout << "请输入新姓名:" << endl;
cin >> newName;
cout << "请输入新岗位:" << endl;
cout << "1、普通员工" << endl;
cout << "2、产品经理" << endl;
cout << "3、老板" << endl;
cin >> depSelect;
Worker * worker = NULL;
switch (depSelect)
{
case 1:
worker = new Employee(newID, newName, depSelect);
break;
case 2:
worker = new Manager(newID, newName, depSelect);
break;
case 3:
worker = new Boss(newID, newName, depSelect);
break;
default:
break;
}
//更改数据到数组中
this->p_EmpArray[num] = worker;
cout << "修改成功!" << this->p_EmpArray[num]->p_DeptId << endl;
//保存到文件中
this->save();
}
else
{
cout << "查无此人,修改失败!" << endl;
}
}
system("pause");
system("cls");
}
void WorkManager::find_Emp()
{
if (this->p_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入查找的方式:" << endl;
cout << "1、按照员工编号查找" << endl;
cout << "2、按照员工姓名查找" << endl;
int choice = 0;
cin >> choice;
if (choice == 1) {
int id;
cout << "请输入你要查找的员工编号:" << endl;
cin >> id;
int num = isExist(id);
if (num != -1) {
cout << "查到成功,此员工信息如下:" << endl;
this->p_EmpArray[num]->showInfo();
}
else
{
cout << "查找失败,没有此员工的信息!" << endl;
}
}
if (choice == 2) {
string name;
cout << "请输入你要查找的员工姓名:" << endl;
cin >> name;
bool flag = false;
for (int i = 0; i < p_EmpNum; i++) {
if (p_EmpArray[i]->p_Name == name) {
cout << "查到成功,此员工编号为:"
<< p_EmpArray[i]->p_ID
<< "信息如下:" << endl;
flag = true;
this->p_EmpArray[i]->showInfo();
}
}
if (flag == false) {
//查无此人
cout << "查找失败,没有此员工的信息!" << endl;
}
}
else
{
cout << "输入选项有误!" << endl;
}
}
system("pause");
system("cls");
}
void WorkManager::sort_Emp()
{
if (this->p_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 < p_EmpNum; i++)
{
int minOrMax = i;
for (int j = i + 1; j < p_EmpNum; j++)
{
if (select == 1)//升序排列
{
if (p_EmpArray[minOrMax]->p_ID > p_EmpArray[j]->p_ID)
{
minOrMax = j;
}
}
else//降序排列
{
if (p_EmpArray[minOrMax]->p_ID < p_EmpArray[j]->p_ID)
{
minOrMax = j;
}
}
}
if (i != minOrMax)
{
Worker * temp = p_EmpArray[i];
p_EmpArray[i] = p_EmpArray[minOrMax];
p_EmpArray[minOrMax] = temp;
}
}
cout << "排序成功后,排序结果为:" << endl;
this->save();
this->show_Emp();
}
}
//清空文件
void WorkManager::clean_Emp()
{
cout << "是否确认清空?" << endl;
cout << "1、确认" << endl;
cout << "2、返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//如果存在,则删除文件并重新创建
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->p_EmpArray != NULL)
{
for (int i = 0; i < this->p_EmpNum; i++)
{
if (this->p_EmpArray[i] != NULL)
{
delete this->p_EmpArray[i];
}
}
this->p_EmpNum = 0;
delete[] this->p_EmpArray;
this->p_EmpArray = NULL;
this->p_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
WorkManager::~WorkManager()
{ //释放堆区数据
if (this->p_EmpArray != NULL)
{
delete[] this->p_EmpArray;
this->p_EmpArray = NULL;
}
}
emmm,所有的代码就在上面了,需要的自取,这代码我试运行了几次,没有出现什么漏洞,应该能对你们有帮助