C++项目实战:职工管理系统

1.管理系统的要求

系统可以管理公司内部所有员工的信息
主要使用c++实现一个基于多态的职工管理系统

公司中的职工分为三类:普通员工、经理、老板,显示信息时需要显示职工编号、职工姓名、职工岗位以及职责
普通员工职责:完成经理安排的各项任务
经理职责:完成老板交付的任务,并拆解下发到员工
老板职责:统筹管理公司所有事务

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

  • 退出管理系统:退出当前系统
  • 增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号
  • 显示职工信息:显示公司内部所有职工的信息
  • 删除离职员工:按照编号删除指定的职工
  • 修改职工信息:按照编号修改职工个人信息
  • 查找职工信息:按照职工的编号户这话职工姓名查找职工对应信息
  • 按照编号排序:按照职工编号进行排序,排序规则由用户指定
  • 清空所有的文档:清空文件中记录的所有职工的信息(请空前需要再次确认,防止误操作)

2.创建项目

这里不做过多介绍,大家自行创建。

3.创建管理类

管理类负责的内容如下:

  • 与用户的沟通菜单界面
  • 对职工增删改查的操作
  • 与文件的读写交互

3.1创建文件

在头文件和源文件夹下分别创建workerManager.h和workerManager.cpp文件。

目录结构如下:
C++项目实战:职工管理系统_第1张图片

4 菜单界面

 功能描述:与用户的沟通界面

4.1 添加成员函数

在管理类workermanager.h中添加成员函数void show_Menu();

#pragma once //保证头文件只被编译一次
#include 
using namespace std;
#ifndef WORKERMANAGER_H
#define WORKERMANAGER_H
//一般在头文件中做声明,在源文件里实现。
class workerManager {
public:
    workerManager();
    ~workerManager();
    //显示菜单
    void Show_Menu();
};

#endif // WORKERMANAGER_H

workermanager.cpp里实现代码

#include "workermanager.h"
workerManager::workerManager()
{
}
workerManager::~workerManager()
{
}
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;
}

在主文件main.cpp包含头文件,用于代码的实现

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    workerManager work;
    work.Show_Menu();
    system("pause");
    return 0;
}

5. 退出功能

5.1 提供功能接口

在main函数中提供分支选择,提供每个接口

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    workerManager work;
    int select = 0;
    while (true) {
        //显示
        work.Show_Menu();
        cout << "请选择您的操作:" << endl;
        cin >> select;
        switch (select) {
        case 0: //退出系统
            cout << "欢迎下次使用" << endl;
            break;
            return 0;
        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;
        }
    }
    return 0;
}

5.2 退出功能实现

在workermanager.h中提供退出系统的成员函数 void exitSystem();

#pragma once //保证头文件只被编译一次
#include 
using namespace std;
#ifndef WORKERMANAGER_H
#define WORKERMANAGER_H
//一般在头文件中做声明,在源文件里实现。
class workerManager {
public:
    workerManager();
    ~workerManager();
    //显示菜单
    void Show_Menu();
    //退出操作
    void exitSystem();
};

#endif // WORKERMANAGER_H


在workermanager.cpp中实现代码

void workerManager::exitSystem()
{
    cout << "欢迎下次使用" << endl;
    system("pause");
    exit(0);
}

6. 创建职工类

6.1 创建职工抽象类

职工的分类为:普通员工、经理、老板
将三种职工抽象到一个类(woker)中,利用多态管理不同的职工类
职工的属性为:职工编号、职工姓名、职工所在部门编号
职工的行为为:岗位职责信息描述,获取岗位名称

首先需要创建公共的work.h文件,并写入以下代码:

#ifndef WORKER_H
#define WORKER_H
#include 
#include 
using namespace std;

class Worker {
public:
    Worker();
    //显示个人信息
    virtual void showInfo() = 0;
    //获取岗位名称
    virtual void getDeptName() = 0;

    int m_Id; //职工编号
    string m_Name; //职工姓名
    int m_DeptId; //职工所在的部门名称编号
};

#endif // WORKER_H

worker.cpp文件:

#include "worker.h"

Worker::Worker()
{

}

6.2 创建普通员工类

创建employee.h和emploee.cpp文件,类名称为Employee

employee.h文件

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include 

//普通员工类,继承抽象类
class Employee : public Worker {
public:
    Employee(int id, string name, int did);
    //显示个人信息
    virtual void showInfo(); //实现
    //获取岗位名称
    virtual string getDeptName(); //实现
};

#endif // EMPLOYEE_H

employee.cpp文件

#include "employee.h"

Employee::Employee(int id, string name, int did)
{
    //初始化
    m_Id = id;
    this->m_Name = name;
    m_DeptId = did;
}
void Employee::showInfo()
{
    cout << "职工编号 " << this->m_Id << "\t";
    cout << "职工姓名 " << this->m_Name << "\t";
    //    cout << "岗位名称 " << this->m_DeptId << "\t";
    cout << "岗位名称 " << this->getDeptName() << "\t";
    cout << "岗位职责:完成经理交给的任务" << endl;
}
//获取岗位名称
string Employee::getDeptName()
{
    return string("员工"); //返回const char *
}

6.3 创建经理类

经理类继承职工抽象类,并且重写父类中的
纯虚函数,和普通员工类似
在头文件和源文件夹下创建manager.h和manager.cpp文件,类名为Manager

manager.h如下:

#ifndef MANAGER_H
#define MANAGER_H
#include "worker.h"
#include 
using namespace std;

class Manager : public Worker {
public:
    Manager(int id, string name, int did);
    //显示个人信息
    virtual void showInfo(); //实现
    //获取岗位名称
    virtual string getDeptName(); //实现
};

#endif // MANAGER_H

mamager.cpp如下:

#include "manager.h"

Manager::Manager(int id, string name, int did)
{
    //初始化
    m_Id = id;
    this->m_Name = name;
    m_DeptId = did;
}

void Manager::showInfo()
{
    cout << "职工编号 " << this->m_Id << "\t";
    cout << "职工姓名 " << this->m_Name << "\t";
    cout << "岗位名称 " << this->getDeptName() << "\t";
    cout << "岗位职责:完成老板交给的任务,并给员工布置任务" << endl;
}
//获取岗位名称
string Manager::getDeptName()
{
    return string("经理"); //返回const char *
}

6.4 创建老板类

老板类继承职工抽象类,并且重写父类中的纯虚函数,和普通员工类似
在头文件和源文件夹下分辨创建boss.h和boss.cpp文件,类名为Boos

boos.h

#ifndef BOOS_H //宏定义,注意复制的时候,这里不要重复,会报错
#define BOOS_H
#include "worker.h"
#include 
using namespace std;

class Boos : public Worker {
public:
    Boos(int id, string name, int did);
    //显示个人信息
    virtual void showInfo(); //实现
    //获取岗位名称
    virtual string getDeptName(); //实现
};

#endif // MANAGER_H

boos.cpp
 

#include "boos.h"

Boos::Boos(int id, string name, int did)
{
    //初始化
    this->m_Id = id;
    this->m_Name = name;
    this->m_DeptId = did;
}
void Boos::showInfo()
{
    cout << "职工编号 " << this->m_Id << "\t";
    cout << "职工姓名 " << this->m_Name << "\t";
    cout << "岗位名称 " << this->getDeptName() << "\t";
    cout << "岗位职责:管理所有的事物" << endl;
}
//获取岗位名称
string Boos::getDeptName()
{
    return string("老板"); //返回const char *
}

main.cpp测试多态

    Worker* worker = NULL;
    worker = new Employee(1, "张三", 1);
    worker->showInfo();
    Worker* worke = new Manager(1, "张三", 1);
    worke->showInfo();
    worker = new Boos(1, "张三", 1);
    worker->showInfo();

7 添加职工

功能描述:批量添加职工,并且保存到文件中

7.1 功能分析

分析:
用户在批量创建时,可能会创建不同种类的职工,利用多态,用父类的指针保存子类对象。
如果将所有不同种类的员工都放到同一个数组中,可以将所有员工的指针维护到一个数组里面
如果想在程序中维护这个不定长度的数组,可以将数组创建到堆区,并利用Worker ** 的指针维护

C++项目实战:职工管理系统_第2张图片

 7.2 功能实现

在workermanager.h中添加成员属性

    //记录文件中的人数个数
    int m_EmpNum;
    //员工数组指针
    Worker **m_EmpArray; 
    void Add_Emp();

在workermanager.cpp文件中初始化

WorkerManager::WorkerManager()
{
    //数据初始化
    this->m_EmpNum = 0;
    this->m_EmpArray = NULL;
}
void WorkerManager::Add_Emp()
{
    cout << "请输入添加职工的数量" << endl;
    int addNum = 0;
    cin >> addNum;
    if (addNum > 0) {
        //添加
        //计算添加空间的大小
        int newSize = this->m_EmpNum + addNum; //新空间大小 = 元人数+新增元素
        Worker** newSpace = new Worker*[newSize];
        //将原来空间数据,拷贝到新空间下
        if (this->m_EmpArray != NULL) {
            for (int i = 0; i < this->m_EmpNum; i++) {
                newSpace[i] = this->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 = 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 Boos(id, name, dselect);
                break;
            default:
                break;
            }
            //将创建的职工指针,保存到数组中
            newSpace[this->m_EmpNum + i] = worker;
        }
        //释放原有的空间
        delete[] this->m_EmpArray;
        //更改新空间的指向
        this->m_EmpArray = newSpace;
        //更新新职工的人数
        this->m_EmpNum = newSize;
        //提示添加成功
        cout << "成功添加" << addNum << "名职工" << endl;
        //按任意键继续,清理屏幕。
        system("pause");
        system("cls");

    } else {
        cout << "输入有误" << endl;
        system("pause");
        system("cls");
    }
}

在main.cpp文件中调用即可


        case 1: //添加职工
            work.Add_Emp();

8.文件交互-写文件

功能描述:对文件进行读写
在上一个添加功能中,我们只是将所有的数据添加到了内存中,一旦程序结束运行就无法保存数据了,因此文件管理类中需要一个与文件交互的功能,对文件进行读和写的操作。

8.1 设定文件路径

首先我们将文件路径,在workermanager.h中添加宏常量,并包含头文件fstream

#include 
#include 
#define FILENAME "test.txt" //宏定义

写一个保存的函数save

void WorkerManager::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out);
    //将数据写入文件中
    for (int i = 0; i < this->m_EmpNum; i++) {
        ofs << this->m_EmpArray[i]->m_Id << " "
            << this->m_EmpArray[i]->m_Name << " "
            << this->m_EmpArray[i]->getDeptName() << endl;
    }
    ofs.close();
}

9 文件交互-读文件

功能描述:将文件中的内容读取到程序中
虽然我们实现了添加职工后保存到文件中的操作,但是每次开始运行程序,并没有将文件中的数据读取到程序内存中,而我们的程序功能中还有清空文件的需求,所以我们的构造函数初始化分为三种情况

  1. 第一次使用,文件未被创建
  2. 文件存在,但是数据被用户清空
  3. 文件存在,并且保存职工的所有数据

9.1 文件未创建

在wokerManager.h中添加新的成员属性m_FilesEmpty标志文件是否为空

修改构造函数代码

WorkerManager::WorkerManager()
{
    //数据初始化
    //分三种情况
    // 1.文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in); //读文件
    if (!ifs.is_open()) {
        //文件不存在
        cout << "文件不存在" << endl;
        this->m_EmpNum = 0;
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    } else {
        //文件存在
    }
}

删除文件后,测试文件不存在时初始化失败。

9.2 文件存在且数据为空

修改构造函数代码

WorkerManager::WorkerManager()
{
    //数据初始化
    //分三种情况
    // 1.文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in); //读文件
    if (!ifs.is_open()) {
        //文件不存在
        cout << "文件不存在" << endl;
        this->m_EmpNum = 0;
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    } else {
        //文件存在,并且没有记录
        char ch;
        ifs >> ch;
        if (ifs.eof()) //为真代表读取完了,文件为空
        {
            cout << "文件为空" << endl;
            this->m_EmpNum = 0;
            this->m_EmpArray = NULL;
            //初始化文件是否为空
            this->m_FileIsEmpty = true;
            ifs.close();
            return;
        }
    }
}

9.3 文件存在且保存职工数据

9.3.1 获取记录的职工人数

在workermanager.h中添加成员函数 int get_EmpNum();

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++;
    }
    ifs.close();
    return num; //返回记录人数
}

在构造函数中新增

    //    cout << "文件存在,且有数据" << endl;
    // 3.文件存在,且有数据
    int num = this->get_EmpNum();
    cout << "职工人数" << num << endl; //获取人数
    this->m_EmpNum = num;

9.3.2 初始化数组

根据职工的数据,初始化wokermanager中的Woker** m_Emparray指针
在wokermanager.h中添加成员函数void init_Emp();

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* worker = NULL;
        if (did == 1) {
            worker = new Employee(id, name, did); //创建普通员工
        } else if (did == 2) {
            worker = new Manager(id, name, did); //创建经理

        } else {
            worker = new Boos(id, name, did); //创建老板
        }
        this->m_EmpArray[index] = worker;
        index++;
    }
    ifs.close();
}

    // 3.文件存在,且有数据
    int num = this->get_EmpNum();
    cout << "职工人数" << num << endl; //获取人数
    this->m_EmpNum = num;

    //初始化,大小为文件中的大小
    //开辟空间
    this->m_EmpArray = new Worker*[this->m_EmpNum];
    //创建数据
    this->init_Emp();
    //测试代码
//    for (int i = 0; i < this->m_EmpNum; i++) {
//        cout << this->m_EmpArray[i]->m_Id << " "
//             << this->m_EmpArray[i]->m_Name << " "
//             << this->m_EmpArray[i]->m_DeptId << endl;
//        cout << this->m_EmpArray[i]->getDeptName() << endl;
        
//    }

10 显示员工

功能描述:显示当前所有职工的信息

10.1 显示职工函数声明

在workermanager.h中添加一个成员函数void Show_Emp();

void show_Emp();

10.2 显示职工函数

void WorkerManager::show_Emp()
{
    //判断文件是否为空
    if (this->m_FileIsEmpty) {
        // true 为空
        cout << "文件不存在,或者文件为空" << endl;
    } else {
        //不为空
        for (int i = 0; i < this->m_EmpNum; i++) {
            this->m_EmpArray[i]->showInfo(); //不同的对象调用同一个函数,结果一样。
        }
    }
    system("pause");
    system("cls");
}

11 删除职工

功能描述:按照职工的编号进行删除职工操作

11.1 删除职工声明

在workermanager.h中添加成员函数void Del_Emp();

    //删除职工
    void Del_Emp();

11.2 职工是否存在函数声明

很多功能都需要根据职工是否存在来进行操作:删除职工,修改职工,查找职工
因此添加该函数,方便后面调用
在workermanager.h中添加成员函数 int IsExist(int id);

11.3 职工是否存在函数实现

在workermanager中实现成员函数 int IsExist(int id);

int WorkerManager::IsExist(int id)
{
    int index = -1;
    for (int i = 0; i < this->m_EmpNum; i++) {
        if (this->m_EmpArray[i]->m_Id == id) {
            index = id; //找到
            break;
            return index;
        }
    }
    return index;
}

在main函数中使用测试代码:

    case 3: //删除职工
            int ret;
            ret = work.IsExist(5);
            if (ret != -1) {
                cout << "职工存在" << endl;
            } else {
                cout << "职工不存在" << endl;
            }
            break;

11.4 删除职工

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_EmpArray[i] = this->m_EmpArray[i + 1];
            }
            this->m_EmpNum--; //更新数组中记录的人员个数
            //同步更新文件
            this->save();
            cout << "删除成功" << endl;
        } else {
            cout << "删除失败,没找到员工" << endl;
        }

        system("pause");
        system("cls");
    }
}

12 修改职工

功能描述:能够按照职工的编号对职工信息进行修改

12.1 修改职工函数声明

在workermanager.h中添加一个成员函数void Mod_Emp();

12.2 修改职工实现

void WorkerManager::Mod_Emp()
{
    if (this->m_FileIsEmpty) {
        cout << "文件不存在或者记录为空" << endl;

    } else {
        cout << "请输入修改职工的编号" << endl;
        int id;
        cin >> id;
        int ret = this->IsExist(id);
        if (ret != -1) {
            //找到了员工
            cout << "查到:" << id << "号职工,职工号 " << this->m_EmpArray[ret]->m_Id << " "
                 << "姓名" << this->m_EmpArray[ret]->m_Name << " "
                 << "部门" << this->m_EmpArray[ret]->m_DeptId << endl;

            delete this->m_EmpArray[ret];
            int newId = 0;
            string newName = "";
            int newDid = 0;

            cout << "请输入新的职工号" << endl;
            cin >> newId;
            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 Boos(newId, newName, newDid);
                break;
            default:
                break;
            }
            //更新数据 到数组
            this->m_EmpArray[ret] = worker;
            cout << "修改数据成功" << this->m_EmpArray[ret]->m_DeptId << endl;
            //保存到文件
            this->save();

        } else {
            cout << "没有该员工" << endl;
        }
    }
    system("pause");
    system("cls");
}

13 查找职工

功能描述:提供两种职工查询方式,一种按照职工编号,一种按照职工姓名

13.1 查找职工函数声明

在workerManager.h中添加一个成员函数void Find_Emp();

13.2 查找职工函数实现


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 ret = this->IsExist(id);
            if (ret != -1) {
                cout << "查找成功!该职工信息如下" << endl;
                this->m_EmpArray[ret]->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_EmpArray[i]->m_Name == name) {
                    cout << "查找成功,职工编号为" << this->m_EmpArray[i]->m_Id << "号职工信息如下" << endl;
                    this->m_EmpArray[i]->showInfo();
                    flag = true;
                }
            }
            if (flag == false) {
                cout << "查找失败,查无此人" << endl;
            }
        } else {
            cout << "输入的选项有误" << endl;
        }
    }
    //按任意键清频
    system("pause");
    system("cls");
}

14 排序

功能描述:根据职工的编号进行排序,排序的顺序有用户指定

14.1 排序函数声明

vois Sort_Emp();

14.2 排序函数实现

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_EmpArray[minOrmax]->m_Id > this->m_EmpArray[j]->m_Id) {
                        //我们认为的最小数不是最小值
                        minOrmax = j;
                    }
                } else {
                    //降序
                    if (this->m_EmpArray[minOrmax]->m_Id < this->m_EmpArray[j]->m_Id) {
                        //我们认为的最小数不是最小值
                        minOrmax = j;
                    }
                }
            }
            //判断一开始认定的最小值和最大值 是不是计算的最小值,最大值,如果不是 交换数据
            if (i != minOrmax) {
                Worker* temp = this->m_EmpArray[i];
                this->m_EmpArray[i] = this->m_EmpArray[minOrmax];
                this->m_EmpArray[minOrmax] = temp;
            }
        }
        cout << "排序成功!排序成功后的结果为:" << endl;
        this->save(); //将数据保存文件
        this->show_Emp(); //展示所有职工
    }
}

15 清空文件

功能描述:将文件中记录的数据清空

15.1 清空函数声明

void Clear_File();

15.2 函数实现

void WorkerManager::Clear_File()
{
    cout << "确认清空么?" << endl;
    cout << "1.确定" << endl;
    cout << "2.返回" << endl;
    int select;
    cin >> select;
    if (select == 1) {
        //清空
        ofstream ofs(FILENAME, ios::trunc); //删除文件后在创建
        ofs.close();
        if (this->m_EmpArray != NULL) {
            //删除堆区每个对象
            for (int i = 0; i < this->m_EmpNum; i++) {
                delete this->m_EmpArray[i];
                this->m_EmpArray = NULL;
            }
            //删除堆区数组指针
            delete[] this->m_EmpArray;
            this->m_EmpArray = NULL;
            this->m_EmpNum = 0;
            this->m_FileIsEmpty = true;
        }
        cout << "清空成功" << endl;
    }
    system("pause");
    system("cls");
}

16 项目完整代码

16.1 目录结构如下

C++项目实战:职工管理系统_第3张图片

16.2 头文件内容

boos.h

#ifndef BOOS_H
#define BOOS_H
#include "worker.h"
#include 
using namespace std;

class Boos : public Worker {
public:
    Boos(int id, string name, int did);
    //显示个人信息
    virtual void showInfo(); //实现
    //获取岗位名称
    virtual string getDeptName(); //实现
};

#endif // MANAGER_H

 employee.h

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include 

//普通员工类,继承抽象类
class Employee : public Worker {
public:
    Employee(int id, string name, int did);
    //显示个人信息
    virtual void showInfo(); //实现
    //获取岗位名称
    virtual string getDeptName(); //实现
};

#endif // EMPLOYEE_H

manager.h

#ifndef MANAGER_H
#define MANAGER_H

#include "worker.h"
#include 
using namespace std;

class Manager : public Worker {
public:
    Manager(int id, string name, int did);
    //显示个人信息
    virtual void showInfo(); //实现
    //获取岗位名称
    virtual string getDeptName(); //实现
};

#endif // MANAGER1_H

worker.h

#ifndef WORKER_H
#define WORKER_H
#include 
#include 
using namespace std;

//职工抽象类
class Worker {
public:
    Worker();
    //显示个人信息
    virtual void showInfo() = 0; //纯虚函数
    //获取岗位名称
    virtual string getDeptName() = 0; //纯虚函数
    int m_Id; //职工编号
    string m_Name; //职工姓名
    int m_DeptId; //职工所在的部门名称编号
};

#endif // WORKER_H

workermanager.h

#pragma once //保证头文件只被编译一次
#include "boos.h"
#include "employee.h"
#include "manager.h"

#include 
#include 
#define FILENAME "test.txt" //宏定义

using namespace std;
#ifndef WORKERMANAGER_H
#define WORKERMANAGER_H
#include "worker.h"
//一般在头文件中做声明,在源文件里实现。
class WorkerManager {
public:
    //记录文件中的人数个数
    int m_EmpNum;
    //员工数组指针
    Worker** m_EmpArray;
    //添加属性判断文件是否为空
    bool m_FileIsEmpty;
    //统计文件中的人数
    int get_EmpNum();
    WorkerManager();
    ~WorkerManager();
    //显示菜单
    void Show_Menu();
    //退出操作
    void exitSystem();
    // 添加职工
    void Add_Emp();
    //保存文件
    void save();
    //初始化职工
    void init_Emp();
    //显示职工
    void show_Emp();
    //删除职工
    void Del_Emp();

    //判断职工是否存在

    //根据职工编号判断职工是否存在,若存在返回职工在数组中的位置,不存在返回-1
    int IsExist(int id);

    //修改职工
    void Mod_Emp();
    //查找员工
    void Find_Emp();

    //排序算法
    void Sort_Emp();
    //清空文件
    void Clear_File();
};

#endif // WORKERMANAGER_H

16.3 cpp文件

boos.cpp

#include "boos.h"

Boos::Boos(int id, string name, int did)
{
    //初始化
    this->m_Id = id;
    this->m_Name = name;
    this->m_DeptId = did;
}
void Boos::showInfo()
{
    cout << "职工编号 " << this->m_Id << "\t";
    cout << "职工姓名 " << this->m_Name << "\t";
    cout << "岗位名称 " << this->getDeptName() << "\t";
    cout << "岗位职责:管理所有的事物" << endl;
}
//获取岗位名称
string Boos::getDeptName()
{
    return string("老板"); //返回const char *
}

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";
    cout << "职工姓名 " << this->m_Name << "\t";
    //    cout << "岗位名称 " << this->m_DeptId << "\t";
    cout << "岗位名称 " << this->getDeptName() << "\t";
    cout << "岗位职责:完成经理交给的任务" << endl;
}
//获取岗位名称
string Employee::getDeptName()
{
    return string("员工"); //返回const char *
}

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";
    cout << "职工姓名 " << this->m_Name << "\t";
    cout << "岗位名称 " << this->getDeptName() << "\t";
    cout << "岗位职责:完成老板的任务,并且给员工布置任务" << endl;
}
//获取岗位名称
string Manager::getDeptName()
{
    return string("经理"); //返回const char *
}

worker.cpp

#include "worker.h"

Worker::Worker()
{
}

workermanager.cpp

#include "workermanager.h"
WorkerManager::WorkerManager()
{
    //数据初始化
    //分三种情况
    // 1.文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in); //读文件
    if (!ifs.is_open()) {
        //文件不存在
        //        cout << "文件不存在" << endl;
        this->m_EmpNum = 0;
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    // 2.文件存在,并且没有记录
    char ch;
    ifs >> ch;
    if (ifs.eof()) //为真代表读取完了,文件为空
    {
        //        cout << "文件为空" << endl;
        this->m_EmpNum = 0;
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    //    cout << "文件存在,且有数据" << endl;
    // 3.文件存在,且有数据
    int num = this->get_EmpNum();
    //    cout << "职工人数" << num << endl; //获取人数
    this->m_EmpNum = num;

    //初始化,大小为文件中的大小
    //开辟空间
    this->m_EmpArray = new Worker*[this->m_EmpNum];
    //创建数据
    this->init_Emp();

    //测试代码
    //    for (int i = 0; i < this->m_EmpNum; i++) {
    //        cout << this->m_EmpArray[i]->m_Id << " "
    //             << this->m_EmpArray[i]->m_Name << " "
    //             << this->m_EmpArray[i]->m_DeptId << endl;
    //        cout << this->m_EmpArray[i]->getDeptName() << endl;

    //    }
}
WorkerManager::~WorkerManager()
{
    //用于释放数据
    if (this->m_EmpArray != NULL) {
        for (int i = 0; i < this->m_EmpNum; i++) {
            if (this->m_EmpArray[i] != NULL) {
                delete this->m_EmpArray[i];
            }
        }
        delete[] this->m_EmpArray;
        this->m_EmpArray = NULL;
    }
}
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;
}

void WorkerManager::exitSystem()
{
    cout << "欢迎下次使用" << endl;
    //    system("pause");
    exit(0); //代表退出系统,返回代码0,代表正常退出
}

void WorkerManager::Add_Emp()
{
    cout << "请输入添加职工的数量" << endl;
    int addNum = 0;
    cin >> addNum;
    if (addNum > 0) {
        //添加
        //计算添加空间的大小
        int newSize = this->m_EmpNum + addNum; //新空间大小 = 元人数+新增元素
        Worker** newSpace = new Worker*[newSize];
        //将原来空间数据,拷贝到新空间下
        if (this->m_EmpArray != NULL) {
            for (int i = 0; i < this->m_EmpNum; i++) {
                newSpace[i] = this->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 = 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 Boos(id, name, dselect);
                break;
            default:
                break;
            }
            //将创建的职工指针,保存到数组中
            newSpace[this->m_EmpNum + i] = worker;
        }
        //释放原有的空间
        delete[] this->m_EmpArray;
        //更改新空间的指向
        this->m_EmpArray = newSpace;
        //更新新职工的人数
        this->m_EmpNum = newSize;
        //提示添加成功
        cout << "成功添加" << addNum << "名职工" << endl;
        //将数据置为false
        this->m_FileIsEmpty = false;

        //按任意键继续,清理屏幕。
        system("pause");
        system("cls");

    } else {
        cout << "输入有误" << endl;
        system("pause");
        system("cls");
    }
}

void WorkerManager::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out); //这里进行out不会产生文件的覆盖,不知道为啥,按理说out是覆盖写,可以用ios::app,进行追加,文件过多app会数据重复
    //将数据写入文件中
    for (int i = 0; i < this->m_EmpNum; i++) {
        ofs << this->m_EmpArray[i]->m_Id << " "
            << this->m_EmpArray[i]->m_Name << " "
            //            << this->m_EmpArray[i]->getDeptName() << endl;
            << this->m_EmpArray[i]->m_DeptId << endl;
    }
    ofs.close();
}
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++;
    }
    ifs.close();
    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* worker = NULL;
        if (did == 1) {
            worker = new Employee(id, name, did); //创建普通员工
        } else if (did == 2) {
            worker = new Manager(id, name, did); //创建经理

        } else {
            worker = new Boos(id, name, did); //创建老板
        }
        this->m_EmpArray[index] = worker;
        index++;
    }
    this->m_FileIsEmpty = false;

    ifs.close();
}

void WorkerManager::show_Emp()
{
    if (this->m_EmpNum == 0) {
        this->m_FileIsEmpty = true;
    }

    //判断文件是否为空
    if (this->m_FileIsEmpty || this->m_EmpNum == 0) {
        // true 为空
        cout << "文件不存在,或者文件为空" << endl;
    } else {
        //不为空
        for (int i = 0; i < this->m_EmpNum; i++) {
            this->m_EmpArray[i]->showInfo(); //不同的对象调用同一个函数,结果一样。
        }
    }
    system("pause");
    system("cls");
}
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_EmpArray[i] = this->m_EmpArray[i + 1];
            }
            this->m_EmpNum--; //更新数组中记录的人员个数
            //同步更新文件
            this->save();
            cout << "删除成功" << endl;
        } else {
            cout << "删除失败,没找到员工" << endl;
        }

        system("pause");
        system("cls");
    }
}

int WorkerManager::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; //找到索引,i是所在的下标
            break;
        }
    }
    return index;
}

void WorkerManager::Mod_Emp()
{
    if (this->m_FileIsEmpty) {
        cout << "文件不存在或者记录为空" << endl;

    } else {
        cout << "请输入修改职工的编号" << endl;
        int id;
        cin >> id;
        int ret = this->IsExist(id);
        if (ret != -1) {
            //找到了员工
            cout << "查到:" << id << "号职工,职工号 " << this->m_EmpArray[ret]->m_Id << " "
                 << "姓名" << this->m_EmpArray[ret]->m_Name << " "
                 << "部门" << this->m_EmpArray[ret]->m_DeptId << endl;

            delete this->m_EmpArray[ret];
            int newId = 0;
            string newName = "";
            int newDid = 0;

            cout << "请输入新的职工号" << endl;
            cin >> newId;
            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 Boos(newId, newName, newDid);
                break;
            default:
                break;
            }
            //更新数据 到数组
            this->m_EmpArray[ret] = worker;
            cout << "修改数据成功" << this->m_EmpArray[ret]->m_DeptId << 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 ret = this->IsExist(id);
            if (ret != -1) {
                cout << "查找成功!该职工信息如下" << endl;
                this->m_EmpArray[ret]->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_EmpArray[i]->m_Name == name) {
                    cout << "查找成功,职工编号为" << this->m_EmpArray[i]->m_Id << "号职工信息如下" << endl;
                    this->m_EmpArray[i]->showInfo();
                    flag = true;
                }
            }
            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_EmpArray[minOrmax]->m_Id > this->m_EmpArray[j]->m_Id) {
                        //我们认为的最小数不是最小值
                        minOrmax = j;
                    }
                } else {
                    //降序
                    if (this->m_EmpArray[minOrmax]->m_Id < this->m_EmpArray[j]->m_Id) {
                        //我们认为的最小数不是最小值
                        minOrmax = j;
                    }
                }
            }
            //判断一开始认定的最小值和最大值 是不是计算的最小值,最大值,如果不是 交换数据
            if (i != minOrmax) {
                Worker* temp = this->m_EmpArray[i];
                this->m_EmpArray[i] = this->m_EmpArray[minOrmax];
                this->m_EmpArray[minOrmax] = temp;
            }
        }
        cout << "排序成功!排序成功后的结果为:" << endl;
        this->save(); //将数据保存文件
        this->show_Emp(); //展示所有职工
    }
}
void WorkerManager::Clear_File()
{
    cout << "确认清空么?" << endl;
    cout << "1.确定" << endl;
    cout << "2.返回" << endl;
    int select;
    cin >> select;
    if (select == 1) {
        //清空
        ofstream ofs(FILENAME, ios::trunc); //删除文件后在创建
        ofs.close();
        if (this->m_EmpArray != NULL) {
            //删除堆区每个对象
            for (int i = 0; i < this->m_EmpNum; i++) {
                delete this->m_EmpArray[i];
                this->m_EmpArray = NULL;
            }
            //删除堆区数组指针
            delete[] this->m_EmpArray;
            this->m_EmpArray = NULL;
            this->m_EmpNum = 0;
            this->m_FileIsEmpty = true;
        }
        cout << "清空成功" << endl;
    }
    system("pause");
    system("cls");
}

6.4 主文件

main.cpp

#include "boos.h"
#include "employee.h"
#include "worker.h"
#include "workermanager.h"
#include 
#include 
#include 
using namespace std;
#include "manager.h"
int main()
{
    //测试代码

    //    Worker* worker = NULL;
    //    worker = new Employee(1, "张三", 1);
    //    worker->showInfo();
    //    Worker* worke = new Manager(1, "张三", 1);
    //    worke->showInfo();
    //    worker = new Boos(1, "张三", 1);
    //    worker->showInfo();

    WorkerManager work;
    int select = 0;
    while (true) {
        //显示
        work.Show_Menu();
        cout << "请选择您的操作:" << endl;
        cin >> select;
        switch (select) {
        case 0: //退出系统
            work.exitSystem();

        case 1: //添加职工
            work.Add_Emp();
            work.save();
            break;
        case 2: //显示职工
            work.show_Emp();
            break;
        case 3: //删除职工
            work.Del_Emp();
            break;
        case 4: //修改职工
            work.Mod_Emp();
            break;
        case 5: //查找职工
            work.Find_Emp();
            break;
        case 6: //排序职工
            work.Sort_Emp();
            break;
        case 7: //清空文件
            work.Clear_File();
            break;
        default:
            system("cls");
            break;
        }
    }
    return 0;
}

项目exe贴在资源栏,可自取。

你可能感兴趣的:(C++,c++,开发语言)