C++案例——基于多态的员工管理系统
·
确认好项目所需的功能
cout << "|0.退出管理系统 |" << endl;
cout << "|1.添加职工信息 |" << endl;
cout << "|2.显示职工信息 |" << endl;
cout << "|3.删除离职职工 |" << endl;
cout << "|4.修改职工信息 |" << endl;
cout << "|5.查找职工信息 |" << endl;
创建"workManager"相关头文件及源文件,其内容用于实现我们所需所有功能,头文件代码如下:
#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"
class WorkManager
{
public:
WorkManager();
void Showmenu();
void ExitSystem();
int m_EmpNum;//记录人数
Worker** m_EmpArray;//职工数组指针
void ADD_emp();//添加职工
void save();
bool FileisEmpty;//判断文件是否为空
int get_Empnum();//统计人数
void ini_Emp();//初始化员工
void Show_emp();//显示职工
void Del_emp();//删除职工
int isExist(int id);//判断是否存在
void Mod_emp();
void Find_emp();
~WorkManager();
};
源文件代码如下:
#include"workManage.h"
WorkManager::WorkManager()
{
//文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "------------------------------------------" << endl;
cout << "|当前没有创建职工相关文件,请添加职工信息|" << endl;
cout << "------------------------------------------" << endl;
this->m_EmpNum = 0;
this->m_EmpArray = NULL;
this->FileisEmpty = true;
ifs.close();
return;
}
//文件存在 数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "----------------------" << endl;
cout << "|当前职工文件数据为空|" << endl;
cout << "----------------------" << endl;
this->m_EmpNum = 0;
this->m_EmpArray = NULL;
this->FileisEmpty = true;
ifs.close();
return;
}
//文件存在 数据不为空
int num = get_Empnum();
//cout << "职工人数为:" << num;
this -> m_EmpNum = num;
//开辟空间
this->m_EmpArray = new Worker * [this->m_EmpNum];
this->ini_Emp();
}
int WorkManager::get_Empnum()
{
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 WorkManager::Showmenu()
{
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 WorkManager::ExitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
void WorkManager::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 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 = NULL;
switch (dselect)
{
case 1:
worker = new Employee(id, name, 1);
break;
case 2:
worker = new Manager(id, name, 2);
break;
case 3:
worker = new Boss(id, name, 3);
break;
default:
break;
}
newSpace[this->m_EmpNum + i] = worker;
}
delete[] this->m_EmpArray;
this->m_EmpArray = newSpace;
this->m_EmpNum = newSize;
this->FileisEmpty = false;
cout << "成功添加" << addnum << "位新职工" << endl;
this->save();
}
else
{
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
void WorkManager::save()
{
ofstream ofs;
ofs.open(FILENAME, 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_dpetid << endl;
}
ofs.close();
}
void WorkManager::ini_Emp()
{
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 if (did == 3)
{
worker = new Boss(id, name, did);
}
this->m_EmpArray[index] = worker;
index++;
}
ifs.close();
}
void WorkManager::Show_emp()
{
if (this->FileisEmpty)
{
cout << "文件为空" << endl;
}
else
{
for (int i = 0; i < m_EmpNum; i++)
{
this->m_EmpArray[i]->Showid();
}
}
system("pause");
system("cls");
}
void WorkManager::Del_emp()
{
if (this->FileisEmpty)
{
cout << "文件为空" << endl;
system("pause");
system("cls");
}
else
{
cout << "输入想要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->isExist(id);
if (index != -1)
{
for (int i = index; i < this->m_EmpNum - 1 ; i++)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
this->m_EmpNum--;
this->save();//数据同步到文件中
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
}
int WorkManager::isExist(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 WorkManager::Mod_emp()
{
if (this->FileisEmpty)
{
cout << "文件为空" << endl;
}
else
{
cout << "修改职工编号为:" << endl;
int id;
cin >> id;
int ret = this->isExist(id);
if (ret != -1)
{
delete this->m_EmpArray[ret];
int newid = 0;
string newname = "";
int dselect = 0;
cout << "查找到" << id << "号职工,请输入新职工号:" << endl;
cin >> newid;
cout << "请输入姓名:" << endl;
cin >> newname;
cout << "请选择岗位:" << 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_EmpArray[ret] = worker;
cout << "修改成功" << endl;
this->save();
}
else
{
cout << "查无此人" << endl;
}
}
system("pause");
system("cls");
}
void WorkManager::Find_emp()
{
if (this->FileisEmpty)
{
cout << "文件不存在" << endl;
}
else
{
cout << "请输入查找方式:" << endl;
cout << "1.按编号查找" << endl;
cout << "2.按姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
int id;
cout << "请输入该职工编号:" << endl;
cin >> id;
int ret = this->isExist(id);
if (ret != -1)
{
cout << "查找成功,信息如下:" << endl;
this->m_EmpArray[ret]->Showid();
}
else
{
cout << "查无此人" << endl;
}
}
else if (select == 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 << "查找成功,信息如下:" << endl;
flag = true;
this->m_EmpArray[i]->Showid();
}
}
if (flag == false)
{
cout << "查无此人" << endl;
}
}
else
{
cout << "输入有误" << endl;
}
}
system("pause");
system("cls");
}
WorkManager::~WorkManager()
{
if (this->m_EmpArray != NULL)
{
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
我们需要创建“worker”,“boss”,“manager”,“employee”等相关头文件以及源文件,其中“worker”用于充当父类,其中功能全为虚函数(空实现),方便子类继承重写,所以worker不必创建源文件,所有代码如下:
“worker.h”
#pragma once
#include<iostream>
using namespace std;
#include<string>
class Worker
{
public:
virtual void Showid() = 0;
virtual string Getdptid() = 0;
int m_id;
string m_name;
int m_dpetid;
};
"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 Showid();
virtual string Getdptid();
};
"boss.cpp"
#include<iostream>
using namespace std;
#include"boss.h"
Boss::Boss(int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_dpetid = did;
}
void Boss::Showid()
{
cout << "职工编号:" << this->m_id
<< "\t 姓名:" << this->m_name
<< "\t 岗位:" << this->Getdptid()
<< "\t职责:管理公司所有的事务" << endl;
}
string Boss::Getdptid()
{
return string("总裁");
}
"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 Showid();
virtual string Getdptid();
};
"employee.cpp"
#include<iostream>
using namespace std;
#include"employee.h"
Employee::Employee(int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_dpetid = did;
}
void Employee :: Showid()
{
cout << "职工编号:" << this->m_id
<< "\t 姓名:" << this->m_name
<< "\t 岗位:" << this->Getdptid()
<< "\t职责:完成经理交代的任务" << endl;
}
string Employee :: Getdptid()
{
return string("员工");
}
"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 Showid();
virtual string Getdptid();
};
"manager.cpp"
#include<iostream>
using namespace std;
#include"manager.h"
Manager::Manager(int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_dpetid = did;
}
void Manager::Showid()
{
cout << "职工编号:" << this->m_id
<< "\t 姓名:" << this->m_name
<< "\t 岗位:" << this->Getdptid()
<< "\t职责:完成老板交代的任务,并下发给下面的员工" << endl;
}
string Manager::Getdptid()
{
return string("经理");
最后就是主函数的代码
#include<iostream>
using namespace std;
#include"workManage.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
int main()
{
/*Worker* work = NULL;
work = new Boss(1, "张三", 1);
work->Showid();
delete work;
work = new Manager(1, "张三", 1);
work->Showid();
delete work;
work = new Employee(1, "张三", 1);
work->Showid();
delete work;*/
WorkManager user;
int choice = 0;
while (true)
{
user.Showmenu();
cout << "请输入" << endl;
cin >> choice;
switch (choice)
{
case 0:
user.ExitSystem();
break;
case 1:
user.ADD_emp();
break;
case 2:
user.Show_emp();
break;
case 3:
user.Del_emp();
break;
case 4:
user.Mod_emp();
break;
case 5:
user.Find_emp();
break;
case 6:
cout << "暂未开放此功能,请重新输入" << endl;
system("pause");
system("cls");
break;
case 7:
cout << "暂未开放此功能,请重新输入" << endl;
system("pause");
system("cls");
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
其中/**/中的注释为阶段性功能性的测试代码。
以下为代码运行效果,同时实现了对文件的读写功能。

更多推荐


所有评论(0)