公司员工管理系统(C++多态实现)

公司员工管理系统(C++多态实现)

1、某小型公司,主要有四类人员:经理、技术人员、销售经理和推销员。 现在,需要存储这些人员的姓名、编号、级别、当月薪水.计算月薪总额并显示全部信息。 人员编号基数为 1000,每输入一个人员信息编号顺序加 1。 程序要有对所有人员提升级别的功能。 本例中为简单起见, 所有人员的初始级别均为 1级。 然后进行升级,经理升为 4 级,技术人员和销售经理升为 3 级,推销员仍为 1 级。 月薪计算办法是: 经理拿固定月薪 8000 元; 技术人员按每小时 100 元领取月薪; 推销员的月薪按该推销员当月销售额的 4%提成; 销售经理既拿固定月薪也领取销售提成,固定月薪为 5000 元, 销售提成为所管辖部门当月销售总额的 5%。

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class worker //基类:员工
{ 
public:
    worker() { }
    worker(string name) : m_name(name)
    { 
        m_id++; //每进入一名员工,编号加1
        m_ID = m_id;
        m_rank = 1; //所有人初始级别为1
        m_post = "未分配岗位";
        m_salary = 0;
    }

    void print()
    { 
        cout << "\033[0;36m姓名:" << m_name << endl;
        cout << "\033[0;36m编号:" << m_ID << endl;
        cout << "\033[0;36m级别:" << m_rank << endl;
        cout << "\033[0;36m职位:" << m_post << endl;
        cout << "\033[0;36m当月薪水:" << m_salary << endl;
        cout << endl;
    }
    worker *next = NULL;

public:
    string m_name;   //姓名
    int m_ID;        //员工编号
    static int m_id; //系统分配编号
    string m_post;   //职位
    int m_rank;      //级别
protected:
    int m_salary; //当月薪水
};
int worker::m_id = 999;

class Linklist
{ 
public:
    Linklist();                     //创建一个单链表
    ~Linklist();                    //销毁单链表
    void insert_tail(string name);  //尾插
    void update(worker *data);      //更新节点信息
    void print();                   //打印链表内容
    int delete_name(string name);   //按姓名删除
    void DeleteAll();               //删除链表所有数据
    int find_ID(int ID);            //按编号查找
    worker *find_name(string name); //按姓名查找
private:
    worker *head;
};

Linklist::Linklist() //初始化单链表
{ 
    head = new worker("");
    head->next = NULL;
}

Linklist::~Linklist() //销毁单链表
{ 
    delete head; //删除头结点
}

void Linklist::print() //打印链表内容
{ 
    if (head == NULL)
    { 
        cout << "链表为空" << endl;
    }
    worker *p = head;
    while (p->next != NULL)
    { 
        p = p->next;
        p->print();
    }
}

void Linklist::insert_tail(string name) //尾插
{ 
    worker *p = head;
    worker *new_worker = new worker(name);
    new_worker->next = NULL;
    if (p == NULL)
    { 
        p = new_worker;
    }
    else
    { 
        while (p->next != NULL)
        { 
            p = p->next;
        }
        p->next = new_worker;
    }
}

void Linklist::update(worker *data) //更新节点信息
{ 
    worker *p = head;
    worker *ptr;
    while (p->next != NULL)
    { 
        if (p->next->m_name == data->m_name)
        { 
            ptr = p->next;
            if (p->next->next == NULL)
            { 

                p->next = data;
            }
            else
            { 
                data->next = p->next->next;
                p->next = data;
            }
            delete ptr;
            break;
        }
        else
        { 
            p = p->next;
        }
    }
}

void Linklist::DeleteAll() //删除所有数据
{ 
    worker *p = head->next;
    worker *ptemp = new worker;
    while (p != NULL) //在头结点的下一个节点逐个删除节点
    { 
        ptemp = p;
        p = p->next;
        head->next = p;
        ptemp->next = NULL;
        delete ptemp;
    }
    head->next = NULL; //头结点的下一个节点指向NULL
}

int Linklist::find_ID(int ID) //按编号查找
{ 
    worker *p = head;
    if (ID > worker::m_id || ID < 1000)
    { 
        cout << "\033[0;31m该编号不存在\033[0m" << endl;
        return -1;
    }
    while (p->next != NULL)
    { 
        if (p->next->m_ID == ID)
        { 
            p->next->print();
            return 0;
        }
        else
        { 
            p = p->next;
        }
    }
}

worker *Linklist::find_name(string name) //按姓名查找
{ 
    worker *p = head;
    while (p->next != NULL)
    { 
        if (p->next->m_name == name)
        { 
            p->next->print();
            return p->next;
        }
        else
        { 
            p = p->next;
        }
    }
    cout << "\033[0;31m该员工不存在!\033[0m" << endl;
}

int Linklist::delete_name(string name) //按值删除
{ 
    worker *p = head;
    int count = 0;
    while (p->next != NULL)
    { 
        if (p->next->m_name == name)
        { 
            worker *ptr = p->next;
            p->next = ptr->next;
            free(ptr);
            count++;
        }
        else
        { 
            p = p->next;
        }
    }
    if (count == 0)
    { 
        cout << "\033[0;31m未发现该名员工!\033[0m" << endl;
        return -1;
    }
}

class manager : public worker //总经理
{ 
public:
    manager(string name) : worker(name)
    { 
        m_rank = 4;
        m_salary = 8000;
        m_ID = m_id;
        m_post = "总经理";
        count++;
    }
    static int count;
};
int manager::count = 0;

class technicist : public worker //技术人员
{ 
public:
    technicist(string name, int t) : worker(name), m_t(t)
    { 
        m_rank = 3;
        m_salary = m_t * 100;
        m_post = "技术人员";
        m_ID = m_id;
    }

protected:
    int m_t;
};

class sale_man : public worker //销售经理
{ 
public:
    sale_man(string name, int total) : worker(name), m_total(total)
    { 
        m_rank = 3;
        m_salary = m_total * 0.05 + 5000;
        m_post = "销售经理";
        m_ID = m_id;
    }

protected:
    int m_total; //所管辖部门本月总销售额
};

class sale : public worker //推销员
{ 
public:
    sale(string name, int saleroom) : worker(name), m_saleroom(saleroom)
    { 
        m_rank = 1;
        m_salary = m_saleroom * 0.04;
        m_post = "推销员";
        m_ID = m_id;
    }

protected:
    int m_saleroom; //该推销员本月的销售额度
};

class WorkerManager //管理类
{ 
public:
    WorkerManager() { }
    ~WorkerManager() { }

    void Show_Menu(); //显示菜单
    void add();       //添加入职员工
    void find();      //查找员工信息
    void show();      //显示全部员工信息
    void Delete();    //删除离职员工信息
    void change();    //更改员工级别
    void Exit();      //退出管理系统
private:
    Linklist l; //创建链表类对象
};

void WorkerManager::Show_Menu() //显示菜单
{ 
    system("clear");
    cout << "\033[0;34m+==============================================================+\033[0m" << endl;
    cout << "\033[0;34m| \033[0;35m 欢迎进入职工管理系统 \033[0;34m |\033[0m" << endl;
    cout << "\033[0;34m| |\033[0m" << endl;
    cout << "\033[0;34m|--------------------------------------------------------------|\033[0m" << endl;
    cout << "\033[0;34m| \033[0;36m 1.添加入职员工 2.查找员工信息 \033[0;34m |\033[0m" << endl;
    cout << "\033[0;34m| \033[0;36m 3.显示全部员工信息 4.删除离职员工信息 \033[0;34m |\033[0m" << endl;
    cout << "\033[0;34m| \033[0;36m 5.变更人员职位 6.退出管理系统 \033[0;34m |\033[0m" << endl;
    cout << "\033[0;34m+==============================================================+\033[0m" << endl;
    cout << endl;
}

void WorkerManager::add() //添加入职员工
{ 
    string name;
    cout << "\033[0;34m请输入新入职员工姓名:\033[0m";
    cin >> name;
    l.insert_tail(name);
}

void WorkerManager::show() //显示全部员工信息
{ 
    l.print();
}

void WorkerManager::find() //查找员工信息
{ 
    int ID;
    string name;
    int flag = 0;
    cout << "\033[0;34m请选择:1.按编号查找 2.按姓名查找\033[0m" << endl;
    cin >> flag;
    if (flag == 1)
    { 
        cout << "\033[0;34m请输入员工编号:\033[0m" << endl;
        cin >> ID;
        l.find_ID(ID);
    }
    else if (flag == 2)
    { 
        cout << "\033[0;34m请输入员工姓名:\033[0m" << endl;
        cin >> name;
        l.find_name(name);
    }
    else
    { 
        cout << "\033[0;31m输入有误,请重新输入!\033[0m" << endl;
        find();
    }
}

void WorkerManager::change() //变更人员职位
{ 
    string name;
    cout << "\033[0;34m请输入员工姓名:\033[0m" << endl;
    ;
    cin >> name;
    cout << "\033[0;34m该员工当前信息如下:\033[0m" << endl;
    worker *p = l.find_name(name);
    name = p->m_name;
    int ID = p->m_ID;

    int count = 0;
    cout << "\033[0;34m请选择变更职位:1.推销员 2.销售经理 3.技术人员 4.总经理\033[0m" << endl;
    cin >> count;
    switch (count)
    { 
    case 1:
        if (p->m_rank == 4) //如果是总经理降级
        { 
            manager::count--;
        }
        int saleroom;
        cout << "\033[0;34m请输入其当月销售额:\033[0m" << endl;
        cin >> saleroom;
        p = new sale(name, saleroom); //存放该员工新的信息
        p->m_ID = ID;
        worker::m_id--;
        l.update(p); //替换该员工的个人信息
        cout << "\033[0;35m职位变更成功!\033[0m" << '\n'
             << endl;
        break;
    case 2:
        if (p->m_rank == 4)
        { 
            manager::count--;
        }
        int total;
        cout << "\033[0;34m请输入其所管辖部门本月总销售额:\033[0m" << endl;
        cin >> total;
        p = new sale_man(name, total);
        p->m_ID = ID;
        worker::m_id--;
        l.update(p);
        cout << "\033[0;35m职位变更成功!\033[0m" << '\n'
             << endl;
        break;
    case 3:
        if (p->m_post == "推销员" || p->m_post == "销售经理")
        { 
            cout << "\033[0;31m推销员或销售经理不能转为技术人员!\033[0m" << endl;
            break;
        }
        if (p->m_rank == 4)
        { 
            manager::count--;
        }
        int h;
        cout << "\033[0;34m请输入其本月工作时间:\033[0m" << endl;
        cin >> h;
        p = new technicist(name, h);
        p->m_ID = ID;
        worker::m_id--;
        l.update(p);
        cout << "\033[0;35m职位变更成功!\033[0m" << '\n'
             << endl;
        break;
    case 4:
        if (manager::count == 0)
        { 
            p = new manager(name);
            p->m_ID = ID;
            worker::m_id--;
            l.update(p);
            cout << "\033[0;35m职位变更成功!\033[0m" << '\n'
                 << endl;
        }
        else
        { 
            cout << "\033[0;31m已有总经理,总经理只能有一名!\033[0m" << '\n'
                 << endl;
        }
        break;
    default:
        cout << "\033[0;31m输入有误,请重新输入!\033[0m" << endl;
        change();
        break;
    }
}

void WorkerManager::Delete() //删除离职员工信息
{ 
    string name;
    cout << "\033[0;34m请输入离职员工姓名:\033[0m" << endl;
    cin >> name;
    l.delete_name(name);
}

void WorkerManager::Exit() //退出管理系统
{ 
    l.DeleteAll();
    cout << "\033[0;34m欢迎下次使用!\033[0m" << endl;
    exit(0);
}

int main() //测试函数
{ 
    WorkerManager wm; //创建管理类对象
    wm.Show_Menu();   //菜单显示
    int choice = 0;   //功能选择位
    while (true)
    { 

        cout << "\033[0;34m请输入功能选择项:\033[0m" << endl;
        cin >> choice;
        wm.Show_Menu();
        switch (choice)
        { 
        case 1: //添加入职员工
            wm.add();
            cout << "\033[0;35m人员添加成功!\033[0m" << '\n'
                 << endl;
            break;
        case 2: //查找员工信息
            wm.find();
            break;
        case 3: //显示全部员工信息
            wm.show();
            break;
        case 4: //删除离职员工信息
            wm.Delete();
            cout << "\033[0;35m删除成功!\033[0m" << '\n'
                 << endl;
            break;
        case 5: //更改员工级别
            wm.change();
            break;
        case 6: //退出管理系统
            wm.Exit();
            break;
        default:
            break;
        }
        //system("pause");
    }

    return 0;
}
    原文作者:@小高同学
    原文地址: https://blog.csdn.net/G1842965496/article/details/119042154
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞