创建职工管理类:
实现cmd界面的各种功能
#include <iostream>
#include <string>
#include "workmanager.h"
#include "worker.h"
using namespace std;
int main()
{
//实例化管理者对象
WorkerManager wm;
int choice = 0; //用来存储用户的选项
while (true) {
//调用展示菜单成员函数
wm.show_Menu();
cout << "请输入您的选择" << endl;
//获取用户选项
cin >> choice;
switch (choice) {
case 0: //退出系统
wm.ExitSystem();
break;
case 1: //增加员工
wm.AddEmp();
break;
case 2: //显示职工
wm.Show_Emp();
break;
case 3: //删除职工
wm.Del_Emp();
break;
case 4: //修改职工
wm.Mod_Emp();
break;
case 5: //查找职工
wm.Find_Emp();
break;
case 6: //排序职工
wm.Sort_Emp();
break;
case 7: //清空文档
break;
default: //清屏
break;
}
}
system("pause");
return 0;
}
创建职工抽象类:
为了使用多态来创建不同的职工对象
work.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
//职工抽象类
class worker {
public:
//获取个人信息
virtual void showinfo() = 0;
//获取岗位名称
virtual string getDepName() = 0;
//职工编号
int m_id;
//职工姓名
string m_name;
//部门编号
int m_did;
};
普通职工抽象类:
类的头文件:
employee.h
#pragma once
#include <iostream>
#include<string>
#include "worker.h"
using namespace std;
class Employee :public worker {
public:
//构造函数
Employee(int id,string name,int did);
//获取个人信息
virtual void showinfo();
//获取岗位名称
virtual string getDepName();
};
实现类中函数的employee.c
#include "employee.h"
Employee::Employee(int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_did = did;
}
void Employee::showinfo()
{
cout << "岗位编号" << this->m_did
<< "\t职工姓名" << this->m_name
<< "\t岗位:" << this->getDepName()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
string Employee::getDepName()
{
return string("员工");
}
经理头文件manager.h
#pragma once
#include<iostream>
#include <string>
#include"worker.h"
using namespace std;
class Manager:public worker {
public:
Manager(int id, string name, int did);
virtual void showinfo();
virtual string getDepName();
};
实现类中函数的manager.c
#include "manager.h"
Manager::Manager(int id, string name, int did) {
this->m_id = id;
this->m_name = name;
this->m_did = did;
}
void Manager::showinfo() {
cout << "岗位编号" << this->m_did
<< "\t经理姓名" << this->m_name
<< "\t岗位:" << this->getDepName()
<< "\t岗位职责:完成老板交给的任务,并将任务下发给员工" << endl;
}
string Manager::getDepName() {
return string("经理");
}
老板类头文件boss.h
#pragma once
#include <iostream>
#include <string>
#include "worker.h"
using namespace std;
class Boss:public worker {
public:
Boss(int id, string name, int did);
virtual void showinfo();
virtual string getDepName();
};
实现老板类中的boss.c
#include "boss.h"
Boss::Boss(int id, string name, int did) {
this->m_id = id;
this->m_name = name;
this->m_did = did;
}
void Boss::showinfo() {
cout << "岗位编号" << this->m_did
<< "\t老板姓名" << this->m_name
<< "\t岗位:" << this->getDepName()
<< "\t岗位职责:下发任务给经理" << endl;
}
string Boss::getDepName() {
return string("老板");
}
实现管理系统各种功能的函数:
workermanager.h:
#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class WorkerManager {
public:
//构造函数
WorkerManager();
//展示菜单
void show_Menu();
//记录职工人数
int m_EmpNum;
//职工数组指针
worker ** 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();
//按0退出
void ExitSystem();
//析构函数
~WorkerManager();
};
各种功能具体实现细节:
workermanager.c:
#include "workmanager.h"
#include "worker.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
#include <fstream>
#define FILENAME "empFile.txt"
WorkerManager::WorkerManager()
{
//分情况初始化:
ifstream ifs;
ifs.open(FILENAME, ios::in); //读文
//文件不存在
if (!ifs.is_open()) {
// cout << "文件不存在" << endl;
//初始化属性人数为0,指针数组为空
this->m_EmpNum = 0;
this->m_EmpArry = NULL;
this->m_FileIsEmpty = true; //初始化文件为空
ifs.close(); //因为文件不存在所以不需要执行其他操作,直接关闭,在添加时打开文件
return;
}
//文件存在但是为空
char ch;
ifs >> ch;
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 worker*[this->m_EmpNum];
//将文件中的数据存到数组中
this->init_Emp();
}
//统计文件中的人数
int WorkerManager::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++;
}
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) {
worker *work = NULL;
if (did == 1) { //普通职工
work = new Employee(id, name, did);
}
else if (did == 2) { //经理
work = new Manager(id, name, did);
}
else if (did == 3) { //老板
work = new Boss(id, name, did);
}
this->m_EmpArry[index] = work; //维护职工数组;
index++;
}
ifs.close(); //关闭文件
}
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::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_did << " " << endl;
}
ofs.close();
}
//添加职工
void WorkerManager::AddEmp() {
int addNum = 0;
cout << "请输入要添加的职工个数" << endl;
cin >> addNum;
if (addNum > 0) {
int size = this->m_EmpNum + addNum;
//开辟新的数组空间
worker ** newArray = new worker*[size];
if (this->m_EmpArry != NULL) {
for (int i = 0; i < this->m_EmpNum; i++) {
//将原有的数组赋值过来
newArray[i] = this->m_EmpArry[i];
}
}
for (int i = 0; i < addNum; i++) {
int id;
string name;
int did;
cout << "请输入第" << i + 1 << "个职工编号" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个职工姓名" << endl;
cin >> name;
cout << "请选择该职工岗位" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> did;
worker *work = NULL;
switch (did) {
case 1:
work = new Employee(id, name, 1);
break;
case 2:
work = new Manager(id, name, 2);
break;
case 3:
work = new Boss(id, name, 3);
break;
default:
break;
}
//将创建的职工保存到数组中
newArray[this->m_EmpNum + i] = work;
}
//释放原有空间;
delete[] this->m_EmpArry;
//更改新空间指向;
this->m_EmpArry = newArray;
//更新新的职工人数;
this->m_EmpNum = size;
//更新职工不为空的标志
this->m_FileIsEmpty = false;
//提示添加成功
cout << "成功添加" << size << "人" << endl;
save();
}
else {
cout << "输入的职工人数错误" << endl;
}
//清屏回到上级目录
system("pause");
system("cls");
}
//显示职工
void WorkerManager::Show_Emp() {
//判断文件是否为空
if (this->m_EmpArry) {
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)
{
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++) {
if (id == this->m_EmpArry[i]->m_id) {
index = i;
break;
}
}
return index;
}
//删除职工
void WorkerManager::Del_Emp()
{
if (this->m_FileIsEmpty) {
cout << "文件为空或无记录" << endl;
}else {
cout << "请输入需要删除的职工编号" << endl;
int id = 0;
cin >> id;
int index = this->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 {
int id;
cout << "请输入需要修改的职工编号" << endl;
cin >> id;
//查找职工
int index = this->IsExist(id);
if (index != -1) {
delete this->m_EmpArry[index];
int newId = 0;
string name = " ";
int did = 0;
cout << "查找到:" << id << "号职工,请输入新的id号" << endl;
cin >> newId;
cout << "请输入姓名" << endl;
cin >> name;
cout << "请选择该职工岗位" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> did;
worker *work = NULL;
switch (did) {
case 1:
work = new Employee(id, name, 1);
break;
case 2:
work = new Manager(id, name, 2);
break;
case 3:
work = new Boss(id, name, 3);
break;
default:
break;
}
//更新数据到数组中
this->m_EmpArry[index] = work;
cout << "修改成功" << endl;
//保存
this->save();
}
else {
cout << "查无此人" << endl;
}
}
//按任意键清屏
system("pause");
system("cls");
}
//查找职工函数
void WorkerManager::Find_Emp()
{
if (this->m_FileIsEmpty) {
cout << "文件为空或无记录" << endl;
}else {
cout << "请输入查找的方法" << endl;
cout << "1.按照职工编号进行查找" << endl;
cout << "2.按照姓名进行查找" << endl;
int select = 0;
cin >> select;
if (select == 1) {
//按照编号查
int id;
cout << "请输入编号" << endl;
cin >> id;
int index = this->IsExist(id);
if (index != -1) {
//找到该职工
cout << "查找该职工成功" << endl;
this->m_EmpArry[index]->showinfo(); //显示该职工信息
}
else {
cout << "查无此人" << endl;
}
}
else if (select == 2){
//按照姓名查
string name;
cout << "请输入姓名" << endl;
cin >> name;
//加入判断是否查到的标志
bool flag = false;
for (int i = 0; i < this->m_EmpNum; i++) {
if (this->m_EmpArry[i]->m_name == name) {
cout << "查找成功,该职工编号为" << this->m_EmpArry[i]->m_id
<<"该职工信息如下:"<< endl;
flag = true;
//调用显示接口
this->m_EmpArry[i]->showinfo();
}
}
if (flag == false) {
cout << "查无此人" << endl;
}
}
else {
cout << "输入的选项有误" << endl;
}
}
//按任意键清屏
system("pause");
system("cls");
}
//按照编号排序
void WorkerManager::Sort_Emp() {
if (this->m_FileIsEmpty) {
cout << "文件为空或无记录" << endl;
//按任意键清屏
system("pause");
system("cls");
}
else {
cout << "请选择排序的方式" << endl;
cout << "1.按照职工编号升序排" << endl;
cout << "2.按照职工编号降序排" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < this->m_EmpNum; i++) {
int minOrMax = i; // 声明最小值或最大值下标
for (int j = i + 1; j < this->m_EmpNum; j++) {
if (select == 1) { //升序
if (this->m_EmpArry[minOrMax]->m_id > this->m_EmpArry[j]->m_id) {
minOrMax = j;
}
}else {
//降序
if (this->m_EmpArry[minOrMax]->m_id < this->m_EmpArry[j]->m_id) {
minOrMax = j;
}
}
}
//判断最开始认定的最小值或最大值是不是计算的最小值或最大值,如果不是交换数据
if (i != minOrMax) {
worker * work = this->m_EmpArry[i];
this->m_EmpArry[i] = this->m_EmpArry[minOrMax];
this->m_EmpArry[minOrMax] = work;
}
}
}
cout << "排序成功!排序的结果为:" << endl;
this->save();//排序后的结果保存到文件中
this->Show_Emp();
}
//清空文件
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++) {
delete this->m_EmpArry[i];
this->m_EmpArry[i] = NULL;
}
//删除堆区数据指针
delete[] this->m_EmpArry;
this->m_EmpArry = NULL;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
}
cout << "清空成功" << endl;
}
//按任意键清屏
system("pause");
system("cls");
}
WorkerManager::~WorkerManager()
{ //程序结束释放堆区的数据
if (this->m_EmpArry != NULL) {
delete[] this->m_EmpArry;
this->m_EmpArry = NULL;
}
}
learned from:黑马程序员