Boss.h
#pragma once
#include"Worker.h"
class Boss:public Worker
{
public:
Boss(int employeeId, string employeeName, int employDepartId);
string getDepart();
void showMessage();
};
Employer.h
#pragma once
#include"Worker.h"
class Employer :public Worker
{
public:
Employer(int employeeId, string employeeName, int employDepartId);
string getDepart();
void showMessage();
};
Manager.h
#pragma once
#include"Worker.h"
class Manager:public Worker
{
public:
Manager(int employeeId, string employeeName, int employDepartId);
string getDepart();
void showMessage();
};
Worker.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Worker
{
public:
int employeeId;//职工编号
string employeeName;//职工姓名
int employDepartId;//职工部门编号
virtual string getDepart() = 0;
virtual void showMessage() = 0;
};
WorkerManager.h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<string>
class WorkerManager
{
public:
Worker** empArry;//存放职工数据
int empNum;// 职工总数
bool fileIsEmpty;// 用来记录文件信息状态
// 初始化数据
WorkerManager();
// 显示菜单
void menu();
// 退出系统
void exitSystem();
// 添加职工
// 升级,判断如何让职工编号不重复
void addEmployer();
// 将数据存储到文件中
void saveToFile();
// 获取文件中职工个数
int getEmpNum();
// 初始化数组数据,也就是将文件内的信息读到内存中
void init();
// 显示职工信息
void showEmpMessage();
// 删除职工信息
void deleteEmployer();
// 通过职工编号寻找职工,并返回职工在数组中的位置
// 若未找到,返回-1
int findEmpById(int id);
// 修改员工信息
void modifyEmpMessage();
//查找职工
//1.按照职工编号查找
//2.按照姓名查找
void findEmpMessage();
//按照职工号进行排序
//1.正序
//2.倒序
void sortEmpById();
//清空文件
void emptyEmpMessage();
// 析构函数
// 回收空间
~WorkerManager();
};
Boss.cpp
#include"Boss.h"
Boss::Boss(int employeeId, string employeeName, int employDepartId)
{
this->employeeId = employeeId;
this->employeeName = employeeName;
this->employDepartId = employDepartId;
}
string Boss::getDepart()
{
if (employDepartId == 1)
{
return "普通员工";
}
else if (employDepartId == 2)
{
return "经理";
}
else
{
return "老板";
}
}
void Boss::showMessage()
{
cout << "员工编号:" << employeeId << "\t"
<< "员工姓名:" << employeeName << " \t"
<< "员工部门:" << this->getDepart() << " \t"
<< "职责:" << "管理公司所有事物" << endl;
}
Employer.cpp
#include"Employer.h"
Employer::Employer(int employeeId, string employeeName, int employDepartId)
{
this->employeeId = employeeId;
this->employeeName = employeeName;
this->employDepartId = employDepartId;
}
string Employer::getDepart()
{
if (employDepartId == 1)
{
return "普通员工";
}
else if (employDepartId == 2)
{
return "经理";
}
else
{
return "老板";
}
}
void Employer::showMessage()
{
cout << "员工编号:" << employeeId << "\t"
<< "员工姓名:" << employeeName << " \t"
<< "员工部门:" << this->getDepart() << " \t"
<< "职责:" << "完成经理交给的任务" << endl;
}
Manager.cpp
#include"Manager.h"
Manager::Manager(int employeeId, string employeeName, int employDepartId)
{
this->employeeId = employeeId;
this->employeeName = employeeName;
this->employDepartId = employDepartId;
}
string Manager::getDepart()
{
if (employDepartId == 1)
{
return "普通员工";
}
else if (employDepartId == 2)
{
return "经理";
}
else
{
return "老板";
}
}
void Manager::showMessage()
{
cout << "员工编号:" << employeeId << "\t"
<< "员工姓名:" << employeeName << " \t"
<< "员工部门:" << this->getDepart() << " \t"
<< "职责:" << "完成老板交给的任务并下发给员工" << endl;
}
WorkerManager.cpp
#include"WorkerManager.h"
#include"Worker.h"
#include"Employer.h"
#include"Manager.h"
#include"Boss.h"
#include<fstream>
using namespace std;
#define FILENAME "empFile.txt"
// 初始化数据
WorkerManager::WorkerManager()
{
// 将文件中的内容先读取到内存中
// 1.文件为空
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
//cout << "文件为空或不存在!" << endl;
this->empNum = 0;
this->fileIsEmpty = true;
this->empArry = new Worker * [this->empNum];
ifs.close();
return;
}
// 2.文件不存在
char ch;
ifs >> ch;
if (ifs.eof())
{
//cout << "文件为空或不存在!" << endl;
this->empNum = 0;
this->fileIsEmpty = true;
this->empArry = new Worker * [this->empNum];
ifs.close();
return;
}
// 3.文件不为空
// 获取文件中职工个数
empNum = getEmpNum();
// 创建数组存储职工信息
empArry = new Worker * [empNum];
fileIsEmpty = false;
// 初始化数组数据,也就是将文件内的信息读到内存中
init();
测试代码
//for (int i = 0; i < this->empNum; i++)
//{
// cout << "职工编号: " << this->empArry[i]->employeeId
// << " 姓名: " << this->empArry[i]->employeeName
// << " 部门编号: " << this->empArry[i]->employDepartId << endl;
//}
}
// 显示菜单
void WorkerManager::menu()
{
cout << "***********************************" << endl;
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;
}
// 退出系统
void WorkerManager::exitSystem()
{
cout << "退出成功,欢迎下次使用!" << endl;
exit(0);
}
// 将数据存储到文件中
void WorkerManager::saveToFile()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
int i = 0;
for (i = 0; i < empNum; i++)
{
ofs << empArry[i]->employeeId << "\t"
<< empArry[i]->employeeName << "\t"
<< empArry[i]->employDepartId << endl;
}
ofs.close();
}
// 添加职工
// 可以批量添加职工
// 升级,判断如何让职工编号不重复*******
void WorkerManager::addEmployer()
{
cout << "请输入您要增加的职工数目:" << endl;
int num;
cin >> num;
if (num > 0)
{
// 原文件的职工数目和即将加入的职工数的和
int newEmpNum = empNum + num;
// 创建新数组存储所有职工的数据
Worker** newEmpArry = new Worker * [newEmpNum];
int i = 0;
// 添加结束前,将之前职工的信息也存储到新数组种
for (i = 0; i < empNum; i++)
{
newEmpArry[i] = empArry[i];
}
// 然后存入新职工的信息;
for (i = 0; i < num; i++)
{
int newId;
// 默认重复
bool isRepeat = true;
while (isRepeat)
{
cout << "请输入第" << i + 1 << "个人的职工编号:" << endl;
cin >> newId;
// 判断新id是否重复
int j = 0;
for (j = 0; j < empNum + i; j++)
{
if (newEmpArry[j]->employeeId == newId)
{
isRepeat = true;
cout << "对不起,此id重复,请重新输入!" << endl;
break;
}
else
{
isRepeat = false;
}
}
}
cout << "请输入第" << i + 1 << "个人的姓名:" << endl;
string newName;
cin >> newName;
while (1)
{
cout << "请输入第" << i + 1 << "个人的部门编号:" << endl
<< "1.普通职工" << endl
<< "2.经理" << endl
<< "3.总裁" << endl;
int newDepartId;
cin >> newDepartId;
// 根据不同的部门编号存储
if (newDepartId == 1)
{
newEmpArry[empNum + i] = new Employer(newId, newName, newDepartId);
break;
}
else if (newDepartId == 2)
{
newEmpArry[empNum + i] = new Manager(newId, newName, newDepartId);
break;
}
else if (newDepartId == 3)
{
newEmpArry[empNum + i] = new Boss(newId, newName, newDepartId);
break;
}
else
{
cout << "对不起,输入有误,请从新输入:" << endl;
}
}
}
// 到这步,新数组存储了所有员工的信息
// 更新数据
//删除了数组种的内容,并不是释放了数组中每个元素的内容所指向的那个在堆区开辟的空间
// emrArry[]中每个元素是Worker*类型,emrArry是Worker**类型
// 故delete empArry,数组名代表首元素地址,释放的是首元素地址指向的内容,也就是首元素
// delete[] empArry,释放的是整个数组的所有元素
// delete empArry[i] ,释放的是该元素内容指向的空间
delete[] empArry;
empArry = newEmpArry;
empNum = newEmpNum;
fileIsEmpty = false;
//将数据存储在文件中
saveToFile();
}
else
{
cout << "对不起,输入的数字必须是正整数!" << endl;
}
system("pause");
system("cls");
}
// 获取文件中职工个数
int WorkerManager::getEmpNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int dId;
int cout = 0;
while (ifs >> id && ifs >> name && ifs >> dId)
{
cout++;
}
ifs.close();
return cout;
}
// 初始化数组数据,也就是将文件内的信息读到内存中
void WorkerManager::init()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int dId;
int i = 0;
while ((ifs >> id) && (ifs >> name) && (ifs >> dId))
{
switch (dId)
{
case 1:
empArry[i] = new Employer(id, name, dId);
break;
case 2:
empArry[i] = new Manager(id, name, dId);
break;
case 3:
empArry[i] = new Boss(id, name, dId);
break;
default:
break;
}
i++;
}
ifs.close();
}
// 显示职工信息
// 程序一旦析构之后,就以内存中的数据为准
// 但是增删改查要急时更新到文件中
void WorkerManager::showEmpMessage()
{
if (fileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
int i = 0;
for (i = 0; i < empNum; i++)
{
empArry[i]->showMessage();
}
}
system("pause");
system("cls");
}
// 删除职工信息
void WorkerManager::deleteEmployer()
{
if (fileIsEmpty)
{
cout << "文件为空或不存在" << endl;
}
else
{
// 请输入您要删除的职工号
cout << "清输入您要删除的职工号:" << endl;
int id;
cin >> id;
// 通过职工编号寻找职工,并返回职工在数组中的位置
// 若未找到,返回-1
int ret = findEmpById(id);
if (ret == -1)
{
cout << "对不起,您要删除的职工不存在!" << endl;
}
else
{
int i = 0;
for (i = ret; i < empNum - 1; i++)
{
empArry[i] = empArry[i + 1];
}
empNum--;
if (empNum > 0)
{
fileIsEmpty = false;
}
else
{
fileIsEmpty = true;
}
}
cout << "删除成功!" << endl;
// 将数据更新到文件中
saveToFile();
}
system("pause");
system("cls");
}
// 通过职工编号寻找职工,并返回职工在数组中的位置
// 若未找到,返回-1
int WorkerManager::findEmpById(int id)
{
int i = 0;
for (i = 0; i < empNum; i++)
{
if (empArry[i]->employeeId == id)
{
return i;
}
}
return -1;
}
// 修改员工信息
void WorkerManager::modifyEmpMessage()
{
if (fileIsEmpty)
{
cout << "文件为空或不存在" << endl;
}
else
{
cout << "请输入您要修改职工的职工号" << endl;
int id;//用来接收要修改职工的id
cin >> id;
int ret = findEmpById(id);
if (ret == -1)
{
cout << "对不起,不存在该职工" << endl;
}
else
{
cout << "请输入该职工新的职工编号:" << endl;
cin >> empArry[ret]->employeeId;
cout << "请输入该职工新的职工姓名:" << endl;
cin >> empArry[ret]->employeeName;
cout << "请输入该职工新的职工部门编号:" << endl
<< "1.普通职工" << endl
<< "2.经理" << endl
<< "3.老板" << endl;
cin >> empArry[ret]->employDepartId;
cout << "更改成功" << endl;
//修改后更新到文件中
saveToFile();
}
}
system("pause");
system("cls");
}
//查找职工
//1.按照职工编号查找
//2.按照姓名查找
void WorkerManager::findEmpMessage()
{
if (fileIsEmpty)
{
cout << "文件不存在或者为空" << endl;
}
else
{
// 选择查找方式
int way = 0;
while (1)
{
cout << "请选择查找的方式" << endl
<< "1.按照职工编号" << endl
<< "2.按照职工姓名" << endl;
cin >> way;
int i = 0;//遍历数组
//按照职工编号
if (way == 1)
{
cout << "请输入您要查找的职工编号" << endl;
int id;
cin >> id;
int ret = findEmpById(id);
if (ret == -1)
{
cout << "对不起,该职工不存在" << endl;
}
else
{
cout << "该职工信息如下" << endl;
empArry[ret]->showMessage();
}
break;
}
//按照职工姓名查找
else if (way == 2)
{
string name;
int count = 0;
cout << "请输入您要查找的职工姓名" << endl;
cin >> name;
int i = 0;//遍历数组
for (i = 0; i < empNum; i++)
{
if (empArry[i]->employeeName == name)
{
empArry[i]->showMessage();
count++;
}
}
cout << "共查找到:" << count << "个姓名为:" << name << "的职工" << endl;
break;
}
else
{
cout << "输入错误,请重新输入:" << endl;
}
}
system("pause");
system("cls");
}
}
//按照职工号进行排序
//1.正序
//2.倒序
void WorkerManager::sortEmpById()
{
if (fileIsEmpty)
{
cout << "文件为空或不存在" << endl;
}
else
{
int way = 0;
// 用来排序
int i = 0;
int j = 0;
int minOrMax = 0;
while (1)
{
cout << "请选择排序方式:" << endl
<< " 1.正序" << endl
<< " 2.倒序" << endl;
cin >> way;
if (way == 1 || way == 2)//正序或倒序
{
for (i = 0; i < empNum - 1; i++)
{
minOrMax = i;
for (j = i+1; j < empNum; j++)
{
if (way == 1)
{
if (empArry[j]->employeeId < empArry[minOrMax]->employeeId)
{
minOrMax = j;
}
}
else
{
if (empArry[j]->employeeId > empArry[minOrMax]->employeeId)
{
minOrMax = j;
}
}
}
Worker* t = empArry[i];
empArry[i] = empArry[minOrMax];
empArry[minOrMax] = t;
t = NULL;
}
cout << "排序成功!" << endl;
saveToFile();
break;
}
else
{
cout << "对不起,选择错误,请重新选择!" << endl;
}
}
}
system("pause");
system("cls");
}
//清空文件
void WorkerManager::emptyEmpMessage()
{
if (fileIsEmpty)
{
cout << "文件为空或者不存在" << endl;
}
else
{
int choose;
cout << "请再次确定是否清空文件:" << endl
<< "1.是" << endl
<< "2或其它字符:否" << endl;
cin >> choose;
if (choose == 1)
{
// 清空后文件为空
fileIsEmpty = true;
//将文件置空
ofstream ofs;
ofs.open(FILENAME, ios::trunc);
ofs.close();
int i = 0;
for (i = 0; i < empNum; i++)
{
delete empArry[i];
empArry[i] = NULL;
}
empNum = 0;
delete[] empArry;
empArry = NULL;
cout << "清空成功" << endl;
}
else
{
cout << "取消清空文件成功" << endl;
}
}
system("pause");
system("cls");
}
// 析构函数
// 回收空间
WorkerManager::~WorkerManager()
{
int i = 0;
for (i = 0; i < empNum; i++)
{
delete empArry[i];
empArry[i] = NULL;
}
delete[] empArry;
empArry = NULL;
}
职工管理系统.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"WorkerManager.h"
#include"Worker.h"
int main()
{
int input = -1;
WorkerManager workerManager;
while (1)
{
workerManager.menu();
cout << "请选择:" << endl;
cin >> input;
switch (input)
{
case 0:// 退出该系统
// 同时析构函数释放空间
workerManager.exitSystem();
break;
case 1://添加职工
// 升级,判断如何让职工编号不重复
workerManager.addEmployer();
break;
case 2://删除职工
workerManager.deleteEmployer();
break;
case 3://显示职工所有信息
workerManager.showEmpMessage();
break;
case 4://修改职工信息
workerManager.modifyEmpMessage();
break;
case 5://查找职工
//1.按照职工编号查找
//2.按照姓名查找
workerManager.findEmpMessage();
break;
case 6://按照职工号进行排序
//1.正序
//2.倒序
workerManager.sortEmpById();
break;
case 7://清空文件
workerManager.emptyEmpMessage();
break;
default:
cout << "对不起,您选择错误,请重新输入" << endl;
system("pause");
system("cls");
break;
}
}
return 0;
}