基于控制台的职工管理系统
公司中职工分为三类:
普通员工
、 经理
、 老板
,显示信息时,需要显示 职工编号
、 职工姓名
、 职工岗位
、以及 职责
普通员工职责:完成经理交给的任务
经理职责:完成老板交给的任务,并下发给员工
老板职责:管理公司所有事务
管理系统中需要实现的功能如下:
退出管理程序
:退出当前管理系统增加职工信息
:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号显示职工信息
:显示公司内部所有职工的信息删除离职职工
:按照编号删除指定的职工修改职工信息
:按照编号修改职工的个人信息查找职工信息
:按照职工的编号或者职工的姓名进行查找相关的人员信息按照编号排序
:按照职工编号,进行排序,排序规则由用户指定清空所有文档
:清空文件中记录的所有职工信息(清空前需要再次确认、防止误删)
代码结构:
Main.cpp
#include <iostream>
#include "Manager.h"
using namespace std;int main()
{Manager manager;manager.init();while (true){std::cout << "************************************" << std::endl;std::cout << "职工管理系统" << std::endl;std::cout << "1:退出管理程序" << std::endl;std::cout << "2:添加职工信息" << std::endl;std::cout << "3:显示所有职工信息" << std::endl;std::cout << "4:删除离职员工" << std::endl;std::cout << "5:修改职工信息" << std::endl;std::cout << "6:查找职工信息" << std::endl;std::cout << "7:按照编号排序" << std::endl;std::cout << "8:清空所有文档" << std::endl;char select;std::cin >> select;switch (select){case '1': manager.exit(); break;case '2': manager.addStaff(); break;case '3': manager.showStaff(); break;case '4': {std::cout << "请选择要删除的用户编号" << std::endl;int num;std::cin >> num;manager.deleteStaff(num);break;}case '5': {std::cout << "请选择要更新的用户编号" << std::endl;int num;std::cin >> num;manager.updateStaff(num);break;}case '6': {std::cout << "请选择要查找的用户编号" << std::endl;int num;std::cin >> num;manager.findStaff(num);break;}case '7': {std::cout << "请选择排序方式: d:desc a:asc" << std::endl;char sort;std::cin >> sort;manager.sort(sort=='d'); break;}case '8': manager.clear(); break;default:break;}}manager.StoreToFile();
}
Manager.h
#pragma once
#include <iostream>
#include <string>
#include "Staff.h"
#include <fstream>
using namespace std;#define FILE_NAME "STAFF.DAT"class Manager
{
private://职员数量int empNumber;//职员列表Staff** staff_array;
public:Manager(){this->empNumber = 0;this->staff_array = NULL;}~Manager(){std::cout << "系统Manager类析构函数调用" << std::endl;if (this->staff_array != NULL){delete[] this->staff_array;this->staff_array = NULL;}}//启动时初始化方法void init();//存储到磁盘void StoreToFile();//加载文件void LoadFile();//退出系统void exit();//增加职工void addStaff();//显示职工信息void showStaff();//删除离职员工void deleteStaff(int staffId);//修改职工信息void updateStaff(int staffId);//查找职工信息void findStaff(int staffId);//查找职工信息void findStaff(string staffName);//按照编号排序职工void sort(bool desc);//清空所有职工信息void clear();
};
staff.h
#pragma once
#include <iostream>
#include <string>
using namespace std;class Staff
{
private://职工编号int m_Id;//职工姓名string m_Name;
public:~Staff();
public:int getId();string getName();void setId(int id);void setName(string name);void printfInfo();//获取岗位职责virtual string getDuty();//获取岗位名称virtual string getJobName();
};class ManageJob : public Staff
{
public:ManageJob(int id, string name){this->setId(id);this->setName(name);}string getDuty();string getJobName();
};class BossJob : public Staff
{
public:BossJob(int id, string name){this->setId(id);this->setName(name);}string getDuty();string getJobName();
};class OrdinaryJob : public Staff
{
public:OrdinaryJob(int id, string name){this->setId(id);this->setName(name);}string getDuty();string getJobName();
};
Manager.cpp
#include "Manager.h"//启动时初始化方法
void Manager::init()
{this->empNumber = 0;this->staff_array = NULL;this->LoadFile();
}
//退出系统
void Manager::exit()
{std::cout << "您确定退出系统吗? y:退出系统 " << std::endl;char input;std::cin >> input;if (input == 'y' || input == 'Y'){std::exit(0);}return;
}
//将所有的职工信息保存到文件中
void Manager::StoreToFile()
{ofstream ofs;ofs.open(FILE_NAME, ios::out);for (int i = 0; i < this->empNumber; i++){ofs << this->staff_array[i]->getId() << " "<< this->staff_array[i]->getName() << " " << this->staff_array[i]->getJobName() << std::endl;}ofs.close();
}
//初始化加载文件
void Manager::LoadFile()
{ifstream ifs;ifs.open(FILE_NAME, ios::in);if (!ifs.is_open()) {cout << "文件不存在" << std::endl;ifs.close();return;}char ch;ifs >> ch;if (ifs.eof()){//文件为空cout << "文件为空" << std::endl;ifs.close();return;}int id;string name;string job_name;int index = 0;while (ifs >> id && ifs >> job_name && ifs >> name ){index++;}ifs.close();ifs.open(FILE_NAME, ios::in);this->empNumber = index+1;Staff** newSpace = new Staff * [this->empNumber];int ix = 0;while (ifs >> id && ifs >> name && ifs >> job_name){Staff* staff = NULL;if (job_name == "总裁"){staff = new BossJob(id, name);}else if (job_name == "经理"){staff = new ManageJob(id, name);}else{staff = new OrdinaryJob(id, name);}newSpace[ix] = staff;ix++;}delete[] this->staff_array;this->staff_array = newSpace;
}
//增加职工
void Manager::addStaff()
{std::cout << "请输入您要添加的职员数量:" << std::endl;int addNum;std::cin >> addNum;if (addNum <= 0){return;}int new_size = this->empNumber + addNum;Staff** newSpace = new Staff * [new_size];//原有的数据放置到新的内存队列中if (this->staff_array != NULL){for (int i = 0; i < this->empNumber; i++){newSpace[i] = this->staff_array[i];}}//插入新数据for (int i = 0; i < addNum; i++){std::cout << "您正在添加第 " << i + 1 << " 个职员信息" << std::endl;std::cout << "请输入职员名称:";string name;std::cin >> name;std::cout << "请输入职员类型: 1.总裁 2.经理 3.员工";char input;std::cin >> input;Staff* staff = NULL;if (input == '1'){staff = new BossJob(this->empNumber + i + 1, name);}else if (input == '2'){staff = new ManageJob(this->empNumber + i + 1, name);}else if (input == '3'){staff = new OrdinaryJob(this->empNumber + i + 1, name);}else{std::cout << "您输入的选项不存在,本次添加跳过" << std::endl;continue;}//存储newSpace[this->empNumber + i] = staff;}delete[] this->staff_array;this->staff_array = newSpace;this->empNumber = new_size;std::cout << "成功添加 " << addNum << " 个员工信息" << std::endl;StoreToFile();
}
//显示职工信息
void Manager::showStaff()
{for (int i = 0; i < this->empNumber; i++){this->staff_array[i]->printfInfo();}
}
//删除离职员工
void Manager::deleteStaff(int staffId)
{int i = 0;for (; i < this->empNumber; i++){if (this->staff_array[i]->getId() == staffId){staff_array[i]->printfInfo();//删除,将所有的用户的后续编号前移//注意,这里的操作不符合实际情况,实际情况下id不允许更改for (int j = i; j < empNumber - 1; j++){staff_array[j] = staff_array[j+1];staff_array[j]->setId(staff_array[j]->getId() - 1);}this->empNumber = this->empNumber - 1;std::cout << "用户删除成功!" << std::endl;break;}}if (i >= this->empNumber){std::cout << "您要删除的用户不存在" << std::endl;}
}
//修改职工信息
void Manager::updateStaff(int staffId)
{int i = 0;for (; i < this->empNumber; i++){if (this->staff_array[i]->getId() == staffId){staff_array[i]->printfInfo();std::cout << "请输入您要修改的用户名称" << std::endl;string name;std::cin >> name;staff_array[i]->setName(name);}}if (i >= this->empNumber){std::cout << "您要修改的用户不存在" << std::endl;}
}
//查找职工信息
void Manager::findStaff(int staffId)
{int i = 0;for (; i < this->empNumber; i++){if (this->staff_array[i]->getId() == staffId){staff_array[i]->printfInfo();}}if (i > this->empNumber){std::cout << "您要查找的用户不存在" << std::endl;}
}
//查找职工信息
void Manager::findStaff(string staffName)
{int i = 0;for (; i < this->empNumber; i++){if (this->staff_array[i]->getName() == staffName){staff_array[i]->printfInfo();}}if (i > this->empNumber){std::cout << "您要查找的用户不存在" << std::endl;}
}
//按照编号排序职工
void Manager::sort(bool desc)
{if (desc){//根据id降序排列for (int i = this->empNumber-1; i >= 0; i--){this->staff_array[i]->printfInfo();}}else{//根据id升序排列this->showStaff();}
}
//清空所有职工信息
void Manager::clear()
{std::cout << "您确定要清除所有职工信息吗?? y:确认操作 " << std::endl;char input;std::cin >> input;if (input == 'y' || input == 'Y'){this->empNumber = 0;delete[] this->staff_array;this->staff_array = NULL;}
}
Staff.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "Staff.h"
using namespace std;Staff::~Staff()
{
}int Staff::getId()
{return this->m_Id;
}string Staff::getName()
{return this->m_Name;
}void Staff::setId(int id)
{this->m_Id = id;
}void Staff::setName(string name)
{this->m_Name = name;
}void Staff::printfInfo()
{std::cout << "职工编号:" << this->m_Id << "\t 职工姓名:" << this->m_Name << "\t 职工岗位:"<< this->getJobName() << "\t 岗位职责:" << this->getDuty() << std::endl;
}//获取岗位职责
string Staff::getDuty()
{return "";}//获取岗位名称
string Staff::getJobName()
{return "";
}string ManageJob::getDuty()
{return "完成老板交给的任务,并下发任务给员工";
}string ManageJob::getJobName()
{return "经理";
}string BossJob::getDuty()
{return "管理公司所有事务";
}string BossJob::getJobName()
{return "总裁";
}string OrdinaryJob::getDuty()
{return "完成经理交给的任务";
}string OrdinaryJob::getJobName()
{return "员工";
}
项目运行效果图: