1.职工管理系统的需求
本教程主要利用C++实现一个职工管理系统
公司职工分类:普通员工,经理,老板,显示信息时,需要显示职工的编号,职工姓名,职工岗位,以及职责。
普通员工的职责:完成经理交给的任务。
经理职责:完成老板交给的任务,并下发任务给员工。
老板职责:管理公司所有事物。
程序 | 功能 |
---|---|
退出管理程序 | 退出当前管理系统 |
增加职工信息 | 实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号,姓名,部门编号 |
显示职工信息 | 显示公司内部所有职工信息 |
删除离职职工 | 按照编号删除指定的职工 |
修改职工信息 | 按照职工的编号或者职工的姓名查找相关的人员信息 |
按照编号排序 | 按照职工编号,进行排序,排序规则由用户指定 |
清空所有文档 | 清空文件中记录的所有职工信息 |
2.功能实现
2.1创建管理类
class Worker_Manger { public: Worker_Manger(); //展示菜单 void Dispaly() ; //退出系统 void ExitSystem(); //记录职工的人数 int M_EmpNum=0; //职工数组指针 Worker ** M_EmpArray=0; //添加职工 void Add_Emp(); //判断是否编号重复,姓名可能相同 bool If_Id_Repeat(int id); //保存文件 void Save(); //判断文件是否为空 bool M_FileEmpty=0; //统计文件中的人数 int Get_EmpNum(); //初始化员工 void init_Emp(); //显示职工 void Show_Emp(); //删除职工 void Delete_Emp(); //判断是否存在要查找的职工 int IsExit_Id(int id);//Id查找方式 int IsExit_Name(string name);//Name查找方式 string IsExit_Id_Name_choice(int cho); //查找联系人 void Find_Emp_Name(); void Find_Emp_Id(); void Find_Emp(); //修改职工信息 void Modify(); //编号排序 void Sort_Emp(); //清空操作 void Clean_File(); //析构函数 ~Worker_Manger(); };
2.2退出功能
void Worker_Manger::ExitSystem() { cout << "欢迎下次使用" << endl; system("pause"); exit(0); }
2.3增加联系人信息
oid Worker_Manger::Add_Emp() { cout << "请输入添加的职工数量:" << endl; int addnum = 0;//保存用户的输入数量 cin >> addnum; if (addnum > 0) { int newSize = this->M_EmpNum + addnum;//新空间人数=原来的人数+新增加人数 //开辟新空间 Worker ** newspace = new Worker * [newSize+4]; //拷贝原来空间的数据 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; //If_Id_Repeat(id); while (If_Id_Repeat(id)) { int cho = IsExit_Id(id);//Id查找方式 cout << "通讯录已经有相同的编号,请重新输入" << endl; this->M_EmpArray[cho]->Showinformation(); 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, 1); break; case 2: worker = new Manager(id, name, 2); break; case 3: worker = new Boss(id, name, 3); break; default: break; } //将创建职工职责,保存到数组中 newspace[this->M_EmpNum + i] = worker; } //释放空间 delete[] this->M_EmpArray; //更改新空间的指向 this->M_EmpArray = newspace; //更新新空间的人数 this->M_EmpNum = newSize; //更新职工不为空的情况标志 this->M_FileEmpty = false; //提示添加成功 cout << "成功添加" << addnum << "名新职工" << endl; //保存数据 this->Save(); } else { cout << "输入有误" << endl; } system("pause"); system("cls"); }
2.4显示职工信息
void Worker_Manger::Show_Emp() { //判断文件是否为空 if (this->M_FileEmpty) { cout << "文件不存在或文件为空!" << endl; } else { for (int i = 0; i < M_EmpNum; i++) { //利用多态 this->M_EmpArray[i]->Showinformation(); } } //按任意键清屏 system("pause"); system("cls"); //Name查找方式 int Worker_Manger::IsExit_Name(string name) { int index = -1; for (int i = 0; i < this->M_EmpNum; i++) { if (this->M_EmpArray[i]->M_name == name) { //找到 index = i; break; } } return index; } //Id查找方式 int Worker_Manger::IsExit_Id(int id) { 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; } }
2.5删除离职职工
void Worker_Manger::Delete_Emp() { if (this->M_FileEmpty) { cout << "文件不存在或者为空" << endl; } else { int choose=0; cout << "请选择通过方式几查找要删除的联系人" << endl; cout << "1.通过姓名查找要删除的联系人" << endl; cout << "2.通过编号查找要删除的联系人" << endl; cin >> choose; switch (choose) { case 1: { cout << "请输入要删除职工的姓名" << endl; string name; cin >> name; int index = this->IsExit_Name(name); if (index != -1) { string ch = IsExit_Id_Name_choice(index); if (ch == "是") { for (int i = index; 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; break; } } else { cout << "未找到此人,删除失败!" << endl; break; } } case 2: { cout << "请输入要删除职工的编号" << endl; int id=0; cin >>id; int index=this->IsExit_Id(id); if (index != -1) { string ch= IsExit_Id_Name_choice(index); if (ch == "是") { for (int i = index; i < this->M_EmpNum - 1; i++) { this->M_EmpArray[i] = this->M_EmpArray[i + 1]; } this->M_EmpNum--; //数据同步更新文件当中 this->Save(); cout << "删除成功!" << endl; } else { break; } } else { cout << "未找到此人,删除失败!" << endl; break; } } } } //按照任意键返回 system("pause"); system("cls"); }
2.6修改职工信息
void Worker_Manger::Modify() { if (this->M_FileEmpty) { cout << "文件不存在或记录为空" << endl; } else { int choose = 0; cout << "请选择对应方式来查找要修改的联系人" << endl; cout << "1.通过姓名查找要修改的联系人" << endl; cout << "2.通过编号查找要修改的联系人" << endl; cin >> choose; switch (choose) { case 1: { cout << "请输入修改的职工的姓名:" << endl; string m_name = " "; cin >> m_name; int ret1 = this->IsExit_Name(m_name); if (ret1 != -1) { string str1 = IsExit_Id_Name_choice(ret1); if (str1 == "是") { //查找到编号的职工 delete this->M_EmpArray[ret1]; int newId = 0; string newName = " "; int dSelect = 0; cout << "输入新的职工编号" << endl; cin >> newId; If_Id_Repeat(newId); while (If_Id_Repeat(newId)) { int cho= IsExit_Id(newId);//Id查找方式 cout << "通讯录已经有相同的编号,请重新输入" << endl; this->M_EmpArray[cho]->Showinformation(); //cout << "职工编号:" << this->M_EmpArray[cho]->M_id // << "\t职工姓名:" << this->M_EmpArray[cho]->M_name // << "\t岗位名称:" << this->M_EmpArray[cho]->GetDeptname() // << "\t岗位职责:" << this->M_EmpArray[cho]->GetDeptduty() // << endl; cin >> newId; } cout << "输入新的姓名" << endl; cin >> newName; cout << "请输入新的岗位" << endl; cout << "1.职员" << endl; cout << "2.经理" << endl; cout << "3.总裁" << endl; cin >> dSelect; Worker* worker = NULL; switch (dSelect) { case 1: worker = new Employee(newId, newName, 1); break; case 2: worker = new Manager(newId, newName, 2); break; case 3: worker = new Boss(newId, newName, 3); break; default: break; } this->M_EmpArray[ret1] = worker; cout << "更改信息成功" << endl; //保存到文件 this->Save(); } else if(str1 == "否") { cout << "取消成功!" << endl; } } else { cout << "修改失败,查无此人!" << endl; } break; } case 2: { cout << "请输入修改的职工的编号:" << endl; int id; cin >> id; int ret2 = this->IsExit_Id(id); if (ret2 != -1) { string str2 = IsExit_Id_Name_choice(ret2); //查找到编号的职工 if (str2 == "是") { delete this->M_EmpArray[ret2]; int newId = 0; string newName = " "; int dSelect = 0; cout << "输入新的职工编号" << endl; cin >> newId; If_Id_Repeat(newId); while (If_Id_Repeat(newId)) { int cho = IsExit_Id(newId);//Id查找方式 cout << "通讯录已经有相同的编号,请重新输入" << endl; this->M_EmpArray[cho]->Showinformation(); //cout << "职工编号:" << this->M_EmpArray[cho]->M_id // << "\t职工姓名:" << this->M_EmpArray[cho]->M_name // << "\t岗位名称:" << this->M_EmpArray[cho]->GetDeptname() // << "\t岗位职责:" << this->M_EmpArray[cho]->GetDeptduty() // << endl; cin >> newId; } cout << "输入新的姓名" << endl; cin >> newName; cout << "请输入新的岗位" << endl; cout << "1.职员" << endl; cout << "2.经理" << endl; cout << "3.总裁" << endl; cin >> dSelect; Worker* worker = NULL; switch (dSelect) { case 1: worker = new Employee(newId, newName, 1); break; case 2: worker = new Manager(newId, newName, 2); break; case 3: worker = new Boss(newId, newName, 3); break; default: break; } this->M_EmpArray[ret2] = worker; cout << "更改信息成功" << endl; //保存到文件 this->Save(); } else if (str2 == "否") { cout << "取消成功!" << endl; } } else { cout << "修改失败,查无此人!" << endl; } break; } } system("pause"); system("cls"); } }
2.7查找职工信息
void Worker_Manger::Find_Emp() { cout << "请选择查找方式" << endl; cout << "1.通过姓名查找联系人" << endl; cout << "2.通过编号查找联系人" << endl; int choose = 0; cin >> choose; switch (choose) { case 1:Find_Emp_Name(); break; case 2:Find_Emp_Id(); break; default: break; } }
2.8按照编号排序
void Worker_Manger::Sort_Emp() { if (this->M_FileEmpty) { cout << "文件不存在或记录为空" << endl; system("pause"); system("cls"); } cout << "选择排序的方式" << endl; cout << "1.按照编号升序排列" << endl; cout << "2.按照编号进行降序排列" << endl; int choose = 0; cin >> choose; switch (choose) { case 1: { for (int i = 0; i < this->M_EmpNum; i++) { int MIN=i; for (int j = i+1; j < this->M_EmpNum; j++) { if (this->M_EmpArray[MIN]->M_id>this->M_EmpArray[j]->M_id) { MIN = j; } } if (i != MIN) { Worker* temp = M_EmpArray[i]; M_EmpArray[i] = M_EmpArray[MIN]; M_EmpArray[MIN] = temp; } } this->Save(); this->Show_Emp(); break; } case 2: { for (int i = 0; i < this->M_EmpNum; i++) { int MAX = i; for (int j = i + 1; j <this->M_EmpNum; j++) { if (this->M_EmpArray[MAX]->M_id < this->M_EmpArray[j]->M_id) { MAX = j; } } if (i != MAX) { Worker* temp = M_EmpArray[i]; M_EmpArray[i] = M_EmpArray[MAX]; M_EmpArray[MAX] = temp; } } this->Save(); this->Show_Emp(); break; } default: break; } }
2.9清空所有文档
void Worker_Manger::Clean_File() { 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->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 = 0; this->M_EmpArray = NULL; this->M_FileEmpty = true; } cout << "清空成功" << endl; } system("pause"); system("cls"); }
3.代码下载
链接:
提取码: 65qj
您可能感兴趣的文章: