C++职工管理系统

本项目是学习B站黑马C++课程的项目

项目简介

本项目使用C++的多态实现一个简易的职工管理系统

其中抽象基类为:Worker类,包含纯虚函数 显示个人信息showInfo() 和 获取岗位名称getDeptName(); 还包含3个属性,分别是职工编号:m_Id、职工姓名:m_Name、部门编号:m_DeptId

基于抽象基类-抽象员工类派生出3个派生类:普通职工类(Employee),经理类(Manager)和老板类(Boss);使用多态重写基类中的2个纯虚函数供后续调用

项目功能

如图,实现8个功能

项目结构

 

 

 

 项目代码

文件结构

 "worker.h"

#pragma once
#include <iostream>
#include <string>
#include <iomanip> //输出流格式控制头文件
using namespace std;

//抽象职工类
class Worker
{
public:

	//显示个人信息-纯虚函数
	virtual void showInfo() = 0;

	//获取岗位名称-纯虚函数
	virtual string getDeptName() = 0;

	//职工编号
	int m_Id;
	//职工姓名
	string m_Name;
	//部门编号
	int m_DeptId;
};

"employee.h"

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

class Employee :public Worker
{
public:

	//构造函数
	Employee(int id,string name,int dId);

	//显示个人信息-纯虚函数
	virtual void showInfo();

	//获取岗位名称-纯虚函数
	virtual 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);

	//显示个人信息-纯虚函数
	virtual void showInfo();

	//获取岗位名称-纯虚函数
	virtual 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);

	//显示个人信息-纯虚函数
	virtual void showInfo();

	//获取岗位名称-纯虚函数
	virtual string getDeptName();

};

"workerManager.h"

#pragma once //避免头文件被重复包含
#include <iostream> //包含输入输出流头文件
using namespace std; //使用标准命名空间
#include "worker.h"
#include "boss.h"
#include "employee.h"
#include "manager.h"
#include <fstream>
#define FILENAME "empFile.txt"
//#define MAXLEN 100


class WorkerManager
{
public:

	//构造函数
	WorkerManager();

	//展示菜单
	void Show_Menu();

	//0.退出管理程序
	void ExitSystem();

	//记录职工人数
	int m_EmpNum;

	//职工数组指针-数组中存放的是各个工种对象的指针,且数组开辟在堆区,所以使用2重指针
	Worker** m_EmpArray;

	//判断文件是否为空
	bool m_FileIsEmpty = false;

	//1.增加职工信息
	void Add_Emp();

	//保存文件
	void save();

	//员工初始化
	void init_Emp();

	//2.显示职工信息
	void show_Emp();

	//判断职工是否存在-根据id
	int IsExist(int id);

	//判断职工是否存在-根据name
	int* IsExist_Name(string name);

	//3.删除职工
	void Del_Emp();

	//4.修改职工信息
	void Mod_Emp();

	//5.查找职工
	void Find_Emp();

	//6.排序职工
	void Sort_Emp();

	//7.清空文件
	void Clean_File();

	//统计文件中人数
	int get_EmpNum();

	//析构函数
	~WorkerManager();

};

"employee.cpp"

#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 << left << setw(10) << "职工编号: " << left << setw(9) << this->m_Id
		<< left << setw(10) << "职工姓名: " << left << setw(9) << this->m_Name
		<< left << setw(6) << "岗位: " << left << setw(9) << this->getDeptName()
		<< "岗位职责: 完成经理交给的任务" << endl;
}

//获取岗位名称-纯虚函数
string Employee::getDeptName()
{
	return string("员工");
}

"manager.cpp"

#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 << left << setw(10) << "职工编号: " << left << setw(9) << this->m_Id
		<< left << setw(10) << "职工姓名: " << left << setw(9) << this->m_Name
		<< left << setw(6) << "岗位: " << left << setw(9) << this->getDeptName()
		<< "岗位职责: 完成老板的任务,给员工下达任务" << endl;
}

//获取岗位名称-纯虚函数
string Manager::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_DeptId = dId;
}

//显示个人信息-纯虚函数
void Boss::showInfo()
{
	cout << left << setw(10) << "职工编号: " << left << setw(9) << this->m_Id
		<< left << setw(10) << "职工姓名: " << left << setw(9) << this->m_Name
		<< left << setw(6) << "岗位: " << left << setw(9) << this->getDeptName()
		<< "岗位职责: 管理整个公司,给经理下达任务" << endl;
}

//获取岗位名称-纯虚函数
string Boss::getDeptName()
{
	return string("老板");
}

"workerManager.cpp"

#include "workerManager.h"

WorkerManager::	WorkerManager()
{

	//1.文件不存在

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

	//2.文件存在,但是为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//cout << "文件为空" << endl;
		//初始化属性
		//初始化记录人数
		this->m_EmpNum = 0;
		//初始化数组指针
		this->m_EmpArray = NULL;
		//初始化文件是否为空
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//3.文件存在,且存有数据
	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::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;
}

//0.退出管理程序
void WorkerManager::ExitSystem()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

//1.增加职工信息
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];
				*(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;

			//判断是否已经有这个编号的职工存在
			while (1)
			{
				if (this->IsExist(id) == -1)
				{
					break;
				}
				else
				{
					cout << "已经存在编号为: " << id << " 的职工!" << endl;
					cout << "请重新输入员工编号: " << 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;
			*(newSpace + this->m_EmpNum + i) = worker; //--------------------------------------------------------------
		}

		//释放原有空间
		delete[] this->m_EmpArray;

		//更改新空间的指向
		this->m_EmpArray = newSpace;

		//更新新的职工人数
		this->m_EmpNum = newSize;

		//保存到文件中
		this->save();

		//提示成功添加
		cout << "成功添加 " << addNum << " 名新员工" << endl;
		
		//更新职工不为空标志
		this->m_FileIsEmpty = false;
	}
	else
	{
		cout << "输入有误!" << endl;
	}

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

//保存文件
void WorkerManager::save()
{
	ofstream ofs; //向文件内输出,out file stream
	ofs.open(FILENAME,ios::out); //写文件

	for (int i = 0; i < 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();
}

//统计文件中人数
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);
		}

		if (dId == 2) //经理
		{
			worker = new Manager(id, name, dId);
		}

		if (dId == 3) //老板
		{
			worker = new Boss(id, name, dId);
		}
		this->m_EmpArray[index] = worker;
		index++;
	}
	//关闭文件
	ifs.close();
}

//2.显示职工信息
void WorkerManager::show_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者文件为空" << endl;
	}
	else
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			//利用多态调用程序接口
			this->m_EmpArray[i]->showInfo();
		}
	}
	//按任意键清屏
	system("pause");
	system("cls");
}

//判断职工是否存在-根据id
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;
}

//判断职工是否存在-根据name
int* WorkerManager::IsExist_Name(string name)
{
	int len = 0;
	int* index = NULL;

	for (int i = 0; i < this->m_EmpNum; i++)
	{
		if (this->m_EmpArray[i]->m_Name == name)
		{
			len++;
		}
	}
	if (len != 0)
	{
		index = new int[len];
		int j = 0;
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			if (this->m_EmpArray[i]->m_Name == name)
			{
				index[j] = i;
				j++;
			}
		}
	}

	return index;
}

//3.删除职工
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) //职工存在
		{
			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");
}

//4.修改职工信息
void WorkerManager::Mod_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或文件为空" << endl;
	}
	else
	{
		int id = 0;
		cout << "请输入需要修改职工的编号: " << endl;
		cin >> id;
		int index = this->IsExist(id);
		if (index != -1)
		{
			//查找到需要修改的员工

			delete this->m_EmpArray[index];

			int newId = 0;
			string newName = "";
			int newDid = 0;

			cout << "查找到 " << id << " 号职工,请输入新职工号: " << endl;
			cin >> newId;

			cout << "请输入新职工姓名: " << endl;
			cin >> newName;

			cout << "请选择职工的新岗位: " << endl;
			cout << "1、职工" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> newDid;

			Worker* worker = NULL;

			switch (newDid)
			{
			case 1:
				worker = new Employee(newId, newName, newDid);
				break;
			case 2:
				worker = new Manager(newId, newName, newDid);
				break;
			case 3:
				worker = new Boss(newId, newName, newDid);
				break;
			default:
				break;
			}

			//更新数据到数组中
			this->m_EmpArray[index] = worker;

			cout << "修改成功" << endl;
			//保存到文件
			this->save();
		}
		else
		{
			cout << "职工不存在" << endl;
		}
	}
	//按任意键清屏
	system("pause");
	system("cls");
}

//5.查找职工
void WorkerManager::Find_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或文件为空" << endl;
	}
	else
	{
		int findMethod = 0;
		
		cout << "请选择查找方式: " << endl;
		cout << "1、按编号id查询" << endl;
		cout << "2、按姓名name查询" << endl;
		cin >> findMethod;
		if (findMethod == 1)
		{
			//按照编号查询
			int id = 0;
			int index = 0;
			cout << "请输入要查询的职工编号: " << endl;
			cin >> id;
			index = this->IsExist(id);
			if (index != -1)
			{
				cout << "查询到编号为: " << id << " 的职工,信息如下:" << endl;
				this->m_EmpArray[index]->showInfo();
			}
			else
			{
				cout << "不存在编号为: " << id << " 的职工" << endl;
			}
		}
		else if (findMethod == 2)
		{
			//按照姓名查询
			string name = "";
			int* ret = NULL;
			cout << "请输入要查询的职工姓名: " << endl;
			cin >> name;
			ret = this->IsExist_Name(name); //符合姓名的对象数组下标组成的数组,开辟在堆区
			//求ret数组的长度
			int len = _msize(ret) / sizeof(ret[0]);
			
			if (ret != NULL)
			{
				cout << "查询到姓名为: " << name << " 的职工,信息如下:" << endl;
				for (int i = 0; i < len; i++)
				{
					this->m_EmpArray[ret[i]]->showInfo();
				}
				//手动释放在堆区开辟的空间
				delete [] ret;
				ret = NULL;
			}
			else
			{
				cout << "不存在姓名为: " << name << " 的职工" << endl;
			}
		}
		else
		{
			cout << "输入有误,请输入1或者2" << endl;
		}
	}

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

//6.排序职工
void WorkerManager::Sort_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或文件为空" << endl;
		//按任意键清屏
		system("pause");
		system("cls");
		return;
	}
	else
	{
		cout << "请选择排序方式: " << endl;
		cout << "1、升序" << endl;
		cout << "2、降序" << endl;

		int select = 0;
		cin >> select;
		for (int i = 0; i < this->m_EmpNum - 1; i++) //选择排序进行len-1趟单排
		{
			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();
}

//7.清空文件
void WorkerManager::Clean_File()
{
	cout << "确定清空吗?" << endl;
	cout << "1、确定" << endl;
	cout << "2、返回" << endl;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		//清空文件 trunc方式,如果存在同名文件,就删除后再创建空的同名文件
		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");
}

WorkerManager::~WorkerManager()
{
	//手动释放堆区开辟的空间
	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;
	}
}

"main.cpp"

#include <iostream>
using namespace std;
#include "workerManager.h"


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.Clean_File();
			break;
		default:
			system("cls"); //清屏
			break;
		}
	}

	system("pause");
	return 0;
}



//ANSI编码格式可用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值