【C++】基于多态实现员工管理系统

代码
1、主程序:
#include<iostream>
using namespace std;
#include"workerManager.h"

#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"

int main()
{
	WorkerManager wm;
	int choice = 0;

	while (true)
	{
		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;
				break;//清空文件
			default: 
				system("cls");
				break;
		}
	
	}
	
	system("pause");
	return 0;
}
以下所有类均需要再头文件中声明函数
2、老板类
#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;
	cout << "\t当前职工姓名:" << this->m_Name;
	cout << "\t\t当前职工职位:" << this->getDeptName();
	cout << "\t岗位职责:管理公司所有事务" << endl;
}

string Boss::getDeptName()
{
	return string("总裁");
}
3、经理类
#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;
	cout << "\t当前职工姓名:" << this->m_Name;
	cout << "\t当前职工职位:" << this->getDeptName();
	cout << "\t岗位职责:完成老板下发的任务,将任务拆分下发给普通员工" << endl;
}

string Manager::getDeptName()
{
	return string("经理");
}
4、员工类
#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;
	cout << "\t当前职工姓名:" << this->m_Name;
	cout << "\t当前职工职位:" << this->getDeptName();
	cout << "\t岗位职责:完成经理下发的任务" << endl;
}

string Employee::getDeptName()
{
	return string("员工");
}
5、函数类
#include "workerManager.h"


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

	//文件不存在
	if ( !ifs.is_open() ) 
	{
		cout << "文件不存在" << endl;
		this->m_EmpNum = 0;//初始化人数
		this->m_FileIsEmpty = true;//初始化文件为空标志
		this->m_EmpArray = NULL;//初始化数组
		ifs.close();
		return;
	}

	//文件存在但是数据为空
	char ch;
	//用输入流对象读取文件中的一个字符
	if ( (ifs >> ch).eof() ) //如果读文件读到结尾标 则表示文件为空
	{
		cout << "文件为空" << endl;
		this->m_EmpNum = 0;//初始化人数
		this->m_FileIsEmpty = true;//初始化文件为空标志
		this->m_EmpArray = NULL;//初始化数组
		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();

	/*for (int i = 0; i < this->m_EmpNum; i++)
	{
		cout << "职工编号:" << this->m_EmpArray[i]->m_Id << "  ";
		cout << "职工姓名:" << this->m_EmpArray[i]->m_Name << "  ";
		cout << "职工部门:" << this->m_EmpArray[i]->m_DeptId << endl;
	}*/
}

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*[this->m_EmpNum + addNum];

		//将原空间下内容存放到新空间下
		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;

			//初始化为空 防止出现野指针
			//在for循环中只初始化一次 生命周期只有一次
			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;
			}
			//创建了一个足够容纳newSize个worker对象的新数组
			//newSpace[newSize * sizeof(worker)] = worker;
			newSpace[this->m_EmpNum+i] = worker;
		}

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

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

		//更新新空间个数
		this->m_EmpNum = newSize;

		//标志
		this->m_FileIsEmpty = false;

		//提示
		cout << "成功添加"  << addNum << "名员工" << endl;

		//成功添加后需要保存到文件中
		this->save();
	}
	else
	{
		cout << "输入有误" << endl;
	}

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

}

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

	ifs.close();

	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;
		//根据不同部门Id创建不同对象
		if (dId == 1)
		{
			worker = new Employee(id, name, 1);
		}
		else if (dId == 2)
		{
			worker = new Manager(id, name, 2);
		}
		else if (dId == 3)
		{
			worker = new Boss(id, name, 3);
		}
		//存放在数组中
		this->m_EmpArray[index] = worker;
		index++;
	}
	//关闭文件
	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");
}

void WorkerManager::Del_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		//按照职工编号删除
		cout << "请输入要删除的员工编号:";
		int num = 0;
		cin >> num;

		if (this->IsExist(num) == -1)
		{
			cout << "不存在该员工" << endl;
		}
		else
		{
			for (int i = this->IsExist(num); i < this->m_EmpNum-1; i++)
			{
				this->m_EmpArray[i] = this->m_EmpArray[i + 1];
			}
			this->m_EmpNum--; //更新数组中人数
			//同步更新到文件中
			this->save();
			cout << "删除成功" << endl;
		}
	}
	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::Mod_Emp()
{

	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或文件内容为空" << endl;
	}
	else
	{
		cout << "当前员工表如下:" << endl;
		this->Show_Emp();
		cout << "请输入要修改职工的编号" << endl;
		int num;
		cin >> num;

		if (this->IsExist(num) != -1)
		{
			delete this->m_EmpArray[this->IsExist(num)];

			int newid;
			string newname;
			int newdid;

			cout << "当前将" << num << "号员工编号修改为:";
			cin >> newid;

			cout << "当前将" << num << "号员工姓名修改为:";
			cin >> newname;

			cout << "当前将" << num << "号员工编号职位(1、普通员工 2、经理 3、总裁)为:";
			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[this->IsExist(num)] = worker;
			cout << "修改成功" << this->m_EmpArray[this->IsExist(num)]->m_DeptId << 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 sel = 0;
		cin >> sel;

		if (sel == 1)
		{
			cout << "请输入要查找的员工编号:" << endl;
			int num;
			cin >> num;
			int ret = IsExist(num);

			if (ret != -1)
			{
				cout << "查找成功,信息如下" << endl;
				this->m_EmpArray[ret]->showInfo();
			}
			else
			{
				cout << "不存在该员工" << endl;
			}
		}
		else if (sel == 2)
		{
			cout << "请输入查找的名字" << endl;
			string name;
			cin >> name;

			bool f = false; //查找标志位

			for (int i=0; i<this->m_EmpNum; i++)
			{
				if (this->m_EmpArray[i]->m_Name == name )
				{
					cout << "查找成功" << this->m_EmpArray[i]->m_Id << "号员工信息如下:" << endl;
					f = true;
					this->m_EmpArray[i]->showInfo(); 
				}
			}
			if(f == 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 sel = 0;
		cin >> sel;

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

			}

			if (i != minOrMax)
			{
				Worker* temp = m_EmpArray[i];
				m_EmpArray[i] = m_EmpArray[minOrMax];
				m_EmpArray[minOrMax] = temp;
			}
		}

		cout << "排序完成,排序结果为:" << endl;
		this->save();
		this->Show_Emp();
	}
}

void WorkerManager::Clean_File()
{
	cout << "确认是否清空文件?" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;

	int sel = 0;
	cin >> sel;

	if (sel == 1)
	{
		ofstream ofs;
		ofs.open(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_EmpNum = 0;
			delete[] this->m_EmpArray;
			this->m_EmpArray = NULL;
			this->m_FileIsEmpty = true;
		}
		cout << "清空成功" << endl;
	}
	system("pause");
	system("cls");
}

WorkerManager::~WorkerManager() //堆区数据手动开辟手动释放
{
	if (this->m_EmpArray != NULL)
	{
		delete[] this->m_EmpArray;
		this->m_EmpArray = NULL;
	}
}

查漏补缺

1、数组开辟到栈区和堆区的区别

2、worker** 二级指针

worker ** = new worker*[5]

第一个星号是为了找到数组的首地址,第二个星号是为了解开每一个元素的内容【worker*类似int类型】

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值