c++实现职工管理系统

需求分析

系统可以管理公司内部所有员工的信息
主要使用c++实现一个基于多态的职工管理系统

公司中的职工分为三类:普通员工、经理、老板,显示信息时需要显示职工编号、职工姓名、职工岗位以及职责
普通员工职责:完成经理安排的各项任务
经理职责:完成老板交付的任务,并拆解下发到员工
老板职责:统筹管理公司所有事务

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

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

main.cpp

#include"WorkManger.h"
#include"Worker.h"
#include"Employee.h"
#include"Boss.h"
#include"Manager.h"


int main()
{
	WorkManager wm;
	
	int choice = 0;
	while (true)
	{
		wm.Show_menu();
		cout << "请选择" << endl;
		cin >> choice;

		switch (choice)
		{
		case ADD:
			wm.Add_Emp();
			break;
		case SHOW:
			wm.show_Emp();
			break;
		case DEL:
			wm.Del_Emp();
			break;
		case MODIFY:
			wm.Mod_Emp();
			break;
		case SEARCH:
			wm.Search_Emp();
			break;
		case SORT:
			wm.Sort_Emp();
			break;
		case CLEAN:
			wm.Clean_Emp();
			break;
		case EXIT:
			wm.ExitSystem();
			break;
		default:
			cout << "选择错误,请重新选择" << endl;
			break;
		}
		
	}

	system("pause");
	return 0;
}

WorkManager.cpp

#include"WorkManger.h"

void WorkManager::init_Emp()//初始化职工(3)
{
	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;
		index++;
	}
	ifs.close();
}

int WorkManager::get_EmpNum()//获取职工人数(3)
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int num = 0;
	string name;
	int id;
	int dId;
	while (ifs >> id && ifs >> name && ifs >> dId)
	{
		num++;
	}
	return num;
}

WorkManager::WorkManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	//1.文件不存在的初始化
	if (!ifs.is_open())
	{
		cout << "文件打开失败" << endl;
		this->m_EmpNum = 0;
		this->m_EMpArray = NULL;
		this->m_FileEmpty = true;
		ifs.close();
		return;
	}

	//2.文件存在内容为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		cout << "文件为空" << endl;
		this->m_EmpNum = 0;
		this->m_EMpArray = NULL;
		this->m_FileEmpty = true;
		ifs.close();
		return;
	}

	//3.文件存在内容不为空
	int num = get_EmpNum();
	cout << "职工人数:" << num << endl;
	this->m_EmpNum = num;
	//开辟空间,把文件的数据存到数组中
	this->m_EMpArray = new Worker * [this->m_EmpNum];
	this->init_Emp();
}

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_DepId << endl;
	}

	ofs.close();
}

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::ExitSystem()//退出程序
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);//退出
}

void WorkManager::show_Emp()//显示
{
	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");
}

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::Del_Emp()//按编号删除职工
{
	if (this->m_FileEmpty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		cout << "请输入要删除编号" << endl;
		int id = 0;
		cin >> id;
		int index = this->isExit(id);
		if (index==-1)
		{
			cout << "职工不存在" << endl;
		}
		else
		{
			for (int i = index; i < this->m_EmpNum-1; i++)
			{
				this->m_EMpArray[i] = this->m_EMpArray[i + 1];			
			}
			this->m_EmpNum--;
			this->save();//将删除保存到文件中
		}
	}
	
	system("pause");
	system("cls");
}

void WorkManager::Mod_Emp()//修改职工
{
	if (this->m_FileEmpty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		cout << "请输入要修改编号" << endl;
		int id = 0;
		cin >> id;
		int index = this->isExit(id);
		if (index == -1)
		{
			cout << "要修改的职工不存在" << endl;
		}
		else
		{
			delete this->m_EMpArray[index];

			int newid=0;
			string newname="";
			int newdSelect=0;//部门的选择

			cout << "找到了第 " << id << "个职工的编号" << endl;
			cout << "请输入新的id:" << endl;
			cin >> newid;

			cout << "请输入新的姓名:" << endl;
			cin >> newname;

			cout << "请输入新的部门:" << endl;
			cout << "1.普通员工" << endl;
			cout << "2.部门经理" << endl;
			cout << "3.公司老板" << endl;
			cin >> newdSelect;

			Worker* worker = NULL;
			switch (newdSelect)
			{
			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:
				cout << "输入错误" << endl;
				break;
			}
			this->m_EMpArray[index] = worker;
			cout << "该职工修改成功" << endl;
		}
	}
	system("pause");
	system("cls");
}

void WorkManager::Search_Emp()//查找职工
{
	if (this->m_FileEmpty)
	{
		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 = isExit(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 < this->m_EmpNum; i++)
			{
				if (this->m_EMpArray[i]->m_Name == name)
				{
					cout << "查找成功,信息如下" << endl;
					this->m_EMpArray[i]->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;
			for (int j = i+1; j < this->m_EmpNum; j++)
			{
				if (select == 1)//升序
				{
					if (this->m_EMpArray[minOrmax]->m_Id > this->m_EMpArray[j]->m_Id)
					{
						minOrmax = j;
					}
				}
				else//降序
				{
					if (this->m_EMpArray[minOrmax]->m_Id < this->m_EMpArray[j]->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 WorkManager::Clean_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++)
			{
				if (this->m_EMpArray[i] != NULL)
				{
					delete this->m_EMpArray[i];
					this->m_EMpArray[i] = NULL;
				}
			}
			delete[] this->m_EMpArray;
			this->m_EMpArray = NULL;
			this->m_EmpNum = 0;
			this->m_FileEmpty = true;
		}
		cout << "清空成功" << endl;
	}

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

}

void WorkManager::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 << "请输入第 " << i + 1 << "个职工的部门" << 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:
				cout << "输入错误" << endl;
				break;
			}
			//如果原先没有员工,直接加入
			//如果有,则将员工加入到原先员工的后面
			newSpace[this->m_EmpNum + i] = worker;
		}

		delete[] this->m_EMpArray;//释放原有空间
		this->m_EMpArray = newSpace;//更改新空间的指向
		this->m_EmpNum = newSize;//更新职工人数

		this->m_FileEmpty = false;//把文件改为不为空

		this->save();//把数据保存文件中

		cout << "成功添加:" <<addNum<<"名职工" << endl;
	}
	else
	{
		cout << "输入错误" << endl;
	}
	system("pause");
	system("cls");
}

WorkManager::~WorkManager()
{
	if (this->m_EMpArray != NULL)
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			if (this->m_EMpArray[i] != NULL)
			{
				delete this->m_EMpArray[i];
			}
		}
		delete[] this->m_EMpArray;
		this->m_EMpArray = NULL;
	}
}

Employee.cpp`

#include"Employee.h"

Employee::Employee(int id, string name, int dId)
{
	this->m_Id = id;
	this->m_Name = name;
	this->m_DepId = dId;
}


void Employee::showInfo()//显示个人信息
{
	cout << "职工编号:" << this->m_Id
		<< "\t职工姓名:" << this->m_Name
		<< "\t岗位:" << this->getDeptName()
		<< "\t完成经理的任务" << endl;
}


string Employee::getDeptName()//获取岗位信息
{
	return string("员工");
}

Manager.cpp

#include"Employee.h"

Employee::Employee(int id, string name, int dId)
{
	this->m_Id = id;
	this->m_Name = name;
	this->m_DepId = dId;
}


void Employee::showInfo()//显示个人信息
{
	cout << "职工编号:" << this->m_Id
		<< "\t职工姓名:" << this->m_Name
		<< "\t岗位:" << this->getDeptName()
		<< "\t完成经理的任务" << endl;
}


string Employee::getDeptName()//获取岗位信息
{
	return string("员工");
}

Boss.cpp

#include"Boss.h"

Boss::Boss(int id, string name, int dId)
{
	this->m_Id = id;
	this->m_Name = name;
	this->m_DepId = dId;
}


void Boss::showInfo()//显示个人信息
{
	cout << "职工编号:" << this->m_Id
		<< "\t职工姓名:" << this->m_Name
		<< "\t岗位:" << this->getDeptName() 
		<< "\t完成董事长的任务" << endl;
}


string Boss::getDeptName()//获取岗位信息
{
	return string("老板");
}

WorkManager.h

#pragma once
#include<iostream>
using namespace std;

#include<string>

#include"Worker.h"
#include"Employee.h"
#include"Manager.h"
#include"Boss.h"

#include<fstream>
#define FILENAME "work.txt"//文件名称


enum option//菜单的枚举:为了看更方便观察功能
{
	EXIT,
	ADD,
	SHOW,
	DEL,
	MODIFY,
	SEARCH,
	SORT,
	CLEAN
};

class WorkManager
{
public:
	WorkManager();

	void Show_menu();//菜单

	void ExitSystem();//退出程序

	int m_EmpNum;//记录职工人数
	Worker** m_EMpArray;//职工数组指针:worker类是一个指针,使用要一个二级指针

	void Add_Emp();//添加职工
	void save();//把添加人保存到文件中
	bool m_FileEmpty;//判断文件是否为空
	int get_EmpNum();//获取职工人数
	void init_Emp();//初始化职工

	void show_Emp();//显示

	int isExit(int id);//判断职工是否存在,存在返回下标
	void Del_Emp();//按编号删除职工

	void Mod_Emp();//修改职工

	void Search_Emp();//查找

	void Sort_Emp();//排序

	void Clean_Emp();//清空

	~WorkManager();
};

Work.h

#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_DepId;//职工所在部门编号

};

Employee.h

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


class Employee :public Worker//员工类
{
public:
	Employee(int id,string name,int dId);


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

	string getDeptName();//获取岗位信息

};

Manager.h

#pragma once
#include<iostream>
using namespace std;

#include"Worker.h"


class Manager :public Worker//经理类
{
public:
	Manager(int id, string name, int dId);

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

	string getDeptName();//获取岗位信息
};

Boss.h

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


class Boss :public Worker//老板类
{
public:
	Boss(int id, string name, int dId);

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

	string getDeptName();//获取岗位信息
};

添加职工及人数
在这里插入图片描述
显示职工信息
在这里插入图片描述
查找
在这里插入图片描述
修改
在这里插入图片描述
排序
在这里插入图片描述
删除
在这里插入图片描述
在这里插入图片描述
在文件中显示
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值