c++案例 基于多态的职工管理系统

c++学习笔记

职工管理系统可以用来管理公司内所有员工的信息
本教程主要利用C++来实现一个基于多态的职工管理系统
公司中职工分为三类:普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位

管理系统中需要实现的功能如下:
●退出管理程序:退出当前管理系统
●增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号
●显示职工信息:显示公司内部所有职工的信息
●删除离职职工:按照编号删除指定的职工
●修改职工信息:按照编号修改职工个人信息
●查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息
●按照编号排序:按照职工编号,进行排序,排序规则由用户指定
●清空所有文档:清空文件中记录的所有职工信息(清空前需要再次确认,防止误删)

抽象职工类与其子类员工类、经理类、老板类分别用四个头文件保存。

//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_dept;//员工部门
};

用具体职位类继承抽象职工类

//employee.h
#pragma once
#include <iostream>
using namespace std;
#include <string>
#include "worker.h"

class Employee :public Worker
{
public:
	Employee(int id, string name, int dept);

	virtual void ShowInfo();

	virtual string GetDeptName();
};
//manager.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
#include"worker.h"

class Manager :public Worker
{
public:
	Manager(int id, string name, int dept);

	virtual void ShowInfo();

	virtual string GetDeptName();
};
//boss.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "worker.h"

class Boss :public Worker
{
public:
	Boss(int id, string name, int dept);

	virtual void ShowInfo();

	virtual string GetDeptName();
};

各个具体职位类的函数成员需要具体实现,抽象类由于是纯虚函数成员,不必实现,分别保存至对应的cpp文件中。

//employee.cpp
#include<iostream>
#include<string>
using namespace std;
#include"employee.h"

Employee::Employee(int id, string name, int dept)
{
	this->m_id = id;
	this->m_name = name;
	this->m_dept = dept;
}

void Employee::ShowInfo()
{
	cout << "职工编号:" << this->m_id << "\t"
		<< "职工姓名:" << this->m_name << "\t"
		<< "职工部门:" << this->GetDeptName() << endl;
}

string Employee::GetDeptName()
{
	return "普通职工";
}
//manager.cpp
#include<iostream>
#include<string>
using namespace std;
#include"manager.h"

Manager::Manager(int id, string name, int dept)
{
	this->m_id = id;
	this->m_name = name;
	this->m_dept = dept;
}

void Manager::ShowInfo()
{
	cout << "职工编号:" << this->m_id << "\t"
		<< "职工姓名:" << this->m_name << "\t"
		<< "职工部门:" << this->GetDeptName() << endl;
}

string Manager::GetDeptName()
{
	return "经理";
}
//boss.cpp
#include <iostream>
#include<string>
using namespace std;
#include "boss.h"

Boss::Boss(int id, string name, int dept)
{
	this->m_id = id;
	this->m_name = name;
	this->m_dept = dept;
}

void Boss::ShowInfo()
{
	cout << "职工编号:" << this->m_id << "\t"
		<< "职工姓名:" << this->m_name << "\t"
		<< "职工部门:" << this->GetDeptName() << endl;
}

string Boss::GetDeptName()
{
	return "老板";
}

再提供一个workerManager类来存储顺序表与提供函数接口

#pragma once
#include <iostream>
using namespace std;
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
#include <fstream>
#define FILENAME "workerlist.txt"

class WorkerManager
{
public:
	WorkerManager();

	void ShowMenu();

	void ExitSystem();

	void AddWorker();

	void Save();

	bool m_FileEmpty;

	int GetCounter();

	void InitList();

	int IsExist(int id);

	void DeletWorker();

	void ModifyWorker();

	void SearchWorker();

	void SortList();

	void ClearList();

	void ShowWorker();

	~WorkerManager();
private:

	Worker **workerList;

	int counter;
};

其对应的函数实现存储在对应cpp文件中

//workerManager.cpp
#pragma once
#include"workerManager.h"
using namespace std;

WorkerManager::WorkerManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件不存在!" << endl;
		this->counter = 0;
		this->workerList = NULL;
		this->m_FileEmpty = true;
		ifs.close();
		return;
	}

	char ch;
	ifs >> ch;
	if (ch = ifs.eof())
	{
		cout << "文件为空!" << endl;
		this->counter = 0;
		this->workerList = NULL;
		this->m_FileEmpty = true;
		ifs.close();
		return;
	}

	int num = this->GetCounter();
	cout << "当前有" << num << "位职员" << endl;
	this->counter = num;
	this->workerList = new Worker *[this->counter];
	this->InitList();
}

void WorkerManager::ShowMenu()
{
	cout << "-----------------------------------------" << endl;
	cout << "- Welcome to the worker manager system! -" << 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::AddWorker()
{
	cout << "请输入要添加的职员个数:"<<endl;
	int addNum;
	cin >> addNum;
	
	if (addNum > 0)
	{
		int newNum = addNum + this->counter;
		Worker **newList = new Worker*[newNum];
		
		if (this->workerList != NULL)
		{
			for (int i = 0; i < counter; i++)
			{
				newList[i] = this->workerList[i];
			}
		}
			for (int i = 0; i < addNum; i++)
			{
				int addId;
				string addName;
				int addDept;

				cout << "请输入第" << i+1 << "个要添加的职员的编号:" << endl;
				cin >> addId;
				cout << "请输入第" << i+1 << "个要添加的职员的姓名:" << endl;
				cin >> addName;
				cout << "请输入第" << i+1 << "个要添加的职员的部门:" << endl;
				cout << "1.普通员工   2.经理   3.老板" << endl;
				cin >> addDept;

				Worker *newWorker = NULL;

				switch (addDept)
				{
				case 1:
					newWorker = new Employee(addId, addName, 1);
					break;
				case 2:
					newWorker = new Manager(addId, addName, 2);
					break;
				case 3:
					newWorker = new Boss(addId, addName, 3);
					break;
				}
				newList[i + this -> counter] = newWorker;

			}
			delete[] this->workerList;
			this -> workerList = newList;
			this -> counter = newNum;
			cout << "添加完成!" << endl;
			this->m_FileEmpty = false;
			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->counter; i++)
	{
		ofs << this->workerList[i]->m_id << " "
			<< this->workerList[i]->m_name << " "
			<< this->workerList[i]->m_dept<<endl;
	}

	ofs.close();
}

int WorkerManager::GetCounter()
{
	ifstream ifs;
	ifs.open(FILENAME, ifs.in);
	int num = 0;
	int id;
	string name;
	int dept;

	while (ifs >> id && ifs >> name && ifs >> dept)
	{
		num++;
	}
	return num;
}

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

	int id;
	string name;
	int dept;
	Worker *worker = NULL;
	int index = 0;
	while (ifs >> id && ifs >> name && ifs >> dept)
	{
		if (dept == 1)
			worker = new Employee(id, name, 1);
		else if (dept == 2)
			worker = new Manager(id, name, 2);
		else
			worker = new Boss(id, name, 3);

		this->workerList[index] = worker;
		index++;
	}
	ifs.close();



}

void WorkerManager::ShowWorker()
{
	if ( this->m_FileEmpty )
	{
		cout << "还没有添加职员!" << endl;
	}
	else
	{
		for (int i = 0; i < counter; i++)
		{
			this->workerList[i]->ShowInfo();
		}
	}
	system("pause");
	system("cls");
}

int WorkerManager::IsExist(int id)
{
	int index = -1;

	for (int i = 0; i < this->counter; i++)
	{
		if (id == this->workerList[i]->m_id)
		{
			index = i;
			break;
		}
	}
	return index;
}

void WorkerManager::DeletWorker()
{
	
	if (!this->m_FileEmpty)
	{
		cout << "输入要删除的职员编号:" << endl;
		int d_num;
		cin >> d_num;
		int index = this->IsExist(d_num);
		if (index != -1)
		{
			for (int i = 0; i < this->counter - index - 1; i++)
			{
				this->workerList[index + i] = this->workerList[index + 1 + i];
			}
			this->counter--;
			this->Save();
			cout << "删除完成!" << endl;
		}
		else
		{
			cout << "该职员不存在!" << endl;
		}
	}
	else
	{
		cout << "文件不存在或为空!" << endl;
	}
	
	system("pause");
	system("cls");
}

void WorkerManager::ModifyWorker()
{
	if (this->m_FileEmpty)
	{
		cout << "文件为空或不存在!";
	}
	else
	{
		cout << "请输入要修改的员工编号:" << endl;
		int m_num;
		cin >> m_num;
		int index = this->IsExist(m_num);
		if (index != -1)
		{
			int id;
			string name;
			int dept;
			cout << "请输入修改后的编号:" << endl;
			cin >> id;
			cout << "请输入修改后的姓名:" << endl;
			cin >> name;
			cout << "请输入修改后的部门:" << endl;
			cout << "1 - 普通员工  2 - 经理  3 - 老板" << endl;
			cin >> dept;
 
			delete this->workerList[index];
			this->workerList[index] = NULL;

			if (dept == 1)
			{
				this->workerList[index] = new Employee(id, name, 1);
			}
			else if (dept == 2)
			{
				this->workerList[index] = new Manager(id, name, 2);
			}
			else
			{
				this->workerList[index] = new Boss(id, name, 3);
			}

			cout << "修改完成!" << endl;
			this->Save();
		}
		else
		{
			cout << "该员工不存在!" << endl;
		}
	}
	
	system("pause");
	system("cls");
}

void WorkerManager::SearchWorker()
{
	if (this->m_FileEmpty)
		cout << "文件不存在或为空!" << endl;
	else
	{
		cout << "1 - 按姓名查找    2 - 按编号查找" << endl;
		int select = 0;
		cin >> select;

		if (select == 1)
		{
			string s_name;
			cout << "请输入要查找的职员姓名:" << endl;
			cin >> s_name;
			int flag = 0;
			for (int i = 0; i < this->counter; i++)
			{
				if (this->workerList[i]->m_name == s_name)
				{
					cout << "查找成功!" << endl;
					this->workerList[i]->ShowInfo();
					flag = 1;
				}
			}
			if (flag == 0)
				cout << "查找失败!查无此人!" << endl;

		}
		else if (select == 2)
		{
			cout << "请输入要查找的职员编号:" << endl;
			int s_id;
			cin >> s_id;
			int index = this->IsExist(s_id);
			if (index != -1)
			{
				cout << "查找成功!" << endl;
				this->workerList[index]->ShowInfo();
			}
			else
				cout << "查找失败!查无此人!" << endl;
		}
		else
			cout << "输入有误!" << endl;


	}
	system("pause");
	system("cls");
}

void WorkerManager::SortList()
{
	if (this->m_FileEmpty)
	{
		cout << "文件为空或不存在!" << endl;
	}
	else
	{
		if (this->counter == 0)
			cout << "职员为空!" << endl;
		else
		{
			cout << "1 - 从大到小排序    2 - 从小到大排序" << endl;
			int select = 0;
			cin >> select;
			if (select == 1)
			{
				for (int i = 0; i < counter - 1; i++)
				{
					int flag = 0;
					for (int j = this->counter - i - 1; j > 0; j--)
					{
						if (this->workerList[flag]->m_id < this->workerList[flag + 1]->m_id)
						{
							Worker *temp = NULL;
							temp = this->workerList[flag];
							this->workerList[flag] = this->workerList[flag + 1];
							this->workerList[flag + 1] = temp;
						}
						flag++;
					}
					
				}
			}
			else if (select == 2)
			{
				for (int i = 0; i < counter - 1; i++)
				{
					int flag = 0;
					for (int j = this->counter - i - 1; j > 0; j--)
					{
						if (this->workerList[flag]->m_id > this->workerList[flag + 1]->m_id)
						{
							Worker *temp = NULL;
							temp = this->workerList[flag];
							this->workerList[flag] = this->workerList[flag + 1];
							this->workerList[flag + 1] = temp;
						}
						flag++;
					}

				}
			}
			
		}
		this->Save();
		cout << "排序完成!" << endl;
	}
	system("pause");
	system("cls");
}

void WorkerManager::ClearList()
{
	if (!this->m_FileEmpty)
	{
		for (int i = 0; i < counter; i++)
		{
			delete this->workerList[i];
			this->workerList[i] = NULL;
		}
		this->counter = 0;
		this->m_FileEmpty = true;
	}
	this->Save();
	cout << "清空完成" << endl;
	system("pause");
	system("cls");
}

WorkerManager::~WorkerManager()
{
	delete[] this->workerList;
	this->workerList = NULL;
}

最后用main函数调用接口即可。

//职工管理系统.cpp
#include<iostream>
using namespace std;
#include"workerManager.h"
#include"employee.h"
#include "manager.h"
#include "boss.h"

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

	while (true)
	{
		wm.ShowMenu();
		cin >> op;

		switch (op)
		{
		case 0://退出程序
			wm.ExitSystem();
			break;
		case 1://添加职工
			wm.AddWorker();
			break;
		case 2://显示职工
			wm.ShowWorker();
			break;
		case 3://删除职工
			wm.DeletWorker();
			break;
		case 4://修改职工
			wm.ModifyWorker();
			break;
		case 5://查找职工
			wm.SearchWorker();
			break;
		case 6://职工排序
			wm.SortList();
			break;
		case 7://清空职工
			wm.ClearList();
			break;
		default:
			system("cls");
			break;
		}
	}
	system("pause");
	return 0;
}
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值