职工管理项目模型在编程作业随处可见,如【学生信息管理系统】、【图书馆管理系统】等等
这里给出【IT公司职工管理系统】的实现👇
系统要求描述:
有一家小小的IT公司,只有三类员工:普通程序员、项目经理、公司董事,每个员工只有编号ID、姓名、代表职位的dID(1,2,3)。
要求你制作一个完善职工管理系统:
实现添加、查找、删除、清空、修改等操作
由于这是公司所用,不能使用那种只能使用一下的临时程序🌀
系统实现:
1.单文件⋙
/*
IT公司员工分为三类:普通程序员、项目经理、董事,显示信息时,需要显示职工编号、职工姓名、职工岗位,以及职责
普通程序员:完成项目经理交给的任务
项目经理职责:完成老板的任务并且下发任务给员工
董事职责:管理公司所有任务
职工岗位用1,2,3表示,代表三类员工
*/
//-------------------------------------
//-------------------------------------
#include<iostream>
#include<string>
using namespace std;
#include<fstream>
constexpr auto myFILE = "empFile.txt"; //对非VS,如CB、DV玩家,使用#define myFILE "empFile.txt"
typedef unsigned int ui;
//-----------------------------------职工类设计-----------------------
//员工基类
class Worker {
public:
Worker(ui id, string name, string dId) :m_id(id), m_name(name), m_DeptId(dId) {
}
//显示员工个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
ui m_id;//职工编号
string m_name;//职工姓名
string m_DeptId;//职工所在部门编号
};
//------------------普通程序员类
class Employee :public Worker {
public:
//普通程序员的构造函数
Employee(ui id, string name, string dId) :Worker(id, name, dId) {
}
//显示普通程序员的个人信息
void showInfo() {
cout << "职工编号:" << this->m_id
<< "\t职工姓名:" << this->m_name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成经理交给的项目,满足客户需求" << endl;
}
//获取岗位名称
string getDeptName() {
return string("普通程序员");
}
};
//------------------项目经理类
class Manager :public Worker {
public:
//项目经理的构造函数
Manager(ui id, string name, string dId) :Worker(id, name, dId) {
}
//显示项目经理的个人信息
void showInfo() {
cout << "职工编号:" << this->m_id
<< "\t职工姓名:" << this->m_name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成老板的任务,下发任务给普通程序员" << endl;
}
//获取岗位名称
string getDeptName() {
return string("项目经理");
}
};
//------------------老板类
class Boss :public Worker {
public:
//董事的构造函数
Boss(ui id, string name, string dId) :Worker(id, name, dId) {
}
//显示董事的个人信息
void showInfo() {
cout << "职工编号:" << this->m_id
<< "\t职工姓名:" << this->m_name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:管理公司职责,全局掌控,发布任务" << endl;
}
//获取岗位名称
string getDeptName() {
return string("IT公司董事");
}
};
//-----------------职工类实现完毕!!!
//---------------------------------系统接口API设计-----------------------
class workerManager
{
private://敏感文件
int m_EmpNum;
Worker** m_Emparray;
bool m_FileIsEmpty;
void save() {
//保存文件
ofstream ofs;
ofs.open(myFILE, 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;
cout << "Id为" << this->m_Emparray[i]->m_id << "的员工" << "保存成功" << endl;
}
ofs.close();
return;
}
void up_qssort(Worker** m_Emparray, int l, int r) {
int i = l, j = r, flag = this->m_Emparray[l + (r - l) / 2]->m_id;//先选出标杆flag
do {
while (this->m_Emparray[i]->m_id < flag)i++;//从左找比标杆大的数
while (this->m_Emparray[j]->m_id > flag)j--;//从右找比标杆小的数
if (i <= j) {
//位置不匹配的元素,交换
swap(this->m_Emparray[i]->m_id, this->m_Emparray[j]->m_id);
swap(this->m_Emparray[i]->m_name, this->m_Emparray[j]->m_name);
swap(this->m_Emparray[i]->m_DeptId, this->m_Emparray[j]->m_DeptId);
i++;
j--;
}
} while (i <= j);
if (l < j)up_qssort(m_Emparray, l, j);
if (i < r)up_qssort(m_Emparray, i, r);
}
void low_qssort(Worker** m_Emparray, int l, int r) {
int i = l, j = r, flag = this->m_Emparray[l + (r - l) / 2]->m_id;//先选出标杆flag
do {
while (this->m_Emparray[i]->m_id > flag)i++;//从左找比标杆小的数
while (this->m_Emparray[j]->m_id < flag)j--;//从右找比标杆大的数
if (i <= j) {
//位置不匹配的元素,交换
swap(this->m_Emparray[i]->m_id, this->m_Emparray[j]->m_id);
swap(this->m_Emparray[i]->m_name, this->m_Emparray[j]->m_name);
swap(this->m_Emparray[i]->m_DeptId, this->m_Emparray[j]->m_DeptId);
i++;
j--;
}
} while (i <= j);
if (l < j)low_qssort(m_Emparray, l, j);
if (i < r)low_qssort(m_Emparray, i, r);
}
public:
//构造函数,三种情况,分别判断
workerManager() {
ifstream ifs;
ifs.open(myFILE, ios::in);//读文件
if (!ifs.is_open()) {
//情况一:文件未创建,即文件不存在
//初始化属性
cout << "-----------------------------------------------------" << endl;
cout << "提示:--!!!文件未创建,运行系统可自动生成!!! --" << endl;
cout << "-----------------------------------------------------" << endl;
this->m_EmpNum = 0; //人数为0
this->m_Emparray = NULL;//指针数组为空
this->m_FileIsEmpty = true;//初始化文件为空
ifs.close();
return;
}
//2.文件存在,但数据为空
char ch;
ifs >> ch;
if (ifs.eof()) {
cout << "--------------------------------------------------------" << endl;
cout << "提示:--!!!职工管理文件为空,等待添加数据信息!!!--" << endl;
cout << "--------------------------------------------------------" << endl;
this->m_EmpNum = 0; //人数为0
this->m_Emparray = NULL;//指针数组为空
this->m_FileIsEmpty = true;//初始化文件为空
ifs.close();
return;
}
//3.文件存在,数据存在时,需要我们进行一下数据的保存
int num = this->get_EmpNum();
cout << "--------------------------------------------------------" << endl;
cout << "提示:--《《《我公司现有的员工为:" << num << "位》》》 ---" << endl;
cout << "--------------------------------------------------------" << endl;
this->m_EmpNum = num;
this->m_Emparray = new Worker * [this->m_EmpNum];//开辟空间,将文件中的数据存到数组中
this->init_Emp();
}
void init_Emp() {
ifstream ifs;
ifs.open(myFILE, ios::in);
ui id;
string name;
string 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 {
worker = new Boss(id, name, dId);
}
this->m_Emparray[index++] = worker;
}
//关闭文件
ifs.close();
}
int get_EmpNum() {
ifstream ifs;
ifs.open(myFILE, ios::in);//读文件
ui id;
string name;
string dId;
int cnt = 0;
while (ifs >> id && ifs >> name && ifs >> dId) {
//统计人数
cnt++;
}
return cnt;
}
int Find_worker(int id) {
//遍历查找职工,返回其在数组中的位置,找不到则返回-1
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++) {
if (this->m_Emparray[i]->m_id == id) {
index = i;
break;
}
}
return index;
}
//-----------------------------------------------------------------------------------
//显示菜单
void Show_Menu() {
cout << " 《开始菜单》 " << endl;
cout << " ***********************************************" << endl;
cout << " ********** ---------------------- ********" << endl;
cout << " ********** 欢迎使用程序员管理系统 ********" << endl;
cout <&