c++ 实现职工管理系统

发布于:2023-01-04 ⋅ 阅读:(294) ⋅ 点赞:(0)

一、案例描述:

某公司中的职工分为三种:普通员工,经理和老板。

每个职工都有自己的职工编号、姓名、年龄、电话号码和岗位。

管理系统需要实现的功能如下:

●添加职工信息

●显示职工信息

●删除离职职工

●修改职工信息

●查找职工信息

●职工编号排序

●清空职工信息

●退出管理系统

尝试用c++的面向对象内容和多态技术实现一个简单的职工管理系统并锻炼自己分文件编写的能力。

二、案例分析

构造一个抽象类 class Worker,让普通员工类class Employee,经理类 class Manager,老板类 class Boss继承抽象类。

构造一个管理员类 class Administer,实现菜单显示,功能实现,退出系统等操作。

 

三、头文件

五个头文件,包含类的声明。

//Woker.h
#pragma once
using namespace std;

class Worker
{
public:
	virtual void ShowInfo() = 0;
	virtual string GetStation() = 0;

	int number;    //职工编号
	string name;   //姓名
	int age;       //年龄
	string phone;  //电话号码
	int num;       //部门编号
};
//Employee.h
#pragma once
#include"Woker.h"
using namespace std;

class Employee :public Worker
{
public:
	Employee(int number, string name, int age, string phone, int num);
	virtual void ShowInfo();
	virtual string GetStation();
};
//Manager.h
#pragma once
#include"Woker.h"
using namespace std;

class Manager :public Worker
{
public:
	Manager(int number, string name, int age, string phone, int num);
	virtual void ShowInfo();
	virtual string GetStation();
};
//Boss.h
#pragma once
#include"Woker.h"

class Boss:public Worker
{
public:
	Boss(int number, string name, int age, string phone, int num);
	virtual void ShowInfo();
	virtual string GetStation();
};
//Administer.h
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include"Woker.h"
using namespace std;

//管理员
class Administer
{
public:
	Administer();	//构造函数

	void ShowMenu();	//展示菜单
	
	int GetNumber();	//获取文件中员工个数
	
	void InitWorker();	//初始化数组

	void Addmenber();	//添加职工

	void ShowWorker();	//显示职工信息

	void DelWorker();	//删除职工

	void ModWorker();	//修改职工信息

	void FindWorker();	//查找职工

	void SortWorker();	//排序

	int IsExist(int number);	//通过编号判断职工是否存在

	void Save();	//保存文件

	void CleanWorker();		//清空文件

	void QuitSystem();		//退出系统

	~Administer();		//析构函数 内存释放

	int Number;		//员工总人数
	Worker** Array;	//员工数组指针
	bool IsEmpty;	//判断文件是否存在

};

四、源文件

类的实现

 

 

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

//构造函数 初始化属性
Employee::Employee(int number, string name, int age, string phone, int num)
{
	this->number = number;
	this->name = name;
	this->age = age;
	this->phone = phone;
	this->num = num;
}

void Employee::ShowInfo()
{
	cout << "职工编号: " << this->number;
	cout << "\t姓名: " << this->name;
	cout << "\t年龄: " << this->age;
	cout << "\t电话号码: " << this->phone;
	cout << "\t岗位: " << this->GetStation() << endl;
}

string  Employee::GetStation()
{
	string station;
	station = "普通员工";
	return station;
}

 

//Manager.cpp
#include<iostream>
#include<string>
#include"Manager.h"

Manager::Manager(int number, string name, int age, string phone, int num)
{
	this->number = number;
	this->name = name;
	this->age = age;
	this->phone = phone;
	this->num = num;
}

void Manager::ShowInfo()
{
	cout << "职工编号: " << this->number;
	cout << "\t姓名: " << this->name;
	cout << "\t年龄: " << this->age;
	cout << "\t电话号码: " << this->phone;
	cout << "\t岗位: " << this->GetStation() << endl;
}

string  Manager::GetStation()
{
	string station;
	station = "经理";
	return station;
}
//Boss.cpp
#include<iostream>
#include<string>
#include"Boss.h"


Boss::Boss(int number, string name, int age, string phone, int num)
{
	this->number = number;
	this->name = name;
	this->age = age;
	this->phone = phone;
	this->num = num;
}

void Boss::ShowInfo()
{
	cout << "职工编号: " << this->number;
	cout << "\t姓名: " << this->name;
	cout << "\t年龄: " << this->age;
	cout << "\t电话号码: " << this->phone;
	cout << "\t岗位: " << this->GetStation() << endl;
}

string Boss::GetStation()
{
	string station;
	station = "老板";
	return station;
}
//Administer.cpp
#include<iostream>
#include<fstream>
#include"Administer.h"
#include"Employee.h"
#include"Manager.h"
#include"Boss.h"
#define FILENAME "Wokers.txt"
using namespace std;

//管理员类构造函数
Administer::Administer()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	//文件不存在
	if (!ifs.is_open())
	{
		this->Number = 0;
		this->Array = NULL;
		this->IsEmpty = true;
		ifs.close();
		return;
	}

	//文件为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		this->Number = 0;
		this->Array = NULL;
		this->IsEmpty = true;
		ifs.close();
		return;
	}

	//文件存在且不为空
	else
	{
		this->Number=this->GetNumber();
		this->Array = new Worker * [this->Number];
		this->IsEmpty = false;
		InitWorker();
	}
}

//菜单显示
void Administer::ShowMenu()
{
	cout << "------------------------------------------" << endl;
	cout << "--------  欢迎使用职工管理系统!  --------" << endl;
	cout << "----------  1.添加职工信息  ----------" << endl;
	cout << "----------  2.显示职工信息  ----------" << endl;
	cout << "----------  3.删除职工信息  ----------" << endl;
	cout << "----------  4.修改职工信息  ----------" << endl;
	cout << "----------  5.查找职工信息  ----------" << endl;
	cout << "----------  6.排序职工信息  ----------" << endl;
	cout << "----------  7.清空职工信息  ----------" << endl;
	cout << "----------  0.退出管理系统  ----------" << endl;
	cout << "------------------------------------------" << endl;
}

//获取文件里职工人数
int Administer::GetNumber()
{
	int count = 0;
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	string str;
	while (getline(ifs,str))
	{
		count++;
	}
	ifs.close();
	return count;
}

//初始化数组
void Administer::InitWorker()
{
	int number;
	string name;
	int age;
	string phone;
	int num;
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	Worker* p;
	for (int i = 0; i < this->Number; i++)
	{
		ifs >> number;
		ifs >> name;
		ifs >> age;
		ifs >> phone;
		ifs >> num;
		if (num == 1)
		{
			p = new Employee(number, name, age, phone, num);
		}
		if (num == 2)
		{
			p = new Manager(number, name, age, phone, num);
		}
		else
		{
			p = new Boss(number, name, age, phone, num);
		}
		this->Array[i] = p;
	}
	ifs.close();
}

//添加职工 可以批量添加
void Administer::Addmenber()
{
	int addNum = 0;
	cout << "请输入您要添加的职工数量:" << endl;
	cin >> addNum;
	if (addNum > 0)
	{
		int size = 0;
		size = Number + addNum;
		Worker** newArray= new Worker * [size];
		if (Number != 0)
		{
			for (int i = 0; i < Number; i++)
			{
				newArray[i] = Array[i];
			}
		}

	
		for (int i = 0; i < addNum; i++)
		{
			int number;
			string name;
			int age;
			string phone;
			int num;
			cout << "请输入添加第" << i +1 << "个职工的编号:" << endl;
			int flag = 0;
			while (1)//员工编号唯一  添加职工时判断有无此编号的职工
			{
				cin >> number;
				flag = this->IsExist(number);
				if (flag != -1)
				{
					cout << "职工号为" << number << "的职工已存在,请重新输入!" << endl;
				}
				else
				{
					break;
				}
			}
			cout << "请输入添加第" << i + 1 << "个职工的姓名:" << endl;
			cin >> name;
			cout << "请输入添加第" << i + 1 << "个职工的年龄:" << endl;
			cin >> age;
			cout << "请输入添加第" << i + 1 << "个职工的电话号码:" << endl;
			cin >> phone;
			cout << "请输入添加第" << i + 1 << "个职工的部门编号:" << endl;
			cout << "1 普通员工" << endl;
			cout << "2 经理" << endl;
			cout << "3 老板" << endl;
			cin >> num;

			Worker* p = NULL;
			switch (num)
			{
			case 1:
				p = new Employee(number, name, age, phone, 1);
				break;
			case 2:
				p = new Manager(number, name, age, phone, 2);
				break;
			case 3:
				p = new Boss(number, name, age, phone, 3);
			default:
				break;
			}
			newArray[Number + i] = p;
		}
		//释放原有空间
		delete[]this->Array;
		this->Array = newArray;
		this->Number = size;
		this->Save();
		this->IsEmpty = false;
		cout << "成功添加" << addNum << "新员工" << endl;
	}
	else
	{
		cout << "输入错误!" << endl;
	}
	system("pause");
	system("cls");
}

//保存到文件
void Administer::Save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	for (int i = 0; i < this->Number; i++)
	{
		ofs << this->Array[i]->number << "  "
			<< this->Array[i]->name << "  "
			<< this->Array[i]->age << "  "
			<< this->Array[i]->phone << "  "
			<< this->Array[i]->num << endl;
	}
	ofs.close();
}

//显示职工信息
void Administer::ShowWorker()
{
	if (this->IsEmpty)
	{
		cout << "文件不存在或文件为空!" << endl;
	}
	else
	{
		for (int i = 0; i < this->Number; i++)
		{
			this->Array[i]->ShowInfo();
		}
	}
	system("pause");
	system("cls");
}

int Administer::IsExist(int number)
{
	int index = -1;
	for (int i = 0; i < this->Number; i++)
	{
		if (this->Array[i]->number == number)
		{
			index = i;
		}
	}
	return index;
}

void Administer::DelWorker()
{
	if (this->IsEmpty)
	{
		cout << "文件不存在或者为空!" << endl;
		return;
	}
	int a;
	int ret = 0;
	cout << "请输入您要删除的员工编号:" << endl;
	cin >> a;
	ret = this->IsExist(a);
	if (ret == -1)
	{
		cout << "您输入的员工不存在!" << endl;
	}
	else
	{
		for (int i = ret; i < this->Number - 1; i++)
		{
			this->Array[i] = this->Array[i + 1];
		}
		this->Number --;
		this->Save();
		cout << "删除成功!" << endl;
	}
	system("pause");
	system("cls");
}

void Administer::ModWorker()
{
	if (this->IsEmpty)
	{
		cout << "文件为空或不存在!" << endl;
		return;
	}
	else
	{
		cout << "请输入您要修改的员工的编号:" << endl;
		int a;
		cin >> a;
		int ret = this->IsExist(a);
		if (ret != -1)
		{
			cout << "员工信息为:" << endl;
			this->Array[ret]->ShowInfo();

			delete this->Array[ret];
			int number;
			string name;
			int age;
			string phone;
			int num;
			int flag = 0;
			cout << "请输入新职工编号:" << endl;
			while (1)
			{
				cin >> number;
				flag = this->IsExist(number);
				if (flag != -1)
				{
					cout << "员工编号为" << number << "的员工已存在,请重新输入!" << endl;
				}
				else
				{
					break;
				}
			}
			cout << "请输入姓名:" << endl;
			cin >> name;
			cout << "请输入年龄:" << endl;
			cin >> age;
			cout << "请输电话号码入:" << endl;
			cin >> phone;
			cout << "请输入岗位:" << endl;
			cout << "1 普通员工" << endl;
			cout << "2 经理" << endl;
			cout << "3 老板" << endl;
			cin >> num;

			switch (num)
			{
			case 1:
				this->Array[ret] = new Employee(number, name, age, phone, 1);
				break;
			case 2:
				this->Array[ret] = new Manager(number, name, age, phone, 2);
				break;
			case 3:
				this->Array[ret] = new Boss(number, name, age, phone, 3);
				break;
			default:
				break;
			}
			cout << "修改成功!修改后的职工信息为:" << endl;
			this->Save();
			this->Array[ret]->ShowInfo();
		}
		else
		{
			cout << "职工不存在!" << endl;
		}
	}
	system("pause");
	system("cls");
}

void Administer::FindWorker()
{
	if (this->IsEmpty)
	{
		cout << "文件为空或不存在!" << endl;
		return;
	}
	else
	{
		cout << "请输入: 1 按编号查找     2 按姓名查找" << endl;
		int choose;
		cin >> choose;
		if (choose == 1)

		{
			cout << "请输入您要查找的员工编号:" << endl;
			int number;
			cin >> number;
			int ret = 0;
			ret = this->IsExist(number);
			if (ret != -1)
			{
				this->Array[ret]->ShowInfo();
			}
			else
			{
				cout << "员工编号为 " << number << "的员工不存在!" << endl;
			}
		}
		else
		{
			cout << "请输入您要查找的员工姓名:" << endl;
			string name;
			cin >> name;
			int index = -1;
			for (int i = 0; i < this->Number; i++)
			{
				if (this->Array[i]->name == name)
				{
					index = i;
					this->Array[index]->ShowInfo();
				}
			}
			if (index == -1)
			{
				cout << "员工姓名为 " << name << "的员工不存在!" << endl;
			}
		}
		system("pause");
		system("cls");
	}
}

void Administer::SortWorker()
{
	if (this->IsEmpty)
	{
		cout << "文件不存在或文件为空!" << endl;
		return;
	}
	else
	{
		cout << "请输入排序方式:1 按职工号升序  2 按职工号降序" << endl;
		int choose;
		cin >> choose;
		if (choose == 1)
		{
			for (int i = 0; i < this->Number - 1; i++)
			{
				for (int j = 0; j < this->Number - 1 - i; j++)
				{
					if (this->Array[j]->number > this->Array[j + 1]->number)
					{
						Worker* temp = this->Array[j + 1];
						this->Array[j + 1] = this->Array[j];
						this->Array[j] = temp;
					}
				}
			}
			cout << "升序排序完成!" << endl;
			this->Save();
			this->ShowWorker();
		}
		else
		{
			for (int i = 0; i < this->Number - 1; i++)
			{
				for (int j = 0; j < this->Number - 1 - i; j++)
				{
					if (this->Array[j]->number < this->Array[j + 1]->number)
					{
						Worker* temp = this->Array[j + 1];
						this->Array[j + 1] = this->Array[j];
						this->Array[j] = temp;
					}
				}
			}
			cout << "降序排序完成!" << endl;
			this->Save();
			this->ShowWorker();
		}
	}	
}

void Administer::CleanWorker()
{
	cout << "确定清空吗?  y/n" << endl;
	char ch;
	cin >> ch;
	if (ch == 'y' || ch == 'Y')
	{
		ofstream ofs;
		ofs.open(FILENAME, ios::trunc);
		ofs.close();

		if (this->Array != NULL)
		{
			for (int i = 0; i < this->Number; i++)
			{
				delete this->Array[i];
			}
		}

		this->Number = 0;
		delete [] this->Array;
		this->Array = NULL;
		this->IsEmpty = true;
		cout << "清空成功!" << endl;
	}
	else
	{
		return;
	}
	system("pause");
	system("cls");
}

void Administer::QuitSystem()
{
	char flag;
	cout << "确定退出管理系统吗?(y/n)" << endl;
	cin >> flag;
	if (flag == 'y' || flag == 'Y')
	{
		cout << "欢迎下次使用" << endl;
		exit(0);
	}
	if (flag == 'n' || flag == 'N')
	{
		cout << "欢迎继续使用!" << endl;
		system("pause");
		system("cls");
		return;
	}
	else
	{
		cout << "输入错误!" << endl;
		return;
	}
	system("pause");
	system("cls");
}

Administer::~Administer()
{
	if (this->Array != NULL)
	{
		for (int i = 0; i < this->Number; i++)
		{
			delete this->Array[i];
		}
		delete[] this->Array;
		this->Array = NULL;
	}
}

//职工管理系统.cpp
#include<iostream>
#include<string>
#include"Administer.h"
#include"Woker.h"
#include"Employee.h"
#include"Manager.h"
#include"Boss.h"
using namespace std;

int main()
{
	
	Administer Ad;
	int choice = 0;
	while (true)
	{
		Ad.ShowMenu();
		cout << "请输入你的选择:" << endl;
		cin >> choice;
		switch (choice)
		{
		case 1:		//添加职工
			Ad.Addmenber();
			break;
		case 2:		//显示职工
			Ad.ShowWorker();
			break;
		case 3:		//删除职工
			Ad.DelWorker();
			break;
		case 4:		//修改职工
			Ad.ModWorker();
			break;
		case 5:		//查找职工
			Ad.FindWorker();
			break;
		case 6:		//排序职工
			Ad.SortWorker();
			break;
		case 7:		//清空职工
			Ad.CleanWorker();
			break;
		case 0:		//退出系统
			Ad.QuitSystem();
			break;
		default:
			cout << "您的输入有误!" << endl;
			system("pause");
			break;
		}

	}
	system("pause");
	return 0;
}

五、总结

1.开辟在堆区的数据要手动释放。

2.添加新员工,删除员工等功能实现后要调用save()函数,保存到txt文件中。

3. 二级指针是指向指针的指针,而指针存放的是内存的地址。二级指针Array指向本案例中的指针数组,指针数组保存了不同类类型的指针。也就是二级指针可以作为指针数组的形参使用。


网站公告

今日签到

点亮在社区的每一天
去签到