关于系统设计:职工管理系统设计思路

54次阅读

共计 8778 个字符,预计需要花费 22 分钟才能阅读完成。

需要概述:

既然设计这个零碎,就要思考对应需要,职工管理系统,顾名思义,是为了治理职工而设计,零碎中最根本的“增删改查”天然少不了。

首先设计头文件

首先,咱们定义多个头文件 (设立头文件的目标次要是:提供全局变量、全局函数的申明或专用数据类型的定义,从而实现拆散编译和代码复用。同时,也有利于强化安全性。)

别离如下 (以下每个头文件与每种身份的人对应):

boss.h

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

class boss :public worker
{
public:
    boss(int id, string name, int post);
    virtual void showinfo();
    virtual string getpost();};

employee.h

#pragma once
class employee :public worker
{
public:
    employee(int id, string name, int post);
    virtual void showinfo();
    virtual string getpost();};

manager.h

#pragma once
class Manager :public Worker
{
public:

    Manager(int id, string name, int post);
    virtual void ShowInfo();
    virtual string getPost();};

worker manager.h

#pragma once
#include<iostream>
using namespace std;
#include"boss.h"
#include"manager.h"
#include"worker.h"
#include"employee.h"
#include<fstream>
#define TXT "empfine.txt"
class worker_manager
{
public:
    worker_manager();
    void showmenu();
    int m_EmpNum;
    worker** m_EmpArray;
    void Add_Emp();
    void save();
    bool_m_File;
    int get_EmpNum();
    void In_emp();
    void Show_emp();
    int    Ison_Emp(int id);
    void Del_Emp();
    void Mod_Emp();
    void Find_Emp();
    void Sort_Emp();
    void Clean_File();

    ~worker_manager();};

worker.h

#pragma once
class worker
{
public:
    virtual void showinfo() = 0;
    virtual string getpost() = 0;
    int m_id;
    string m_name;
    int m_post;
};

源文件如下:

boss.cpp

#include "boss.h"

Boss::Boss(int id, string name, int post)
{
    this->m_Id = id;
    this->m_Name = name;
    this->m_Post = post;

}
void Boss::ShowInFo()
{
    cout << "职工编号:" << this->m_Id
        << "\t 职工姓名:" << this->m_Name
        << "\t 职工部门:" << this->getpost()
        << "\t 岗位职责:治理公司所有事务" << endl;
}
string Boss::getPost()
{return string("老板");
}

employee.cpp

include "employee.h"
employee::employee(int id, string name, int post)
{
    this->m_Id = id;
    this->m_Name = name;
    this->m_Post = post;

}
void employee::showinfo(
    {
     cout << "职工编号:" << this->m_Id
     << "\t 职工姓名:" << this->m_Name
     << "\t 职工部门:" << this->getPost()
    << "\t 岗位职责:实现经理安排的工作" << endl;
    }
    string employee::getpost()
    {return string("员工");
    }

manager.cpp

#include "manager.h"
Manager::Manager(int id, string name, int post)
{
    this->m_Id = id;
    this->m_Name = name;
    this->m_Post = post;
}
void Manager::ShowInfo()
{
    cout << "职工编号:" << this->m_Id
        << "\t 职工姓名:" << this->m_Name
        << "\t 职工部门:" << this->getPost()
        << "\t 岗位职责:实现老板安排的工作,并下发工作给员工" << endl;

}
string Manager::getPost()
{return string("经理");
}

workmanager.h

#include "worker manager.h"
worker_manager::Worker_Manager()
{
    ifstream ifs;
    ifs.open(TXT, Ios::in);
    if (!ifs.is_open())
    {
        this->m_EmpNum = 0;
        this->m_EmpArray = NULL;
        this->m_File = true;
        ifs.close();
        return;
    }
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        this->m_EmpNum = 0;
        this->m_EmpNum = NULL;
        this->m_File = true;
        ifs.close();
        return;
    }
    int num = this->get_EmpNum();
    this->m_EmpNum = num;
    this->m_EmpArray = new ew Worker * [this->m_EmpNum];
    this->In_Emp();}
void Worker_Manager::ShowMenu()
{
    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;
}


void Worker_Manager::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 post;
            cout << "请输出第" << i + 1 << "个新职工的编号:" << endl;
            cin >> id;

            cout << "请输出第" << i + 1 << "个新职工的姓名:" << endl;
            cin >> name;

            cout << "请抉择职工岗位:" << endl;
            cout << "1、普通员工" << endl;
            cout << "2、经理" << endl;
            cout << "3、老板" << endl;
            cin >> post;

            Worker* worker = NULL;
            switch (post)
            {
            case 1:
                worker = new Employee(id, name, post);
                break;
            case 2:
                worker = new Manager(id, name, post);
                break;
            case 3:
                worker = new Boss(id, name, post);
                break;
            default:
                cout << "谬误输出" << endl;
                break;
            }
            // 将创立的职工指针,保留到数组中
            newSpace[this->m_EmpNum + i] = worker;
        }
        // 开释原指针
        delete[] this->m_EmpArray;

        // 更改新空间的指向
        this->m_EmpArray = newSpace;

        // 更新职工人数
        this->m_EmpNum = newSize;

        cout << "胜利增加" << addNum << "名新职工" << endl;

        this->m_File = false;

        // 保留数据到文件中
        this->save();}
    else
    {cout << "输出有误" << endl;}
    system("pause");
    system("cls");
}

void Worker_Manager::save()
{
    ofstream ofs;
    ofs.open(TXT, 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]->m_Post << " "
            << endl;
    }
    ofs.close();}


int Worker_Manager::get_EmpNum()
{
    ifstream ifs;
    ifs.open(TXT, ios::in);
    int id;
    string name;
    int post;

    int num = 0;
    while (ifs >> id && ifs >> name && ifs >> post)
    {num++;}
    return num;
}

void Worker_Manager::In_Emp()
{
    ifstream ifs;
    ifs.open(TXT, ios::in);

    int id;
    string name;
    int post;

    int index = 0;
    while (ifs >> id && ifs >> name && ifs >> post)
    {
        Worker* worker = NULL;
        if (post == 1)
        {worker = new Employee(id, name, post);// 普通员工
        }
        else if (post == 2)
        {worker = new Manager(id, name, post);// 经理
        }
        else
        {worker = new Boss(id, name, post);// 老板
        }
        this->m_EmpArray[index] = worker;
        index++;
    }
    ifs.close();}

void Worker_Manager::Show_Emp()
{if (this->m_File)
    {cout << "该文件不存在或为空" << endl;}
    else
    {for (int i = 0; i < this->m_EmpNum; i++)
        {
            // 利用多态调用
            this->m_EmpArray[i]->ShowInfo();}
    }
    // 清屏
    system("pause");
    system("cls");
}

int Worker_Manager::Ison_Emp(int id)
{
    int index = -1;
    for (int i = 0; i < this->m_EmpNum; i++)
    {if (this->m_EmpArray[i]->m_Id == id)
        {
            index = i;
            break;
        }
    }
    return index;
}

void Worker_Manager::Del_Emp()
{if (this->m_File)
    {cout << "该文件不存在或为空" << endl;}
    else
    {
        cout << "请输出你要删除员工的编号:" << endl;
        int id;
        cin >> id;
        int ret = this->Ison_Emp(id);
        // 职工存在,且在 index 地位上
        if (ret != -1)
        {for (int i = ret; i < this->m_EmpNum - 1; i++)
            {
                // 数据前移
                this->m_EmpArray[i] = this->m_EmpArray[i + 1];
            }
            // 更新人数
            this->m_EmpNum--;
            cout << "删除胜利" << endl;

            // 同步到文件中
            this->save();}
        else
        {cout << "删除失败" << endl;}
    }

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

void Worker_Manager::Mod_Emp()
{if (this->m_File)
    {cout << "该文件不存在或为空" << endl;}
    else
    {
        int id = 0;
        cout << "输出你要批改的员工编号:" << endl;
        cin >> id;
        int ret = this->Ison_Emp(id);
        if (ret != -1)
        {delete this->m_EmpArray[ret];
            int Newid = 0;
            string name = "";
            int post = 0;
            cout << "查到:" << id << "号员工,请输出新的员工编号:" << endl;
            cin >> Newid;

            cout << "查到:" << id << "号员工,请输出新的员工姓名:" << endl;
            cin >> name;

            cout << "请输出新岗位" << endl;
            cout << "1、普通员工" << endl;
            cout << "2、经理" << endl;
            cout << "3、老板" << endl;
            cin >> post;

            Worker* worker = NULL;
            switch (post)
            {
            case 1:
                worker = new Employee(Newid, name, post);
                break;
            case 2:
                worker = new Manager(Newid, name, post);
                break;
            case 3:
                worker = new Boss(Newid, name, post);
                break;
            default:
                break;
            }
            // 将批改的数据放回原来地位
            this->m_EmpArray[ret] = worker;
            cout << "批改胜利" << endl;

            this->save();}
        else
        {cout << "批改失败" << endl;}
    }

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


void Worker_Manager::Find_Emp()
{if (this->m_File)
    {cout << "该文件不存在或为空" << endl;}
    else
    {
        cout << "请抉择查找的形式" << endl;
        cout << "1、按编号查找" << endl;
        cout << "2、按姓名查找" << endl;
        int input = 0;
        cin >> input;
        if (input == 1)
        {
            // 按编号查找
            int id = 0;
            cout << "请输出你要查找的编号为:" << endl;
            cin >> id;
            int ret = this->Ison_Emp(id);
            if (ret != -1)
            {
                cout << "查找到此人信息  如下:" << endl;
                this->m_EmpArray[ret]->ShowInfo();}
            else
            {cout << "查无此人" << endl;}
        }
        else if (input == 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 Worker_Manager::Sort_Emp()
{if (this->m_File)
    {
        cout << "该文件不存在或为空" << endl;
        system("pause");
        system("cls");
    }
    else
    {
        cout << "请抉择排序形式" << endl;
        cout << "1、按职工号升序" << endl;
        cout << "2、按职工号降序" << endl;
        int input = 0;
        cin >> input;
        for (int i = 0; i < this->m_EmpNum; i++)
        {
            int minOrmax = i;
            for (int j = i + 1; j < this->m_EmpNum; j++)
            {if (input == 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 (minOrmax != i)
            {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 Worker_Manager::Clean_File()
{
    cout << "确定清空?" << endl;
    cout << "1、确定" << endl;
    cout << "2、勾销" << endl;

    int input = 0;
    cout << "请输出:" << endl;
    cin >> input;

    if (input == 1)
    {
        ofstream ofs;
        ofs.open(TXT, 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[i] = NULL;
            }
            // 删除堆区指针
            delete[] this->m_EmpArray;
            this->m_EmpArray = NULL;
            this->m_File = true;
            this->m_EmpNum = 0;
        }
        cout << "清空胜利" << endl;
    }
    system("pause");
    system("cls");
}


Worker_Manager::~Worker_Manager()
{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;
    }

职工治理.cpp

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

int main()
{
    // 实例化对象, 缩写一个货色,不便前面操作
    worker_manager wm;
    int input = 0;
    do
    {wm.showmenu();
        cout << "请输出你的抉择:" << endl;
        cin >> input;
        switch (input)
        {
        case 0:
            cout << "欢送下次应用" << endl;
            break;
        case 1:
            wm.Add_Emp();
            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:  // 清空文档
            wm.Clean_File();
            break;
        default:
            system("cls"); // 清屏
            break;
        }

    } while (input);

    system("pause");

    return 0;
}


正文完
 0