职工管理系统.cpp
#include"workerManager.h"
#include"boss.h"
#include"employee.h"
#include"manager.h"
#include"worker.h"
int main()
{
int id = 0;
WorkerManager wm;
int choice = 0;
do
{
wm.Menu();
cout << "请输入你要进行的操作:";
cin >> choice;
switch (choice)
{
case 0://退出系统
cout << "退出系统成功!" << endl;
return 0;
break;
case 1://添加职工
wm.Add_Emp();
break;
case 2://显示职工
wm.ShowEmp();
break;
case 3://删除职工
cout << "请输入你要删除的员工编号:";
cin >> id;
wm.DeleEmp(id);
break;
case 4://修改职工
cout << "请输入你要修改的员工编号:";
cin >> id;
wm.ChangeEmp(id);
break;
case 5://查找职工
cout << "请输入你要查找的员工编号:";
cin >> id;
wm.FindEmpP(id);
break;
case 6://排序职工
wm.RankEmp();
break;
case 7://清空文件
wm.CleanFile();
break;
}
} while (1);
return 0;
}
worker.h
#include<iostream>
#pragma once//防止头文件重复包含
#include<string>
#include<fstream>
using namespace std;
class Worker
{
public:
//显示个人信息
virtual void ShowInfo() = 0;
//获取岗位名称
virtual string GetDeptName() = 0;
int m_Id;//职工编号
string m_Name;//职工姓名
int m_DeptId;//职工所在部门编号
};
workerManager.cpp
#include"workerManager.h"
#include"employee.h"
#include "manager.h"
#include"boss.h"
#include"worker.h"
//构造函数
WorkerManager::WorkerManager()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
//文件不存在情况
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
//初始化人数
this->m_EmpNum = 0;
//初始化文件为空标志
this->m_FileIsEmpty = true;
//初始化数组指针
this->m_EmpArray = NULL;
//关闭文件
ifs.close();
return;
}
//文件存在,但是没有记录
char ch;
ifs >> ch;//右移一个字符去读
if (ifs.eof())
{
cout << "文件内容为空" << endl;
//初始化人数
this->m_EmpNum = 0;
//初始化文件为空标志
this->m_FileIsEmpty = true;
//初始化数组指针
this->m_EmpArray = NULL;
//关闭文件
ifs.close();
return;
}
//文件存在 且记录数据
int num = this->GetNum();
cout << "当前员工个数为:" << num << endl;
this->m_EmpNum = num;
//根据职工数创建数组
this->m_EmpArray = new Worker*[this->m_EmpNum];
//初始化职工
InitEmp();
/*for (int i = 0; i < this->m_EmpNum; i++)
{
cout << "职工号为:" << this->m_EmpArray[i]->m_Id
<< " 职工名称:" << this->m_EmpArray[i]->m_Name
<< " 部门编号:" << this->m_EmpArray[i]->m_DeptId << endl;
}*/
}
//析构函数
WorkerManager::~WorkerManager()
{
if (this->m_EmpArray != NULL)
{
delete[]this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
//打印菜单
void WorkerManager::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;
}
//增加职工
void WorkerManager::Add_Emp()
{
cout << "请输入添加员工的个数:";
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 << "个新员工的编号:" ;
cin >> id;
cout << "请输入第" << i + 1 << "个新员工的名字:" ;
cin >> name;
cout << "请输入第" << i + 1 << "个新员工的岗位:" << 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:
system("cls");
break;
}
newspace[this->m_EmpNum + i] = worker;
}
//释放原有的空间
delete[] this->m_EmpArray;
//更新新空间的指向
this->m_EmpArray = newspace;
//更新新的个数
this->m_EmpNum = newsize;
//更新职工不为空标志
this->m_FileIsEmpty = false;
cout << "成功添加" << addnum << "名新员工!" << endl;
this->save();//保存文件
}
else
{
cout << "输入有误" << endl;
}
}
//保存职工信息
void WorkerManager::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_DeptId << endl;
}
ofs.close();
}
//统计人数
int WorkerManager::GetNum()
{
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 WorkerManager::InitEmp()
{
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++;
}
}
//显示职工
void WorkerManager::ShowEmp()
{
if (this->m_FileIsEmpty )
{
cout << "职工信息为空" << endl;
}
else
{
for (int i = 0; i < this->m_EmpNum; i++)
{
//利用多态调用打印
this->m_EmpArray[i]->ShowInfo();
}
}
}
//根据职工编号查找职工
int WorkerManager::FindEmp(int id)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Id == id)
{
return i;
}
}
return -1;
}
//根据职工编号查找职工并打印
void WorkerManager::FindEmpP(int id)
{
int ret =this-> FindEmp(id);
if (-1 == ret)
{
cout << "你要查找的编号不存在!" << endl;
}
else
{
this->m_EmpArray[ret]->ShowInfo();
}
}
//删除职工
void WorkerManager::DeleEmp(int id)
{
int ret =this-> FindEmp(id);
if (ret == -1)
{
cout << "你要删除的员工编号不存在!" << endl;
return;
}
for (int i = ret; i < this->m_EmpNum-1; i++)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
this->m_EmpNum--;
this->save();//同步到文件中
cout << "删除成功!" << endl;
}
//修改员工
void WorkerManager::ChangeEmp(int id)
{
int input = 0;
string name;
int m_id;
int did;
int ret = this->FindEmp(id);
if (-1 == ret)
{
cout << "你输入的员工编号不存在!" << endl;
}
else
{
cout << "*******************" << endl;
cout << "***1、修改编号*****" << endl;
cout << "***2、修改姓名*****" << endl;
cout << "***3、修改部门*****" << endl;
cout << "*******************" << endl;
cout << "请选择你要进行的操作:";
cin >> input;
FindEmpP(input);
switch (input)
{
case 1:
cout << "请输入修改后的编号:";
cin >> m_id;
this->m_EmpArray[ret]->m_Id = m_id;
break;
case 2:
cout << "请输入修改后的姓名:";
cin >> name;
this->m_EmpArray[ret]->m_Name = name;
break;
case 3:
cout << "请输入修改后的部门编号:";
cin >> did;
this->m_EmpArray[ret]->m_DeptId = did;
break;
default:
cout << "你的输入有误!" << endl;
break;
}
cout << "修改成功" << endl;
}
}
//排序职工
void WorkerManager::RankEmp()
{
for (int i = 0; i < this->m_EmpNum - 1; i++)
{
for (int j = 0; j < this->m_EmpNum - 1 - i; j++)
{
if (this->m_EmpArray[j]->m_Id > this->m_EmpArray[j + 1]->m_Id)
{
int m_id = 0;
m_id = this->m_EmpArray[j]->m_Id;
this->m_EmpArray[j]->m_Id = this->m_EmpArray[j+1]->m_Id;
this->m_EmpArray[j + 1]->m_Id = m_id;
}
}
}
cout << "排序完成" << endl;
}
void WorkerManager::CleanFile()
{
cout << "是否确认清空?" << endl;
cout << "1、确认" << endl;
cout << "2、返回" << endl;
int input = 0;
cin >> input;
if (input == 1)
{
//打开模式 ios::trync 如果存在则删除文件并重建
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->m_EmpArray != NULL)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i] != NULL)
{
delete this->m_EmpArray[i];
}
}
this->m_EmpNum = 0;
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
this->m_FileIsEmpty = true;
}
cout << "清空完成" << endl;
}
else
{
return;
}
}
workerManager.h
#pragma once//防止头文件重复包含
#include<iostream>
#include<string>
#include<fstream>
#define FILENAME "empFile.txt"
#include"worker.h"
using namespace std;//使用标准的命名空间
class WorkerManager
{
public:
WorkerManager();
int m_EmpNum;//记录文件中的人数个数
Worker** m_EmpArray;//员工数组的指针
void Menu();//显示菜单
void Add_Emp();//添加员工
void save();//保存文件
bool m_FileIsEmpty;//标志文件是否为空
int GetNum();//统计人数
void InitEmp();//初始化员工
void ShowEmp();//显示职工
int FindEmp(int id);//查找职工
void FindEmpP(int id);//查找职工并打印
void DeleEmp(int id);//删除职工
void ChangeEmp(int id);//修改员工
void RankEmp();//排序职工
void CleanFile();//清空文件
~WorkerManager();
};
employee.cpp
#include "employee.h"
Employee::Employee(int id, string name, int did)//构造函数传入员工信息
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = did;
}
//显示个人信息
void Employee::ShowInfo()
{
cout << "职工编号:" << this->m_Id;
cout << " 职工姓名:" << this->m_Name;
cout << " 所在岗位:" << this->GetDeptName();
cout << " 岗位职责:完成经理交给的任务!" << endl;
}
//获取岗位名称
string Employee::GetDeptName()
{
return string("员工");
}
employee.h
#include<iostream>
#include<string>
#include<fstream>
#include "worker.h"
using namespace std;
//员工类
class Employee:public Worker
{
public:
Employee(int id, string name, int did);//构造函数传入员工信息
//显示个人信息
virtual void ShowInfo();
//获取岗位名称
virtual string GetDeptName();
};
manager.cpp
#include "manager.h"
Manager::Manager(int id, string name, int did)//构造函数传入员工信息
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = did;
}
//显示个人信息
void Manager::ShowInfo()
{
cout << "职工编号:" << this->m_Id;
cout << " 职工姓名:" << this->m_Name;
cout << " 所在岗位:" << this->GetDeptName();
cout << " 岗位职责:完成老板交给的任务!" << endl;
}
//获取岗位名称
string Manager::GetDeptName()
{
return string("经理");
}
manager.h
#include<iostream>
#include<string>
#include<fstream>
#include "worker.h"
using namespace std;
//经理类
class Manager:public Worker
{
public:
Manager(int id, string name, int did);
//显示个人信息
virtual void ShowInfo();
//获取岗位名称
virtual string GetDeptName();
};
boss.h
#include<iostream>
#include<string>
#include<fstream>
#include "worker.h"
using namespace std;
//老板类
class Boss :public Worker
{
public:
Boss(int id, string name, int did);
//显示个人信息
virtual void ShowInfo();
//获取岗位名称
virtual string GetDeptName();
};
Boss.cpp
?
#include<iostream>
#include<string>
#include<fstream>
#include "worker.h"
using namespace std;
//老板类
class Boss :public Worker
{
public:
Boss(int id, string name, int did);
//显示个人信息
virtual void ShowInfo();
//获取岗位名称
virtual string GetDeptName();
};
?
|