2021-12-28 职工管理系统案例

这是一个使用C++编写的员工管理系统,包括了员工、经理和老板的类,并实现了添加、删除、显示、修改、查找和排序等功能。系统通过文件进行数据持久化,支持动态扩展员工数组并保存至文件,同时具备清空文件的功能。
摘要由CSDN通过智能技术生成

在这里插入图片描述
在这里插入图片描述

一 主函数

#include <stdio.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string> 
#include<fstream>

#include "workerManager.h"
#include "worker.h"
#include "employee.h"

#include "manager.h"
#include "boss.h"

using namespace std;
int main()
{
	//测试代码
	//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;

	//实例化一个管理者对象
	WorkerManager wm;

	int choice = 0;  //存储用户的选项

	while (true)
	{
		//调用展示菜单的成员函数
		wm.ShowMenu();

		cout << "请输入您的选择:" << endl;
		cin >> choice;

		switch (choice)
		{
		case 0:
			wm.ExitSystem();
			break;

		case 1:  //add worker 
			wm.AddWorker();
			break;
		case 2:  //show worker list
			wm.Show_Emp();
			break;
		case 3:  //delete worker 
			wm.Del_Emp();
			break;
		case 4:  //change worker
			wm.Modify_Emp();
			break;
		case 5:  //find worker
			wm.Find_Emp();
			break;
		case 6:   //sort out 
			wm.Sort_Emp();
			break;
		case 7:   // clear all
			wm.Clear_File();
			break;
		default:
			system("cls"); //清屏
			break;
		}
	}

	system("pause");
	return 0;
}


二 workerManger.cpp

.cpp文件

#include <stdio.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string> 
#include<fstream>

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

using namespace std;

//构造函数  初始化属性
WorkerManager::WorkerManager()
{
	//1.文件不存在
	ifstream isf;
	isf.open(FILENAME, ios::in);

	if (!isf.is_open())  //文件不存在,则打开失败
	{
		cout << "file is not eaxit " << endl;
		this->m_EmpNum = 0;
		this->m_EmpArray = NULL;
		this->m_FileIsEmpyt = true;

		isf.close();

		return;
	}

	//2.文件存在,但是数据是空的
	char ch;
	isf >> ch;
	if (isf.eof())
	{
		cout << "file is not eaxit " << endl;
		this->m_EmpNum = 0;
		this->m_EmpArray = NULL;
		this->m_FileIsEmpyt = true;

		isf.close();

		return;

	}

	//3.文件存在,且有数据
	int num = this->get_EmpNum();
	cout << "the number of worker:" << 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 << "the id of the worker: " << this->m_EmpArray[i]->m_id
		<< " name: " << this->m_EmpArray[i]->m_name
		<< " department: " << this->m_EmpArray[i]->m_departID << endl;
	}
}


void WorkerManager::ShowMenu()
{
	cout << "****************************************************" << endl;
	cout << "*********************** welcome ********************" << endl;
	cout << "*************** 0. quit ****************************" << endl;
	cout << "*************** 1. add worker **********************" << endl;
	cout << "*************** 2. show worker list ****************" << endl;
	cout << "*************** 3. delete worker *******************" << endl;
	cout << "*************** 4. change worker *******************" << endl;
	cout << "*************** 5. find worker *********************" << endl;
	cout << "*************** 6. sort out information by number*** " << endl;
	cout << "*************** 7. clear all ***********************" << endl;
	cout << "****************************************************" << endl;
	
}

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


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

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_departID << endl;
	}
	ofs.close();
}


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

	int id;
	string name;
	int dId;

	int num = 0;
	//ifs >> id ;   ifs >> name ; ifs >> dId分别去读第一行数据,第二行数据,第三行数据,读完三行,是一个人的数据
	while(ifs >> id && ifs >> name && ifs >> dId)
	{
		num++;
	}
	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;

		if (dId == 1)  //1.普通员工
		{
			worker = new Employee(id, name, dId);
		}
		else if (dId == 2)
		{
			worker = new Manager(id, name, dId);
		}
		else
		{
			worker = new Boss(id, name, dId);
		}

		this->m_EmpArray[index] = worker;
		index++;
	}

	ifs.close();
}

void WorkerManager::Show_Emp()
{
	//判断文件是否存在
	if (this->m_FileIsEmpyt)
	{
		cout << "file is empty or not exist" << endl;
	}
	else
	{
		for (int i = 0; i < m_EmpNum; i++)
		{
			//利用多态调用程序接口
			this->m_EmpArray[i]->showInfo();
		}
	}
	//按任意键后清屏
	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::Del_Emp()
{
	if (this->m_FileIsEmpyt)
	{
		cout << "file is empty or not exist" << endl;
	}
	else
	{
		//按照ID来删除员工
		cout << "enter the id of the worker who you want to delete:" << endl;
		int id = 0;
		cin >> id;

		int index = this->IsExist(id);  //返回找到的元素的索引

		if (index != -1)
		{
			//数据前移
			for (int i = index; i < this->m_EmpNum - 1; i++)
			{
				this->m_EmpArray[i] = this->m_EmpArray[i + 1];
			}
			this->m_EmpNum--;
			this->save();
			cout << "succeed delete " << endl;

		}
		else
		{
			cout << "failed to delete  " << endl;
		}

	}
	system("pasue");
	system("cls");
}


void WorkerManager::Modify_Emp()
{
	if (this->m_FileIsEmpyt)
	{
		cout << "file is empty or not exist" << endl;
	}
	else
	{
		cout << "enter the id of the worker who you want to change:" << endl;
		int id = 0;
		cin >> id;

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

			int newId = 0;
			string newName = "";
			int dpartId = 0;

			cout << " find : " << id << " worker,enter newnewId " << endl;
			cin >> newId;

			cout <<" enter newName " << endl;
			cin >> newName;

			cout << "enter new dpartId " << endl;
			cout << "1.ordinary worker " << endl;
			cout << "2.manager" << endl;
			cout << "3.boss " << endl;
			cin >> dpartId;

			Worker* worker = NULL;
			switch (dpartId)
			{
			case 1:
				worker = new Employee(newId, newName, dpartId);
				break;
			case 2:
				worker = new Manager(newId, newName, dpartId);
				break;
			case 3:
				worker = new Boss(newId, newName, dpartId);
				break;
			default:
				break;
			}

			//将上面输入的数据在数组里面更新
			this->m_EmpArray[ret] = worker;

			cout << "succeed chaanging" << endl;

			//save 
			this->save();

		}
		else
		{
			cout << "bot find the people" << endl;
		}
	}

	system("pasue");
	system("cls");
}

//按照编号排序
void WorkerManager::Sort_Emp()
{
	if (this->m_FileIsEmpyt)
	{
		cout << "file is empty or not exist" << endl;
		system("pasue");
		system("cls");
	}
	else
	{
		cout << " 请选择排序方式:" << endl;
		cout << "1.按照职工编号升序排列" << endl;
		cout << "2.按照职工编号降序排列" << endl;
		int select = 0;
		cin >> select;
		for (int i = 0; i < this->m_EmpNum-1; i++)
		{
			int minOrMax = i;  //声明最小值或者最大值下表
			for (int j = i+1; j < this->m_EmpNum; j++)
			{
				if (select == 1)  //升序
				{
					if (this->m_EmpArray[minOrMax]->m_id > this->m_EmpArray[j]->m_id)
					{
						minOrMax = j;
					}
				}
				else  //降序
				{
					if (this->m_EmpArray[minOrMax]->m_id < this->m_EmpArray[j]->m_id)
					{
						minOrMax = j;
					}
				}
			}
			

			//判定一开始设定的最小值或者最大值是不是真实的最小值或最大值,如果不是,要交换数据
			if (i != minOrMax)
			{
				Worker* temp = this->m_EmpArray[i];
				this->m_EmpArray[i] = this->m_EmpArray[minOrMax];
				this->m_EmpArray[minOrMax] = temp;
			}
		}

		cout << "succeed sorting " << endl;
		this->save();
		this->Show_Emp();
	}

}

void WorkerManager::Clear_File()
{
	cout << "are you sure to clear all data" << endl;
	cout << "1. yes" << endl;
	cout << "2. No" << endl;
	int select = 0;
	cin >> select;

	if (select == 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] = NULL;
			}

			//删除堆区数据
			delete[] this->m_EmpArray;
			this->m_EmpArray = NULL;
			this->m_EmpNum = 0;
			this->m_FileIsEmpyt = true;
		}

		cout << "clear all" << endl;
	}

}

void WorkerManager::Find_Emp()
{
	if (this->m_FileIsEmpyt)
	{
		cout << "file is empty or not exist" << endl;
	}
	else
	{
		cout << " 请输入查找方式:" << endl;
		cout << "1.按照职工编号查找" << endl;
		cout << "2.按照姓名查找" << endl;

		int select = 0;
		cin >> select;

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

			int ret = IsExist(id);
			if (ret != -1)
			{
				cout << "succeed fingding, the info of the work is below:" << endl;
				this->m_EmpArray[ret]->showInfo();
			}
			else
			{
				cout << "wrong enter!!!!" << endl;
			}
		}
		else if (select == 2)
		{
			string name;
			cout << "enter name:" << endl;
			cin >> name;

			bool flag = false;
			for (int i = 0; i < m_EmpNum; i++)
			{
				if (this->m_EmpArray[i]->m_name == name)
				{
					cout << "succeed fingding, the info of the work is below:" << endl;
					flag = true;
					this->m_EmpArray[i]->showInfo();
					break;
				}
			}

			if (flag == false)
			{
				cout << "failed finding" << endl;
			}
		}
		else
		{
			cout << "wrong enter!!!!" << endl;
		}
	}
}
void WorkerManager::AddWorker()
{
	int addNum = 0;
	cout << "请输入要添加员工的数量:" << endl;
	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;  //name
			int dpartSelect; //department
			cout << "enter the id of" << i + 1 << "worker" << endl;
			cin >> id;

			cout << "enter the name of" << i + 1 << "worker" << endl;
			cin >> name;


			cout << "please choice the job of the worker: " << endl;
			cout << "1. oridinary worker" <<endl;
			cout << "2. manager" << endl;
			cout << "3. boss" << endl;
			cin >> dpartSelect;

			Worker* worker = NULL;
			switch (dpartSelect)
			{
			case 1:
				worker = new Employee(id, name, 1);

			case 2:
				worker = new Manager(id, name, 2);

			case 3:
				worker = new Boss(id, name, 3);
		    
			default:
				break;
			}
			//将创建的职工的头衔,保存到数组
			newSpace[this->m_EmpNum + i] = worker;

		}

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

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

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

		//更新标志
		this->m_FileIsEmpyt = false;
		cout << "succeed adding  " << addNum << " worker" << endl;

		//保存数据到文件中
		this->save();
	}
	else
	{
		cout << "wrong enter " << endl;
	}

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


.h文件

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

#define FILENAME "empFile.txt"

class WorkerManager
{
public:
	WorkerManager();     //构造函数

    //展示菜单
	void ShowMenu();

	//退出系统
	void ExitSystem();

	//添加职工
	//职工分类为:普通员工,经理,老板,将三种职工抽象为一个类,利用多态管理
	//职工属性为职工编号,姓名,部门编号
	//职工的行为

	~WorkerManager();    //析构函数

	//记录职工的人数
	int m_EmpNum;
	Worker**  m_EmpArray; //定义数组指针

	//添加职工的函数
	void AddWorker();

	//保存文件
	void save();

	//判断文件是否为空的标志
	bool m_FileIsEmpyt;

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

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

	//显示职工
	void Show_Emp();

	//删除职工
	void Del_Emp();

	//判断职工是否存在,如果存在返回员工在数组中的位置,如果不存在返回-1
	int IsExist(int id);

	//修改职员信息
	void Modify_Emp();

	//查找职工
	void Find_Emp();

	//职工排序
	void Sort_Emp();

	//清空文件
	void Clear_File();

};

三 老板类

.cpp

using namespace std;
#include "boss.h"

Boss::Boss(int id, string name, int dID)
{
	this->m_id = id;
	this->m_departID = dID;
	this->m_name = name;  //自身的这个变量的赋值
}

//显示个人信息
void Boss::showInfo()
{
	cout << "职工编号:" << this->m_id
		<< "\t职工姓名:" << this->m_name
		<< "\t职工岗位:" << this->getDepartName()
		<< "\t职工职责: 发财" << endl;
}

//获取岗位名称
string Boss::getDepartName()
{
	return  string("老板");
}

.h

#pragma once
#include <iostream>
#include<string> 

using namespace std;
#include "worker.h"

class Boss :public Worker
{
public:
	//构造函数
	Boss(int id, string name, int dID);

	//显示个人信息
	virtual void showInfo();

	//获取岗位名称
	virtual string getDepartName();
};

四 经理类

.h

#pragma once
#include <iostream>
#include<string> 

using namespace std;
#include "worker.h"

class Manager :public Worker
{
public:
	//构造函数
	Manager(int id, string name, int dID);

	//显示个人信息
	virtual void showInfo();

	//获取岗位名称
	virtual string getDepartName();
};

.cpp

using namespace std;
#include "manager.h"

Manager::Manager(int id, string name, int dID)
{
	this->m_id = id;
	this->m_departID = dID;
	this->m_name = name;  //自身的这个变量的赋值
}

//显示个人信息
void Manager::showInfo()
{
	cout << "职工编号:" << this->m_id
		<< "\t职工姓名:" << this->m_name
		<< "\t职工岗位:" << this->getDepartName()
		<< "\t职工职责: 完成老板的任务" << endl;
}

//获取岗位名称
string Manager::getDepartName()
{
	return  string("经理");
}

五 普通职工类

.h

#pragma once
#include <iostream>
#include<string> 

using namespace std;
#include "worker.h"

class Employee :public Worker
{
public:
	//构造函数
	Employee(int id, string name, int dID);

	//显示个人信息
	virtual void showInfo();

	//获取岗位名称
	virtual string getDepartName();
};

.cpp

#include "employee.h"
#include<string> 
#include <stdio.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string> 
#include<fstream>
using namespace std;

//构造函数
Employee::Employee(int id,string name ,int dID)
{
	this->m_id = id;
	this->m_departID = dID;
	this->m_name = name;  //自身的这个变量的赋值
}

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

}

//获取岗位名称
string Employee::getDepartName()
{
	return  string("员工");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值