day12 每天回顾一下职工操作系统的案例,想想其实现是如何做到

管理系统需求

职工管理系统可以用来管理公司内所有员工的信息本教程主要利用C++来实现一个基于多态的职工管理系统
公司中职工分为三类 : 普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责

普通员工职责:完成经理交给的任务
经理职责 : 完成老板交给的任务,并下发任务给员工
老板职责 : 管理公司所有事务
管理系统中需要实现的功能如下 :
退出管理程序  :  退出当前管理系统
增加职工信息 : 实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号
显示职工信息 : 显示公司内部所有职工的信息
删除离职职工 : 按照编号删除指定的职工
修改职工信息:照编号修改职工个人信息
查找职工信息 : 按照职工的编号或者职工的姓名进行查找相关的人员信息
按照编号排序 : 按照职工编号,进行排序,排序规则由用户指定
清空所有文档 : 清空文件中记录的所有职工信息(清空前需要再次确认,防止误删)*/
首先创建管理类
与用于的沟通菜单界面
对职工增删改查操作
与文件的读写交互

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

int main()
{

}

与用户的沟通菜单界面

#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
#include"employee.h"
#include"boss.h"
#include"manager.h"
#include<fstream>
#define FILENAME "empfile.txt"

//workerManger.h
class workerManger
{
public:
    void Show_Menu;
    void exitSystem;
    
};

//workerManger.cpp
void workerManger::Show_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 workerManger::exitSystem()
{
    cout << "欢迎下次使用" << endl
    system("cls");
    exit(0); // 只要调用这个函数程序就退出
}

接着我们再看增加职工信息,首先我们要将普通员工、经理、老板抽象出来一个类,然后上述三个子类继承一个父类。

三种职工抽象到一个类worker中,利用多态管理不同的职工种类
职工属性:职工编号、职工姓名、职工所在部门编号
职工行为:岗位职责信息描述、获取岗位名称
//worker.h
class worker
{
public:
    virtual void showInfo() = 0 ;//显示信息
    virtual string m_DepltId() = 0; // 获取岗位名称

    string m_name;
    int m_id;
    int m_DeptId;
};

//employee.h
class employee:public worker
{
public:
    employee(string name ; int id ; int did);
    void showInfo();
    string m_DepltId();
};

//employee.cpp
#include"employee.h"
employee::employee(string name ; int id ; int did)
{
    this->m_name = name ; 
    this->m_ID = id;
    this->m_DeptId = did;
}
void employee::showInfo()
{
    cout << "职工编号:" << this->m_ID
	<< "\t职工姓名:" << this->m_name
	<< "\t岗位:" << this->getDeptName()
	<< "\t岗位职责:完成经理交给的任务" << endl;
}
string employee::m_DepltId()
{
    return "员工";
}

//manager.h
class manager:public workerManger
{
public:
    employee(string name ; int id ; int did);
    void showInfo();
    string m_DepltId();
};

//manager.cpp
#include"manager.h"
employee::manager(string name ; int id ; int did)
{
    this->m_name = name ; 
    this->m_ID = id;
    this->m_DeptId = did;
}
void manager::showInfo()
{
    cout << "职工编号:" << this->m_ID
	<< "\t职工姓名:" << this->m_name
	<< "\t岗位:" << this->getDeptName()
	<< "\t岗位职责:完成经理交给的任务" << endl;
}
string manager::m_DepltId()
{
    return "经理";
}

//boss.h
#include"worker.h"

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

	virtual void showInfo();

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

//boss.cpp
#include"boss.h"

Boss::Boss(int id, string name, int did) 
{
	this->m_ID = id;
	this->m_name = name;
	this->m_DeptId = did;
}

void Boss::showInfo() 
{
	cout << "职工编号:" << this->m_ID
		<< "\t职工姓名:" << this->m_name
		<< "\t岗位:" << this->getDeptName()
		<< "\t岗位职责:管理公司所有业务" << endl;
}

//获取岗位名称
string Boss::getDeptName() 
{
	return string("总裁");
}

接下来是添加职工功能

//在管理员中加入添加职工功能的函数
//workermanager.h
class workerManger
{
public:
    workerManger();
    ~workerManager();
    void Show_Menu();
    void exitSystem();
    void Add_Emp();
    bool m_FileIsEmpty;
    init_Emp();//写入文件操作
    int get_Num();
    int IsExist();
    void save();
    int m_EmpNum;//职工人数
    Worker** m_EmpArry;//职工数组指针
};

//workermanager.cpp
#define FILENAME "empfile.txt"
workerManger::workerManger()
{
    this->m_EmpNum = 0;
    this->m_EmpArry = NULL;
    ifstream ifs;
    ifs.open(FILENAME , ios::in); // 读入文件
    
    if (!ifs.is_open())
    {
        cout << "文件不存在" << endl;
        //初始化属性
        this->m_EmpNum = 0;
        this->m_FileIsEmpty = true;
        this->m_EmpArry = NULL;
        this->m_FileIsEmpty = true;
        ifs.close();
        return ; 
    } 
    //分三种情况 , 文件不存在 , 文件存在但被清空无数据 , 文件存在
    //文件存在但被清空
    char ch; 
    ifs >> ch ;
    if(ifs.eof())
    {
        cout << "文件为空!" << endl;
        this->m_EmpNum = 0;
        this->m_FileIsEmpty = true;
        this->m_EmpArry = NULL;//初始化数组指针
        ifs.close();
        return;
    }
    //文件存在
    int num = this->get_Num();
    this->Emp_Num = num ;
    //根据职工数创建数组  //开辟空间
    this->m_EmpArry = new Worker * [this->m_EmpNum];
    
    this->init_Emp();
    
}
workerManger::~workerManager()
{
    if(this->m_EmpArry != NULL)
    {
        for (int i = 0; i < m_EmpNum; i++) 
        {
	        if (this->m_EmpArry[i] != NULL) 
	        {
		        delete this->m_EmpArry[i]; // 删除每个位置的堆区数据
	        }
        }
        delete[]  this->m_EmpArry;
        this->m_EmpArry = NULL;//堆区内存要释放
    }
}
void workerManger::Add_Emp()
{
    
    cout << "请选择要添加的员工数量" << endl;
    int addnum = 0 ;
    cin >> a ;
    if ( a < 0 )
    {
        cout << "输入的信息有误" << endl; 
    }
    else
    {
        int newsize = this->m_EmpNum + addnum ;  
        Worker** newSpace = new Worker* (newsize);
        //将旧数据写到新的空间中
        if(this->m_EmpNum != NULL)
        {
            for (int i = 0; i < this->m_EmpNum; i++)
            {
	            newSpace[i] = this->m_EmpArry[i];
            }
        }
        for(int i = 0 ; i < addnum ; i ++ )    
        {
            int id = 0 ;
            int did = 0 ;
            string name = "";
            int dSelect = 0;
            cout << "请输入第" << i + 1 << "个新职工编号:" << endl;
            cin >> id; 
            if (this->IsExist(id) != -1) //判断编号是否存在
            {
                cout << "员工编号已经存在,请勿重复添加" << endl;
                break;
            }
            else
            {
                cout << "请输入第" << i + 1 << "个新职工姓名:" << endl;
                cin >> name;
                cout << "选择该职工的岗位" << endl;
                cout << "1、普通员工" << endl;
                cout << "2、经理" << endl;
                cout << "3、总裁" << endl;
                cin >> dSelect;
                Worker* worker = NULL;
                switch(dSelect)
                {
                case 1:
                    worker = new employee(name , id , 1);
                    break;
                case 2:
                    worker = new manager(name , id , 2);
                    break;
                case 3:
                    worker = new boss(name , id , 3);
                    break;
                }
                newSpace[this->m_EmpNum + i ] = worker;
                delete[] this->m_EmpArry;
                this->FileIsEmpty = false ; 
                this->m_EmpArry = newSpace ; 
                this->m_EmpNum = newsize;
                save();
                cout << "添加成功" << addNum << "名新职工!" << endl;
            }
        } 
    }
}

int workerManger::get_Num()
{
    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 workerManger::init_Emp()
{
    ofstream ofs ; 
    ofs.open(FILENAME , ios::out);
    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_EmpArry[index] = worker;
		index++;
	}
	ifs.close();
}

int workerManger::IsExist()
{
    int index = -1;//先默认不存在
    for (int i = 0; i < this->m_EmpNum; i++) 
    {
	    if (this->m_EmpArry[i]->m_ID == id) 
	    {
		    index = i;
		    break;
	    }
    }        
    return index;
}

void workerManger::save()
{
    ofstream ofs ; 
    ofs.open(FILENAME , ios::out);
    for(int i = 0 ; i < m_EmpNum ; i++)
    {
        ofs << this->m_EmpArry[i]->m_ID << " " << this->m_EmpArry[i]->m_name
	    << " " << this->m_EmpArry[i]->m_DeptId << endl;
    }
    ofs.close();
}

上述便是添加职工 和 相应的其他功能的代码实现,添加员工这边应该算是这个模块中最难理解的部分了。

显示职工信息

void show_Emp;


void show_Emp()
{
    if (this->m_FileIsEmpty) 
    {
	    cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        for(int i = 0; i < m_EmpNum ; i++)
        {
            this->m_EmpArry[i].showinfo();
        }
    }
    system("pause");
    system("cls");
}

删除离职职工 

void Del_Emp();

void Del_Emp()
{
    if (this->m_FileIsEmpty) 
    {
	    cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout<< "请输入要删除员工的id" << endl;
        int id = 0;
        cin >> id ;
        int index = this->IsExist(id);
        if(index != -1)
        {
            for(int i = index ; i < this->EmpNum ; i++ )
            {
                this->EmpArry[i] = this -> EmpArry[i+1];
            }  
            this->EmpNum--;
            this->save();
            cout << "删除成功" << endl;
        }
        else
        {
            cout << "删除失败,员工不存在" << endl;
        }    
    }
}

修改职工信息:照编号修改职工个人信息

void Mod_Emp;

void Mod_Emp()
{
    if (this->m_FileIsEmpty) 
    {
	    cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout << "请输入要修改的员工信息编号:" << endl;
        int id = 0 ;
        cin >> id ;
        int ref = this -> IsExist(id);
        if (ref != -1)
        {
            delete this->EmpArry[ref] ;
            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 = NULL;
            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->m_EmpArry[index] = worker;
            cout << "修改成功!" << endl;
            this->save();
            else
            {
                cout << "修改失败,员工不存在" << endl;
            }
        }
    }
}


查找职工信息 : 按照职工的编号或者职工的姓名进行查找相关的人员信息

void Find_Emp;

void Find_Emp()
{
    if (this->m_FileIsEmpty) 
    {
	    cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        int select = 0;
        cout << "请输入想要查找的方式:" << endl;
        cout << "1、按照职工编号查找   " << endl;
        cout << "2、按照职工姓名查找   " << endl;
        cin >> select;
        if(select == 1)
        {
            int id = 0;
            cout << "请输入员工编号:" << endl;
            cin >> id;
            int ref = this -> IsExit(id)
            if(ref != -1)
            {
                cout << "查到此人,信息如下:" << endl;
                this->EmpArry[ref].showinfo();
            }
            else
            {
                cout << "查无此人" << endl;
            }
        }
        else if(select == 2)
        {
            cout<< "请输入员工姓名:" << endl;
            string name = "";
            cin >> name ;
            bool flag = false;//查找到的标志
            for(int i = 0 ; i < EmpNum ; i++)
            {
                if(this->EmpArry[i]->m_name == name )
                {
                    cout << "查到此人,信息如下:" << endl;
                    flag = true;
                    this->EmpArry[i]->showinfo();
                    break;
                }
                if (flag == false)
                {
	                cout << "查无此人" << endl;
                }
            }
        }
        else
        {
            cout << "输入有误"<< endl;
        }
    }
    system("pause");
    system("cls");
}


按照编号排序 : 按照职工编号,进行排序,排序规则由用户指定(这边排序方式是选择排序 重点)

void Sort_Emp();

void Sort_Emp()
{
    if (this->m_FileIsEmpty)
    {
	    cout << "文件不存在或记录为空" << endl;
	    system("pause");
	    system("cls");
    }
    else
    {
        cout << "请选择想要排序的方式:" << endl;
        cout << "1、按照职工号进行升序" << endl;
        cout << "2、按照职工号进行降序" << endl;
        int select = 0;
        cin >> select; // 选择排序
  
        for (int i = 0 ; i < this->Emp_Num ; i++)
        {
            int maxormin = i ;
            if (select == 1)
            {
				if (m_EmpArry[minOrMax]->m_ID > m_EmpArry[j]->m_ID)
				{
					minOrMax = j;
				}
            }
            else 
            {
				if (m_EmpArry[minOrMax]->m_ID < m_EmpArry[j]->m_ID) 
				{
					minOrMax = j;
				}
            }  
        }
        if(i != minOrMax)
        {
            Worker* temp = m_EmpArry[i];
            m_EmpArry[i] = m_EmpArry[minOrMax];
            m_EmpArry[minOrMax] = temp;
        }
    cout << "排序成功,排序后结果为:" << endl;
    this->save();
    this->show_Emp();
    }
}


清空所有文档 : 清空文件中记录的所有职工信息(清空前需要再次确认,防止误删)

Clean_File() ;

void Clean_File() 
{
    if (this->m_FileIsEmpty)
    {
	    cout << "文件不存在或记录为空" << endl;
    }
    else
    {
        cout << "你确定要清空文件么?" << endl;
        cout << "1、确认" << endl;
        cout << "2、返回" << endl;
        int select = 0 ;
        cin >> select;
        if (select == 1)
        {
            ofstream ofs;
            //打开模式 ios::trunc 如果存在,删除文件并重新创建
            ofs.open(FILENAME , ios::trunc)
            ofs.close();
            if(this->EmpArry != NULL)    
            {
                for(int i = 0 ; i < EmpNum ; i++)
                {
                    if(this->EmpArry[i] != NULL)
                    {
                        delete this->m_EmpArry[i];
                        this->m_EmpArry[i] = NULL;
                    }
                }    
                this->m_EmpNum = 0;
                delete[] this->m_EmpArry; 
                this->m_EmpArry = NULL;
                this->m_FileIsEmpty = true;
            }
            cout << "清空成功!" << endl;
        }
    }
    system("pause");
    system("cls");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值