前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++课程设计:员工管理系统实战

C++课程设计:员工管理系统实战

作者头像
耕耘实录
发布2022-05-09 16:14:03
6810
发布2022-05-09 16:14:03
举报
文章被收录于专栏:耕耘实录耕耘实录

一 概述

公司员工分为3种类型,老板,经理,普通员工,使用C++面向对象特性针对该3种类型员工进行抽象,实现对员工的基本管理,包括增、删、改、查等常规操作,并将数据保存到本地时间数据持久化。

二 详细设计

2.1 系统菜单

代码语言:javascript
复制
************************************
********欢迎使用职工管理系统!********
********** 0. 退出管理程序 **********
********** 1. 增加职工信息 **********
********** 2. 显示职工信息 **********
********** 3. 删除职工信息 **********
********** 4. 修改职工信息 **********
********** 5. 查找职工信息 **********
********** 6. 按照编号排序 **********
********** 7. 清空所有信息 **********
************************************

输入对应操作后执行相应操作。

2.2 抽象父类

代码语言:javascript
复制
#pragma once
#include <iostream>
#include <string>

using namespace std;

class Worker {
public:
    int mId;
    string mName;
    int mDepId;
    virtual void showInfo()=0;
    virtual string getDepName()=0;
};

2.3 老板类

代码语言:javascript
复制
#pragma once
#include<iostream>
#include<string>
#include "worker.h"

using namespace std;

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

    Boss();
    void showInfo() override;
    string getDepName();
};
代码语言:javascript
复制
#include "boss.h"

string Boss::getDepName() {
    return "经理";
}

void Boss::showInfo() {
    cout << "职工编号:" << this->mId
         << "\t职工姓名:" << this->mName
         << "\t岗位:" << this->getDepName()
         << "\t岗位职责:管理公司所有的事务。" << endl;
}

Boss::Boss(int id, string name, int dId) {
    this->mId = id;
    this->mName = name;
    this->mDepId = dId;
}

2.4 经理类

代码语言:javascript
复制
#pragma once
#include<iostream>
#include<string>
#include "worker.h"

using namespace std;

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

    Manager();
    void showInfo() override;
    string getDepName();
};
代码语言:javascript
复制
#include "manager.h"

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

void Manager::showInfo() {
    cout << "职工编号:" << this->mId
         << "\t职工姓名:" << this->mName
         << "\t岗位:" << this->getDepName()
         << "\t岗位职责:完成老板交给的任务。" << endl;
}

Manager::Manager(int id, string name, int dId) {
    this->mId = id;
    this->mName = name;
    this->mDepId = dId;
}

2.5 员工类

代码语言:javascript
复制
#pragma once
#include<iostream>
using namespace std;
#include "worker.h"
#include<string>

class Employee: public Worker{
public:
    Employee(int id, string name, int dId);
    void showInfo() override;
    string getDepName() override;
};
代码语言:javascript
复制
#include "employee.h"

void Employee::showInfo() {
    cout<<"职工编号:"<<this->mId
    <<"\t职工姓名:"<<this->mName
    <<"\t岗位:"<<this->getDepName()
    <<"\t岗位职责:完成经理交给的任务。"<<endl;
}

string Employee::getDepName() {
    return "员工";
}

Employee::Employee(int id, string name, int dId){
    this->mId = id;
    this->mName = name;
    this->mDepId = dId;
}

2.6 员工管理类

代码语言:javascript
复制
#pragma once
#include<iostream>
#include "worker.h"
using namespace std;

class WorkerManager {
public:
    WorkerManager();
    ~WorkerManager();
    void showMenu();
    void exitSystem();
    void AddEmp();
    static int getEmpNum();
    void initEmp() const;
    void save() const;
    void showEmp();
    void delEmp();
    void modEmp();
    void findEmp();
    void sortEmp();
    int isExist(int id);
    void cleanFile();
    int mEmpNum;
    Worker **empArray;
    bool mFileIsEmpty;
};
代码语言:javascript
复制
#include "workerManager.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
#include<fstream>

#define FILENAME "emp.txt"

WorkerManager::WorkerManager() {
   ifstream ifs;
   // 文件不存在
   ifs.open(FILENAME, ios::in);
   if (!ifs.is_open()) {
       //cout << "文件不存在" << endl;
       this->mEmpNum = 0;
       this->empArray = nullptr;
       this->mFileIsEmpty = true;
       ifs.close();
       return;
   }
   // 文件存在,但是数据为空
   char ch;
   ifs >> ch;
   if (ifs.eof()) {
       // cout << "文件存在,且为空" << endl;
       this->mEmpNum = 0;
       this->empArray = nullptr;
       this->mFileIsEmpty = true;
       ifs.close();
       return;
   }
   // 当文件存在,且存在记录
   int num = WorkerManager::getEmpNum();
   // cout << "职工人数为:" << num << endl;
   this->mEmpNum = num;
   this->mFileIsEmpty = false;
   this->empArray = new Worker *[this->mEmpNum];
   this->initEmp();
/*    for (int i = 0; i < this->mEmpNum; i++) {
       cout << "员工编号:" << this->empArray[i]->mId << "姓名:" << this->empArray[i]->mName << "部门:"
            << this->empArray[i]->mDepId << endl;
   }*/
}

WorkerManager::~WorkerManager() {
   if (this->empArray != nullptr) {
       delete[] this->empArray;
       this->empArray = nullptr;
   }
}

void WorkerManager::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;
   cout << "************************************" << endl;
}

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

void WorkerManager::AddEmp() {
   cout << "请输入添加员工的数量:" << endl;
   int addNum = 0;
   cin >> addNum;
   if (addNum > 0) {
       int newSize = this->mEmpNum + addNum;
       Worker **newSpace = new Worker *[newSize];
       //将原空间中的数据,拷贝到新空间下
       if (this->mEmpNum != 0) {
           for (int i = 0; i < this->mEmpNum; i++) {
               newSpace[i] = this->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 = nullptr;
           switch (dSelect) {
               case 1:
                   worker = new Employee(id, name, dSelect);
                   break;
               case 2:
                   worker = new Manager(id, name, dSelect);
                   break;
               case 3:
                   worker = new Boss(id, name, dSelect);
                   break;
               default:
                   break;
           }
           newSpace[this->mEmpNum + i] = worker;
       }
       delete[] this->empArray;
       this->empArray = newSpace;
       this->mEmpNum = newSize;
       this->mFileIsEmpty = false;
       cout << "成功添加" << addNum << "名新员工" << endl;
       this->save();
   } else {
       cout << "输入有误,请核实!" << endl;
   }
   system("clear");
}

void WorkerManager::save() const {
   ofstream ofs;
   ofs.open(FILENAME, ios::out);
   for (int i = 0; i < this->mEmpNum; i++) {
       ofs << this->empArray[i]->mId << " "
           << this->empArray[i]->mName << " "
           << this->empArray[i]->mDepId << endl;
   }
   ofs.close();
}

int WorkerManager::getEmpNum() {
   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::initEmp() const {
   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 = nullptr;
       // 普通员工
       if (dId == 1) {
           worker = new Employee(id, name, id);
       } else if (dId == 2) {
           worker = new Manager(id, name, id);
       } else if (dId == 3) {
           worker = new Boss(id, name, id);
       } else {

       }
       this->empArray[index] = worker;
       index++;
   }
   ifs.close();
}

void WorkerManager::showEmp() {
   if (this->mFileIsEmpty) {
       cout << "文件不存在或者为空!" << endl;
   } else {
       for (int i = 0; i < mEmpNum; i++) {
           this->empArray[i]->showInfo();
       }
   }
   system("clear");
}

void WorkerManager::delEmp() {
   if (this->mFileIsEmpty) {
       cout << "文件不存在或记录为空" << endl;
   } else {
       cout << "请输入要删除的职工编号:" << endl;
       int id = 0;
       cin >> id;
       int index = this->isExist(id);
       if (index != -1) {
           for (int i = index; i < this->mEmpNum - 1; i++) {
               this->empArray[i] = this->empArray[i + 1];
           }
           this->mEmpNum--;
           this->save();
           cout << "删除成功!" << endl;
       } else {
           cout << "删除失败,未找到该职工!" << endl;
       }
   }
   system("clear");
}

int WorkerManager::isExist(int id) {
   int index = -1;
   for (int i = 0; i < this->mEmpNum; i++) {
       if (this->empArray[i]->mId == id) {
           index = i;
           break;
       }
   }
   return index;
}

void WorkerManager::modEmp() {
   if (this->mFileIsEmpty) {
       cout << "文件不存在或者记录为空!" << endl;
   } else {
       cout << "请输入修改职工的编号:" << endl;
       int id;
       cin >> id;
       int ret = this->isExist(id);
       if (ret != -1) {
           delete this->empArray[ret];
           int newId = 0;
           string newName = "";
           int dSelect = 0;
           cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
           cin >> newId;
           cout << "请输入新的姓名:" << endl;
           cin >> newName;
           cout << "请输入岗位:" << endl;
           cout << "1、普通职工" << endl;
           cout << "2、经理" << endl;
           cout << "3、老板" << endl;
           cin >> dSelect;
           Worker *worker = nullptr;
           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;
           }
           this->empArray[ret] = worker;
           cout << "员工信息修改成功!" << endl;
           this->save();
       } else {
           cout << "查无此人!修改失败!" << endl;
       }
   }
   system("clear");
}

void WorkerManager::findEmp() {
   if (this->mFileIsEmpty) {
       cout << "文件不存在或者记录为空!" << endl;
   } else {
       cout << "请输入查找的方式:" << endl;
       cout << "1. 按照职工的编号查找" << endl;
       cout << "2. 按照职工的姓名查找" << endl;
       int select = 0;
       cin >> select;
       bool flag = false;
       switch (select) {
           case 1: {
               int id;
               cout << "请输入员工的ID!" << endl;
               cin >> id;
               int ret = isExist(id);
               if (ret != -1) {
                   cout << "查找成功!改职工的信息如下:" << endl;
                   this->empArray[ret]->showInfo();
               }
               flag = true;
               break;
           }
           case 2: {
               string name;
               cout << "请输入员工的姓名:" << endl;
               cin >> name;
               for (int i = 0; i < this->mEmpNum; i++) {
                   if (this->empArray[i]->mName == name) {
                       cout << "查找成功,该员工的编号为:" << this->empArray[i]->mId << ",信息如下" << endl;
                       flag = true;
                       this->empArray[i]->showInfo();
                   }
               }
               break;
           }
           default:
               cout << "查找方式不正确!" << endl;
               break;
       }
       if (!flag) {
           cout << "查找失败!" << endl;
       }
   }
}

void WorkerManager::sortEmp() {
   if (this->mFileIsEmpty) {
       cout << "文件不存在或者为空!" << endl;
       system("clear");
   } else {
       cout << "请输入排序的方式:" << endl;
       cout << "1. 按照职工的编号升序" << endl;
       cout << "2. 按照职工的编号降序" << endl;
       int select = 0;
       cin >> select;
       for (int i = 0; i < this->mEmpNum; i++) {
           int minOrMax = i;
           for (int j = i + 1; j < this->mEmpNum; j++) {
               if (select == 1) {
                   if (this->empArray[i]->mId > this->empArray[j]->mId) {
                       minOrMax = j;
                   }
               } else {
                   if (this->empArray[i]->mId < this->empArray[j]->mId) {
                       minOrMax = j;
                   }
               }
           }
           if (i != minOrMax) {
               Worker *temp = this->empArray[i];
               this->empArray[i] = this->empArray[minOrMax];
               this->empArray[minOrMax] = temp;
           }
       }
       cout << "排序成功!排序后的结果为:" << endl;
       this->save();
       this->showEmp();
   }
}

void WorkerManager::cleanFile() {
   cout << "确定清空文件?" << endl;
   cout << "Y/y. 确定" << endl;
   cout << "N/n. 返回" << endl;
   char select = 0;
   cin >> select;
   switch (select) {
       case 'Y' :
       case 'y' : {
           ofstream ofs(FILENAME, ios::trunc);
           ofs.close();
           if (this->empArray != nullptr) {
               for (int i = 0; i < this->mEmpNum; i++) {
                   delete this->empArray[i];
                   this->empArray[i] = nullptr;
               }
               delete[] this->empArray;
               this->empArray = nullptr;
               this->mEmpNum = 0;
               this->mFileIsEmpty = true;
           }
           cout << "清空成功!" << endl;
           system("clear");
           break;
       }
       case 'N' :
       case 'n' : {
           break;
       }
       default:
           cout << "输入无效!";
           break;
   }
}

2.7 主函数

代码语言:javascript
复制
#include<iostream>

using namespace std;

#include "workerManager.h"
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"

int main() {
    WorkerManager wm;
    int choice = 0;
    while (true) {
        wm.showMenu();
        cout << "请输入您的选择:" << endl;
        cin >> choice;
        switch (choice) {
            case 0:
                wm.exitSystem();
                break;
            case 1:
                wm.AddEmp();
                break;
            case 2:
                wm.showEmp();
                break;
            case 3:
                wm.delEmp();
                break;
            case 4:
                wm.modEmp();
                break;
            case 5:
                wm.findEmp();
                break;
            case 6:
                wm.sortEmp();
                break;
            case 7:
                wm.cleanFile();
                break;
            default:
                system("clear");
                break;
        }
    }
}

三 总结

3.1 本次实战主要实践了C++多态的特性; 3.2 文件操作; 3.3 指向指针的指针。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-01-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一 概述
  • 二 详细设计
    • 2.1 系统菜单
      • 2.2 抽象父类
        • 2.3 老板类
          • 2.4 经理类
            • 2.5 员工类
              • 2.6 员工管理类
                • 2.7 主函数
                • 三 总结
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档