C++面向对象速览(四):职工管理系统实现

78 阅读6分钟

实现职工管理系统

EMS.h(EmployerManagerSystem)

#pragma once
#include<iostream>
using namespace std;
#include "worker.h"
#include<fstream>#define FILENAME "Worker.txt"class EmployerManager {
public:
    bool m_fileIsEmpty;
​
    int m_workerNum;
​
    Worker** m_workerArray;
​
    EmployerManager();
​
    void add_worker();
​
    void init_workerArray();
​
    int get_workerNum();
​
    void show_worker();
​
    void delete_worker();
​
    void modify_worker();
​
    void find_worker();
​
    int isExist(int id);
​
    void showMenu();
​
    void sort_worker();
​
    void clean_file();
​
    void exitSystem();
​
    void save();
​
    ~EmployerManager() ;
};

worker.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
​
class Worker {
public:
    virtual void showInfo() = 0;
​
    virtual string getDeName() = 0;
​
    int m_Id; //职工编号
    string m_Name;
    int m_DeId; //部门编号
    /*string m_DeName;*/
​
};

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 getDeName();
};

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 getDeName();
​
};

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 getDeName();
​
};

EMS.cpp

#include "EMS.h"
#include<fstream>
#include "employee.h"
#include"manager.h"
#include"boss.h"
​
int getValidInt(const string& prompt) {
    int value;
    while (true) {
        cout << prompt << endl;
        cin >> value;
​
        if (cin.fail()) { // 检测输入是否失败
            cin.clear(); // 清除错误标志
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 丢弃错误输入
            cout << "Invalid input! Please enter a number." << endl;
        }
        else {
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除缓冲区
            return value;
        }
    }
}
​
string getValidString(const string& prompt) {
    string value;
    cout << prompt << endl;
    cin >> value;
    return value;
}
​
​
EmployerManager::EmployerManager() {
​
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
​
    if (!ifs.is_open()) {
        cout << "FILE is not Exist!" << endl;
        this->m_workerNum = 0;
        this->m_fileIsEmpty = true;
        this->m_workerArray = NULL;
        ifs.close();
        return;
    }
​
    char ch;
    ifs >> ch;
    if (ifs.eof()) {
        cout << "FILE is empty!" << endl;
        this->m_workerNum = 0;
        this->m_fileIsEmpty = true;
        this->m_workerArray = NULL;
        ifs.close();
        return;
    }
​
    int num = this->get_workerNum();
    cout << "Worker Count: " << num << endl;
​
    this->m_workerNum = num;
​
​
    this->m_workerArray = new Worker * [this->m_workerNum];
​
    init_workerArray();
​
    for (int i = 0; i < m_workerNum; i++) {
        cout << "Worker serial number : " << this->m_workerArray[i]->m_Id
            << "\tWorker name : " << this->m_workerArray[i]->m_Name
            << "\tWorker Department ID : " << this->m_workerArray[i]->m_DeId << endl;
    }
}
​
void EmployerManager::showMenu() {
    cout << R"(
职工管理系统已启动!
0,退出程序
1,增加职工
2,显示职工
3,删除职工
4,修改职工
5,查找职工
6,职工排序
7,清空数据
            )" << endl;
}
​
void EmployerManager::add_worker() {
    cout << "Put in the number of worker you want add" << endl;
​
    int addNum = 0;
    cin >> addNum;
​
    if (addNum > 0) {
        int newSize = this->m_workerNum + addNum;
​
        Worker** newSpace = new Worker * [newSize];
​
        if (this->m_workerArray != NULL) {
            for (int i = 0; i < this->m_workerNum; i++) {
                newSpace[i] = this->m_workerArray[i];
            }
        }
​
​
        for (int i = 0; i < addNum; i++) {
​
            int id = getValidInt("Please put in the " + to_string(i + 1) + " worker serial number");
​
            string name = getValidString("Please put in the " + to_string(i + 1) + " worker name");
​
            int dId;
​
            /*cout << "Please put in the " << i + 1 << " worker serial number" << endl;
            cin >> id;*/
​
            /*cout << "Please put in the " << i + 1 << " worker name" << endl;
            cin >> name;*/
​
            cout << R"(Please choose the worker's job
1,Employee
2,Manager
3,Boss)" << 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 << "Invalid choice! Please enter 1, 2, or 3." << endl;
                break;
            }
            //i
            newSpace[this->m_workerNum + i] = worker;
​
            cout << "Worker " << id << " (" << name << ") added successfully with job ID: " << dId << endl;
        }
​
        delete[] this->m_workerArray;
​
        this->m_workerArray = newSpace;
​
        this->m_workerNum = newSize;
​
        //Update File is not empty
        this->m_fileIsEmpty = false;
​
        cout << "Add Succeed " << addNum << " new worker" << endl;
​
        this->save();
    }
    else {
​
        cout << "Error put in" << endl;
    }
​
​
    //清屏,回到上级目录
    system("pause");
    system("cls");
}
​
void EmployerManager::init_workerArray() {
    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;
        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_workerArray[index] = worker;
        index++;
    }
​
}
​
int EmployerManager::get_workerNum() {
    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 EmployerManager::save() {
    ofstream ofs;
    ofs.open(FILENAME, ios::out);
​
    for (int i = 0; i < this->m_workerNum; i++) {
​
        ofs << this->m_workerArray[i]->m_Id <<" "
            << this->m_workerArray[i]->m_Name <<" "
            << this->m_workerArray[i]->m_DeId << endl;
    }
​
    ofs.close();
​
}
​
void EmployerManager::show_worker() {
    if (this->m_fileIsEmpty) {
        cout << "File is not exist or is empty !" << endl;
    }
​
    else {
        for (int i = 0; i < m_workerNum; i++) {
            this->m_workerArray[i]->showInfo();
        }
    }
​
    system("pause");
    system("cls");
}
​
int EmployerManager::isExist(int id) {
    int index = -1;
​
    for (int i = 0; i < this->m_workerNum; i++) {
        if (this->m_workerArray[i]->m_Id == id) {
            index = i;
​
            break;
        }
    }
​
    return index;
}
​
void EmployerManager::delete_worker() {
​
    if (this->m_fileIsEmpty) {
        cout << "File is not exist or is empty" << endl;
    }
    else {
        cout << "Put in the ID you want to delete" << endl;
        int id = 0;
        cin >> id;
​
        int index = this->isExist(id);
​
        if (index != -1) {
            for (int i = index; i < this->m_workerNum - 1; i++) {
                this->m_workerArray[i] = this->m_workerArray[i + 1];
            }
            this->m_workerNum--;
​
            this->save();
            cout << "Delete Succeed" << endl;
        }
        else {
            cout << "Delete failed , can't find the id" << endl;
        }
    }
​
    system("pause");
    system("cls");
}
​
void EmployerManager::exitSystem() {
    cout << "Welcome use next time" << endl;
    system("pause");
    exit(0);
}
​
void EmployerManager::modify_worker() {
    if (this->m_fileIsEmpty) {
        cout << "File is not exist or is empty" << endl;
    }
    else {
        cout << "Put in the ID you want to modify" << endl;
        int id;
        cin >> id;
​
        int ret = this->isExist(id);
        if (ret != -1) {
            delete this->m_workerArray[ret];
​
            int newId = 0;
            string newName = "";
            int newdId = 0;
​
            cout << "Find id: " << id << " worker,Please put in newId" << endl;
            cin >> newId;
​
            cout << "Please put in newName" << endl;
            cin >> newName;
​
            cout << R"(Please choose the worker's job
1,Employee
2,Manager
3,Boss)" << endl;
​
            Worker* worker = NULL;
​
            cin >> newdId;
            switch (newdId) {
            case 1:
                worker = new Employee(newId, newName, newdId);
                break;
            case 2:
                worker = new Manager(newId, newName, newdId);
                break;
            case 3:
                worker = new Boss(newId, newName, newdId);
                break;
            default:
                cout << "Invalid choice! Please enter 1, 2, or 3." << endl;
                break;
            }
​
            this->m_workerArray[ret] = worker;
            cout << "Modify succeed " << this->m_workerArray[ret]->m_DeId << endl;
​
            this->save();
        }
        else {
            cout << "Modify Failed , There is no such man" << endl;
​
        }
    }
    system("pause");
    system("cls");
}
​
void EmployerManager::find_worker() {
    if (this->m_fileIsEmpty) {
        cout << "File is not exist or is empty" << endl;
    }
    else {
        cout << "Please put in the way to look up" << endl;
        cout << R"(1,Look up from id
2,Look up from name)" << endl;
​
        int select;
        cin >> select;
​
        if (select == 1) {
            int id;
            cout << "Please put in the worker id want find" << endl;
            cin >> id;
​
            int ret = isExist(id);
            if (ret != -1) {
                cout << "Find succeed! The worker info :" << endl;
                this->m_workerArray[ret]->showInfo();
            }
            else {
                cout << "Find failed! No man is this id ." << endl;
            }
        }
        else if (select == 2) {
            string name;
            cout << "Please put in the worker name want find" << endl;
            cin >> name;
​
            bool flag = false;
            for (int i = 0; i < m_workerNum; i++) {
                if (m_workerArray[i]->m_Name == name) {
                    cout << "Find succeed, worker id is: "
                        << m_workerArray[i]->m_Id
                        << "The detail infomation is : " << endl;flag == true;
​
                    this->m_workerArray[i]->showInfo();
                }
            }
            if (flag == false) {
                cout << "Find failed! No man is this name ." << endl;
            }
        }
        else {
            cout << "Put in error!" << endl;
        }
    }
​
    system("pause");
    system("cls");
​
}
​
void EmployerManager::sort_worker() {
    if (this->m_fileIsEmpty) {
        cout << "File is not exist or is empty" << endl;
​
        system("pause");
        system("cls");
    }
    else {
        cout << "Please choose sort way :" << endl;
        cout << R"(1,id ascending order
2,id descending order
)" << endl;
        
        int select = 0;
        cin >> select;
​
        for (int i = 0; i < m_workerNum; i++) {
            int minOrMaxId = i;
            for (int j = i + 1; j < this->m_workerNum; j++) {
                if (select == 1) {
                    if (m_workerArray[minOrMaxId]->m_Id > m_workerArray[j]->m_Id) {
                        minOrMaxId = j;
                    }
                }
​
                else {
                    if (m_workerArray[minOrMaxId]->m_Id < m_workerArray[j]->m_Id) {
                        minOrMaxId = j;
                    }
                }
            }
​
            if (i != minOrMaxId) {
                    Worker* temp = m_workerArray[i];
                    m_workerArray[i] = m_workerArray[minOrMaxId];
                    m_workerArray[minOrMaxId] = temp;
                }
            }
​
        cout << "Sort succeed, sorted result is :" << endl;
        this->save();
        this->show_worker();
    }
}
​
void EmployerManager::clean_file() {
    cout << "Confirm clear? " << endl;
    cout << "1,Yes" << endl;
    cout << "2,No" << endl;
​
    int select = 0;
    cin >> select;
​
    if (select == 1) {
        ofstream ofs(FILENAME, ios::trunc);
        ofs.close();
​
        if (this->m_workerArray != NULL) {
            for (int i = 0; i < this->m_workerNum; i++) {
                if (this->m_workerArray[i] != NULL) {
                    delete this->m_workerArray[i];
                }
            }
            this->m_workerNum = 0;
            delete[] this->m_workerArray;
            this->m_workerArray = NULL;
            this->m_fileIsEmpty = true;
        }
        cout << "Clean succeed!" << endl;
    }
​
    system("pause");
    system("cls");
}
​
EmployerManager::~EmployerManager() {
​
    if (this->m_workerArray != NULL) {
        delete[] this->m_workerArray;
        this->m_workerArray = NULL;
    }
​
}

employee.cpp

#include "employee.h"
#include<string>
​
Employee::Employee(int id, string name, int dId) {
    this->m_Id = id;
    this->m_Name = name;
    this->m_DeId = dId;
}
​
void Employee::showInfo() {
    cout << "职工编号: " << this->m_Id
        << "\t职工姓名: " << this->m_Name
        << "\t岗位:  "<<this->getDeName()
        << "\t岗位职责: 完成经理交给的任务" << endl;
}
​
string Employee::getDeName() {
    return "员工";
}

Manager.cpp

#include "manager.h"
​
Manager::Manager(int id, string name, int dId) {
    this->m_Id = id;
    this->m_Name = name;
    this->m_DeId = dId;
}
​
void Manager::showInfo() {
    cout << "职工编号: " << this->m_Id
        << "\t职工姓名: " << this->m_Name
        << "\t岗位:  " << this->getDeName()
        << "\t岗位职责: 完成boss交给的任务,hand out undertake to employee" << endl;
​
}
​
string Manager::getDeName() {
    return "Manager";
}

Boss.cpp

#include "boss.h"
​
Boss::Boss(int id, string name, int dId) {
    this->m_Id = id;
    this->m_Name = name;
    this->m_DeId = dId;
}
​
void Boss::showInfo() {
    cout << "职工编号: " << this->m_Id
        << "\t职工姓名: " << this->m_Name
        << "\t岗位:  " << this->getDeName()
        << "\t岗位职责: Control interprize everything" << endl;
​
}
​
string Boss::getDeName() {
    return "Boss";
}

EMSmain.cpp

#include<iostream>
#include<string>#include "EMS.h"using namespace std;
​
#include "employee.h"
#include"manager.h"
#include"boss.h"void test() {
​
    //使用父类指针指向子类对象,体现多态性
    Worker* worker = NULL;
    worker = new Employee(1, "zhangsan", 1);
    worker->showInfo();
    delete worker;
​
    worker = new Manager(1, "LiSi", 1);
    worker->showInfo();
    delete worker;
​
    worker = new Boss(1, "WangWu", 1);
    worker->showInfo();
    delete worker;
​
​
    /*Employee e1(1, "张十三", 2);
    e1.showInfo();*/
}
​
int main() {
​
    /*test();*/
​
    EmployerManager em;
    int choice = 0;
    while (true) {
        em.showMenu();
        cout << "请输入你的选择操作" << endl;
​
        cin >> choice;
        switch (choice) {
        case 0:
            em.exitSystem();
            break;
        case 1:
            em.add_worker();
            break;
        case 2:
            em.show_worker();
            break;
        case 3:
            em.delete_worker();
            break;
        case 4:
            em.modify_worker();
            break;
        case 5:
            em.find_worker();
            break;
        case 6:
            em.sort_worker();
            break;
        case 7:
            em.clean_file();
            break;
        default:
            system("cls");
            break;
        }
    }
​
    
​
    system("pause");
    return 0;
}

相关代码上传至github:github.com/kair998/Emp…