【C++项目实践】_职工管理系统

【C++项目实践】_职工管理系统

职工管理系统.cpp

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

//测试多态
void test01()
{
	//测试代码
	AbstractWorker* 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();

 	WorkerManager wm;



	int choice = 0;

	while (true)
	{
		//展示菜单
		wm.Show_Menu();
		//请用户输入操作

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

		switch (choice)
		{
		//0 退出管理程序
		case 0:
			wm.exitSystem();
			break;
		//1 增加职工信息
		case 1:
			wm.AddEmp();
			break;
		//2 显示职工信息
		case 2:
			wm.Show_Emp();
			break;
		//3 删除离职职工
		case 3:
			wm.Del_Emp();
			break;

		//4 修改职工信息
		case 4:
			wm.Mod_Emp();
			break;
		//5 查找职工信息
		case 5:
			wm.Find_Emp();
			break;

		//6 按照编号排序
		case 6:
			wm.Sort_Emp();
			break;
		//7 清空所有文档
		case 7:
			wm.Clean_File();
			break;

		default: 
			system("cls");
			break;

		}

	}
	system("pause");
	return 0;



}

workerManager.h

#pragma once			//防止头文件重复包含
#include<iostream>		//包含输入输出流对象
using namespace std;	//使用标准的命名空间
#include<string>
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"

#include<fstream>

#define FILENAME "empFile.txt"

class WorkerManager {
public:
	WorkerManager();

	void Show_Menu();
	void exitSystem();

	int m_EmpNum;
	AbstractWorker** m_EmpArry;

	void AddEmp();
	void Save();

	bool m_FileIsEmpty;
	int get_EmpNum();
	void init_Emp();
	void Show_Emp();
	int IsExist(int id);
	void Del_Emp();
	void Mod_Emp();
	void Find_Emp();
	void Sort_Emp();
	void Clean_File();

	~WorkerManager();



};

workerManager.cpp

#include"workerManager.h"


WorkerManager::WorkerManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	//文件不存在情况下
	if(!ifs.is_open())
	{
		cout << "文件不存在!" << endl;
		this->m_EmpNum = 0;
		this->m_EmpArry = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}
	//文件存在,数据为空
	char ch;
	ifs >> ch;
	//ch的作用是将文件读取位置向右移动一个字节,然后用ifs.eof()检测当前位置是否为尾部标志!是的话返回真,则原文件为空
	if (ifs.eof())
	{
		cout << "文件为空!" << endl;
		this->m_EmpNum = 0;
		this->m_EmpArry = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

    //其他情况下,就是文件存在,而且不为空
	//获得职工数量,开辟内存,初始化员工(读取文件内的数据存入内存) 
	int num = this->get_EmpNum();
	cout << "当前文件中存储" << num << "位职工的信息" << endl;
	this->m_EmpNum = num;
	this->m_EmpArry = new AbstractWorker * [this->m_EmpNum];
	init_Emp();

}


void WorkerManager::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 WorkerManager::exitSystem()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);

}

void WorkerManager::AddEmp()
{
	cout << "请输入要添加的职工数量:" << endl;
	int addNum = 0;
	cin >> addNum;

	if (addNum > 0)
	{
		int newSize = this->m_EmpNum + addNum;
		//开辟一个新空间,防止之前开辟的空间不够
		//等号左边如果是一个*,代表定义一个指向子类的地址,这个地址指向的数据在堆取
		//两个**代表解指针,直接代表地址指向的数据,等号右边也是???

		//这里newspace被开辟在栈区
		 AbstractWorker** newspace = new AbstractWorker * [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<<"位职工的编号:" << endl;
			cin >> id;
			cout << "请输入第" << i+1 << "位职工的姓名:" << endl;
			cin >> name;
			cout << "请选择该职工的岗位:" << endl;
			cout << "1、普通职工" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> dSelect;

			AbstractWorker* 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:
				cout << "职位输入错误,添加失败!" << endl;
				break;
			}

			newspace[this->m_EmpNum + i] = worker;

		}

		delete[] this->m_EmpArry;
		this->m_EmpArry = newspace;
		this->m_EmpNum = newSize;
		this->m_FileIsEmpty = false;

		//将新添加的员工数据保存到文件
		this->Save();

		cout << "成功添加" << addNum << "名新职工!" << endl;
	}
	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_EmpArry[i]->m_Id << " "
			<< this->m_EmpArry[i]->m_Name << " "
			<< this->m_EmpArry[i]->m_DeptId << endl;

	}
	ofs.close();

}

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

	int id;
	string name;
	int dId;
	int num = 0;	
	//读取一行数据,num加1
	while (ifs >> id && ifs >> name && ifs >> dId)
	{
		num++;
	}

	ifs.close();
	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)
	{
		AbstractWorker* 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->m_EmpArry[index] = worker;
		index++;
	}

}


void WorkerManager::Show_Emp()
{

	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空" << endl;

	}
	else
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			this->m_EmpArry[i]->showInfo();
		}
	}

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

}

int WorkerManager::IsExist(int id)
{
	for (int i = 0; i < this->m_EmpNum; i++)
	{
		if (this->m_EmpArry[i]->m_Id == id)
		{
			return i;
		}
	}

	return -1;


}

void WorkerManager::Del_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空" << endl;

	}
	else
	{
		cout << "请输入您要删除的职工编号:" << endl;
		int id;
		cin >> id;
		int index = IsExist(id);
		if (index != -1)
		{
			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 WorkerManager::Mod_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空" << endl;

	}
	else 
	{
		cout << "请输入您要修改的职工编号:" << endl;
		int id;
		cin >> id;
		int index = IsExist(id);
		if (index != -1)
		{
			cout << "查找到职工!" << endl;
			//删除此位置的数据
			delete this->m_EmpArry[index];

			int newId;
			string newName;
			int dSelect;

			cout << "请输入修改后的职工编号:" << endl;
			cin >> newId;
			cout << "请输入修改后的职工姓名:" << endl;
			cin >> newName;
			cout << "请选择修改后的职工岗位:" << endl;
			cout << "1、普通职工" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> dSelect;

			AbstractWorker* worker = NULL;
			switch (dSelect)
			{
			case 1:
				worker = new Employee(newId, newName, 1);
				break;
			case 2:
				worker = new Manager(newId, newName, 2);
				break;
			case 3:
				worker = new Boss(newId, newName, 3);
				break;
			default:
				cout << "职位输入错误,添加失败!" << endl;
				break;
			}
			//将删除数据的地址重新填入数据
			this->m_EmpArry[index] = worker;
			this->Save();
			cout << "修改成功" << endl;

		}
		else
		{
			cout << "您输入的职工编号不存在!" << endl;
		}
	
	}


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

void WorkerManager::Find_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空" << endl;

	}
	else
	{
		int select = 0;
		cout << "请输入查找方式:1 编号,2 姓名" << endl;
		cin >> select;

		switch(select)
		{
		case 1:
		{
			cout << "请输入您要查找的职工编号:" << endl;
			int id;
			cin >> id;
			int index = IsExist(id);
			if (index != -1)
			{
				cout << "查找成功,职工信息如下:" << endl;
				this->m_EmpArry[index]->showInfo();
			}
			else
			{
				cout << "您输入的职工编号不存在!" << endl;
			}
			break;
		}
		case 2:
		{
			cout << "请输入您要查找的职工姓名:" << endl;
			string name;
			cin >> name;
			int flag = 0;
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				if (this->m_EmpArry[i]->m_Name == name)
				{
					cout << "查找成功,信息如下:" << endl;
					this->m_EmpArry[i]->showInfo();
					flag = 1;
				}
			}
			if(!flag)
			{
				cout << "您输入的职工姓名不存在!" << endl;
			}

			break;
		
		}
		default:
			break;
		}

	}


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

}


void WorkerManager::Sort_Emp()
{

	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空" << endl;

	}
	else
	{
		cout << "选择排序类型:1 从大到小,2 从小到大" << endl;
		int select = 0;
		cin >> select;

		if (select == 1)
		{
			AbstractWorker* worker = NULL;
			for(int i=0;i<this->m_EmpNum;i++)
				for (int j = i+1; j < this->m_EmpNum ; j++)
				{
					if (this->m_EmpArry[i]->m_Id < this->m_EmpArry[j]->m_Id)
					{
						worker  = this->m_EmpArry[i];
						this->m_EmpArry[i] = this->m_EmpArry[j];
						this->m_EmpArry[j] = worker;

					}


				}
			cout << "从大到小排序成功!" << endl;
			this->Save();
		}
		else if (select == 2)
		{
			AbstractWorker* worker = NULL;
			for (int i = 0; i < this->m_EmpNum; i++)
				for (int j = i+1; j < this->m_EmpNum ; j++)
				{
					if (this->m_EmpArry[i]->m_Id > this->m_EmpArry[j]->m_Id)
					{
						worker = this->m_EmpArry[i];
						this->m_EmpArry[i] = this->m_EmpArry[j];
						this->m_EmpArry[j] = worker;

					}


				}
			cout << "从小到大排序成功!" << endl;
			this->Save();

		}
		else
		{
			cout << "您输入的排序方式有错!" << endl;
		}



	}

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

}


void WorkerManager::Clean_File()
{
	cout << "确定要清空吗?" << endl;
	cout << "1 确定" << endl;
	cout << "2 返回" << endl;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		//删除文件,重新创建一个同名新文件
		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_EmpNum = 0;
			delete[] this->m_EmpArry;
			this->m_EmpArry = NULL;
			this->m_FileIsEmpty = true;
		}
		cout << "已清空!" << endl;
	}

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

}




WorkerManager::~WorkerManager()
{
	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_EmpNum = 0;
		delete[] this->m_EmpArry;
		this->m_EmpArry = NULL;
	}
	//cout << "调用析构函数" << endl;

}

worker.h

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


class AbstractWorker {
public:
	//AbstractWorker();

	//纯虚函数!!!只要在子类中实现即可!!!
	//显示个人信息
	virtual void showInfo() = 0;
	//获得岗位名称
	virtual string getDeptName() = 0;


	//~AbstractWorker();

	int m_Id;
	string m_Name;
	int m_DeptId;

};

worker.cpp

#include"worker.h"

//AbstractWorker::AbstractWorker()
//{
//
//}
//
//AbstractWorker::~AbstractWorker()
//{
//
//	
//
//}

employee.h

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

class Employee:public AbstractWorker {
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->m_Id = id;
	this->m_Name = name;
	this->m_DeptId = dId;

}

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

}
string Employee::getDeptName()
{
	return string("员工");

}

manager.h

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

class Manager :public AbstractWorker {
public:

	Manager(int id, string name, int dId);
	//纯虚函数!!!只要在子类中实现即可!!!
	//显示个人信息
	virtual void showInfo();
	//获得岗位名称
	virtual string getDeptName();


	//~Employee();

};


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
		<< "\t职工姓名:" << this->m_Name
		<< "\t岗位:" << this->getDeptName()
		<< "\t岗位职责:完成老板交给的任务,并下发任务给员工" << endl;

}
string Manager::getDeptName()
{
	return string("经理");

}

boss.h

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

class Boss :public AbstractWorker {
public:

	Boss(int id, string name, int dId);
	//纯虚函数!!!只要在子类中实现即可!!!
	//显示个人信息
	virtual void showInfo();
	//获得岗位名称
	virtual string getDeptName();


	//~Employee();

};

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
		<< "\t职工姓名:" << this->m_Name
		<< "\t岗位:" << this->getDeptName()
		<< "\t岗位职责:管理公司所有事物" << endl;

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

}

开发环境:VS2019
参考:黑马程序员b站视频

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值