1、管理系统需求
管理公司所有员工的信息
基于C++和多态进行实现
公司员工分为三类:老板 普工 经理
显示信息:职工编号 姓名 岗位 职责
普工职责:完成经理交代的任务
经理职责:完成老板的任务并下发给普工
老板职责:管理公司所有事物
系统功能:
- 退出管理程序
- 增加信息:编号 姓名 部门编号
- 显示信息
- 删除信息:按照编号删除指定员工
- 修改职工信息:按编号查找
- 查找职工:按照编号或者姓名
- 按照编号排序
- 清空文档
2、创建项目
3、创建管理类
管理类负责以下内容
- 与用户沟通的菜单界面
- 对职工增删改查的工作
- 与文件的读写操作
3.1、管理类创建
//WorkerManager.h
#pragma once
#include<iostream>
using namespace std;
class WorkerManager
{
public:
WorkerManager();
~WorkerManager();
private:
};
//WorkerManager.cpp
#include"WorkerManager.h"
WorkerManager::WorkerManager()
{
}
WorkerManager::~WorkerManager()
{
}
3.2、菜单实现
void WorkerManager::Show_Menu(void)
{
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;
}
3.3、退出功能实现
3.3.1、提供功能接口
main()函数分支选择
while (true)
{
wm.Show_Menu();
cout << "请输入您的选择" << endl;
cin >> choice;
switch (choice)
{
case 0://退出管理系统
break;
case 1://增加职工信息
break;
case 2://显示职工信息
break;
case 3://删除离职职工
break;
case 4://修改职工信息
break;
case 5://查找职工信息
break;
case 6://按照编号排序
break;
case 7://清空所有文档
break;
default:
system("cls");
break;
}
3.3.2、实现退出系统功能
void WorkerManager::ExitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);//退出程序
}
4、创建职工类
4.1、创建职工抽象类
- 属性:职工编号;姓名;部门编号
- 行为:获取职责描述,获取岗位名称;显示个人信息
//Worker.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
class Worker
{
public:
//行为:获取职责描述,获取岗位名称;显示个人信息
virtual void ShowInfo() = 0;
virtual string GetDepartName() = 0;
Worker();
~Worker();
//属性:职工编号;姓名;部门编号
int m_Id;
string m_name;
int dId;
private:
};
4.2、创建普通员工类
Employee.h代码如下
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include"Worker.h"
class Employee:public Worker
{
public:
//行为:获取职责描述,获取岗位名称;显示个人信息
virtual void ShowInfo();
virtual string GetDepartName();
Employee(int Id, string name, int dId);
~Employee();
private:
};
Employee.cpp代码如下
#include"Employee.h"
Employee::Employee(int Id, string name, int dId)
{
this->m_Id = Id;
this->m_name = name;
this->m_dId = dId;
}
Employee::~Employee()
{
}
void Employee::ShowInfo()
{
cout << "姓名:" << this->m_name
<< "\t编号:" << this->m_Id
<< "\t岗位:" << this->GetDepartName()
<< "\t职责:" << "完成经理分配的任务" << endl;
}
string Employee::GetDepartName()
{
return string("普通员工");
}
主函数测试代码如下
void test()
{
Worker* worker = NULL;
worker = new Employee(1, "张三", 1);
worker->ShowInfo();
delete worker;
worker = new Employee(2, "李四", 2);
worker->ShowInfo();
delete worker;
worker = new Employee(3, "老王", 3);
worker->ShowInfo();
delete worker;
}
4.3、创建经理类
Manager.h如下
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
class Manager:public Worker
{
public:
//行为:获取职责描述,获取岗位名称;显示个人信息
virtual void ShowInfo();
virtual string GetDepartName();
Manager(int Id, string name, int dId);
~Manager();
private:
};
Manager.cpp
#include"Manager.h"
Manager::Manager(int Id, string name, int dId)
{
this->m_Id = Id;
this->m_name = name;
this->m_dId = dId;
}
Manager::~Manager()
{
}
//行为:获取职责描述,获取岗位名称;显示个人信息
void Manager::ShowInfo()
{
cout << "姓名:" << this->m_name
<< "\t编号:" << this->m_Id
<< "\t岗位:" << this->GetDepartName()
<< "\t职责:" << "完成总裁分配的任务并下发给员工" << endl;
}
string Manager::GetDepartName()
{
return string("经理");
}
4.4、创建老板类
Boss.h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
class Boss :public Worker
{
public:
//行为:获取职责描述,获取岗位名称;显示个人信息
virtual void ShowInfo();
virtual string GetDepartName();
Boss(int Id, string name, int dId);
~Boss();
private:
};
Boss.cpp
#include"Boss.h"
Boss::Boss(int Id, string name, int dId)
{
this->m_Id = Id;
this->m_name = name;
this->m_dId = dId;
}
Boss::~Boss()
{
}
//行为:获取职责描述,获取岗位名称;显示个人信息
void Boss::ShowInfo()
{
cout << "姓名:" << this->m_name
<< "\t编号:" << this->m_Id
<< "\t岗位:" << this->GetDepartName()
<< "\t职责:" << "管理公司所有事物" << endl;
}
string Boss::GetDepartName()
{
return string("总裁");
}
测试代码
void test()
{
Worker* 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;
}
测试结果
5、添加职工
批量添加职工,并保存到文件中
5.1、功能分析
创建用户时,用new关键字实例化worker类,返回指针类型,连续的指针用二级指针进行维护
5.1.1、添加属性
记录当前人数
记录当前员工数组的指针
//WorkerManager.h
//记录当前员工数组的指针
Worker** m_EmpArry;
//记录当前人数
int m_CurreEmpNum;
5.1.2、实现添加职工的函数
//添加员工函数
void WorkerManager::AddEmp()
{
int addNum = 0;//记录要添加的人数
cout << "请输入要添加的人数" << endl;
cin >> addNum;
if (addNum > 0)
{
//添加
//计算新空间的大小
int newSize = this->m_CurreEmpNum + addNum;
//开辟新的堆区空间
Worker** newSpace = new Worker * [newSize];
//将原来空间的数据拷贝到新空间
if (this->m_EmpArry != NULL)
{
for (int i = 0; i < this->m_CurreEmpNum; 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 << "请输入第" << i + 1 << "个新职工的部门:" << endl;
cout << "1、普通员工" << endl;
cout << "2、经理" << endl;
cout << "3、总裁" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect)
{
case 1:
worker = new Employee(id, name, dSelect);
break;
case 2:
worker = new Manager(id, name, dSelect);
break;
case 3:
worker = new Boss(id, name, dSelect);
break;
default:
break;
}
//将worker放在数组对应的位置
newSpace[this->m_CurreEmpNum + i] = worker;
}
//释放原来的空间
delete[] this->m_EmpArry;
//修改存储员工数据的指针指向
this->m_EmpArry = newSpace;
//更新职工人数
this->m_CurreEmpNum = newSize;
cout << "成功添加" << addNum << "名新职工" << endl;
}
else
{
cout << "Input error!" << endl;
}
system("pause");
system("cls");
}
6、文件交互-写文件
6.1、设定文件路径
设定文件路径并且包含文件的头文件
//WorkerManager.h
#include<fstream>
#define FILNAME "empFile.txt"
6.2、实现保存函数
void WorkerManager::Save()
{
ofstream ofs;
//打开文件
ofs.open(FILNAME, ios::out);
//将每个数据写入到文件中
for (int i = 0; i < this->m_CurreEmpNum; i++)
{
ofs << this->m_EmpArry[i]->m_Id << " "
<< this->m_EmpArry[i]->m_name << " "
<< this->m_EmpArry[i]->m_dId << endl;
}
//关闭文件
ofs.close();
}
7、文件交互-读文件
7.1、文件未创建
7.1.1 创建标志位属性
//WorkerManager.h
bool IsFileEmpty;
7.1.2 修改构造函数
WorkerManager::WorkerManager()
{
ifstream ifs;//创建文件输入流对象
ifs.open(FILNAME, ios::in);//读文件
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
//初始化当前人数和员工指针数组
this->m_CurreEmpNum = 0;
this->m_EmpArry = NULL;
//初始化文件空标志位
this->IsFileEmpty = true;
ifs.close();
return;
}
}
7.2、文件存在但记录为空
//文件为空
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空" << endl;
//初始化当前人数和员工指针数组
this->m_CurreEmpNum = 0;
this->m_EmpArry = NULL;
//初始化文件空标志位
this->IsFileEmpty = true;
ifs.close();
return;
}
注意:要更新文件空标志位
7.3、文件存在且文件之中包含数据
获取当前职工人数
int WorkerManager::GetWorkerNum()
{
ifstream ifs;
string name;
int Id;
int dId;
int num;
ifs.open(FILNAME, ios::in);
while (ifs >> Id && ifs >> name && ifs >> dId)
{
num++;
}
ifs.close();
return num;
}
构造函数追加第三种情况
//文件存在并且包含数据
int num = this->GetWorkerNum();
cout << "当前员工人数为:" << num << endl;
this->m_CurreEmpNum = num;
//文件读取测试单元
//开辟新空间
this->m_EmpArry = new Worker * [this->m_CurreEmpNum];
//将文件读取的数据放入新空间
this->InitEmpArr();
//打印堆区数据
for (int i = 0; i < this->m_CurreEmpNum; i++)
{
cout << "职工编号:" << this->m_EmpArry[i]->m_Id
<< "职工姓名:" << this->m_EmpArry[i]->m_name
<< "部门编号:" << this->m_EmpArry[i]->m_dId
<< endl;
}
8、显示职工
void WorkerManager::ShowEmp()
{
if (this->IsFileEmpty)
{
cout << "文件为空或文件不存在!" << endl;
}
else
{
for (int i = 0; i < m_CurreEmpNum; i++)
{
this->m_EmpArry[i]->ShowInfo();
}
}
system("pause");
system("cls");
}
9、按照职工编号进行删除
9.1、删除职工函数
//删除职工函数
void WorkerManager::DelEmp()
{
int id;
int index;
//判断要删除数据的文件是否存在,是否为空
if (this->IsFileEmpty)
{
cout << "文件为空或文件不存在!" << endl;
}
//文件存在且有数据
else
{
cout << "请输入要删除的职工编号" << endl;
cin >> id;
//判断要删除的职工编号是否存在
index = this->IsEmpExit(id);
if (index != -1)//职工存在
{
for (int i = index; i < this->m_CurreEmpNum - 1; i++)
{
this->m_EmpArry[i] = this->m_EmpArry[i + 1];
}
//更新职工人数
this->m_CurreEmpNum--;
//更新文件数据
this->Save();
//打印提示信息
cout << "删除成功" << endl;
}
else
{
cout << "职工不存在" << endl;
}
}
system("pause");
system("cls");
}
9.2、判断职工是否存在函数
//判断职工是否存在,返回职工数组位置
int WorkerManager::IsEmpExit(int id)
{
int index = -1;
for (int i = 0; i < this->m_CurreEmpNum; i++)
{
if (this->m_EmpArry[i]->m_Id == id)
{
index = i;
break;
}
}
return index;
}
10、修改职工信息
//修改职工信息
void WorkerManager::ModEmp()
{
int id;
int ret;
//判断文件有没有数据
if (this->IsFileEmpty)
{
cout << "文件为空或文件不存在!" << endl;
}
else
{
cout << "请输入待修改职工编号:" << endl;
cin >> id;
//判断员工是否存在
ret = this->IsEmpExit(id);
if (ret != -1)//找到了职工
{
//释放原来的数据空间
delete this->m_EmpArry[ret];
int newId;
string newName;
int newDid;
cout << "查到编号为" << id << "的职工" << endl;
cout << "请输入该员工的新编号" << endl;
cin >> newId;
for (int i = 0; i < this->m_CurreEmpNum; i++)
{
while (this->m_EmpArry[i]->m_Id == newId)
{
cout << "你输入的编号已存在,请重新输入编号:" << endl;
cin >> id;
i = 0;
}
}
cout << "请输入该员工的新姓名" << endl;
cin >> newName;
cout << "请输入该员工的新岗位" << endl;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、总裁" << endl;
cin >> newDid;
//记录新添加员工的信息
Worker* worker = NULL;
switch (newDid)
{
case 1:
worker = new Employee(newId, newName, newDid);
break;
case 2:
worker = new Manager(newId, newName, newDid);
break;
case 3:
worker = new Boss(newId, newName, newDid);
break;
default:
cout << "你输入的岗位不存在" << endl;
break;
}
//更新数组中的数据
this->m_EmpArry[ret] = worker;
cout << "职工信息更新成功" << endl;
//更新文件中的数据
this->Save();
}
else
{
cout << "查无此人" << endl;
}
}
system("pause");
system("cls");
}
11、查找职工
//查找职工信息
void WorkerManager::FindEmp()
{
int Fselect;
string Fname;
int Fid;
bool Fflag = false;//是否找到标志
//判断文件
if (this->IsFileEmpty)
{
cout << "文件为空或文件不存在!" << endl;
}
else
{
cout << "请选择你的查找方式" << endl;
cout << "1、按姓名查找" << endl;
cout << "2、按编号查找" << endl;
cin >> Fselect;
switch (Fselect)
{
case 1:
{
cout << "请输入被查找人员的姓名" << endl;
cin >> Fname;
for (int i = 0; i < this->m_CurreEmpNum; i++)
{
if (this->m_EmpArry[i]->m_name == Fname)
{
cout << "查找成功,该职工信息如下:" << endl;
this->m_EmpArry[i]->ShowInfo();
Fflag = true;
}
}
if (!Fflag)
{
cout << "查无此人" << endl;
}
}
break;
case 2:
{
cout << "请输入被查找人员的编号" << endl;
cin >> Fid;
int ret = this->IsEmpExit(Fid);
if (ret != -1)
{
cout << "查找成功,该职工信息如下:" << endl;
this->m_EmpArry[ret]->ShowInfo();
}
else
{
cout << "查无此人" << endl;
}
}
break;
default:
cout << "你输入的查找方式不存在" << endl;
break;
}
}
system("pause");
system("cls");
}
12、按照职工编号排序
//按职工编号进行排序
void WorkerManager::SortEmp()
{
int SortWay;
//判断文件
if (this->IsFileEmpty)
{
cout << "文件为空或文件不存在!" << endl;
}
else
{
cout << "请选择排序方式" << endl;
cout << "1、升序" << endl;
cout << "2、降序" << endl;
cin >> SortWay;
switch (SortWay)
{
case 1:
{
for (int i = 0; i < this->m_CurreEmpNum - 1; i++)
{
for (int j = 0; j < this->m_CurreEmpNum - 1 - i; j++)
{
if (this->m_EmpArry[j]->m_Id > this->m_EmpArry[j + 1]->m_Id)
{
//交换数组的位置
Worker* temp = this->m_EmpArry[j];
this->m_EmpArry[j] = this->m_EmpArry[j + 1];
this->m_EmpArry[j + 1] = temp;
}
}
}
cout << "排序已完成" << endl;
this->Save();
}
break;
case 2:
{
for (int i = 0; i < this->m_CurreEmpNum - 1; i++)
{
for (int j = 0; j < this->m_CurreEmpNum - 1 - i; j++)
{
if (this->m_EmpArry[j]->m_Id < this->m_EmpArry[j + 1]->m_Id)
{
//交换数组的位置
Worker* temp = this->m_EmpArry[j];
this->m_EmpArry[j] = this->m_EmpArry[j + 1];
this->m_EmpArry[j + 1] = temp;
}
}
}
cout << "排序已完成" << endl;
this->Save();
}
break;
default:
break;
}
}
system("pause");
system("cls");
}
13、清空数据
- 确认是否清空
- 清空文件
- 清空元素指针
- 清空数组指针
- 更新相应数据
//清空数据
void WorkerManager::ClearFile()
{
int Select = 0;
cout << "是否确认清空数据" << endl;
cout << "1、清空" << endl;
cout << "2、返回" << endl;
cin >> Select;
if (Select == 1)
{
//若文件存在,删除并重新创建
ofstream ofs(FILNAME, ios::trunc);
ofs.close();
if (this->m_EmpArry != NULL)
{
for (int i = 0; i < this->m_CurreEmpNum; i++)
{
//释放每个元素的空间
delete this->m_EmpArry[i];
}
//更新当前职工人数
this->m_CurreEmpNum = 0;
delete[] this->m_EmpArry;
this->m_EmpArry = NULL;
//更新标志位
this->IsFileEmpty = true;
}
cout << "清除成功" << endl;
}
system("pause");
system("cls");
}
标签:WorkerManager,职工,cout,管理系统,int,worker,EmpArry,name
From: https://www.cnblogs.com/goldenFantome/p/17008064.html