该代码是此项目的初步实现,仍然存在很多不妥之处,欢迎各位读者提供自己的改进意见!
我会结合大家的已经对项目进行改进,做出更好的项目!
项目简介:
首先,程序读取文件,会加载上次操作后的数据到内存区
其次,我们可以选择增加员工信息、显示员工信息、删除离职员工、修改职工信息、查找职工信息、按编号进行排序、清空、退出
直接上代码:
首先是实现这个主要功能的类:
workerManager.h
#pragma once //防止头文件重复包含
#include<iostream> //输入输出流
#include<vector>
#include<fstream>
#include"worker.h"
#include"boss.h"
#include"employee.h"
#include"manager.h"
using namespace std;
#define FILENAME "empFile.txt"
//管理者类
class WorkerManager {
public:
WorkerManager();
WorkerManager(int worker_num);
//显示菜单
void showMenu();
//添加职工
void addEmp();
//保存文件
void save();
//统计文件中的人数
int getWorkerNum();
//初始化员工
void initWorker();
//显示职工
void showEmp();
//删除职工
void delEmp(int id);
//判断职工是否存在
int isExist(int id);
//修改职工
void modEmp();
//查找职工
void findWorker();
//按编号排序
void sortById();
//清空
void clearFile();
~WorkerManager();
private:
bool is_file_empty = false; //文件是否为空
int worker_num; //记录职工人数
vector<Worker*> m_EmpArr; //职工数组
};
对应的函数实现:
workerManager.cpp
#include"workerManager.h"
WorkerManager::WorkerManager() {
//程序初始化,首先加载文件
ifstream ifs(FILENAME, ios::in);
//如果文件不存在
if (!ifs.is_open()) {
cout << "文件不存在" << endl;
//初始化属性
this->worker_num = 0;
this->is_file_empty = true;
} else {
//文件存在,判断文件是否为空
char ch;
ifs >> ch;
//通过读取第一个字符判断
if (ifs.eof()) {
cout << "文件为空" << endl;
this->worker_num = 0;
this->is_file_empty = true;
} else {
cout << "文件加载成功!" << endl;
int num = this->getWorkerNum();
cout << "职工人数:" << num << endl;
//将文件数据加载到数组中
this->initWorker();
//遍历打印
for (int i = 0; i < this->worker_num; i++) {
cout << "职工编号:" << this->m_EmpArr[i]->getWorkerId() << "\t"
<< "姓名:" << this->m_EmpArr[i]->getWorkerName() << "\t"
<< "部门编号:" << this->m_EmpArr[i]->getDeptId() << endl;
}
}
}
ifs.close();
}
WorkerManager::WorkerManager(int worker_num){
}
void WorkerManager::showMenu() {
cout << "***********************************************************************" << endl;
cout << "************************** 职工管理系统 v1.0 ***********************" << endl;
cout << "**************************** 1.增加职工信息 *************************" << endl;
cout << "**************************** 2.显示职工信息 *************************" << endl;
cout << "**************************** 3.删除离职职工 *************************" << endl;
cout << "**************************** 4.修改职工信息 *************************" << endl;
cout << "**************************** 5.查找职工信息 *************************" << endl;
cout << "**************************** 6.按照编号排序 *************************" << endl;
cout << "**************************** 7.清空所有文档 *************************" << endl;
cout << "**************************** 0.退出管理系统 *************************" << endl;
cout << "***********************************************************************" << endl << endl;
cout << " 请输入选择[ ]\b\b";
}
void WorkerManager::addEmp(){
int n;
cout << "请输入要添加的职工的数量:" << endl;
cin >> n;
if (n > 0) {
this->worker_num += n;
//遍历添加职工信息
for (int i = 0; i < n; i++) {
Worker* w; //创建职工对象
int id;
string name;
int deptId;
int pos_choice;
cout << "请选择职工岗位:" << endl;
cout << "1.老板" << endl;
cout << "2.经理" << endl;
cout << "3.普通员工" << endl;
cin >> pos_choice;
cout << "请输入第" << i + 1 << "个新职工的编号:";
cin >> id;
cout << "请输入第" << i + 1 << "个新职工的姓名:";
cin >> name;
cout << "请输入第" << i + 1 << "个新职工的部门编号:";
cin >> deptId;
//利用多态new相应的职工
switch (pos_choice) {
case 1:
w = new Boss(id, name, deptId);
break;
case 2:
w = new Manager(id, name, deptId);
break;
case 3:
w = new Employee(id, name, deptId);
break;
default:
break;
}
//将创建的职工指针保存到数组中
m_EmpArr.push_back(w);
}
cout << "已成功添加" << n << "名新职工" << endl;
this->is_file_empty = false;
this->save();
} else {
return;
}
system("pause");
}
void WorkerManager::save() {
ofstream ofs;
ofs.open(FILENAME, ios::out);
//遍历写入文件
for (int i = 0; i < this->worker_num; i++) {
ofs << this->m_EmpArr[i]->getWorkerId() << " "
<< this->m_EmpArr[i]->getWorkerName() << " "
<< this->m_EmpArr[i]->getDeptId() << endl;
}
//关闭文件
ofs.close();
}
int WorkerManager::getWorkerNum() {
ifstream ifs(FILENAME, ios::in);
int id;
string name;
int deptId;
int num = 0; //统计人数
while (ifs >> id && ifs >> name && ifs >> deptId) {
num++;
}
return num;
}
void WorkerManager::initWorker(){
ifstream ifs(FILENAME, ios::in);
int id;
string name;
int deptId;
while (ifs >> id && ifs >> name && ifs >> deptId) {
Worker* w;
if (deptId == 1) {
w = new Boss(id, name, deptId);
} else if (deptId == 2) {
w = new Manager(id, name, deptId);
} else {
w = new Employee(id, name, deptId);
}
this->m_EmpArr.push_back(w);
this->worker_num++;
}
ifs.close();
}
void WorkerManager::showEmp(){
if (this->worker_num > 0) {
for (int i = 0; i < worker_num; i++) {
this->m_EmpArr[i]->showInfo();
}
} else {
cout << "文件为空或不存在" << endl;
}
system("pause");
}
void WorkerManager::delEmp(int id) {
//判断职工是否存在
if (isExist(id) == -1) {
cout << "职工不存在!" << endl;
} else {
//删除操作
for (vector<Worker*>::iterator it = m_EmpArr.begin(); it != m_EmpArr.end(); it++) {
if ((*it)->getWorkerId() == id) {
//数组中删除
this->m_EmpArr.erase(it);
this->worker_num--;
//更新到文件中
this->save();
cout << "成功删除!" << endl;
break;
}
}
}
system("pause");
return;
}
int WorkerManager::isExist(int id) {
int index = -1;
for (int i = 0; i < this->worker_num; i++) {
if (this->m_EmpArr[i]->getWorkerId() == id) {
index = i;
break;
}
}
return index;
}
void WorkerManager::modEmp() {
if (this->getWorkerNum() <= 0) {
cout << "文件为空或不存在!" << endl;
} else {
int id;
cout << "请输入要删除的职工编号:";
cin >> id;
int res = this->isExist(id);
if (res == -1) {
cout << "该职工不存在!" << endl;
return;
}
//新职工的信息
int newId = 0;
string newName = "";
int newDeptId = 0;
cout << "查找到" << this->m_EmpArr[res]->getWorkerId() << "号职工,请输入新的职工号" << endl;
cin >> newId;
cout << "请输入姓名" << endl;
cin >> newName;
while (newDeptId > 3 || newDeptId <= 0) {
cout << "请输入部门编号" << endl;
cin >> newDeptId;
}
//创建新的对象
Worker* w, *p = this->m_EmpArr[res];
if (newDeptId == 1) {
w = new Boss(newId, newName, newDeptId);
} else if (newDeptId == 2) {
w = new Manager(newId, newName, newDeptId);
} else {
w = new Employee(newId, newName, newDeptId);
}
this->m_EmpArr[res] = w; //更新数组
delete p; //释放空间
cout << "修改信息成功!" << endl;
}
system("pause");
}
void WorkerManager::findWorker(){
int id;
cout << "请输入要查找的职工的编号:";
cin >> id;
int res = this->isExist(id);
if (res == -1) cout << "该职工不存在" << endl;
else this->m_EmpArr[res]->showInfo();
system("pause");
}
void WorkerManager::sortById() {
if (this->is_file_empty) {
cout << "文件为空!" << endl;
} else {
for (int i = 0; i < this->worker_num - 1; i++) {
for (int j = i; j < this->worker_num - i - 1; j++) {
if (this->m_EmpArr[j]->getWorkerId() > this->m_EmpArr[j + 1]->getWorkerId()) {
Worker* p = this->m_EmpArr[j];
this->m_EmpArr[j] = this->m_EmpArr[j + 1];
this->m_EmpArr[j + 1] = p;
}
}
}
cout << "排序成功!" << endl;
}
system("pause");
}
void WorkerManager::clearFile() {
cout << "确定清空吗?" << endl;
cout << "1.确定" << endl;
cout << "2.返回" << endl;
int choice;
cin >> choice;
if (choice == 1) {
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
for (int i = 0; i < this->worker_num; i++) {
//释放每一个空间
delete this->m_EmpArr[i];
this->m_EmpArr[i] = NULL;
}
this->m_EmpArr.clear();
this->worker_num = 0;
this->is_file_empty = true;
cout << "清空成功!" << endl;
}
system("pause");
}
WorkerManager::~WorkerManager() {
}
其次,职工分为普通员工、经理和老板,都继承自抽象类worker
worker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
//职工类
class Worker {
public:
//显示个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
//获取id
int getWorkerId();
//获取name
string getWorkerName();
//获取deptId
int getDeptId();
protected:
int worker_id;
string worker_name;
int department_id;
};
worker.cpp
#include "worker.h"
int Worker::getWorkerId(){
return this->worker_id;
}
string Worker::getWorkerName(){
return string(worker_name);
}
int Worker::getDeptId(){
return department_id;
}
下面是三种职工分别各自的类,继承了上面的worker,对其中的函数进行了重写
employee.h
#pragma once
#include<iostream>
#include<string>
#include"worker.h"
using namespace std;
//职工类
class Employee : public Worker {
public:
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
Employee(int id, string name, int deptId);
Employee();
};
employee.cpp
#include "employee.h"
void Employee::showInfo(){
cout << "职工编号:" << this->worker_id << "\t";
cout << "职工姓名:" << this->worker_name << "\t";
cout << "岗位:" << this->getDeptName() << "\t";
cout << "岗位职责:完成经理交给的任务" << endl;
}
string Employee::getDeptName(){
return string("员工");
}
Employee::Employee(int id, string name, int deptId){
this->worker_id = id;
this->worker_name = name;
this->department_id = deptId;
}
Employee::Employee(){
}
manager.h
#pragma once
#include<iostream>
#include "worker.h"
using namespace std;
class Manager : public Worker {
public:
Manager() = default;
Manager(int id, string name, int deptId);
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
};
manager.cpp
#include "manager.h"
Manager::Manager(int id, string name, int deptId){
this->worker_id = id;
this->worker_name = name;
this->department_id = deptId;
}
void Manager::showInfo(){
cout << "职工编号:" << this->worker_id << "\t";
cout << "职工姓名:" << this->worker_name << "\t";
cout << "岗位:" << this->getDeptName() << "\t";
cout << "岗位职责:完成老板交给的任务,并下发给普通的员工" << endl;
}
string Manager::getDeptName(){
return string("经理");
}
boss.h
#pragma once
#include<iostream>
#include "Worker.h"
using namespace std;
class Boss : public Worker {
public:
Boss() = default;
Boss(int id, string name, int deptId);
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
};
boss.cpp
#include "boss.h"
Boss::Boss(int id, string name, int deptId){
this->worker_id = id;
this->worker_name = name;
this->department_id = deptId;
}
void Boss::showInfo(){
cout << "职工编号:" << this->worker_id << "\t";
cout << "职工姓名:" << this->worker_name << "\t";
cout << "岗位:" << this->getDeptName() << "\t";
cout << "岗位职责:管理公司所有事务" << endl;
}
string Boss::getDeptName(){
return string("老板");
}
最后,主函数main.cpp
#include<iostream>
#include"workerManager.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
using namespace std;
int main() {
//Worker* w = new Employee(1, "张三", 1001);
//Worker* w2 = new Manager(2, "李四", 1002);
//Worker* w3 = new Boss(3, "王五", 1003);
//w->showInfo();
//w2->showInfo();
//w3->showInfo();
//delete w, w2, w3;
int choice = 0; //存储用户的选择项
WorkerManager wm; //实例化管理者对象
while (1) {
wm.showMenu(); //展示菜单
cin >> choice; //接收用户的选择
switch (choice) {
case 0: //退出系统
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
break;
case 1: //增加职工
wm.addEmp();
break;
case 2: //显示职工
wm.showEmp();
break;
case 3: //删除职工
{
int id;
cout << "请输入要删除的职工的编号:" << endl;
cin >> id;
wm.delEmp(id);
}
break;
case 4: //修改职工
wm.modEmp();
break;
case 5: //查找职工
wm.findWorker();
break;
case 6: //排序
wm.sortById();
break;
case 7: //清空文档
wm.clearFile();
break;
default:
system("cls");
break;
}
system("cls");
}
system("pause");
return 0;
}