2020-08-04

``## 基于多态实现职工管理系统

代码部分

main.app
#include<iostream>
#include<stdlib.h>
#include"WorkerManager.h"
#include"employee.h"
#include"Worker.h"
#include"manager.h"
#include"boss.h" 
using namespace std;
int main()
{ 
	//实例化对象 
	WorkerManager wm;
	int choice=0;
	while(true)
	{
		wm.ShowMenu();
		cout << "请输入选项" << endl;
		cin >> choice;
		switch(choice)
		{
			case 1:
				//增加职工信息 
				wm.AddEmp();
				break;
			case 2:
				//显示职工信息 
				wm.ShowEmp();
				break;
			case 3:
				//删除离职职工 
				wm.DelEmp();
				break;
			case 4:
				//修改职工信息 
				wm.ChangeEmp();
				break;
			case 5:
				//查找职工信息 
				wm.SerEmp(); 
				break;
			case 6:
				//按照编号排序 
				wm.SortEmp();
				break;
			case 7:
				//清空所有文档 
				wm.ClearEmp();
				break;
			case 0:
				//退出管理系统 
				wm.ExitSystem();
				break;
			default:
				system("cls");
				break;
		}
	}
	system("pause");
	return 0;
} 
WorkerManager.h
#pragma once      //避免重复调用 
#include<iostream>
#include<stdlib.h> 
#include"Worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include<fstream>
#define FILENAME "text.txt"
using namespace std;

class WorkerManager
{
public:
	WorkerManager();
	~WorkerManager();
	
	//成员函数(行为) 
	void ShowMenu();
	void ExitSystem(); 
	void AddEmp(); 
	void save();
	void ShowEmp(); 
	void DelEmp();
	void ChangeEmp();
	void SerEmp();
	void SortEmp(); 
	void ClearEmp();
	
	//记录职工人数 
	int m_EmpNum;

	//职工数组指针
	Worker **m_EmpArray; 
	
	//标志文件是否为空
	bool m_FileIsEmpty;
	
	//统计人数
	int getEmpNum();
	
	//将文件数据读到动态数组中
	 void InitEmp();
	 
	//判断员工是否存在
	int IsExist(int id);	 
};

WorkerManager.app
#include"WorkerManager.h"

WorkerManager::WorkerManager()
{
	ifstream ifs;
	ifs.open(FILENAME,ios::in);
	//1.文件不存在 
	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->getEmpNum();
	cout << "职工人数为:" << num << endl;
	this->m_EmpNum = num;
	this->m_EmpArray = new Worker*[this->m_EmpNum];
	this->m_FileIsEmpty = false;
	this->InitEmp();
} 

void WorkerManager::ShowMenu()
{
	cout << "**********************************" << endl;
	cout << "** \t  职工管理系统    \t**" << endl;
	cout << "**********************************" << endl;
	cout << "** \t 1.增加职工信息   \t**" << endl;
	cout << "** \t 2.显示职工信息 \t**" << endl;
	cout << "** \t 3.删除离职职工 \t**" << endl;
	cout << "** \t 4.修改职工信息 \t**" << endl;
	cout << "** \t 5.查找职工信息 \t**" << endl;
	cout << "** \t 6.按照编号排序 \t**" << endl;
	cout << "** \t 7.清空所有文档 \t**" << endl;
	cout << "** \t 0.退出管理系统 \t**" << endl;
	cout << "**********************************" << endl;
} 

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

void WorkerManager::AddEmp()
{
	cout << "请输入添加职工数量" << endl;
	int AddNum;
	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 Depchoice; 
			cout<< "请输入第" << i+1 << "个新员工的编号" << endl; 
			cin >> Id;
			cout<< "请输入第" << i+1 << "个新员工的姓名" << endl; 
			cin >> Name; 
			cout << "请输入员工职位编号" << endl;
			cout << "1.普通员工" << endl;
			cout << "2.经理" << endl;  
			cout << "3.总裁" << endl; 
			cin >> Depchoice;
			Worker *worker;
			switch(Depchoice)
			{
				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; 
			} 
			cout << "成功添加" << AddNum << "名新员工" <<endl;
			NewSpace[this->m_EmpNum+i] = worker; 
			delete[] this->m_EmpArray;
			this->m_EmpArray = NewSpace;
			this->m_EmpNum = NewSize; 
			//保存数据到文件中  
			this->save();
			this->m_FileIsEmpty = false;
		}
	}
	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;
	}
}

int WorkerManager::getEmpNum()
{
	int num = 0;
	ifstream ifs;
	ifs.open(FILENAME,ios::in);
	int Id;
	string Name;
	int DaptId;
	while(ifs>>Id&&ifs>>Name&&ifs>>DaptId)
	{
		num++;
	}
	ifs.close();
	return num;
}

void WorkerManager::ShowEmp()
{
	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");
}

void WorkerManager::InitEmp()
{
	ifstream ifs;
	ifs.open(FILENAME,ios::in);
	
	int Id;
	int Dep;
	string Name;
	
	int index=0;
	
	while(ifs>>Id&&ifs>>Name&&ifs>>Dep)
	{
		Worker *worker = NULL;
		switch(Dep)
		{
			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; 
		} 
		this->m_EmpArray[index] = worker;
		index++;	 
	}
	ifs.close();
}

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::DelEmp()
{
	if(this->m_FileIsEmpty)
	{
		cout << "文件不存在或数据为空" << endl;
	}
	else
	{
		cout << "请输入要删除员工编号" << endl;
		int id; 
		cin >> id;
		if(this->IsExist(id)==-1)
		{
			cout << "删除失败,未找到该职工" << endl;
		}
		else
		{
			for(int i= this->IsExist(id);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");	
}

void WorkerManager::ChangeEmp()
{
	if(this->m_FileIsEmpty)
	{
		cout << "文件不存在或数据为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请输入要修改员工编号" << endl;
		int id; 
		cin >> id;
		int ret = this->IsExist(id);
		if(ret == -1)
		{
			cout << "查找失败,未找到该职工" << endl;
			system("pause");
			system("cls");
		}
		else
		{
			delete this->m_EmpArray[ret];  //ret是编号 
			int Id = 0;
			string Name = " ";
			int Depchoice = 0;
							
			cout<< "查到"<<id << "号员工,请输入新职工号" << endl; 
			cin >> Id;
			cout<< "请输入修改后员工的姓名" << endl; 
			cin >> Name; 
			cout << "请输入员工职位编号" << endl;
			cout << "1.普通员工" << endl;
			cout << "2.经理" << endl;  
			cout << "3.总裁" << endl; 
			cin >> Depchoice;
			Worker *worker;
			switch(Depchoice)
			{
				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; 
			} 
			 this->m_EmpArray[ret] = worker;
			 cout << "修改成功" << endl; 
			 this->save();
		}	 
	}
	system("pause");
	system("cls");
}

void WorkerManager::SerEmp()
{
	if(this->m_FileIsEmpty)
	{
		cout << "文件不存在或数据为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		bool flag = false; 
		cout << "请选择查找方式" << endl;
		cout << "1.按姓名查找" << endl; 
		cout << "2.按编号查找" << endl; 
		int cnt;
		cin >> cnt;
		if(cnt==1)
		{
			cout << "请输入要查找员工姓名" << endl;
			string name;
			cin >> name;
			for(int i=0;i<this->m_EmpNum;i++) 
			{
				if(this->m_EmpArray[i]->m_Name == name)
				{
					flag = true;
					cout << "查找成功职工姓名为" << name <<  "的信息如下:" << endl;
					this->m_EmpArray[i]->showInfo();
					system("pause");
					system("cls");
					break;
				}
			}
		}
		else if(cnt==2)
		{
			cout << "请输入要查找员工编号" << endl;
			int id;
			cin >> id;
			int ret = this->IsExist(id);
			if(ret==-1)
			{
				flag = false;
			}
			else
			{
				flag = true;
				cout << "查找成功职工编号为" << id <<  "号的信息如下:" << endl; 
				this->m_EmpArray[ret]->showInfo();	
				system("pause");
				system("cls");		
			}
		}
		else
		{
			cout << "输入有误" << endl;
		} 
		if(!flag)
		{
			cout << "查找失败,查无此人" << endl;
			system("pause");
			system("cls");
		}
	}
}

void WorkerManager::SortEmp()
{
	if(this->m_FileIsEmpty)
	{
		cout << "文件不存在或数据为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		for(int i=0;i<this->m_EmpNum-1;i++)
		{
			for(int j=i;j<this->m_EmpNum-1;j++)
			{
				if(this->m_EmpArray[j]->m_Id > this->m_EmpArray[j+1]->m_Id)
				{
					Worker *worker = this->m_EmpArray[j];
					this->m_EmpArray[j] = this->m_EmpArray[j+1];
					this->m_EmpArray[j+1] = worker; 
				}
			}
		}
		cout << "排序成功!结果为:" << endl;
		this->save(); 
		this->ShowEmp(); 
	}
}

void WorkerManager::ClearEmp()
{
	cout << "确认清空?" << endl;
	cout << "1.确认" << endl;
	cout << "2.返回" << endl;
	int num;
	cin >> num;
	if(num==2)
	{
		system("pause");
		system("cls");
	} 
	else if(num==1)
	{
		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] = 0;
			}
			//删除堆区数组指针 
			delete[] this->m_EmpArray;
			this->m_EmpArray = NULL;
			this->m_EmpNum = 0;
			this->m_FileIsEmpty = true; 
		}
		cout << "清空成功" << endl; 
		system("pause");
		system("cls");	
	}
	else
	{
		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];	
			}
		}
		delete[] this->m_EmpArray;
		this->m_EmpArray = NULL;
	} 
	
}



Worker.h
#pragma once       
#include<iostream>
#include<stdlib.h> 
#include<string>
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>
#include<stdlib.h> 
#include"Worker.h"
#include<string>
using namespace std;

class Employee:public Worker
{
public:
	Employee(int Id,string name,int dId); 
	//显示个人信息
	void showInfo(); 
	//获取岗位信息 
	string getDeptName();

};

employee.app
#include"employee.h"


//构造函数 
Employee::Employee(int Id,string name,int dId)
{
	m_Id = Id;
	m_Name = name;
	m_DeptId = dId; 
}


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

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

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

//经理类
class Manager:public Worker
{
public:
	Manager(int Id,string name,int dId);
	//显示个人信息
	void showInfo();
	//获取岗位信息 
	string getDeptName();
}; 

manager.app
#include"manager.h"


//构造函数 
Manager::Manager(int Id,string name,int dId)
{
	m_Id = Id;
	m_Name = name;
	m_DeptId = dId; 
}


//显示个人信息
void Manager::showInfo()
{
	cout << "职工编号"       << this->m_Id 
		 << "\t职工姓名" 	 << this->m_Name 
		 << "\t岗位"         << this->getDeptName()
		 << "\t岗位职责:完成老板交给的任务,并下发任务给员工"  << endl; 
}

//获取岗位信息 
string Manager::getDeptName()
{
	return string("经理") ;
}

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


//老板类 
class Boss:public Worker
{
public:
	Boss(int Id,string name,int dId);
	//显示个人信息
	void showInfo();
	//获取岗位信息 
	string getDeptName();
}; 

boss.app
#include"boss.h"


//构造函数 
Boss::Boss(int Id,string name,int dId)
{
	m_Id = Id;
	m_Name = name;
	m_DeptId = dId; 
}


//显示个人信息
void Boss::showInfo()
{
	cout << "职工编号"       << this->m_Id 
		 << "\t职工姓名" 	 << this->m_Name 
		 << "\t岗位"         << this->getDeptName() 
		 << "\t岗位职责:管理公司所有事物"  << endl; 
}

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

代码介绍
本系统是基于面向对象开发的职工管理系统,使用多级指针,多态,继承,动态数组等技术实现。其实非常low,但作为我写的众多项目中较为优秀的一个,对当前阶段的学习有较好的总结作用。对此将项目的学习点和新知识进行总结。

代码的主体当然是我们的main函数,main.app在此通过while死循环加上switch开关选择实现调用功能接口,并构造唯一出口,即当只有选择0时才可以退出程序,在此退出程序使用的是exit(0),并不同以往使用return直接退出。所有的功能调用都通过WorkerManager的实例化调用。

系统的功能实现主要依靠WorkerManager的一个大类控制,WorkerManager类控制各种功能的调用。程序打开时编译器自动调用构造函数从文件提取信息,开辟堆区动态数组实现数据每次打开都可以同步,并检测各种基本数据。程序关闭编译器调用析构函数,可以很好的将堆区开辟的空间统一释放。WorkerManager中写有所有的功能函数和辅助函数。功能函数实现用户需求,辅助函数提供必要数据,辅助功能函数避免不必要的运行消耗,提供数据并及时更新及时同步到文件中存储方便下次程序启动时更新数据。

本程序三种职工区分由多态实现,继承Worker类。在添加职工操作中通过在堆区开辟指针数组依次指向堆区开辟的各个相异的职工岗位。当显示职工操作时直接调用Worker类因多态显示不同职工工作分配。

存储使用的是动态数组,动态数组为顺序存储结构具有所有顺序存储结构共同的优缺点。此处链表的表现应该更加出色但能力不怎么允许。鄙人的链表能存储的数据还没bug多。动态数组是通过再次开辟更大的数据空间,并将原有数据复制粘贴同步到新空间,然后卸磨杀驴,把原有数据存储空间释放。动态数组的删除较为有意思,只是将要删除元素后一位向前覆盖,然后元素数量减一,自然而然的将数据抛弃,剩下的由操作系统解决。

程序问题有一点吧,添加元素时批量添加会导致程序崩溃,但审视代码后又没有发现错误可能有些隐式错误或编译器的问题。

知识点
通过对象建立实现各种功能调用,使用析构构造函数同步信息和释放空间
动态数组的各种操作
多态实现职工类型的区别调用
项目文件的分配
———— 不多说别的,都在代码里。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值