基于多态的职工管理系统
worker.h
#pragma once
#include <iostream>
using namespace std;
class Worker {
public:
int m_ID;//职工编号
string m_Name;//姓名
int m_DeptID;//部门编号
//岗位职责信息描述
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
};
employee.h
#pragma once
#include <iostream>
#include "worker.h"
using namespace std;
class Employee : public Worker {
public:
Employee(int id, string name, int did);
void showInfo();
string getDeptName();
};
manager.h
#pragma once
#include <iostream>
#include "worker.h"
using namespace std;
class Manager : public Worker {
public:
Manager(int id, string name, int did);
void showInfo();
string getDeptName();
};
boss.h
#pragma once
#include <iostream>
#include "worker.h"
using namespace std;
class Boss : public Worker {
public:
Boss(int id, string name, int did);
void showInfo();
string getDeptName();
};
workManager.h
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include "worker.h"
using namespace std;
#define FILENAME "empfile.txt"
class WorkManager {
public:
//记录文件中的人数个数
int m_EmpNum;
//员工数组指针
Worker** m_EmpArray;
//文件是否为空
bool m_FileIsEmpty;
WorkManager();
//初始化数组
void init_Emp();
//统计文件中的人数
int get_EmpNum();
//保存到文件中
void save();
//增加职工
void add_Emp();
//显示菜单
void showMenu();
//退出程序
void exitSystem();
//显示职工
void show_Emp();
//判断职工是否存在---存在 返回其在数组的下标;不存在,返回-1
int IsExist(int id);
//删除职工
void delete_Emp();
//判断编号是否重复
bool isRepeat(int n_id);
//修改职工
void modify_Emp();
//查找职工
void find_Emp();
//排序职工
void sort_Emp();
//清空文档
void clean_File();
~WorkManager();
};
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.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.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("老板");
}
workManager.cpp
#include "workManager.h"
#include "employee.h"
#include "Manager.h"
#include "boss.h"
WorkManager::WorkManager() {
//1、文件未创建
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open()) {
//初始化属性
this->m_EmpNum = 0;
this->m_EmpArray = nullptr;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//2、文件存在 数据为空
char ch;
ifs >> ch;//拿走一个字符
if (ifs.eof()) {
//初始化属性
this->m_EmpNum = 0;
this->m_EmpArray = nullptr;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//3、文件存在 有数据记录
int num = this->get_EmpNum();
this->m_EmpNum = num;
//开辟空间
this->m_EmpArray = new Worker * [this->m_EmpNum];
//将文件中的数据存到数组中
this->init_Emp();
}
void WorkManager::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;
}
void WorkManager::exitSystem() {
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
}
void WorkManager::save() {
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < m_EmpNum; ++i) {
ofs << this->m_EmpArray[i]->m_ID << " "
<< this->m_EmpArray[i]->m_Name << " "
<< this->m_EmpArray[i]->m_DeptID << endl;
}
ofs.close();
}
void WorkManager::add_Emp() {
cout << "请输入增加职工数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0 ) {
//计算新空间大小
int newSize = addNum + m_EmpNum;
//开辟新空间
Worker** newArray = new Worker * [newSize];
//将原空间的内容存放到新空间下
if (m_EmpArray != nullptr) {
for (int i = 0; i < newSize; ++i) {
newArray[i] = m_EmpArray[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;
Worker* worker = nullptr;
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;
}
//将新职工插入新数组的合适位置
newArray[m_EmpNum + i] = worker;
}
//释放原有空间
delete[] m_EmpArray;
//更新新空间的指向
this->m_EmpArray = newArray;
//更新个数
m_EmpNum = newSize;
//更新职工不为空的标志
this->m_FileIsEmpty = false;
//提示信息(添加成功)
cout << "成功添加 " << addNum << " 名新职工" << endl;
//保存到文件中
this->save();
}
else {
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
int WorkManager::get_EmpNum() {
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did) {
++num;
}
ifs.close();
return num;
}
void WorkManager::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 = nullptr;
if (did == 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 WorkManager::show_Emp() {
if (m_FileIsEmpty) {//判断文件是否为空
cout << "文件不存在或记录为空" << endl;
}
else {
for (int i = 0; i < m_EmpNum; ++i) {
this->m_EmpArray[i]->showInfo();
}
}
system("pause");
system("cls");
}
int WorkManager::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 WorkManager::delete_Emp() {
//判断文件是否存在
if (this->m_FileIsEmpty)
cout << "文件不存在或记录为空" << endl;
else {
int id;
cout << "请输入想删除的职工编号" << endl;
cin >> id;
int index = this->IsExist(id);
if (index >= 0) {//想删除的职工存在
//逻辑删除---数据前移
for (int i = index + 1; i < this->m_EmpNum; ++i) {
this->m_EmpArray[i - 1] = this->m_EmpArray[i];
}
--this->m_EmpNum;
//数据同步更新到文件中
this->save();
cout << "删除成功!" << endl;
}
else {
cout << "删除失败,未找到该职工" << endl;
}
}
system("pause");
system("cls");
}
bool WorkManager::isRepeat(int n_id) {
bool flag = false;
for (int i = 0; i < this->m_EmpNum; ++i) {
if (this->m_EmpArray[i]->m_ID == n_id) {
flag = true;
break;
}
}
return flag;
}
void WorkManager::find_Emp() {
if (this->m_FileIsEmpty)
cout << "文件不存在或记录为空" << endl;
else {
cout << "请输入查找方式:" << endl;
cout << "1.按职工编号查找" << endl;
cout << "2.按姓名查找" << endl;
int choice = 0;
cin >> choice;
if (choice == 1) {//按职工编号
int id;
cout << "请输入要查找的职工编号:" << endl;
cin >> id;
int index = this->IsExist(id);
if (index >= 0) {
cout << "查找成功,该职工的信息如下:" << endl;
this->m_EmpArray[index]->showInfo();
}
else
cout << "查无此人,查找失败!" << endl;
}
else if (choice == 2) {//按姓名
string name;
cout << "请输入要查找的职工姓名:" << endl;
cin >> name;
bool flag = false;
for (int i = 0; i < this->m_EmpNum; ++i) {
if (this->m_EmpArray[i]->m_Name == name) {
cout << "查找成功,职工编号为:" << this->m_EmpArray[i]->m_ID << " 号的信息如下:" << endl;
flag = true;
this->m_EmpArray[i]->showInfo();
}
}
if(flag == false)
cout << "查无此人,查找失败!" << endl;
}
else
cout << "输入选项有误" << endl;
}
system("pause");
system("cls");
}
void WorkManager::modify_Emp() {
if (this->m_FileIsEmpty)
cout << "文件不存在或记录为空" << endl;
else {
cout << "请输入修改职工的编号" << endl;
int id;
cin >> id;
int index = this->IsExist(id);
if (index >= 0) {//想要修改的职工存在
//先清空原数据
delete this->m_EmpArray[index];
int newId;
string newName;
int newdSelect;
cout << "查到:" << id << "号职工,请输入新职工号" << endl;
int count = 0;//记录输入次数
while (true) {
if (count == 3) {
int choice = 0;
cout << "输入次数已达三次,是否取消此次修改" << endl;
cout << "1.继续" << endl;
cout << "2.取消" << endl;
cin >> choice;
if (choice == 2) {
cout << "已取消" << endl;
system("pause");
system("cls");
return;
}
}
++count;
cin >> newId;
if (this->isRepeat(newId))
break;
else
cout << "工号重复,请重新输入!" << endl;
}
cout << "请输入新姓名:" << endl;
cin >> newName;
cout << "请输入新岗位:" << endl;
cout << "1. 普通职工" << endl;
cout << "2. 经理" << endl;
cout << "3. 老板" << endl;
cin >> newdSelect;
Worker* worker = nullptr;
switch (newdSelect) {//给所修改的职工申请新类型的空间
case 1:
worker = new Employee(newId, newName, newdSelect);
break;
case 2:
worker = new Manager(newId, newName, newdSelect);
break;
case 3:
worker = new Boss(newId, newName, newdSelect);
break;
default:
break;
}
//更改数据到数组中
this->m_EmpArray[index] = worker;
//保存到文件中
this->save();
cout << "修改成功!" << endl;
}
else
cout << "查无此人,修改失败" << endl;
}
system("pause");
system("cls");
}
void WorkManager::sort_Emp() {
if (this->m_FileIsEmpty) {
cout << "文件不存在或记录为空" << endl;
system("pause");
system("cls");
}
else {
cout << "请选择排序方式:" << endl;
cout << "1.按职工号进行升序排列" << endl;
cout << "2.按职工号进行降序排列" << endl;
//排序方式---选择排序
int choice = 0;
cin >> choice;
for (int i = 0; i < this->m_EmpNum; ++i) {
int min0rmax = i;
for (int j = i + 1; j < this->m_EmpNum; ++j) {
if (choice == 1) {//升序
if (this->m_EmpArray[min0rmax]->m_ID > this->m_EmpArray[j]->m_ID)
min0rmax = j;
}
else {//降序
if (this->m_EmpArray[min0rmax]->m_ID < this->m_EmpArray[j]->m_ID)
min0rmax = j;
}
}
if (min0rmax != i) {
Worker* temp = this->m_EmpArray[i];
this->m_EmpArray[i] = this->m_EmpArray[min0rmax];
this->m_EmpArray[min0rmax] = temp;
}
}
cout << "排序成功,排序后的结果为:" << endl;
//将排序后的结果保存到文件中
this->save();
//展示
this->show_Emp();
}
}
void WorkManager::clean_File() {
cout << "确定清空?" << endl;
cout << "1.确定" << endl;
cout << "2.返回" << endl;
//清空文件
ofstream ofs;
ofs.open(FILENAME, ios::trunc);//删除文件后重新创建
ofs.close();
int choice = 0;
cin >> choice;
if (choice == 1) {//确认清空
if (this->m_EmpArray != nullptr) {
for (int i = 0; i < this->m_EmpNum; ++i) {
delete this->m_EmpArray[i];
this->m_EmpArray[i] = nullptr;
}
delete[] this->m_EmpArray;
this->m_EmpArray = nullptr;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
WorkManager::~WorkManager() {
if (this->m_EmpArray != nullptr) {
for (int i = 0; i < this->m_EmpNum; ++i) {
delete this->m_EmpArray[i];
this->m_EmpArray[i] = nullptr;
}
delete[] this->m_EmpArray;
this->m_EmpArray = nullptr;
}
}
职工管理系统.cpp
#include <iostream>
#include <string>
#include "workManager.h"
#include "employee.h"
#include "Manager.h"
#include "boss.h"
using namespace std;
int main() {
WorkManager wm;
int choice = 0;;
while (true) {
wm.showMenu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice) {
case 0://退出系统
wm.exitSystem();
break;
case 1://增加职工
wm.add_Emp();
break;
case 2://显示职工
wm.show_Emp();
break;
case 3://删除职工
wm.delete_Emp();
break;
case 4://修改职工
wm.modify_Emp();
break;
case 5://查找职工
wm.find_Emp();
break;
case 6://排序职工
wm.sort_Emp();
break;
case 7://清空文档
wm.clean_File();
break;
default:
system("cls");
}
}
system("pause");
return 0;
}