C++自学笔记小记(十三)面向对象编程综合案例:职工信息管理系统(黑马)

面向对象编程自学笔记整理

十三、面向对象编程综合案例:职工信息管理系统

职工管理系统制作.cpp

//C++实现基于多态的职工管理系统
//管理系统实现功能:
//1.退出当前管理系统
//2.增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号,职工姓名,部门编号
//3.显示职工信息:显示公司内部的所有职工的信息
//4.删除离职员工:按照编号删除指定职工
//5.修改职工信息:按照编号修改职工个人信息
//6.查找职工信息:按照编号或者职工姓名查找相关人员信息
//7.按照编号排序:按照职工编号,进行排序,排序规则由用户指定
//8.清空所有文档:清空文件记录中所有职工信息(删除之前要再次确认)

#include<iostream>
#include"workerManger.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"

using namespace std;

//测试案例
void test01()
{
	//父类为worker,为抽象类
	//父类指针指向子类对象
	worker * worker = NULL;//产生父类的指针,去引用子类的重定义的纯虚函数、
	
	worker = new Employee(1, "张三", 1);
	worker->showInfo();//同一函数名做出不同反应
	delete worker;//手动删除堆区数据

	worker = new Manager(2, "李四", 2);
	worker->showInfo();//多态
	delete worker;//手动删除堆区数据

	worker = new Boss(3, "王五", 3);
	worker->showInfo();//多态
	delete worker;//手动删除堆区数据

}
int main()
{
	//test01();
	//实例化一个管理者对象
	workerManger wkmaner;

	//存储用户的选项
	int choice = 0;
	while (true)//让代码一直在这个循环运行,除非遇到0退出
	{
		//调用展示菜单的成员函数
		wkmaner.Show_Menu();

		cout << "请输入您的选择:";
		cin >> choice;//接收用户的选项

		switch (choice)
		{
		case 0://退出系统
			wkmaner.exitSystem();
			break;
		case 1://增加职工
			wkmaner.Add_emp();
			break;
		case 2://显示职工
			wkmaner.show_emp();
			break;
		case 3://删除职工
			wkmaner.Del_emp();
			break;
		case 4://修改职工
			wkmaner.Mod_emp();
			break;
		case 5://查找职工
			wkmaner.Find_emp();
			break;
		case 6://排序职工
			wkmaner.Sort_emp();
			break;
		case 7://清空文档
			wkmaner.Clean_file();
			break;

		default:
			//system("cls");//清屏操作
			break;//输入其他选项退出系统
		}
	}
	//system("pause");
	return 0;
}

workerManger.cpp

#include"workerManger.h"


workerManger::workerManger()//类的构造函数空实现
{

	//情况1.文件不存在
	ifstream ifs;
	ifs.open(FILENAME, ios::in);//读文件

	if (!ifs.is_open())
	{
		//cout << "文件不存在" << endl;//测试输出
		this->m_Empnum = 0;//初始化人数
		this->m_Fileisempty = true;//初始化文件为空标志
		this->m_Emparry = NULL;//初始化数组
		ifs.close();//关闭文件
		return;
	}

	//情况2.文件存在且为空,没有记录
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//cout << "文件为空" << endl;
		//初始化记录人数
		this->m_Empnum = 0;
		//初始化数组指针
		this->m_Fileisempty = true;
		//初始化文件是否为空
		this->m_Emparry = NULL;
		ifs.close();
		return;
	}

	//情况3.文件存在,并且记录数据
	int num = this->get_Empnum();
	//cout << "职工人数为:" << num << endl;
	this->m_Empnum = num;

	//根据员工数创建数组
	this->m_Emparry = new worker * [this->m_Empnum]; \

	//初始化职工
	init_emp();

	//测试代码
	for (int i = 0; i < m_Empnum; i++)
	{
		//cout << "职工号:" << this->m_Emparry[i]->m_ID
			//<< "\t职工姓名:" << this->m_Emparry[i]->m_name
			//<< "\t部门编号:" << this->m_Emparry[i]->m_deptid << endl;
	
	}



}

//展示菜单定义
void workerManger::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 workerManger::exitSystem()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

添加职工函数定义
void workerManger::Add_emp()
{
	cout << "请输入添加职工数目:" ;

	int addNum = 0;//保存用户的输入数量
	cin >> addNum;
	
	if (addNum > 0)
	{
		//计算新空间大小
		int newSize = this->m_Empnum + addNum;//新空间人数=原来记录人数+新增人数
		//开辟新空间
		worker** newspace = new worker * [newSize];

		//将原来空间内容放到新空间下
		if (this->m_Emparry != NULL)
		{
			for (int i = 0; i < this->m_Empnum; i++)
			{
				newspace[i] = this->m_Emparry[i];
			}
		}

		//输入新数据,批量添加
		for (int i = 0; i < addNum; i++)
		{
			int id;//职工编号
			string name;//职工姓名
			int dselect;//部门选择
			
			cout << "请输入" << i + 1 << "个新员工编号:";
			cin >> id;

			cout << "请输入" << i + 1 << "个新职工姓名:";
			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;
		}
		//释放原有空间
		delete[] this->m_Emparry;

		//更改新空间指向
		this->m_Emparry = newspace;

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

		//更新职工不为空标志
		this->m_Fileisempty = false;
		
		//输入提示信息
		cout << "成功添加" << addNum << "名新员工!" << endl;
	
		//保存到文件中
		this->save();
	
	}
	else
	{
		cout << "输入有误,请重新输入!" << endl;
	
	}
	//按任意键后,清屏返回上一级
	system("pause");
	system("cls");
}



//保存文件
void workerManger::save()
{
	ofstream  ofs;
	ofs.open(FILENAME, ios::out);//用输出的方式打开文件——写文件

	//将每个人数据写入文件中
	for (int i = 0; i < this->m_Empnum;i++)
	{
		ofs << this->m_Emparry[i]->m_ID << " "
			<< this->m_Emparry[i]->m_name << " "
			<< this->m_Emparry[i]->m_deptid << endl;
	}
	//关闭文件
	ofs.close();
}


//统计人数
int workerManger::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 workerManger::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, did);
		}
		else if (did == 2)//经理
		{
			worker = new Manager(id, name, did);
		}
		else if (did == 3)//总裁
		{
			worker = new Boss(id, name, did);
		}

		//存放在数组中
		this->m_Emparry[index] = worker;
		index++;
	}
}

//显示职工
void workerManger::show_emp()
{
	if (this->m_Fileisempty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		for (int i = 0; i < m_Empnum; i++)
		{
			//利用多态调用接口
			this->m_Emparry[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}

//职工是否存在
int workerManger::Isexist(int ID)
{
	int index = -1;
	for (int i = 0; i < this->m_Empnum; i++)
	{
		if (this->m_Emparry[i]->m_ID == ID)
		{
		index = i;
		break;
		}
	}
	return index;
}



//删除职工
void workerManger::Del_emp()
{
	if (this->m_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;	
	}
	else
	{
	//按照职工编号删除
		cout << "请输入要删除的职工编号:";
		int id = 0;
		cin >> id;
		int index = this->Isexist(id);

		if (index != -1)//说明职工存在,要删除index位置上职工
		{
			for (int i = index; i < this->m_Empnum - 1; i++)
			{
			//数据前移
			this->m_Emparry[i] = this->m_Emparry[i + 1];
			}
			this->m_Empnum--;//更新数组中记录人员个数
		//数据同步跟新到文件中
			this->save();
			cout << "删除成功!"<<endl;
		}
		else
		{
			cout << "删除失败,未找到该职工" << endl;
		}
	}

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


//修改职工
void workerManger::Mod_emp()
{
	if (this->m_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;
	}
	else
	{
		cout << "请输入要修改职工的编号:";
		int id;
		cin >> id;

		int ret = this->Isexist(id);
		if (ret != -1)
		{
		//查找到编号职工
			delete this->m_Emparry[ret];

			int newid = 0;
			string newname = "";
			int dselect = 0;

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

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

			cout << "请输入岗位:" << endl
			     << "1.普通职工" << endl
				 << "2.经理" << endl
				 << "3.总裁" << endl;
			cin >> dselect;

			worker* Worker = NULL;
			switch (dselect)
			{ 
			case 1:
				Worker = new Employee(newid, newname, dselect);
				break;
			case 2:
				Worker = new Manager(newid, newname, dselect);
				break;
			case 3:
				Worker = new Boss(newid, newname, dselect);
				break;
			default:
				break;
			}

			//更改数据到数组中
			this->m_Emparry[ret] = Worker;

			cout << "修改成功" << this->m_Emparry[ret]->m_deptid << endl;

			//保存到文件中
			this->save();
		}
		else
		{
			cout << "查找失败,查无此人" << endl;
		}
	}

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

//查找职工
void workerManger::Find_emp()
{
	if (this->m_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;
	}
	else
	{
		cout << "请输入查找方式:" << endl
			<< "1.按职工编号查找" << endl
			<< "2.按姓名查找" << endl;

		int select = 0;
		cin >> select;

		if (select == 1)//按职工编号查找
		{
			int id;
			cout << "请输入要查找的职工编号:";
			cin >> id;

			int ret = Isexist(id);
			if (ret != -1)
			{
				cout << "查找成功,该职工信息如下:"<<endl;
				this->m_Emparry[ret]->showInfo();
			}
			else
			{
				cout << "查找失败,查无此人" << endl;
			}
		}
		else if (select == 2)//按姓名查找
		{
			string name;
			cout << "请输入要查找的姓名:" ;
			cin >> name;

			bool flag = false;//查找到的标志
			for (int i = 0; i < m_Empnum; i++)
			{
				if (m_Emparry[i]->m_name == name)
				{
					cout << "查找成功,职工编号为:"
						<< m_Emparry[i]->m_ID
						<< "号的信息如下:" << endl;

					flag = true;
					this->m_Emparry[i]->showInfo();
				}
			}
			if (flag == false)
			{
			//查无此人
				cout << "查找失败,查无此人" << endl;
			}
		}
		else
		{
			cout << "输入选项有误" << endl;
		}
	}
	system("pause");
	system("cls");
}

//排序职工
void workerManger::Sort_emp()
{
	if (this->m_Fileisempty)
	{
		cout << "文件不存在或记录为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请选择排序方式:" << endl
			<< "1.按职工号进行升序!" << endl
			<< "2.按职工号进行降序!" << endl;

		int select =0 ;
		cin >> select;

		for (int i = 0; i < m_Empnum; i++)
		{
			int minorMAX = i;//申明最小值或最大值下标
			for (int j = i+1; j < m_Empnum; j++)
			{
				if (select == 1)//升序排列
				{
					if (this->m_Emparry[minorMAX]->m_ID > this->m_Emparry[j]->m_ID)
					{
						minorMAX = j;
					}
				}
				else//降序
				{
					if (this->m_Emparry[minorMAX]->m_ID < this->m_Emparry[j]->m_ID)
					{
						minorMAX = j;
					}
				}                         
			}
			if (i != minorMAX)
			{
				worker* temp = this->m_Emparry[i];
				this->m_Emparry[i] =this->m_Emparry[minorMAX];
				this->m_Emparry[minorMAX] = temp;
			}
		
		}
		cout << "排次成功,排序后的结果为:" << endl;
		this->save();
		this->show_emp();
	}
}

//清空文件
void workerManger::Clean_file()
{
	cout << "确认清空?" << endl
		<< "1.确认" << endl
		<< "2.返回" << endl;
	int select = 0;
	cin >> select;

	if (select == 1)
	{

		//打开模式 ios::trunk若文件存在,删除文件并重新创建
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();

		if (this->m_Emparry != NULL)
		{
			//删除堆区的每个职工对象
			for (int i = 0; i < this->m_Empnum; i++)
			{
				if (this->m_Emparry[i] != NULL)
				{
					delete this->m_Emparry[i];
					this->m_Emparry[i] = NULL;
				}
			}
			//删除堆区数组指针
			this->m_Empnum = 0;
			delete[] this->m_Emparry;
			this->m_Emparry = NULL;
			this->m_Fileisempty = true;
		}
		cout << "清空成功!" << endl;
	}
	system("pause");
	system("cls");
}



//释放堆区数据
workerManger::~workerManger()
{
	if (this->m_Emparry != NULL)
	{
		for (int i = 0; i < this->m_Empnum; i++)
		{
			if (this->m_Emparry[i] != NULL)
			{
				delete this->m_Emparry[i];
			}
		}
		delete[] this->m_Emparry;
		this->m_Emparry = NULL;
	}
}

/*int main()
{
	workerManger wkmaner;
	wkmaner.Show_Menu();
	system("pause");
	return 0;
}*/


workerManger.h

#pragma once//防止头文件重复包含
#include<iostream>
#include<fstream>
#include"worker.h"
#include"employee.h"
#include"boss.h"
#include"manager.h"
using namespace std;

#define FILENAME "empfile.txt"


class workerManger
{
public:

	workerManger();//头文件空构造函数声明

	//展示菜单声明
	void Show_Menu();
	  
	//退出系统成员函数声明
	void exitSystem();   

	//添加职工函数声明
	void Add_emp();

	//记文件中人数数目
	int m_Empnum;

	//员工数组统计指针
	worker ** m_Emparry;//堆区开辟的指针

	//保存文件
	void save();

	//标志文件是否为空
	bool m_Fileisempty;

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

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

	//显示职工
	void show_emp();

	//删除职工
	void Del_emp();

	//按照职工编号判断职工是否存在,存在返回数组中的位置,不存在返回-1
	int Isexist(int ID);

	//修改职工
	void Mod_emp();

	//查找职工
	void Find_emp();

	//排序职工
	void Sort_emp();

	//清空文件
	void Clean_file();


	~workerManger();//头文件空析构函数声明
};


worker.h

//职工分为三类:普通员工,经理,老板
//显示信息时显示职工编号,职工姓名,职工岗位,以及职责
//普通职工:完成经理交代的任务
//经理职责:完成老板交代的任务,并下发给员工
//老板职责:管理公司所有的事务
#pragma once
#include<iostream>
#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;//职工部门编号
};

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 << "职工编号:" << this->m_ID;
	cout << "\t职工姓名:" << this->m_name;
	cout << "\t职工岗位:" << this->getDeptname();
	cout << "\t岗位职责:完成老板交代任务,并下发任务给员工" << endl;
}

//获取职工岗位名称函数实现
string Manager::getDeptname()
{
	return string("经理");

}




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();


};

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 << "职工编号:" << this->m_ID;
	cout << "\t职工姓名:" << this->m_name;//\t为制表符
	cout << "\t职工岗位:" << this->getDeptname();
	cout << "\t岗位职责:完成经理交代任务" << endl;
}

//获取职工岗位名称函数实现
string Employee::getDeptname()
{
	return string("员工");

}


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();


};

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 << "职工编号:" << this->m_ID;
	cout << "\t职工姓名:" << this->m_name;
	cout << "\t职工岗位:" << this->getDeptname();
	cout << "\t岗位职责:管理公司所有任务" << endl;
}

//获取职工岗位名称函数实现
string Boss::getDeptname()
{
	return string("老板");

}

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();


};


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值