基于C++的职工管理系统

通过对象的思想创建员工,经理和老板类,以及管理员类。管理员类实现对其他类的添加、显示等操作。通过管理员的成员函数实现对各项功能。

实现步骤:①创建职工基类。
②创建员工类,经理类,老板类,并继承职工基类共同属性。
③类外实现各类的成员函数
④创建管理员类
⑤通过管理员类实现对职工的各项操作。

实现代码:
职工基类:

#pragma once
#include <iostream>
using namespace std;
#include <string>
 //职工抽象类
class worker
{
    
    
public:
	//显示个人信息函数
	virtual void showinfo() = 0;
	//获取个人岗位函数
	virtual string getdeptname() = 0;
	int m_id;//职工编号
	string m_name;//职工姓名
	int m_deptid;//职工岗位编号
};

员工类:

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

class Employee :public worker
{
    
    
public:
	//初始化员工信息
	Employee(int id, string name, int did);
	//显示员工信息
	void showinfo();
	 //获取岗位函数(返回岗位信息)
	string getdeptname();
	
};

经理类:

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

class Manager :public worker
{
    
    
public:
	//初始化员工信息
	Manager(int age,string name,int did);
	//显示员工信息
	void showinfo();
	//获取岗位函数(返回岗位信息)
	string getdeptname();
};

老板类:

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

class Boss :public worker
{
    
    
public:
	//初始化员工信息
	Boss(int age, string name, int did);
	//显示员工信息
	void showinfo();
	//获取岗位函数(返回岗位信息)
	string getdeptname();
};

管理员类:

#pragma once //防止偷文件重复
#include <iostream>//包含输出输入流文件
using namespace std;//使用标准命名空间
#include<string>
#include "worker.h"
#include"manager.h"
#include"boss.h"
#include"Employee.h"
#include<fstream>
#define FILENAME "职工管理系统.txt"
class workManager
{
    
    
public:
	//构造函数
	workManager();
	//展示菜单函数
	void show_menu();
	//退出系统
	void Exitsystem();
	//析构函数
	~workManager();
	//记录员工人数
	int m_empnum;
	//存储员工的指针(二重指针  指向一级指针)
	worker** m_emparray;
	//添加员工函数
	void addemp();
	//显示职工
	void showemp();
	//保存文件函数
	void save();
	//判断文件是否为空的函数
	bool m_fileempty;
	//统计文件中有多少个人函数
	int get_empnum();
	//初始化员工
	void init_emp();
	//删除员工函数
	void deleteemp();
	//判断员工是否存在函数
	int isexit(int id);
	//修改职工
	void mod_emp();
	//查找员工函数
	void findemp();
	//按照编号查找员工函数
	void numfind();
	//排序函数
	void sort_emp();
	//清空函数
	void ddemp();

	
  };

员工类的成员函数实现:

#include "Employee.h"

//初始化员工信息
Employee:: Employee(int id, string name, int did)
{
    
    
	this->m_id = id;
	this->m_name = name;
	this->m_deptid = did;
}
//显示员工信息
 void Employee:: showinfo()
{
    
    
	 cout << "职工编号:" << this->m_id
		 << "\t职工姓名:" << this->m_name
		 << "\t岗位:" << this->getdeptname()
		 << "\t岗位职责:完成经理交给的任务" << endl;
} 
 //获取岗位函数(返回岗位信息)
 string Employee::getdeptname()
 {
    
    
	 return string("员工");
 }

经理类的成员函数实现:

#include "manager.h"
//初始化员工信息
Manager::Manager(int id, string name, int did)
{
    
    
	this->m_id = id;
	this->m_name = name;
	this->m_deptid = did;
}
//显示员工信息
void Manager::showinfo()
{
    
    
	cout << "职工编号:" << this->m_id
		<< "\t职工姓名:" << this->m_name
		<< "\t岗位:" << this->getdeptname()
		<< "\t岗位职责:接老板发的任务,分发给员工" << endl;
}
//获取岗位函数(返回岗位信息)
string Manager::getdeptname()
{
    
    
	return string("经理");
}

老板类的成员函数实现:

#include "boss.h"
//初始化员工信息
Boss::Boss(int id, string name, int did)
{
    
    
	this->m_id = id;
	this->m_name = name;
	this->m_deptid = did;
}
//显示员工信息
void Boss::showinfo()
{
    
    
	cout << "职工编号:" << this->m_id
		<< "\t职工姓名:" << this->m_name
		<< "\t岗位:" << this->getdeptname()
		<< "\t岗位职责:分发任务给经理" << endl;
}
//获取岗位函数(返回岗位信息)
string Boss::getdeptname()
{
    
    
	return string("老板");
}

管理员成员函数的实现:

#include "workManager.h"
#include<string>
//构造函数
workManager ::workManager()
{
    
    
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if (!ifs.is_open())//如果不存在
	{
    
    
		//初始化
		cout << "文件打开失败" << endl;
		this->m_empnum = 0;
		this->m_emparray = NULL;
		this->m_fileempty = true;
		ifs.close();
		}
	//如果文件为空,只有一个结尾标
	char ch;
	ifs >> ch;//将结尾标读走
	if (ifs.eof())//如果文件为空
	{
    
    
		//初始化
		cout << "文件为空" << endl;
		this->m_empnum = 0;
		this->m_emparray = NULL;
		this->m_fileempty = true;
		ifs.close();
	}
	int num = this->get_empnum();
	cout << "员工人数为:" << num << endl;
	this->m_empnum = num;
	//开辟空间
	this->m_emparray = new worker * [this->m_empnum];
	//读入文件信息
	this->init_emp();
	//测试代码
	/*for (int i = 0; i < this->m_empnum; i++)
	{
		cout << this->m_emparray[i]->m_id << endl;
		cout << this->m_emparray[i]->m_name << endl;
		cout << this->m_emparray[i]->m_deptid << endl;
	}*/
}
//统计文件中的人数
int workManager::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 workManager::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;
}
//查找员工函数
void workManager::findemp()
{
    
    
	if (this->m_fileempty)
	{
    
    
		cout << "文件为空或不存在员工记录" << endl;
	}
	else
	{
    
    
		cout << "请输入查询方式" << endl;
		cout << "1.按照编号查询" << endl;
		cout << "2.按照姓名查询" << endl;
		int choice = 0;
		cin >> choice;
		if (choice == 1)
		{
    
    
			this->numfind();//查询并输出该员工信息
		}
		else if (choice == 2)
		{
    
    
			int index = -1;
			string name;
			cout << "请输入你需要查询的名字" << endl;
			cin >> name;
			bool flag = false;
			for (int i = 0; i < this->m_empnum; i++)
			{
    
    
				if (this->m_emparray[i]->m_name == name)
				{
    
    
					index = i;
					this->m_emparray[index]->showinfo();
					flag = true;
				}
			}
			if (flag == false)
			{
    
    
				cout << "查无此人" << endl;
			}
		}
		else
		{
    
    
			cout << "输入错误" << endl;
		}
	}
	system("pause");
	system("cls");
}
//排序函数
void workManager::sort_emp()
{
    
    
	if (this->m_fileempty)
	{
    
    
		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;//默认i最小
			for (int j = i + 1; j < this->m_empnum; j++)
			{
    
    
				if (select == 1)
				{
    
    
					if (m_emparray[minormax]->m_id > m_emparray[j]->m_id)
					{
    
    
						minormax = j;//如果满足条件,复制与minormax
					}
				}
				else
				{
    
    
					if (m_emparray[minormax]->m_id < m_emparray[j]->m_id)
					{
    
    
						minormax = j;
					}
				}
			}
			if (i != minormax)//如果原来默认的i不是最小或最大,就交换两个的值
			{
    
    
				worker* temp = m_emparray[i];
				m_emparray[i] = m_emparray[minormax];
				m_emparray[minormax] = temp;
			}
		}
		cout << "排序成功,结果为:" << endl;
		this->save();
		this->showemp();
	}
	
		
}

void workManager::numfind()
{
    
    
	if (this->m_fileempty)
	{
    
    
		cout << "文件为空,无员工" << endl;
	}
	else
	{
    
    
		int m_id;
		cout << "请输入需要查询的员工编号" << endl;
		cin >> m_id;
		int ret = this->isexit(m_id);
		if (ret == -1)
		{
    
    
			
			cout << "查无此人" << endl;
		}
		else
		{
    
    
			this->m_emparray[ret]->showinfo();
		}
	}
}
//添加员工函数
void workManager::addemp()
{
    
    
	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 != 0)
		{
    
    
			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 did;
			cout << "请输入添加第" << i + 1 << "员工的编号" << endl;
			cin >> id;
			
					cout << "请输入添加第" << i + 1 << "员工的姓名" << endl;
					cin >> name;
					cout << "请输入添加第" << i + 1 << "员工的部门" << endl;
					cout << "1.员工" << endl;
					cout << "2.经理" << endl;
					cout << "3.老板" << endl;
					cin >> did;
					worker* w = NULL;
					switch (did)
					{
    
    
					case 1:w = new Employee(id, name, did);
						break;
					case 2:w = new Manager(id, name, did);
						break;
					case 3:w = new Boss(id, name, did);
						break;
					default:
						break;
					}
					//将第i+1个员工加入到新空间
					newspace[this->m_empnum + i] = w;
		}
		//释放原来的空间
				delete[]this->m_emparray;
				//将新的空间添加到原有数组中
				this->m_emparray = newspace;
				//更新数组中的人数
				this->m_empnum = newsize;
				//返回值为false 表示数组不为空
				this->m_fileempty = false;
				cout << "成功添加" << addnum << "员工" << endl;
			}
		//不添加
	else
	{
    
    
		cout << "输入数据有误" << endl;
	}
	//将数组添加到文件中
	this->save();
	
	system("pause");
	system("cls");
}
//保存文件函数
void  workManager::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_deptid << " " << endl;
	}
	ofs.close();
}
//初始化函数
void workManager::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* w;
		if (did == 1)//普通员工
		{
    
    
			w = new Employee(id, name, did);
		}
		else if (did == 2)//经理
		{
    
    
			w = new Manager(id, name, did);
		}
		else//老板
		{
    
    
			w = new Boss(id, name, did);
		}
		this->m_emparray[index] = w;//放入数组
		index++;
	}
}
//显示职工函数
void workManager::showemp()
{
    
    
	if (this->m_fileempty)
	{
    
    
		cout << "文件为空,无员工" << endl;
	}
	else
	{
    
    
		for (int i = 0; i < this->m_empnum; i++)
		{
    
    
			this->m_emparray[i]->showinfo();//调用每个员工的显示函数
		}
	}
	
	system("pause");
	system("cls");
}
//修改员工函数
void workManager::mod_emp()
{
    
    
	if (this->m_fileempty)
	{
    
    
		cout << "修改失败,名单无员工" << endl;
	}
	else
	{
    
    
		int id;
		cout << "请输入需要修改员工的编号" << endl;
		cin >> id;
		int ret = this->isexit(id);//判断是否存在,并返回员工的编号
		if (ret != -1)
		{
    
    
			//重新录入
			delete this->m_emparray[ret];
			int newid = 0;
			string newname = "";
			int newdid;
			cout << "请输入新的职工编号" << endl;
			cin >> newid;
			cout << "请输入新的名字" << endl;
			cin >> newname;
			cout << "请输入新的岗位编号" << endl;
			cout << "1.员工" << endl;
			cout << "2.经理" << endl;
			cout << "3.老板" << endl;
			cin >> newdid;
			worker* w = NULL;
			switch (newdid)
			{
    
    
			case 1:w = new Employee(newid, newname, newdid);
				break;
			case 2:w = new Manager(newid, newname, newdid);
				break;
			case 3:w = new Boss(newid, newname, newdid);
				break;
			default:
				break;
			}
			this->m_emparray[ret] = w;//保存到原来那个数组
			cout << "修改成功" << endl;
			this->save();
		}
	
		
		else
		{
    
    
			cout << "修改失败,无该员工" << endl;
		}
		
	}
	system("pause");
	system("cls");
}
//删除员工函数
void  workManager::deleteemp()
{
    
    
	if (this->m_fileempty)
	{
    
    
		cout << "文件为空或不存在员工记录" << endl;
	}
	
		int id;
		
		cout << "请输入你需要删除职工的职工编号" << endl;
		cin >> id;
		int ret=this->isexit(id);//判断员工是否存在并返回一个值
		if (ret == -1)
		{
    
    
			cout << "删除失败,没有该员工" << endl;
			
		}
		else
		{
    
    
			for (int i = ret; i < this->m_empnum; i++)//覆盖删除
			{
    
    
				this->m_emparray[i] = this->m_emparray[i + 1];
			}
			this->m_empnum--;
			this->save();
			cout << "删除成功" << endl;
		}
		system("pause");
		system("cls");
}
//判断员工是否存在函数
int workManager::isexit(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 workManager::Exitsystem()
{
    
    
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);//退出程序函数
}
void workManager::ddemp()
{
    
    
	ofstream ofs;
	ofs.open(FILENAME, ios::trunc);
	if (this->m_emparray != NULL)
	{
    
    
		for (int i = 0; i < this->m_empnum; i++)
		{
    
    
			delete this->m_emparray[i];
		}
	}
	this->m_empnum = 0;
	delete[]this->m_emparray;
	this->m_emparray = NULL;
	this->m_fileempty = true;
	cout << "清空成功" << endl;
	system("pause");
	system("cls");
}
//析构函数
workManager::~workManager()
{
    
    

}

main函数:

#include<iostream>
using namespace std;
#include "workManager.h"
#include "worker.h"
#include "Employee.h"
#include "manager.h"
#include "boss.h"



int main()
{
    
    
	
	//实例化管理者对象
	workManager wm;
	int choice = 0 ;//用于保存用户的选项
	while (true)
	{
    
    
		//展示菜单函数 
		wm.show_menu();
		cout << "请输入你的选择" << endl;
		cin >> choice;//接受用户的选择
		switch (choice)
		{
    
    
		case 0:wm.Exitsystem();//退出系统
			break;
		case 1:wm.addemp();//添加职工
			break;
		case 2:wm.showemp();//显示职工
			break;
		case 3:wm.deleteemp();//删除职工
			break;
		case 4:wm.mod_emp();//修改职工信息
			break;
		case 5:wm.findemp();//查找职工
			break;
		case 6:wm.sort_emp();//排序
			break;
		case 7:wm.ddemp();//清空文档
			break;
		default:cout << "输入错误,请重新输入" << endl;
			system("cls");//清屏
			break;

		}
	}
	

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_47414687/article/details/115091017