基于多态的职工管理系统源码与一些理解

职工管理系统可以用来管理公司内所有员工的信息

编写一个管理系统首先应该创建一个管理类:

管理类负责内容:

  1. 与用户的沟通菜单的界面
  2. 对职工增删改查的操作
  3. 与文件的读写和交互

即一切对系统的操作都在管理类中实现!

分别创建WorkerManger.h文件与WorkerManger.cpp文件。

菜单功能的实现:

实现交换界面:

WorkerManger.cpp中:

void WorkerManger::Show_Menu()
{
	std::cout << "------------------------------" << std::endl;
	std::cout << "|***欢迎使用职工管理系统!***|" << std::endl;
	std::cout << "|*******0.退出管理系统*******|" << std::endl;
	std::cout << "|*******1.增加职工信息*******|" << std::endl;
	std::cout << "|*******2.显示职工信息*******|" << std::endl;
	std::cout << "|*******3.删除离职职工*******|" << std::endl;
	std::cout << "|*******4.修改职工信息*******|" << std::endl;
	std::cout << "|*******5.查找职工信息*******|" << std::endl;
	std::cout << "|*******6.按照编号排序*******|" << std::endl;
	std::cout << "|*******7.清空所有文档*******|" << std::endl;
	std::cout << "------------------------------" << std::endl;
	std::cout << std::endl;
}

职工管理系统.cpp(项目文件)中:

#include<iostream> //添加输入输出流头文件
#include"workerManger.h" //添加管理类头文件
#include"worker.h"
#include"employee.h"
#include"Manager.h"
#include"Boss.h"
int main()
{
	WorkerManger wm;    //创建管理类对象
	int choice;
	while (true)    //利用循环与switch语句控制交互界面
	{
		wm.Show_Menu();
		std::cin >> choice;
		switch (choice)
		{
		case 0:
			wm.exit_System(); 
			break;
		case 1:
			wm.add_EMp();
			break;
		case 2:
			wm.show_Info();
			break;
		case 3:
			wm.Delete_Emp();
			break;
		case 4:
			wm.Mod_Emp();
			break;
		case 5:
			wm.Find_Emp();
			break;
		case 6:
			wm.Sort_List();
			break;
		case 7:
			wm.Clear_File();
			break;
		default:
			system("cls");
			break;
		}
	}
	system("pause");
	return 0;
}

实现退出系统功能:

void WorkerManger::exit_System()
{
	std::cout << "欢迎下次使用" << std::endl;
	system("pause");
	exit(0);
}

职工抽象类及派生类的实现:

已知公司中有三类职工,他们都有一个共同的特点是职工。

我们可以封装一个抽象基类Worker来表示职工,并且职工应有与职工相关的属性,如ID号,姓名,工作岗位……

创建好了基类,再创建派生类Employee类、Manager类、Boss类来分别表示普通员工、经理、老板,并且继承基类的属性。

从而我们便可以通过指针(或引用)创建出相应的对象。

创建职工抽象类:

需要特别注意的是,若在抽象类中声明纯虚函数,在派生类中一定要重写纯虚函数!

#pragma once  //保证头文件只被编译一次
#include<iostream>

class Worker
{
public:
	virtual void showInfo() = 0;  //显示个人信息	
	virtual std::string getDepName() = 0;  //获取岗位信息
	virtual std::string getDuty() = 0;  //获取岗位职责

	int m_Id;
	std::string m_Name;
	int m_DeptId;
};

创建派生类:

.h文件:

//普通员工
#pragma once
#include"worker.h"
#include<iostream>

class Employee : public Worker
{
public:
	Employee(int id, std::string name, int deptid);
	virtual void Worker::showInfo();
	virtual std::string Worker::getDepName();
	virtual std::string Worker::getDuty();
};

.cpp文件:

#include"employee.h"

Employee:: Employee(int id, std::string name, int deptid)  //派生类初始化
{
	this->m_Id = id;
	this->m_Name = name;
	this->m_DeptId = deptid;
}

void Employee::showInfo()  //重写纯虚函数
{
	std::cout << "职工编号:" << this->m_Id;
	std::cout << "\t职工姓名:" << this->m_Name;
	std::cout << "\t职工岗位:" << this->getDepName();
	std::cout << "\t岗位职责:" << this->getDuty();
	std::cout << std::endl;
}

std::string Employee ::getDepName() //重写纯虚函数
{
	return std::string("员工");
}

std::string Employee::getDuty() //重写纯虚函数
{
	return std::string("完成经理分配的任务!");
}

此处省略 Manager类和Boss类...

封装好类后,我们应该如果管理(增删改查)这些职工呢?

可以通过二维数组指针来实现!

本系统可实现批量添加职工的功能,用户在批量创建时,可能会创建不同岗位的职工

如何将所有不同岗位的职工(即不同的类)都放入一个数组中?

因为要将不同的类放入到同一数组中,并且数组只能存放相同数据类型

我们可以将所有员工(即创建出来的对象)的指针存放在同一数组里!

同时,因为可以添加,删除员工,故该数组应是个不定长度的动态数组

要维护这个不定长度的数组,我们就要将这个数组开辟到堆区,否则随着函数的结束,析构函数自动释放栈区的内存,这个数组也就随之释放掉了!也就是说不能再利用该数组实现相应的功能了。

图示:

Worker*   *  m_Emparray  =  new Worker[x];//堆区开辟一个存放职工指针的数组传给m_Emparray
                                           // 因为在堆区new出来的是一个指针,故要用指针接收
                                           // m_Emparray的数据类型为Worker类的指针

 Worker*   *  m_Emparray ; //职工指针数组的指针

确定好了大体思路,我们就可以进行项目的编写了!

系统主要功能的源码:

#include"workerManger.h"
#include"employee.h"
#include"Manager.h"
#include"Boss.h"
#include"worker.h"
#include<fstream>
#define FILENAME "empFile.txt"
//通过构造函数,每次启动系统便会及时跟踪文件状态与相关内容
WorkerManger::WorkerManger()  
{	
	std::ifstream ifs;
	ifs.open(FILENAME, std::ios::in);
	//情况1:文件不存在的情况
	if (!ifs.is_open())
	{
		//std::cout << "此文件不存在!" << std::endl;
		this->m_Emparray = NULL;
		this->m_EmpNum = 0;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}
	//情况2:文件存在但没有记录
	char test;
	ifs >> test;
	if (ifs.eof())
	{
		//std::cout << "文件存在,但无内容" << std::endl;
		this->m_Emparray = NULL;
		this->m_EmpNum = 0;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}
	//情况3:文件存在且有数据
	//std::cout << "当前记录人数为: " << this->get_EmpNum() << std::endl;
	this->m_EmpNum = get_EmpNum();
	this->m_Emparray = new Worker * [get_EmpNum()]; //开辟空间存放文件之前的记录到数组中
	this->init_Emp(); 

	/* for (int i = 0; i < get_EmpNum(); i++)
	{
		std::cout << "ID" << this->m_Emparray[i]->m_Id << std::endl;
		std::cout << "姓名" << this->m_Emparray[i]->m_Name << std::endl;
		std::cout << "岗位" << this->m_Emparray[i]->m_DeptId << std::endl;
	} */
}


WorkerManger::~WorkerManger() //系统将要运行结束前,释放堆区数据
{
	if (m_Emparray != NULL)
	{
		delete[] m_Emparray;
		m_Emparray = NULL;
	}
}

void WorkerManger::Show_Menu()
{
	std::cout << "------------------------------" << std::endl;
	std::cout << "|***欢迎使用职工管理系统!***|" << std::endl;
	std::cout << "|*******0.退出管理系统*******|" << std::endl;
	std::cout << "|*******1.增加职工信息*******|" << std::endl;
	std::cout << "|*******2.显示职工信息*******|" << std::endl;
	std::cout << "|*******3.删除离职职工*******|" << std::endl;
	std::cout << "|*******4.修改职工信息*******|" << std::endl;
	std::cout << "|*******5.查找职工信息*******|" << std::endl;
	std::cout << "|*******6.按照编号排序*******|" << std::endl;
	std::cout << "|*******7.清空所有文档*******|" << std::endl;
	std::cout << "------------------------------" << std::endl;
	std::cout << std::endl;
}

void WorkerManger::exit_System()
{
	std::cout << "欢迎下次使用" << std::endl;
	system("pause");
	exit(0);
}

void WorkerManger::add_EMp()
{	
	system("cls");
	std::cout << "请输入你要添加的员工数量" << std::endl;
	int add_Num = 0;
	std::cin >> add_Num;	
	if (add_Num > 0)
	{
		int newSize = this->m_EmpNum + add_Num;
		Worker* * newArray = new Worker * [newSize];
		if (m_EmpNum>0)
		{
			for (int i = 0; i < newSize; i++)
			{
				newArray[i] = this->m_Emparray[i];
			}
		}
		for (int i = 0; i < add_Num; i++)
		{
			int ID;
			std::string name;
			int DepName;
			
			std::cout << "请输入要添加的第" << i + 1 << "位员工的ID" << std::endl;
			std::cin >> ID;
			std::cout << "请输入要添加的第" << i + 1 << "位员工的姓名" << std::endl;
			std::cin >> name;
			std::cout << "请输入要添加的第" << i + 1 << "位员工的职位" << std::endl;
			std::cout << "1.员工" << std::endl;
			std::cout << "2.经理" << std::endl;
			std::cout << "3.老板" << std::endl;
			std::cin >> DepName;

			Worker * worker = NULL;
			switch (DepName)
			{
			case 1:
				worker = new Employee(ID, name, DepName);
				break;
			case 2:
				worker = new Manager(ID, name, DepName);
				break;
			case 3:
				worker = new Boss(ID, name, DepName);
				break;
			default:
				break;
			}
			system("cls");
			newArray[m_EmpNum + i] = worker;
		}
		delete[] m_Emparray;
		this->m_Emparray = newArray;
		this->m_EmpNum = newSize;
		this->m_FileIsEmpty = false;
		std::cout << "添加成功!" << std::endl;
		this->save();
		system("pause");
		system("cls");
	}
	else
	{
		std::cout << "输入有误!" << std::endl;
	}

}

void WorkerManger::save()
{
	std::ofstream ofs;
	ofs.open(FILENAME, std::ios::out);

	for (int i = 0; i < m_EmpNum; i++)
	{
		ofs << m_Emparray[i]->m_Id << "  "
			<< m_Emparray[i]->m_Name << "  ";

		switch (m_Emparray[i]->m_DeptId)
		{
		case 1:
			ofs << std::string("员工") << std::endl;
			break;
		case 2:
			ofs << std::string("经理") << std::endl;
			break;
		case 3:
			ofs << std::string("老板") << std::endl;
			break;
		default:
			std::cout << "输入错误!" << std::endl;
		}	
	}
	ofs.close();
}

int WorkerManger::get_EmpNum()
{
	std::ifstream ifs;
	ifs.open(FILENAME, std::ios::in);
	int id;
	std::string EDepName;
	std::string name;
	int num = 0;
	while (ifs >> id && ifs >> name && ifs >> EDepName)
	{
		num++;
	}

	return num;
}

void WorkerManger::init_Emp()
{
	int num = get_EmpNum();
	std::ifstream ifs;
	ifs.open(FILENAME, std::ios::in);
	int id;
	std::string name;
	std::string EDepName;
	for (int i = 0; i < num; i++)
	{
		Worker* worker = NULL;
		ifs >> id && ifs >> name && ifs >> EDepName;
		if (EDepName == "员工")
		{
			worker = new Employee(id,name,1);
		}
		else if (EDepName == "经理")
		{
			worker = new Employee(id, name, 2);
		}
		else if (EDepName == "老板")
		{
			worker = new Employee(id, name, 3);
		}
		this->m_Emparray[i] = worker;
	}
	ifs.close();
	return;
}

void WorkerManger::show_Info()
{
	if (m_FileIsEmpty)
	{
		std::cout << "文件为空或不存在!" << std::endl;
	}
	else
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			this->m_Emparray[i]->showInfo();
		}
	}
		system("pause");
		system("cls");

}

int WorkerManger::EmpIsExist(int id)
{
	int index = -1;
	for (int i = 0; i < this->m_EmpNum; i++)
	{
		if (id == this->m_Emparray[i]->m_Id)
		{
			index = i;
			return index;
			break;	
		}
	}
	return index;
}

void WorkerManger::Delete_Emp()
{
	if (this->m_FileIsEmpty)
	{
		std::cout << "文件不存在或记录为空!" << std::endl;
	}

	std::cout << "请输入要删除的员工ID" << std::endl;
	int EmpId,index;
	std::cin >> EmpId;
	index = this->EmpIsExist(EmpId);
	if (index == -1)
	{
		std::cout << "删除失败,查无此人!" << std::endl;
		system("pause");
		system("cls");
		return;
	}
	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();
		std::cout << "删除成功!" << std::endl;
		system("pause");
		system("cls");
	}
}

void WorkerManger::Mod_Emp()
{
	if (!this->m_FileIsEmpty)
	{
		std::cout << "请输入要修改的员工编号" << std::endl;
		int Id;
		std::cin >> Id;
		int ret = this->EmpIsExist(Id);
		if (ret != -1)
		{
			delete this->m_Emparray[ret];
			int newid = 0;
			std::string name = "";
			int DId = 0;
			std::cout << "查到" << Id << "号职工,请输入新职工号:" << std::endl;
			std::cin >> newid;
			std::cout << "请输入新姓名:" << std::endl;
			std::cin >> name;
			std::cout << "请输入新职位:" << std::endl;
			std::cout << "1.员工" << std::endl;
			std::cout << "2.经理" << std::endl;
			std::cout << "3.老板" << std::endl;
			std::cin >> DId;
			Worker* worker = NULL;
			switch (DId)
			{
			case 1:
				worker = new Employee(newid,name,1);
				break;
			case 2:
				worker = new Manager(newid, name, 2);
				break;
			case 3:
				worker = new Boss(newid, name, 3);
				break;
			default:
				break;
			}
			this->m_Emparray[ret] = worker;
			std::cout << "修改成功!" << std::endl;
			this->save();
		}
		else
		{
			std::cout << "该编号不存在!" << std::endl;
		}
	}
	else
	{
		std::cout << "文件为空或不存在!" << std::endl;
	}
	system("pause");
	system("cls");
}

void WorkerManger::Find_Emp()
{
	if (this->m_FileIsEmpty)
	{
		std::cout << "此文件不存在或内容为空!" << std::endl;
	}
	else
	{
		std::cout << "请选择查找方式:" << std::endl;
		std::cout << "1.按职工ID查找" << std::endl;
		std::cout << "2.按职工姓名查找" << std::endl;
		int choice;
		std::cin >> choice;
		if (choice == 1)
		{
			std::cout << "请要查找的输入职工ID:" << std::endl;
			int id;
			std::cin >> id;
			int index = this->EmpIsExist(id);
			if (index != -1)
			{
				this->m_Emparray[index]->showInfo();
			}
			else
			{
				std::cout << "查无此人!" << std::endl;
			}
		}
		else if (choice == 2)
		{
			std::string name;
			int index = -1;
			std::cout << "请要查找的输入职工姓名:" << std::endl;
			std::cin >> name;
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				if (name == this->m_Emparray[i]->m_Name)
				{
					index = i;
					this->m_Emparray[index]->showInfo();
				}
				else
				{
					std::cout << "查无此人!" << std::endl;
				}
			}

		}
		else
		{
			std::cout << "输入错误!" << std::endl;
		}
	}
		system("pause");
		system("cls");
}

void WorkerManger::Sort_List()
{
	if (this->m_FileIsEmpty)
	{
		std::cout << "文件为空或不存在!" << std::endl;
	}
	else
	{
		std::cout << "请选择排序方式:" << std::endl;
		std::cout << "1.升序" << std::endl;
		std::cout << "2.降序" << std::endl;
		int choices;
		std::cin >> choices;
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			int Max_or_Min = i;
			for (int j = i+1; j < m_EmpNum; j++)
			{
				if (choices == 1)
				{
					if (this->m_Emparray[Max_or_Min]->m_Id > this->m_Emparray[j]->m_Id)
					{
						Max_or_Min = j;
					}
				}
				else if (choices == 2)
				{
					if (this->m_Emparray[Max_or_Min]->m_Id < this->m_Emparray[j]->m_Id)
					{
						Max_or_Min = j;
					}
				}
				else
				{
					std::cout << "输入错误!" << std::endl;
					
				}
			}
			if (i != Max_or_Min)
			{
				Worker* temp = m_Emparray[i];
				m_Emparray[i] = m_Emparray[Max_or_Min];
				m_Emparray[Max_or_Min] = temp;
 			}
		}
		std::cout << "排序成功!" << std::endl;
		this->save();
		this->show_Info();
	}
}

void WorkerManger::Clear_File()
{
	std::cout << "确认清空?" << std::endl;
	std::cout << "1.确认" << std::endl;
	std::cout << "2.返回" << std::endl;
	int select;
	std::cin >> select;
	if (select == 1)
	{
		std::ofstream ofs;
		ofs.open(FILENAME, std::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 m_Emparray[i];
					m_Emparray[i] = NULL;
				}
			}
			delete[] this->m_Emparray;
			this->m_Emparray = NULL;
			this->m_EmpNum = 0;
			this->m_FileIsEmpty = true;
		}
		std::cout << "清空完成!" << std::endl;
	}
	system("pause");
	system("cls");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值