c++职工管理系统

要求

在这里插入图片描述

代码

management_system.cpp(main函数)

#include<iostream>
#include "WorkerManager.h"
#include "Worker.h"
#include "Employee.h"
#include "Manager.h"
#include "Boss.h"
using namespace std;

int main() {
	WorkerManager manager;
	int choice = 0;

	while (true) {
		manager.showMenu();

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

		switch (choice)
		{
		case 0://退出管理程序
			manager.exitSystem();
			break;
		case 1://增加职工信息
			manager.addEmp();
			break;
		case 2://显示职工信息
			manager.showEmp();
			break;
		case 3://删除离职职工
			manager.delEmp();
			break;
		case 4://修改职工信息
			manager.modifyEmp();
			break;
		case 5://查找职工信息
			manager.findEmp();
			break;
		case 6://按照编号排序
			manager.sortEmp();
			break;
		case 7://清空所有文档
			manager.cleanEmp();
			break;
		default:
			system("cls");
			break;
		}
	}

	system("pause");
	return 0;
}

Worker.h(虚基类,作为父类,形成多态)

#pragma once
#include <iostream>
#include <string>
using namespace std;


class Worker {
public:
	int id;
	string name;
	int did;

public:
	//显示个人信息
	virtual void showInfo() = 0;
	//获取岗位名称
	virtual string getDeptName() = 0;
};

注意:虚基类不能有构造函数

Employee.h(普通员工的类)

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

class Employee : public Worker {
public:

	Employee(int id, string name, int did);

	//显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();

	~Employee();
};

Employee.cpp

#include "Employee.h"


Employee::Employee(int id, string name, int did) {
	this->id = id;
	this->name = name;
	this->did = did;
}

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

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


Employee::~Employee() {

}

Manager.h(经理类)

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

class Manager : public Worker {
public:
	Manager(int id, string name, int did);
	//显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();
};

Boss.h(老板类)

#pragma once
#include "Worker.h"

class Boss : public Worker {
public:
	Boss(int id, string name, int did);
	//显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();
};

注:Manager.cpp、Boss.cpp和Employee.cpp雷同,不再详述

WorkerManager.h(整个系统的职工管理的类)

#pragma once
#include<iostream>
#include"Worker.h"
#include "Employee.h"
#include "Manager.h"
#include "Boss.h"
#include <fstream>
#define FILENAME "worker.txt"
using namespace std;


class WorkerManager {
public:
	int empNum;//记录文件中的人员个数
	Worker** pWorkerArray;//员工数组的指针
	bool fileIsEmpty;//判断文件是否存在

	WorkerManager();

	//展示菜单
	void showMenu();

	//退出系统
	void exitSystem();

	//添加职工
	void addEmp();

	//保存文件
	void saveFile();

	//获取文件中人员的个数
	int getFileEmpNum();

	//根据文件初始化员工
	void initEmp();

	//显示职工
	void showEmp();

	//判断职工是否存在
	int isExist(int id);

	//删除职工
	void delEmp();

	//修改职工
	void modifyEmp();

	//查找职工
	void findEmp();

	//员工按编号排序
	void sortEmp();

	//清空员工
	void cleanEmp();

	~WorkerManager();
};

WorkerManager.cpp

#include "WorkerManager.h"


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

	//文件不存在的情况
	if (!ifs.is_open()) {
		/*cout << "文件不存在" << endl;*/
		this->empNum = 0;
		this->pWorkerArray = NULL;
		this->fileIsEmpty = true;
		ifs.close();
		return;
	}
	
	//文件存在但记录为空的情况
	char ch;
	ifs >> ch;
	if (ifs.eof()) {
		/*cout << "文件内容为空" << endl;*/
		this->empNum = 0;
		this->pWorkerArray = NULL;
		this->fileIsEmpty = true;
		ifs.close();
		return;
	}

	//文件存在且记录不为空的情况
	this->empNum = this->getFileEmpNum();
	this->pWorkerArray = new Worker * [this->empNum];
	this->initEmp();
}


//展示菜单
void WorkerManager::showMenu() {
	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 WorkerManager::exitSystem() {
	cout << "退出成功,欢迎下次使用" << endl;
	system("pause");
	exit(0);
}


//添加职工
void WorkerManager::addEmp() {
	cout << "请输入您要添加的人数" << endl;
	int addNum = 0;//保存用户的输入数量
	cin >> addNum;

	if (addNum > 0) {
		int newSize = addNum + this->empNum;
		Worker** pNewSpace = new Worker * [newSize];

		//将原数组中的元素复制到新数组中
		if (this->pWorkerArray != NULL) {
			for (int i = 0; i < this->empNum; i++) {
				pNewSpace[i] = this->pWorkerArray[i];
			}
		}
		delete[] this->pWorkerArray;

		//添加新元素
		for (int i = 0; i < addNum; i++) {
			cout << "请输入您需要添加的第" << (i + 1) << "个员工的编号" << endl;
			int id = 0;
			cin >> id;
			cout << "请输入您需要添加的第" << (i + 1) << "个员工的姓名" << endl;
			string name;
			cin >> name;
			cout << "请输入您需要添加的第" << (i + 1) << "个员工的部门编号" << endl;
			Flag:
			cout << "1:普通员工\t2:经理\t3:老板" << endl;
			int did = 0;
			cin >> did;

			Worker* workerTemp = NULL;
			switch (did)
			{
			case 1:
				workerTemp = new Employee(id, name, did);
				break;
			case 2:
				workerTemp = new Manager(id, name, did);
				break;
			case 3:
				workerTemp = new Boss(id, name, did);
				break;
			default:
				cout << "请输入正确的部门编号" << endl;
				goto Flag;
			}

			pNewSpace[this->empNum + i] = workerTemp;
		}

		//将新数组pNewSpace设置成pWorkerArray
		this->pWorkerArray = pNewSpace;
		this->empNum = newSize;
		this->fileIsEmpty = false;

		this->saveFile();
		cout << "添加成功" << endl;
	}
	else {
		cout << "请输入正确的数字" << endl;
	}

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


//保存文件
void WorkerManager::saveFile() {
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	for (int i = 0; i < this->empNum; i++) {
		ofs << this->pWorkerArray[i]->id << " " << this->pWorkerArray[i]->name << " " << this->pWorkerArray[i]->did << endl;
	}
	ofs.close();
}


//获取文件中人员的个数
int WorkerManager::getFileEmpNum() {
	ifstream ifs;
	int id;
	string name;
	int did;
	int num = 0;

	ifs.open(FILENAME, ios::in);
	while (ifs >> id && ifs >> name && ifs >> did) {
		num++;
	}
	ifs.close();
	return num;
}


//根据文件初始化员工
void WorkerManager::initEmp() {
	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;
		switch (did)
		{
		case 1:
			worker = new Employee(id, name, did);
			break;
		case 2:
			worker = new Manager(id, name, did);
			break;
		case 3:
			worker = new Boss(id, name, did);
			break;
		default:
			break;
		}
		this->pWorkerArray[index++] = worker;
	}

	ifs.close();
}


//显示职工
void WorkerManager::showEmp() {
	if (this->fileIsEmpty) {
		cout << "文件不存在或文件为空" << endl;
	}
	else {
		for (int i = 0; i < this->empNum; i++) {
			this->pWorkerArray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}


//判断职工是否存在
int WorkerManager::isExist(int id) {
	for (int i = 0; i < this->empNum; i++) {
		if (this->pWorkerArray[i]->id == id) {
			return i;
		}
	}
	return -1;
}


//删除职工
void WorkerManager::delEmp() {
	if (this->fileIsEmpty) {
		cout << "文件不存在和记录为空" << endl;
	}
	else {
		cout << "请输入要删除的员工编号" << endl;
		int id = 0;
		cin >> id;
		int index = this->isExist(id);
		if (index != -1) {
			delete this->pWorkerArray[index];
			for (int i = index; i < this->empNum-1; i++) {
				this->pWorkerArray[i] = this->pWorkerArray[i + 1];
			}
			this->empNum--;
			this->saveFile();
			cout << "删除成功" << endl;
		}
		else {
			cout << "该员工不存在" << endl;
		}
	}
	system("pause");
	system("cls");
}


//修改职工
void WorkerManager::modifyEmp() {
	if (this->fileIsEmpty) {
		cout << "文件不存在或记录为空" << endl;
	}
	else {
		cout << "请输入您要修改的员工编号" << endl;
		int id;
		cin >> id;
		int index = this->isExist(id);

		if (index != -1) {
			string name;
			int did;

			cout << "请输入修改后此人的编号" << endl;
			cin >> id;
			cout << "请输入修改后此人的姓名" << endl;
			cin >> name;
			cout << "请输入修改后此人的部门编号" << endl;
			cout << "1、普通职工" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> did;

			delete this->pWorkerArray[index];
			Worker* worker = NULL;

			switch (did)
			{
			case 1:
				worker = new Employee(id, name, did);
				break;
			case 2:
				worker = new Manager(id, name, did);
				break;
			case 3:
				worker = new Boss(id, name, did);
				break;
			default:
				break;
			}

			this->pWorkerArray[index] = worker;
			this->saveFile();
			cout << "修改完成" << endl;
		}
		else {
			cout << "查无此人" << endl;
		}
	}

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


//查找职工
void WorkerManager::findEmp() {
	if (this->fileIsEmpty) {
		cout << "文件不存在或记录为空" << endl;
	}
	else {
		cout << "请输入您要查找的方式\n1:通过编号查找\n2:通过姓名查找" << endl;
		int findType = 0;
		cin >> findType;

		if (findType == 1) {
			cout << "请输入要查找职工的编号" << endl;
			int id;
			cin >> id;
			int index = this->isExist(id);
			if (index != -1) {
				this->pWorkerArray[index]->showInfo();
			}
			else {
				cout << "查无此人" << endl;
			}
		}

		else if (findType == 2) {
			cout << "请输入要查找的职工的姓名" << endl;
			string name;
			cin >> name;
			bool flag = false;

			for (int i = 0; i < this->empNum; i++) {
				if (this->pWorkerArray[i]->name == name) {
					this->pWorkerArray[i]->showInfo();
					flag = true;
				}
			}

			if (flag==false) {
				cout << "查无此人" << endl;
			}
		}

		else {
			cout << "输入选项有误" << endl;
		}
	}

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


//员工按编号排序
void WorkerManager::sortEmp() {
	if (this->fileIsEmpty) {
		cout << "文件不存在或记录为空" << endl;

		system("pause");
		system("cls");
	}
	else {
		cout << "请选择排序的的顺序:\n1:升序\n2:降序" << endl;
		int selection = 0;
		cin >> selection;
		if (selection == 1) {
			for (int i = 0; i < this->empNum; i++) {
				int min = i;
				for (int j = i+1; j < this->empNum; j++) {
					if (this->pWorkerArray[min]->id > this->pWorkerArray[j]->id) {
						min = j;
					}
				}
				if (min != i) {
					Worker* temp = this->pWorkerArray[min];
					this->pWorkerArray[min] = this->pWorkerArray[i];
					this->pWorkerArray[i] = temp;
				}
			}

			cout << "排序成功,排序后的职工信息为" << endl;
			this->showEmp();
			this->saveFile();
		}

		else if (selection == 2) {
			for (int i = 0; i < this->empNum; i++) {
				int max = i;
				for (int j = i+1; j < this->empNum; j++) {
					if (this->pWorkerArray[max]->id < this->pWorkerArray[j]->id) {
						max = j;
					}
				}

				if (max != i) {
					Worker* temp = this->pWorkerArray[max];
					this->pWorkerArray[max] = this->pWorkerArray[i];
					this->pWorkerArray[i] = temp;
				}
			}

			cout << "排序成功,排序后的职工信息为" << endl;
			this->showEmp();
			this->saveFile();
		}
		else {
			cout << "输入选项有误" << endl;
		}
	}
}


//清空员工
void WorkerManager::cleanEmp() {
	if (this->fileIsEmpty) {
		cout << "文件已清空" << endl;
	}
	else {
		cout << "是否确认清空(y/n)" << endl;
		char ch;
		cin >> ch;
		if (ch == 'y') {
			//清空文件记录
			ofstream ofs;
			ofs.open(FILENAME, ios::trunc);
			ofs.close();

			//清空内存中的记录
			if (this->pWorkerArray != NULL) {
				for (int i = 0; i < this->empNum; i++) {
					if (this->pWorkerArray[i] != NULL) {
						delete this->pWorkerArray[i];
					}
				}
				delete[] this->pWorkerArray;
				this->pWorkerArray = NULL;
				this->empNum = 0;
				this->fileIsEmpty = true;
			}
			cout << "清空成功" << endl;
		}
	}

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


WorkerManager::~WorkerManager() {
	if (this->pWorkerArray != NULL) {
		for (int i = 0; i < this->empNum; i++) {
			delete this->pWorkerArray[i];
		}
		delete[] this->pWorkerArray;
		this->pWorkerArray = NULL;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值