目录
一、功能分析
公司有三种员工分别为 普通员工、经理和老板
每个员工有 职工编号,姓名和部门编号
普通员工职责:完成经理交给的任务
经理职责:完成老板交给的任务,并下发任务给员工
老板职责:管理公司所有事务
要求实现如下8个功能:
退出管理程序
增加职工信息
显示职工信息
删除离职职工
修改职工信息
查找职工信息
按照编号排序
清空所有文档
二、头文件
1.workerManager.h
2.worker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
//职工抽象基类
class Worker
{
public:
//显示个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
int id; // 职工编号
string name; //职工姓名
int did; //职工所在部门编号
};
3.employee.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"
//员工类
class Employee :public Worker
{
public:
//构造函数
Employee(int id, string name, int did);
//显示个人信息
virtual void showInfo();
//获取职工岗位名称
virtual string getDeptName();
};
4.manager.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"
class Manager :public Worker
{
public:
//构造函数
Manager(int id, string name, int did);
//显示个人信息
virtual void showInfo();
//获取职工岗位名称
virtual string getDeptName();
};
5.boss.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"
class Boss :public Worker
{
public:
//构造函数
Boss(int id, string name, int did);
//显示个人信息
virtual void showInfo();
//获取职工岗位名称
virtual string getDeptName();
};
三、源文件
1.职工管理系统.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.showEmp();
break;
case 3://删除离职职工
wm.DelEmp();
break;
case 4://修改职工信息
wm.modEmp();
break;
case 5://查找职工信息
wm.FindEmp();
break;
case 6://按照编号排序
wm.sortEmp();
break;
case 7://清空所有文档
wm.clean_File();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
2.employee.cpp
#include "employee.h"
Employee::Employee(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Employee::showInfo()
{
cout << "职工编号: " << this->id
<< "\t职工姓名: " << this->name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
string Employee::getDeptName()
{
if (did == 1)
{
return string("普通员工");
}
else if (did == 2)
{
return string("经理");
}
else
{
return string("老板");
}
}
3.manager.cpp
#include "manager.h"
Manager::Manager(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Manager::showInfo()
{
cout << "职工编号: " << this->id
<< "\t职工姓名: " << this->name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:完成老板交给的任务,并下发给员工" << endl;
}
string Manager::getDeptName()
{
if (did == 1)
{
return string("普通员工");
}
else if (did == 2)
{
return string("经理");
}
else
{
return string("总裁");
}
}
4.boss.cpp
#include "boss.h"
Boss::Boss(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Boss::showInfo()
{
cout << "职工编号: " << this->id
<< "\t职工姓名: " << this->name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:管理公司所有事务" << endl;
}
string Boss::getDeptName()
{
if (did == 1)
{
return string("普通员工");
}
else if (did == 2)
{
return string("经理");
}
else
{
return string("总裁");
}
}
5.workerManager.cpp
#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())
{
//cout << "文件不存在" << endl;
this->EmpNum = 0;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
ifs.close();
return;
}
//2、文件存在,数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
//文件为空
//cout << "文件为空!" << endl;
this->EmpNum = 0;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
ifs.close();
return;
}
//3、文件存在且数据不为空
int num = this->getEmpNum();
//cout << "职工人数为:" << num << endl;
this->EmpNum = num;
//开辟空间
this->Emp_Arr = new Worker * [this->EmpNum];
//将文件中的数据读到数组中
this->initEmp();
//测试代码
/*for (int i = 0; i < this->EmpNum; i++)
{
cout << "职工编号:" << this->Emp_Arr[i]->id
<< "姓名:" << this->Emp_Arr[i]->name
<< "部门编号:" << this->Emp_Arr[i]->did << endl;
}*/
};
//类外实现析构函数
WorkerManager::~WorkerManager()
{
if (this->Emp_Arr != NULL)
{
delete[] this->Emp_Arr;
this->Emp_Arr = NULL;
}
};
//类外实现菜单函数
void WorkerManager::Show_Menu()
{
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 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->EmpNum + addNum;
//开辟空间
Worker** newSpace = new Worker * [newSize];
//将原来的空间的数据拷贝到新空间上
if (this->Emp_Arr != NULL)
{
for (int i = 0; i < this->EmpNum; i++)
{
newSpace[i] = this->Emp_Arr[i];
}
}
//开始添加新的数据
for (int i = 0; i < addNum; i++)
{
int id;
string name;
int did;
cout << "请输入第" << i + 1 << "个新职工编号:" << endl;
cin >> id;
cout << "请输入该职工姓名:" << endl;
cin >> name;
cout << "请选择该职工岗位:" << endl;
cout << "1、普通员工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
Worker* worker_1 = NULL;
cin >> did;
switch (did)
{
case 1:
worker_1 = new Employee(id, name, did);
case 2:
worker_1 = new Manager(id, name, did);
case 3:
worker_1 = new Boss(id, name, did);
deault:
break;
}
//将创建的职工指针保存到数组中
newSpace[this->EmpNum + i] = worker_1;
}
//释放原有空间
delete[] this->Emp_Arr;
//更改新空间的指向
this->Emp_Arr = newSpace;
//更新新空间人数
this->EmpNum = newSize;
//更新一下文件不为空的情况
this->FileIsEmpty = false;
//提示成功添加
cout << "成功添加" << addNum << "名新职工" << endl;
//保存到文件中
this->save();
}
else
{
cout << "数据有误" << endl;
}
//按任意键后清屏回到上一目录
system("pause");
system("cls");
}
//实现保存数据到文件
void WorkerManager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out); //用输出的方式打开文件,就是写入到文件
for (int i = 0; i < this->EmpNum; i++)
{
ofs << this->Emp_Arr[i]->id << " "
<< this->Emp_Arr[i]->name << " "
<< this->Emp_Arr[i]->did << " " << endl;
}
//关闭文件
ofs.close();
}
//统计文件中的人数
int WorkerManager::getEmpNum()
{
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::initEmp()
{
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, 1);
}
else if (did == 2)
{
worker = new Manager(id, name, 2);
}
else if (did == 3)
{
worker = new Boss(id, name, 3);
}
//存放在数组中
this->Emp_Arr[index] = worker;
index++;
}
}
//显示职工
void WorkerManager::showEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
for (int i = 0; i < this->EmpNum; i++)
{
//利用多态调用接口
this->Emp_Arr[i]->showInfo();
}
}
system("pause");
system("cls");//按任意键以后回到菜单选择状态
}
//删除员工
void WorkerManager::DelEmp()
{
if (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->EmpNum - 1; i++)
{
this->Emp_Arr[i] = this->Emp_Arr[i + 1];
}
this->EmpNum--; //更新一下数组中记录的人员个数
//同步更新到文件
this->save();
cout << "删除成功" << endl;
}
else
{
cout << "删除失败,未找到该职工" << endl;
}
}
system("pause");
system("cls");//按任意键以后回到菜单选择状态
}
//判断职工是否存在
int WorkerManager::IsExit(int id)
{
int index = -1;
for (int i = 0; i < this->EmpNum; i++)
{
if (this->Emp_Arr[i]->id == id)
{
index = i;
break;
}
}
return index;
}
//修改职工
void WorkerManager::modEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
//按照职工编号删除
cout << "请输入想要修改职工编号:" << endl;
int id = 0;
cin >> id;
int ret = this->IsExit(id);
if (ret != -1)
{
//查找到该编号的职工
delete this->Emp_Arr[ret];
int newid = 0;
string newname = "";
int newdid = 0;
cout << "查到" << id << "号职工,请重新输入新职工号:" << endl;
cin >> newid;
cout << "请输入新姓名:" << endl;
cin >> newname;
cout << "请输入岗位:" << endl;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> newdid;
Worker* worker = NULL;
switch (newdid)
{
case 1:
worker = new Employee(newid, newname, newdid);
case 2:
worker = new Manager(newid, newname, newdid);
case 3:
worker = new Boss(newid, newname, newdid);
deault:
break;
}
//更新数据到数组中
this->Emp_Arr[ret] = worker;
cout << "修改成功!" << Emp_Arr[ret]->did << endl;
this->save();
cout << "删除成功" << endl;
}
else
{
cout << "修改失败,未找到该职工" << endl;
}
}
system("pause");
system("cls");//按任意键以后回到菜单选择状态
}
//查找职工
void WorkerManager::FindEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
//按照职工编号删除
cout << "请输入查找方式:" << endl;
cout << "1、按职工号查找" << endl;
cout << "2、按职工姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
cout << "请输入想要查找职工编号:" << endl;
int id = 0;
cin >> id;
int ret = this->IsExit(id);
if (ret != -1)
{
cout << "查找成功,该职工信息如下:" << endl;
this->Emp_Arr[ret]->showInfo();
}
else
{
cout << "修改失败,未找到该职工" << endl;
}
}
else if (select == 2)
{
cout << "请输入想要查找职工姓名:" << endl;
string name;
cin >> name;
bool flag = false; //查找到标志
for (int i = 0; i < this->EmpNum; i++)
{
if (Emp_Arr[i]->name == name)
{
cout << "查找成功,职工编号为:" << Emp_Arr[i]->id
<< ",职工的信息如下" << endl;
flag = true;
this->Emp_Arr[i]->showInfo();
}
}
if (flag == false)
{
cout << "修改失败,未找到该职工" << endl;
}
}
else
{
cout << "输入项有误" << endl;
}
}
system("pause");
system("cls");//按任意键以后回到菜单选择状态
}
//排序职工
void WorkerManager::sortEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1、按职工号升序" << endl;
cout << "2、按职工号降序:" << endl;
int select;
cin >> select;
for (int i = 0; i < this->EmpNum - 1; i++)
{
int minOrmax = i;
for (int j = i + 1; j < this->EmpNum; j++)
{
if (select == 1) //升序
{
if (this->Emp_Arr[minOrmax]->id > Emp_Arr[j]->id)
{
minOrmax = j;
}
}
else //降序
{
if (this->Emp_Arr[minOrmax]->id < Emp_Arr[j]->id)
{
minOrmax = j;
}
}
}
if (i != minOrmax)
{
Worker* temp = Emp_Arr[i];
Emp_Arr[i] = Emp_Arr[minOrmax];
Emp_Arr[minOrmax] = temp;
}
}
cout << "排序成功,排序后的结果为:" << endl;
this->save();
this->showEmp();
}
}
//清空文件
void WorkerManager::clean_File()
{
cout << "取人清空?" << endl;
cout << "1、确认" << endl;
cout << "2、返回" << endl;
int select;
cin >> select;
if (select == 1)
{
//打开模式ios::trunc 如果存在删除文件并重新创建
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->Emp_Arr != NULL)
{
for (int i = 0; i < this->EmpNum; i++)
{
if (this->Emp_Arr[i] != NULL)
{
delete this->Emp_Arr[i];
}
}
this->EmpNum = 0;
delete[] this->Emp_Arr;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}