C++写职工管理系统

boss.h:

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


class Boss :public Worker
{
public:
    Boss(int id, string name, int did);

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

    virtual string getDid();//获取岗位名称

    virtual string getDduty();//获取岗位职责
};
 

employee.h:

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


class Employee:public Worker
{
public:
    Employee(int id, string name, int did);

    virtual void showInfo();//显示个人信息
    
    virtual string getDid();//获取岗位名称

    virtual string getDduty();//获取岗位职责
};
 

manager.h:

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


class Manager :public Worker
{
public:
    Manager(int id, string name, int did);

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

    virtual string getDid();//获取岗位名称

    virtual string getDduty();//获取岗位职责
};
 

Worker.h:

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

class Worker
{
public:
    virtual void showInfo() = 0;//显示个人信息
    virtual string getDid() = 0;//获取岗位名称
    virtual string getDduty() = 0;//获取岗位职责
    int m_id;//员工编号
    string m_name;//员工姓名
    int m_Did;//部门编号
};
 

workerManager.h:

#include "boss.h"
using namespace std;
#define FILENAME "empFile.txt"


class WorkerManager
{
public:
    WorkerManager();

    void menu();
    void ExitSystem();
    int m_EmpNum;
    Worker **EmpArray;
    void Add_emp();
    void save();
    bool m_FileisEmpty;
    int get_EmpNum();
    void init_Emp();

    void show_emp();

    int isExit(string name);

    void delete_emp();
    void change_emp();
    void find_emp();
    void sort_emp();
    void clear_emp();
    ~WorkerManager();
};
 

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;
}
void Boss::showInfo()//显示个人信息
{
    cout << "老板编号: " << this->m_id << " \t老板姓名: " << this->m_name << " \t岗位: " << this->getDid() << " \t岗位职责: " << this->getDduty() << endl;
}
string Boss::getDid()//获取岗位名称
{
    return "老板";
}
string Boss::getDduty()//获取岗位职责
{
    return "处理公司事务,并下发任务给经理";

}

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;
}
void Employee::showInfo()//显示个人信息
{
    cout << "员工编号: " << this->m_id << " \t职工姓名: " << this->m_name << " \t岗位: " << this->getDid() << " \t岗位职责: " << this->getDduty() << endl;
}
string Employee::getDid()//获取岗位名称
{
    return "员工";
}
string Employee::getDduty()//获取岗位职责
{
    return "处理经理下发的任务";
}

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;
}
void Manager::showInfo()//显示个人信息
{
    cout << "经理编号: " << this->m_id << " \t经理姓名: " << this->m_name << " \t岗位: " << this->getDid() << " \t岗位职责: " << this->getDduty() << endl;
}
string Manager::getDid()//获取岗位名称
{
    return "经理";
}
string Manager::getDduty()//获取岗位职责
{
    return "处理老板下发的任务,并下发任务给普通员工";
}

workerManager.cpp

#include "workerManager.h"


WorkerManager::WorkerManager()
{
    //文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    if (!ifs.is_open())
    {
        //cout << "文件不存在" << endl;
        this->m_EmpNum = 0;
        this->EmpArray = NULL;
        this->m_FileisEmpty = true;
        ifs.close();
        return;
    }
    //文件存在,数据为空
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        //cout << "文件为空" << endl;
        this->m_EmpNum = 0;
        this->EmpArray = NULL;
        this->m_FileisEmpty = true;
        ifs.close();
        return;
    }
    //文件存在,记录数据
    int num = this->get_EmpNum();
    //cout << "职工人数为: " << num << endl;
    this->m_EmpNum = num;
    this->EmpArray = new Worker*[this->m_EmpNum];
    this->init_Emp();
    /*for (int i = 0; i < this->m_EmpNum; i++)
    {
        cout << "\t职工编号:" << this->EmpArray[i]->m_id << "\t职工姓名:" << this->EmpArray[i]->m_name << "\t职工岗位编号:" << this->EmpArray[i]->m_Did << endl;
    }
    this->m_FileisEmpty = false;*/
}
void WorkerManager::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->EmpArray[i]->m_id << " " << this->EmpArray[i]->m_name << " " << this->EmpArray[i]->m_Did << endl;
    }
    ofs.close();
}
void WorkerManager::Add_emp()
{
    int num = 0;
    cout << "请输入要增加的员工人数: " ;
    cin >> num;
    if (num > 0)
    {
        int newSize = num + this->m_EmpNum;//新空间大小
        Worker ** newSpace = new Worker*[newSize];
        if (this->m_EmpNum != NULL)
        {
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                newSpace[i] = this->EmpArray[i];
            }
        }
        for (int i = 0; i < num; i++)
        {
            int id;
            string name;
            int did;
            cout << "请输入第" << i + 1 << "个员工的编号:";
            cin >> id;
            cout << "请输入第" << i + 1 << "个员工的姓名:";
            cin >> name;
            cout << "请选择该员工的岗位:" << endl;
            cout << "1.普通职工" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;
            cin >> did;

            Worker *worker = NULL;
            switch (did)
            {
            case 1: worker = new Employee(id, name, did);
                break;
            case 2: worker = new Manager(id, name, did);
                break;
            case 3:worker = new Boss(id, name, did);
                break;
            default:cout << "输入错误,请重新输入" << endl;
                cin >> did;
                break;
            }
            newSpace[i + this->m_EmpNum] = worker;//将创建好的员工保存到数组中
        }
        delete[]this->EmpArray;//释放原有空间
        this->EmpArray = newSpace;//更改新空间指向
        this->m_EmpNum = newSize;//更新职工人数
        this->m_FileisEmpty = false;
        cout << "添加成功" << endl;
        this->save();
    }
    else
    {
        cout << "输入有误" << endl;
    }
    system("pause");
    system("cls");
}
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 *wk = NULL;
        if (did == 1)
        {
            wk = new Employee(id, name, did);
        }
        else if (did == 2)
        {
            wk = new Manager(id, name, did);
        }
        else if (did == 3)
        {
            wk = new Boss(id, name, did);
        }
        this->EmpArray[index] = wk;
        index++;
    }
    ifs.close();
}

void WorkerManager::show_emp()
{
    if (this->m_FileisEmpty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    for (int i = 0; i < this->m_EmpNum; i++)
    {
        cout << "\t职工编号: " << this->EmpArray[i]->m_id << "\t职工姓名: " << this->EmpArray[i]->m_name << "\t职工岗位: " 
            << this->EmpArray[i]->getDid() << "\t职工职责: " << this->EmpArray[i]->getDduty() << endl;
    }
}
int WorkerManager::isExit(string name)
{
    int index = -1;
    for (int i = 0; i < this->m_EmpNum; i++)
    {
        if (this->EmpArray[i]->m_name == name)
        {
            index = i;
            break;
        }
    }
    return index;
}
void WorkerManager::delete_emp()
{
    if (this->m_FileisEmpty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout << "输入要删除的员工姓名:";
        string d_name;
        cin >> d_name;
        int d_num = WorkerManager::isExit(d_name);
        if (d_num == -1)
        {
            cout << "该员工不存在" << endl;
        }
        else
        {
            for (int i = d_num; i < this->m_EmpNum; i++)
            {
                this->EmpArray[i] = this->EmpArray[i + 1];
            }
            this->m_EmpNum--;
            cout << "删除成功" << endl;
        }
    }
    system("pause");
    system("cls");
}
void WorkerManager::change_emp()
{
    if (this->m_FileisEmpty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout << "输入要修改的员工姓名:" << endl;
        string c_name;
        cin >> c_name;
        int c_num = WorkerManager::isExit(c_name);
        if (c_num == -1)
        {
            cout << "该员工不存在" << endl;
        }
        else
        {
            delete this->EmpArray[c_num];
            cout << "该员工存在,输入修改后的员工编号: ";
            int c_id;
            cin >> c_id;
            cout << "输入修改后的员工姓名: ";
            string c_name;
            cin >> c_name;
            cout << "输入修改后的员工岗位编号:" << endl;
            cout << "1.普通职工" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;
            int c_did;
            cin >> c_did;
            Worker * wk = NULL;
            switch (c_did)
            {
            case 1:wk = new Employee(c_id, c_name, c_did);
                break;
            case 2:wk = new Manager(c_id, c_name, c_did);
                break;
            case 3:wk = new Boss(c_id, c_name, c_did);
                break;
            default:break;
            }
            this->EmpArray[c_num] = wk;
            cout << "修改成功" << endl;
            this->save();
        }
    }
    system("pause");
    system("cls");
}
void WorkerManager::find_emp()
{
    if (this->m_FileisEmpty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        string f_name;
        cout << "请输入要查找的职工姓名: ";
        cin >> f_name;
        int f_num = WorkerManager::isExit(f_name);
        if (f_num == -1)
        {
            cout << "该职工不存在" << endl;
        }
        else
        {
            cout <<"\t职工编号: "<< this->EmpArray[f_num]->m_id << "\t职工姓名: " << this->EmpArray[f_num]->m_name << "\t职工岗位: " << this->EmpArray[f_num]->getDid()
                << "\t职工职责: " << this->EmpArray[f_num]->getDduty() << endl;
            cout << "已查找到该职工" << endl;
        }
    }
}
void WorkerManager::sort_emp()
{
    if (this->m_FileisEmpty)
    {
        cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout << "请输入排序方式:" << endl;
        cout << "1.按职工编号升序排列" << endl;
        cout << "2.按职工编号降序排列" << endl;
        int select = 0;
        cin >> select;
        if (select == 1)
        {
            //cout << "升序排列后的职工顺序如下: " << endl;
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                for (int j = i+1; j < this->m_EmpNum; j++)
                {
                    if (this->EmpArray[i]->m_id > this->EmpArray[j]->m_id)
                    {
                        Worker * temp = NULL;
                        temp = this->EmpArray[i];
                        this->EmpArray[i] = this->EmpArray[j];
                        this->EmpArray[j] = temp;
                    }
                }
                
            }
            cout << "排序完成" << endl;
            this->save();
        }
        else if (select == 2)
        {
            //cout << "降序排列后的职工顺序如下: " << endl;
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                for (int j = i + 1; j < this->m_EmpNum; j++)
                {
                    if (this->EmpArray[i]->m_id < this->EmpArray[j]->m_id)
                    {
                        Worker * temp = NULL;
                        temp = this->EmpArray[i];
                        this->EmpArray[i] = this->EmpArray[j];
                        this->EmpArray[j] = temp;
                    }
                }
            }
            cout << "排序完成" << endl;
            this->save();
        }
        else
        {
            cout << "输入错误,请重新输入" << endl;
            cin >> select;
        }
    }
}
void WorkerManager::clear_emp()
{
    cout << "确定清空系统中所有数据吗?" << endl;
    cout << "yes or no" << endl;
    string choose;
    cin >> choose;
    if (choose == "yes")
    {
        ofstream ofs(FILENAME, ios::trunc);
        ofs.close();
        if (this->EmpArray != NULL)
        {
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                if (this->EmpArray[i] != NULL)
                {
                    delete this->EmpArray[i];
                }
            }
        }
        this->m_EmpNum = 0;
        delete[] this->EmpArray;
        this->EmpArray = NULL;
        this->m_FileisEmpty = true;

        cout << "已成功清空数据" << endl;
    }
    else if (choose == "no")
    {
        cout << "未清空数据" << endl;
    }
}
WorkerManager::~WorkerManager()
{
    if (this->EmpArray != NULL)
    {
        delete[]this->EmpArray;
        this->EmpArray = NULL;
    }
}

main.cpp:

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


int main()
{
    WorkerManager wm;
    int choice = 0;
    while (true)
    {
        wm.menu();
        cout << "请输入您的选择: ";
        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.change_emp();
            break;//修改
        case 5:
            wm.find_emp();
            break;//查找
        case 6:
            wm.sort_emp();
            break;//排序
        case 7:
            wm.clear_emp();
            break;//清空
        default:
            system("cls");
            break;
        }
            

    }
    wm.menu();
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值