【项目】职工管理系统

职工管理系统需求

职工管理系统可以用来管理公司内所有员工的信息,本篇文章主要利用C++来实现一个基于多态的职工管理系统。

公司中职工分为三类:普通员工,经理、老板,显示信息时,需要显示职工编号,职工姓名,职工岗位,以及职责。

  • 普通员工职责:完成经理交给的任务
  • 经理职责:完成老板交给的任务,并下发任务给员工
  • 老板职责:管理公司所有事务

管理系统中需要实现的功能如下:

  • 退出管理程序:退出当前管理系统
  • 增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为职工编号、姓名、部门编号
  • 显示职工信息:显示公司内部所有职工信息
  • 删除离职职工:按照编号删除指定的职工
  • 修改职工信息:按照编号修改职工个人信息
  • 查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息
  • 按照编号排序:按照职工编号,进行排序,排序规则由用户指定
  • 清空所有文档:清空文件中记录的所有职工信息(清空前需要再次确认,防止误判)

各文件代码

首先我们看一下这个系统的.h文件和.cpp文件都有哪些:

在这里插入图片描述

  • main.cpp文件

在这里插入图片描述

#include <iostream>
#include "workerManager.h"
#include "employee.h"
#include "worker.h"
#include "manager.h"
#include "boss.h"
using namespace std;

int main()
{
	//实例化管理者对象
	WorkerManager wm;

	int choice = 0;//用来存储用户的选项

	while (1)
	{
		//调用显示成员函数
		wm.Show_Menu();
		cout << "请输入您的选择:" << endl;

		cin >> choice;//用来接收用户的选择

		switch (choice)
		{
		case 0://退出系统
			wm.ExitSystem();
			break;
		case 1://增加职工信息
			wm.Add_Emp();
			break;
		case 2://显示所有职工信息
			wm.Show_Emp();
			break;
		case 3://删除职工信息
			wm.Del_Emp();
			break;
		case 4://修改职工
			wm.Mod_Emp();
			break;
		case 5://查找职工
			wm.find_Emp();
			break;
		case 6://对职工进行排序
			wm.Sort_Emp();
			break;
		case 7://清空文档
			wm.Cleam_Emp();
			break;
		default:
			system("cls");//清屏
			break;
		}
	}
	system("pause");
	return 0;
}

  • work.h文件
#pragma once//防止头文件重复包含
#include <iostream>
#include <string>
using namespace std;


//职工抽象类
//职工分为普通职工、经理和老板三种,具体的在子类中实现
class Worker
{
public:

	//显示个人信息
	virtual void ShowInfo() = 0;

	//获取岗位名称
	virtual string GetDeptName() = 0;

	//职工编号
	int m_id;

	//职工姓名
	string m_name;

	//部门编号
	int m_dept_id;
};
  • boss.h文件

在这里插入图片描述

#pragma once
#include <iostream>
#include "worker.h"
using namespace std;

//老板类
class Boss :public Worker
{
public:

	//构造函数
	Boss(int id, string name, int did);

	//显示信息
	void ShowInfo();

	//获取岗位职责
	string GetDeptName();
};
  • boss.cpp文件

在这里插入图片描述

#include "boss.h"

//构造函数
Boss::Boss(int id, string name, int did)
{
	this->m_id = id;
	this->m_name = name;
	this->m_dept_id = did;
}

//显示信息
void Boss::ShowInfo()
{
	cout << "职工编号:" << this->m_id
		<< "\t职工姓名:" << this->m_name
		<< "\t岗位:" << this->GetDeptName()
		<< "\t岗位职责:管理公司所有事务"
		<< endl;
}

//获取岗位职责
string Boss::GetDeptName()
{
	return string("老板");
}
  • employee.h文件

在这里插入图片描述

//普通员工文件
#pragma once
#include <iostream>
#include "worker.h"
using namespace std;

class Employee :public Worker
{
public:
	//构造函数
	Employee(int id, string name, int did);

	//显示个人信息
	void ShowInfo();

	//获取岗位名称
	string GetDeptName();
};
  • employee.cpp文件
#include "employee.h"

//构造函数
Employee::Employee(int id, string name, int did)
{
	this->m_id = id;
	this->m_name = name;
	this->m_dept_id = did;
}

//显示个人信息
void Employee::ShowInfo()
{
	cout << "职工编号:" << this->m_id
		<< "\t职工姓名:" << this->m_name
		<< "\t岗位:" << this->GetDeptName()
		<<"\t岗位职责:完成经理交给的任务"
		<< endl;
}

//获取岗位名称
string Employee::GetDeptName()
{
	return string("普通员工");
}
  • manager.h文件
#pragma once
#include <iostream>
#include "worker.h"
using namespace std;


//经理类
class Manager :public Worker
{
public:

	//构造函数
	Manager(int id, string name, int did);

	//显示个人信息
	void ShowInfo();

	//获取岗位名称
	string GetDeptName();
};

  • manager.cpp文件
#include "manager.h"

//构造函数
Manager::Manager(int id, string name, int did)
{
	this->m_id = id;
	this->m_name = name;
	this->m_dept_id = did;
}

//显示个人信息
void Manager::ShowInfo()
{
	cout << "职工编号:" << this->m_id
		<< "\t职工姓名:" << this->m_name
		<< "\t岗位:" << this->GetDeptName()
		<< "\t岗位职责:完成老板交给的任务,并下发任务给员工"
		<< endl;
}

//获取岗位名称
string Manager::GetDeptName()
{
	return string("经理");
}
  • workerManager.h文件
#pragma once//防止头文件重复包含
#include <iostream>
#include "worker.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
#include <fstream>
using namespace std;

#define FILENAME "empfile.txt"

//提供一个管理类,主要作用
//1、与用户进行沟通的菜单界面 
//2、进行增删改查操作 
//3、进行文件读写交互
class WorkerManager
{
public:

	//构造函数
	WorkerManager();

	//显示菜单界面
	void Show_Menu();

	//退出系统
	void ExitSystem();

	//添加职工函数
	void Add_Emp();

	//析构函数
	~WorkerManager();

	//保存文件
	void save();

	//获取记录的职工人数
	int get_EmpNum();


	//职工数据初始化
	void Init_Emp();

	//显示职工信息
	void Show_Emp();

	//判断职工是否存在函数
	int isExist(int id);

	//删除职工函数
	void Del_Emp();

	//修改职工信息函数
	void Mod_Emp();

	//查找职工函数
	void find_Emp();

	//对职工进行排序函数
	void Sort_Emp();

	//清空文件操作
	void Cleam_Emp();

	//判断文件是否为空的标志
	bool m_FileIsEmpty;

	//记录职工人数
	int m_EmpNum;

	//职工数组指针
	Worker** m_EmpArray;
};
  • workermanager.cpp文件
#include "workerManager.h"

//构造函数
WorkerManager::WorkerManager()
{

	//文件不存在
	ifstream ifs;
	ifs.open(FILENAME, ios::in);//读文件
	if (!ifs.is_open())
	{
		cout << "文件不存在" << endl;
		//初始化属性
		//初始化记录人数
		this->m_EmpNum = 0;
		//初始化指针
		this->m_EmpArray = NULL;

		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//文件存在并且里面没有记录的情况
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//文件为空
		cout << "文件为空" << endl;
		//初始化属性
		//初始化记录人数
		this->m_EmpNum = 0;
		//初始化指针
		this->m_EmpArray = NULL;

		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//文件存在且记录数据
	int num = this->get_EmpNum();

	//cout << "职工的人数为:" << num << endl;
	this->m_EmpNum = num;

	//开辟空间
	this->m_EmpArray = new Worker * [this->m_EmpNum];
	//将文件中的数据存到数组中
	this->Init_Emp();
}

//保存文件函数
void WorkerManager::save()
{
	ofstream ofs;
	ofs.open(FILENAME, 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_dept_id << endl;
	}

	//关闭文件
	ofs.close();
}

//析构函数
WorkerManager::~WorkerManager()
{
	if (this->m_EmpArray != NULL)
	{
		delete[]this->m_EmpArray;
		this->m_EmpArray = NULL;
	}
}


//显示菜单函数
void WorkerManager::Show_Menu()
{
	cout << "**********************************************************" << endl;
	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;
	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->m_EmpNum + addNum;

		//开辟新空间
		Worker** newSpace = new Worker * [newSize];

		//将原来空间下的数据拷贝到新空间下
		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;

			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_FileIsEmpty = false;

		//将添加的职工保存到文件中做持久化
		save();

		//提示添加成功
		cout << "成功添加" << addNum << "名新职工!" << endl;

	}
	else
	{
		cout << "输入数据有误!!" << endl;
	}

	//按任意键继续后,清屏回到上级目录
	system("pause");
	system("cls");
}

//统计文件中的人数
int WorkerManager::get_EmpNum()
{
	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::Init_Emp()
{
	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, 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();//关闭文件
}

//显示职工信息函数
void WorkerManager::Show_Emp()
{
	//判断文件是否存在
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者记录为空!!!" << endl;
	}
	else
	{
		for (int i = 0; i < m_EmpNum; i++)
		{
			//利用多态调用程序接口
			this->m_EmpArray[i]->ShowInfo();
		}
	}

	//请按任意键继续
	system("pause");
	system("cls");//清屏
}

//判断职工是否存在函数
int WorkerManager::isExist(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;
}

//按照职工编号删除职工信息函数
void WorkerManager::Del_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者记录为空!!!" << endl;
	}
	else
	{
		//按照职工编号进行删除职工
		cout << "请输入想要删除职工的编号:" << endl;
		int id = 0;
		cin >> id;

		int index = this->isExist(id);
		if (index != -1)//说明职工存在并且要删除index位置上的职工
		{
			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;
		}
	}

	system("pause");
	system("cls");
}

//修改职工信息函数
void WorkerManager::Mod_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者记录为空!!" << endl;
	}
	else
	{
		cout << "请输入所要修改职工的编号:" << endl;
		int id;
		cin >> id;

		int ret = this->isExist(id);
		if (ret != -1)
		{
			//查找到了该编号的职工
			delete this->m_EmpArray[ret];

			int newid = 0;
			string newname = "";
			int dselect = 0;

			cout << "查到编号为" << id << "的职工" << endl;
			cout << "请输入新的职工编号:" << 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, dselect);
				break;
			case 2:
				worker = new Manager(newid, newname, dselect);
				break;
			case 3:
				worker = new Boss(newid, newname, dselect);
				break;
			default:
				break;
			}

			//更新数据到数组中
			this->m_EmpArray[ret] = worker;
			cout << "修改成功!!" << endl;

			//将数据保存到文件中
			this->save();
		}
		else
		{
			cout << "没有查找到你所要修改职工的编号!!!" << endl;
		}
	}

	system("pause");
	system("cls");
}

//查找职工函数
void WorkerManager::find_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者记录为空" << endl;
	}
	else
	{
		cout << "请输入查找方法:" << endl;
		cout << "1-按照职工编号进行查找" << endl;
		cout << "2-按照职工姓名进行查找" << endl;

		int select = 0;
		cin >> select;

		if (select == 1)//按照编号查找
		{
			int id;
			cout << "请输入查找的编号:" << endl;
			cin >> id;

			int ret = this->isExist(id);
			if (ret != -1)
			{
				cout << "查找成功!该职工的信息如下:" << endl;
				this->m_EmpArray[ret]->ShowInfo();
			}
			else
			{
				cout << "查找失败!查无此人" << endl;
			}
		}
		else if (select == 2)//按照姓名查找
		{
			string name;
			cout << "请输入查找的职工姓名:" << endl;
			cin >> name;

			//加入判断是否查到的标志
			bool flag = false; //未找到该职工

			for (int i = 0; i < m_EmpNum; i++)
			{
				if (this->m_EmpArray[i]->m_name == name)
				{
					cout << "查找成功!!该职工信息如下:" << endl;

					flag = true;
					this->m_EmpArray[i]->ShowInfo();
				}
			}

			if (flag == false)
			{
				cout << "查找失败,查无此人" << endl;
			}
		}
		else
		{
			cout << "输入选项有误!!" << endl;
		}
	}

	//按任意键清屏
	system("pause");
	system("cls");
}

//对职工进行排序函数
void WorkerManager::Sort_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者记录为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		//对职工按照升序和降序进行排序
		cout << "请选择排序方式:" << endl;
		cout << "1-按照职工编号进行升序排序" << endl;
		cout << "2-按照职工编号进行降序排序" << endl;

		int select = 0;
		cin >> select;

		for (int i = 0; i < this->m_EmpNum; i++)
		{
			int minOrmax = i;
			
			for (int j = i + 1; j < this->m_EmpNum; j++)
			{
				if (select == 1)//升序
				{
					if (this->m_EmpArray[j]->m_id < this->m_EmpArray[minOrmax]->m_id)
					{
						minOrmax = j;
					}
				}
				else //降序
				{
					if (this->m_EmpArray[j]->m_id > this->m_EmpArray[minOrmax]->m_id)
					{
						minOrmax = j;
					}
				}
			}

			if (i != minOrmax)
			{
				Worker* temp = this->m_EmpArray[i];
				this->m_EmpArray[i] = this->m_EmpArray[minOrmax];
				this->m_EmpArray[minOrmax] = temp;
			}

		}

		cout << "排序成功,排序后的结果为:" << endl;
		this->save();//排序后的结果保存到文件中

		this->Show_Emp();//显示所有职工
	}
}

//清空文件操作
void WorkerManager::Cleam_Emp()
{
	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_EmpArray = NULL;
			this->m_EmpNum = 0;
			this->m_FileIsEmpty = true;

		}

		cout << "清空成功!" << endl;
	}
	system("pause");
	system("cls");
}

😊😊😊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值