C++职工管理系统(具备增删改查功能 & 涉及文件操作、指针数组操作、升序降序、多态、虚函数)

🌕需求分析

⭐职工分为三类:

老板 Boss
经理 Manager
普通员工 Employee

⭐显示信息时需要显示:

职工编号
姓名
岗位
职责 Duty
普通员工职责:完成经理交代的任务。
经理职责:完成老板交给的任务,并下发任务给员工。
老板职责:管理公司所有事务。

⭐要实现的功能:

在这里插入图片描述

🌕创建项目

windwos下vscode多文件项目Employee_Manager的创建、编译、运行

🌕完整代码

🌙项目结构

在这里插入图片描述

🌙include

⭐worker.h (它是后面employ,boss,manager的基类)

#pragma once
#include<iostream>
#include<string>
using namespace std;

// 职工抽象基类
class Worker
{
private:
    
public:
    int m_Id; //worker number
    string m_Name; //worker name;
    int m_DeptId;//职工所在的部门名称编号

    // show personal information.
    virtual void showInfo()=0;

    // get the dept of worker
    virtual string getDeptName() = 0;    
};

⭐boss.h

#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

//员工类
class Boss :public Worker
{
public:
    //构造函数
    Boss(int id,string name,int dId);

    //展示个人信息
    virtual void showInfo();

    //获取职工岗位名称
    virtual string getDeptName();
};

⭐employee.h

#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

//员工类
class Employee :public Worker
{
public:
    //构造函数
    Employee(int id,string name,int dId);

    //展示个人信息
    virtual void showInfo();

    //获取职工岗位名称
    virtual string getDeptName();
};

⭐manager.h

#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

// 经理类: 经理类有经理的个人信息,有岗位名称
class Manager :public Worker
{
public:
    Manager(int id,string name,int dId);

    //显示个人信息
    virtual void showInfo();

    //获取职工岗位名称
    virtual string getDeptName();

};

⭐workerManager.h

#pragma once
#include<iostream>
#include"worker.h"
#include"employee.h"
#include"boss.h"
#include"manager.h"
#include<fstream>
#define FILENAME "empFile.txt"
using namespace std;

class WorkerManager
{
public:
    // 记录职工的个数
    int m_EmpNum;

    // 存放职工的指针数组 worker* a; 
    Worker ** m_EmpArray;

    // 文件为空的标志
    bool m_FileIsEmpty;

    //构造函数
    WorkerManager();

    // 统计文件中的人数
    int get_EmpNum();

    //当文件里面有员工信息 读取它们
    void init_Emp();

    //显示职工
    void show_Emp();

    //删除时需要先判断输入的职工id是否存在。存在则返回该职工在数组中的位置,不存在返回-1
    //修改,查找,删除都需要用到该函数。
    int isExist(int id);

    //删除员工
    void del_Emp();

    //展示菜单
    void showMenu();

    //退出系统
    void exitSystem();

    //添加员工函数
    void add_Emp();

    //modify employee
    void mod_Emp();

    // find employee
    void find_Emp();

    // two way for sort.
    void sort_Emp();

    // clean file.
    void clean_File();

    void save();

    //析构函数
    ~WorkerManager();

};

🌙src

⭐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<<"Num:"<<this->m_Id
        <<"\tName:"<<this->m_Name
        <<"\tDept:"<<this->getDeptName()
        <<"\tDuty: Managing the company's overall affairs."<<endl;
}

string Boss::getDeptName()
{
    return string("Boss");
}

⭐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<<"Num:"<<this->m_Id
        <<"\tName:"<<this->m_Name
        <<"\tDept:"<<this->getDeptName()
        <<"\tDuty: Completing tasks assigned by the manager."<<endl;
}

string Employee::getDeptName()
{
    return string("Employee");
}

⭐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<<"Num:"<<this->m_Id
        <<"\tName:"<<this->m_Name
        <<"\tDept:"<<this->getDeptName()
        <<"\tDuty: Completing tasks assigned by the boss and distributing them to regular employees."<<endl;
}

string Manager::getDeptName()
{
    return string("Manager");
}

⭐workerManager.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<<"Num:"<<this->m_Id
        <<"\tName:"<<this->m_Name
        <<"\tDept:"<<this->getDeptName()
        <<"\tDuty: Managing the company's overall affairs."<<endl;
}

string Boss::getDeptName()
{
    return string("Boss");
}

🌙tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\\Users\\X2006600\\Desktop\\MinGW\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g","${file}","${fileDirname}\\src\\*.cpp",
                "-I","${fileDirname}\\include",
                "-o","${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

🌙main.cpp

#include<iostream>
using namespace std;
#include "workerManager.h"
#include "boss.h"
#include "employee.h"
#include "manager.h"

void test()
{
    Worker *worker = NULL;
    worker = new Employee(1,"Bruce",1);
    worker->showInfo();
    delete worker;

    worker = new Manager(2,"Tom",2);
    worker->showInfo();
    delete worker;

    worker = new Boss(3,"Jerry",3);
    worker->showInfo();
    delete worker;
}

int main()
{
    //声明一个管理系统类
    WorkerManager wm;
    int choice = 0;
    while (true)
    {
        //展示菜单
        wm.showMenu();
        cout<<"Please input the number you choiced:";
        cin>>choice;
        switch (choice)
        {
        case 0: //exit system;
            wm.exitSystem();
            break;
        case 1: //add employee
            wm.add_Emp();
            break;
        case 2: //show employee
            wm.show_Emp();
            break;
        case 3: //delete employee
            wm.del_Emp();
            break;
        case 4: //modify employee information
            wm.mod_Emp();
            break;
        case 5: //find employee information
            wm.find_Emp();
            break;
        case 6:
            wm.sort_Emp();
            break;
        case 7:
            wm.clean_File();
            break;
        default:
            break;
        }
    }
    
    system("Pause");
    return 0;
}

🌕一步一步的实现功能

🌙菜单功能实现

🌙退出功能实现

🌙添加职工功能


批量添加职工,并且保存到文件中。

用户在批量创建时可能会创建不同种类的职工。

因此这个数组是不定长的,可以增加的。

这个数组可以容纳不同种类员工,所以数组单元的类型可以是指针。


workerManager.h文件下

void add_Emp();

workerManager.cpp文件下:

void WorkerManager::add_Emp()
{
    cout << "Please enter the number of employees to be added:"<<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 << "Please enter the ID of the " << i + 1 << " employee:" << endl;
            cin >> id; 

            cout << "Please enter the name of the " << i + 1 << " employee:" << endl;
            cin >> name;

            cout << "Please select the dept for this employee:";
            cout << "1.Employee." << endl;
            cout << "2.Manager." << endl;
            cout << "3.Boss." << endl;
            cin >> dSelect;

            Worker *worker = NULL;
            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;

            default:
                break;
            }
            newSpace[this->m_EmpNum + i] = worker;
        }
        delete[] this->m_EmpArray;
        this->m_EmpArray = newSpace;
        this->m_EmpNum = newSize;
        this->m_FileIsEmpty = false;
        cout << "Successfully added " << addNum << " new employees!" << endl;
        this->save();
    }
    else
    {
        cout << "Your input is incorrect." << endl;
    }
    system("pause");
    system("cls");
}

🌙初始化运行时读取员工信息文件功能

⭐第一次使用,文件未创建的情况

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

在WorkerManager.h头文件中加入

bool m_fileIsEmpty;

修改WorkerManager.cpp的构造函数为:

WorkerManager::WorkerManager()
{
    ifstream ifs;
    ifs.open(FILENAME,ios::in);
    //文件不存在的情况
    if(!ifs.is_open())
    {   
        cout<<"The file does not exist."<<endl;
        //初始化文件为空的标志
        this->m_FileIsEmpty = true;
        //初始化人数
        this->m_EmpNum = 0;
        //初始化数组指针
        this->m_EmpArray = NULL;
        ifs.close(); //关闭文件
        return;
    }
    
}

⭐文件存在,但是数据被用户清空的情况

思路:读取一个字符,判断它是否是文件末尾的标志,如果是,则表示文件为空。

eof 是文件尾部的标志
    //----------------判断文件存在并且没有记录的情况-------------------
    char ch;
    ifs>>ch; // 读取一个字符,下一行判断它是否是文件末尾
    if(ifs.eof()) // eof是文件尾部的标志
    {
        cout<<"The file is empty!"<<endl;
        this->m_EmpNum = 0; //职工人数设置为0
        this->m_FileIsEmpty = true; //文件为空设置为true
        this->m_EmpArray = NULL; //存放职员的数组设置为空
        ifs.close();
        return;
    }
当添加职工成功之后,要更新 m_FileIsEmpty = false

⭐文件存在,并且已经保存了职工的所有数据的情况

在workerManager.h头文件中加入成员函数

int get_EmpNum();
void init_Emp();

在workerManager.cpp中实现这两个成员函数:

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;
        //根据不同的部门id创建不同的对象
        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();
}

构造函数中添加:

//---------------------文件存在并且里面有数据-------------------------
    int num = this->get_EmpNum();
    cout<<"Now, we have "<<num<<" employees."<<endl;
    this->m_EmpNum = num; //更新成员属性
    
    //更新文件是否为空的标志为 false
    this->m_FileIsEmpty = false;
    
    // 根据职工数创建数组
    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;
    }

在这里插入图片描述

🌙显示职工信息

在workerManager.h头文件中加入成员函数

void show_Emp();

workerManager.cpp中实现该函数

void WorkerManager::show_Emp()
{
    if(this->m_FileIsEmpty)
    {
        cout<<"File is not exist or the file is empty!"<<endl;
    }
    else
    {
        for (int i = 0; i < this->m_EmpNum; i++)
        {
            //利用多态调用接口
            this->m_EmpArray[i]->showInfo();
        }
        
    }
    //暂停一下,按任意键取消暂停
    system("pause");
    system("cls");
}

🌙删除职工

⭐判断要删除的员工号是否存在?

先在workerManager.h中添加成员函数


//删除时需要先判断输入的职工id是否存在。存在则返回该职工在数组中的位置,不存在返回-1
//修改,查找,删除都需要用到该函数。
int isExist(int id);
void Del_Emp();

⭐实现删除员工功能

删除就是数据前移,并把数组长度减一。

在这里插入图片描述

🌙修改职工信息

workerManager.h中添加如下内容:

 //modify employee
 void mod_Emp();

workerManager.cpp中添加如下内容:

void WorkerManager::mod_Emp()
{
    if(this->m_FileIsEmpty)
    {
        cout<<"file is not exist or the data is null"<<endl;
    }
    else
    {
        cout<<"please input the id of this employee:";
        int id;
        cin>>id;

        int ret = this->isExist(id);
        if(ret!=-1)
        {
            // delete the old employee of this id.
            delete this->m_EmpArray[ret];
            int newId = 0;
            string newName = "";
            int dSelect = 0;

            // cout<<"The employee information with ID "<<id<<" has been found."<<endl;
            cout<<"Please input the new id of this employee:";
            cin>>newId;

            cout<<"Please input the new name of this employee:";
            cin>>newName;

            cout << "Please select the new dept for this employee:"<<endl;;
            cout << "1.Employee." << endl;
            cout << "2.Manager." << endl;
            cout << "3.Boss." << endl;
            cin >> dSelect;

            Worker* worker = NULL;
            switch (dSelect)
            {
            case 1:
                worker = new Employee(newId,newName,dSelect);
                break;
            case 2:
                worker = new Manager(newId,newName,dSelect);
                break;
            case 3:
                worker = new Boss(newId,newName,dSelect);
                break;
            default:
                break;
            }

            // update the new info to the EmpArray;
            this->m_EmpArray[ret] = worker;
            cout<<"modifies is sucessful! the new info is:"<<endl;
            this->m_EmpArray[ret]->showInfo();

            // save to file.
            this->save();
        }
        else
        {
            cout<<"Failed to modify, as the employee cannot be found."<<endl;
        }
    }

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

}

main.cpp中的case 4中调用 mod_Emp();

🌙查找职工

WorkerManager.h添加如下内容:

// find employee
void find_Emp();

WorkerManager.cpp添加如下内容:

void WorkerManager::find_Emp()
{
    // first, judge whether the file or data is exist?
    if(this->m_FileIsEmpty)
    {
        cout<<"file is doesn't exist or the data is null."<<endl;
    }
    else
    {
        cout<<"1.find by id."<<endl;
        cout<<"2.find by name."<<endl;
        cout<<"please input the method you select:";
        int select = 0;
        cin>>select;
        if (select==1)
        {
            cout<<"please input the id that you find:";
            int id,index;
            cin>>id;
            // whether the id is exist?
            index = this->isExist(id);
            if (index!=-1)
            {
                this->m_EmpArray[index]->showInfo();
            }
            else
            {
                cout<<"the id is not exist.";
            }
        }
        else if (select==2)
        {
            string name;
            cout<<"please the name you want to find:";
            cin>>name;
            bool flag = false;
            for (int i = 0; i < m_EmpNum; i++)
            {
                if (this->m_EmpArray[i]->m_Name==name)
                {
                    cout<<"sucessful! the info of employee that you find is:"<<endl;
                    this->m_EmpArray[i]->showInfo();
                    flag = true;
                }
                
            }
            if(flag==false)
            {
                cout<<"There is no one here named ."<<name<<endl;
            }
            
        }
        else
        {
            cout<<"mistake, please re-input."<<endl;
        }
    }
    system("pause");
    system("cls");
}

主函数中添加如下内容:

        case 5: //modify employee information
            wm.find_Emp();
            break;

🌕参考视频

B站黑马C++教程

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

computer_vision_chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值