c++ 基础知识-类与对象-实践-员工管理系统(记录)2
1.员工管理系统操作类
#pragma once
#ifndef _WORKMANAGER_
#define _WORKMANAGER_
#include <iostream>
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
#include <fstream>
#include <ostream>
using namespace std;
#define FILENAME "E:\\c++_workspace\\code_practice\\empFile.txt"
class workManager
{
public:
workManager();
void Show_Menu();
void Choice_Putton();
void Exit_System();
void Add_Worker();
void Save_Data();
int Get_EmpNum();
void Init_Worker();
void Display_Information();
void Remove_Worker();
void Mod_Worker();
void Find_Worker();
void Sort_Worker();
void Clear_Worker();
~workManager();
public:
int m_EmpNum;
Worker ** m_EmpArray;
bool m_FileIsEmpty;
};
#endif
#include "workManager.h"
workManager::workManager()
{
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_EmpArray = NULL;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
int num = this->Get_EmpNum();
cout<<"职工人数为: "<<num<<endl;
this->m_EmpNum = num;
this->Init_Worker();
}
void workManager::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 workManager::Choice_Putton()
{
int choice = 0;
while(true)
{
this->Show_Menu();
cout<<"请输入您的选择!!!"<<endl;
cin >> choice;
switch(choice)
{
case 0:
this->Exit_System();
break;
case 1:
this->Add_Worker();
break;
case 2:
this->Display_Information();
break;
case 3:
this->Remove_Worker();
break;
case 4:
this->Mod_Worker();
break;
case 5:
this->Find_Worker();
break;
case 6:
this->Sort_Worker();
break;
case 7:
this->Clear_Worker();
break;
default:
system("cls");
break;
}
}
}
void workManager::Exit_System()
{
cout<<"您选择退出系统!!!"<<endl;
system("pause");
exit(0);
}
void workManager::Add_Worker()
{
cout<<"请输入增加职工数量: "<<endl;
int addNum = 0;
cin>>addNum;
if(addNum > 0)
{
int newSize = 0;
if (this->m_EmpNum > 0 )
{
newSize = this->m_EmpNum + addNum;
}else{
newSize = 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;
for (int j = 0; j < (this->m_EmpNum+i); j++)
{
if(id == newSpace[j]->m_Id)
{
cout<<"该员工编号重复"<<endl;
cout<<"请重新输入该员工编号"<<endl;
cin>>id;
}
}
cout<<"请输入第 "<<i + 1<<" 个新职工姓名: "<<endl;
cin >>name;
for (int j = 0; j < (this->m_EmpNum+i); j++)
{
if(name == newSpace[j]->m_Name)
{
cout<<"该员工姓名重复"<<endl;
cout<<"请重新输入该员工姓名"<<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;
}
newSpace[this->m_EmpNum+i] = worker;
}
delete[] this->m_EmpArray;
this->m_EmpArray = newSpace;
this->m_EmpNum = newSize;
this->m_FileIsEmpty = false;
this->Save_Data();
cout<<"成功添加"<<addNum<<"名员工"<<endl;
}
else
{
cout<<"输入有误"<<endl;
}
system("pause");
system("cls");
}
void workManager::Save_Data()
{
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 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::Init_Worker()
{
ifstream ifs;
ifs.open(FILENAME,ios::in);
int id;
string name;
int dId;
int num = 0;
Worker ** newSpace = new Worker*[this->m_EmpNum];
while(ifs >> id && ifs >> name && ifs >> 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;
}
newSpace[num++] = worker;
}
this->m_EmpArray = newSpace;
}
void workManager::Display_Information()
{
if (this->m_FileIsEmpty == true)
{
cout<<"文件不存在或文件为空!!!"<<endl;
system("pause");
system("cls");
}else{
cout<<"显示员工信息"<<endl;
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;
}
system("pause");
system("cls");
}
}
void workManager::Remove_Worker()
{
if (this->m_FileIsEmpty == true)
{
cout<<"文件不存在或文件为空!!!"<<endl;
system("pause");
system("cls");
}else{
cout<<"请输入待删除员工数量: "<<endl;
int removeNum = 0;
cin>>removeNum;
if(removeNum > 0 && this->m_EmpNum >= removeNum)
{
for(int i = 0; i < removeNum;i++)
{
cout<<"请输入第 "<<i + 1<<" 名待删除员工信息标识: "<<endl;
cout<<"1.员工编号"<<endl;
cout<<"2.员工姓名"<<endl;
bool flag = false;
int choice;
cin>>choice;
switch(choice)
{
case 1:
cout<<"请输入待删除员工编号"<<endl;
int id;
cin>>id;
for (int i = 0; i < this->m_EmpNum;i++)
{
if(id == this->m_EmpArray[i]->m_Id)
{
while (i < this->m_EmpNum-1)
{
this->m_EmpArray[i] = this->m_EmpArray[i+1];
i++;
}
flag = true;
this->m_EmpNum--;
}
}
if (flag == false)
{
cout<<"没有该员工编号,请核对后重新输入!!!"<<endl;
return;
}
break;
case 2:
cout<<"请输入待删除员工姓名"<<endl;
string name;
cin>>name;
for (int i = 0; i < this->m_EmpNum; i++)
{
if(name == this->m_EmpArray[i]->m_Name)
{
while (i < this->m_EmpNum-1)
{
this->m_EmpArray[i] = this->m_EmpArray[i+1];
i++;
}
flag = true;
this->m_EmpNum--;
}
}
if (flag == false)
{
cout<<"没有该员工姓名,请核对后重新输入!!!"<<endl;
return;
}
break;
}
}
this->Save_Data();
cout<<"成功删除"<<removeNum<<"名员工"<<endl;
}
else
{
cout<<"输入有误"<<endl;
}
system("pause");
system("cls");
}
}
void workManager::Mod_Worker()
{
if (this->m_FileIsEmpty == true)
{
cout<<"文件不存在或文件为空!!!"<<endl;
system("pause");
system("cls");
}else{
cout<<"请输入待修改员工数量: "<<endl;
int modNum = 0;
cin>>modNum;
if(modNum > 0 && this->m_EmpNum >= modNum)
{
for(int i = 0; i < modNum;i++)
{
int id;
string name;
int dId;
bool flag = false;
int choice;
cout<<"请输入第 "<<i + 1<<" 名待修改员工信息标识: "<<endl;
cout<<"1.员工编号"<<endl;
cout<<"2.员工姓名"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"请输入待修改员工编号"<<endl;
int id;
cin>>id;
for (int i = 0; i < this->m_EmpNum;i++)
{
if(id == this->m_EmpArray[i]->m_Id)
{
cout<<"请输入待修改员工信息"<<endl;
cout<<"请输入修改后员工编号:"<<endl;
cin>>id;
cout<<"请输入修改后员工姓名:"<<endl;
cin>>name;
cout<<"请输入修改后员工职位:"<<endl;
cin>>dId;
for (int j = 0; j < this->m_EmpNum; ++j)
{
if(id != this->m_EmpArray[j]->m_Id || name != this->m_EmpArray[j]->m_Name)
{
this->m_EmpArray[i]->m_Id = id;
this->m_EmpArray[i]->m_Name = name;
this->m_EmpArray[i]->m_DeptId = dId;
flag = true;
}else{
if(name == this->m_EmpArray[j]->m_Name)
{
cout<<"该员工姓名重复"<<endl;
cout<<"请重新输入该员工姓名"<<endl;
cin>>name;
}
else if(id == this->m_EmpArray[j]->m_Id)
{
cout<<"该员工编号重复"<<endl;
cout<<"请重新输入该员工编号"<<endl;
cin>>id;
}else{
cout<<"该员工编号姓名均重复"<<endl;
cout<<"请重新输入该员工编号"<<endl;
cin>>id;
cout<<"请重新输入该员工姓名"<<endl;
cin>>name;
}
}
}
}
}
if (flag == false)
{
cout<<"没有该员工编号,请核对后重新输入!!!"<<endl;
return;
}
}
break;
case 2:
{
cout<<"请输入待修改员工姓名"<<endl;
string name;
cin>>name;
for (int i = 0; i < this->m_EmpNum; i++)
{
if(name == this->m_EmpArray[i]->m_Name)
{
cout<<"请输入待修改员工信息"<<endl;
cout<<"请输入修改后员工编号:"<<endl;
cin>>id;
cout<<"请输入修改后员工姓名:"<<endl;
cin>>name;
cout<<"请输入修改后员工职位:"<<endl;
cin>>dId;
for (int j = 0; j < this->m_EmpNum; ++j)
{
if(id != this->m_EmpArray[j]->m_Id || name != this->m_EmpArray[j]->m_Name)
{
this->m_EmpArray[i]->m_Id = id;
this->m_EmpArray[i]->m_Name = name;
this->m_EmpArray[i]->m_DeptId = dId;
flag = true;
}else{
if(name == this->m_EmpArray[j]->m_Name)
{
cout<<"该员工姓名重复"<<endl;
cout<<"请重新输入该员工姓名"<<endl;
cin>>name;
}
else if(id == this->m_EmpArray[j]->m_Id)
{
cout<<"该员工编号重复"<<endl;
cout<<"请重新输入该员工编号"<<endl;
cin>>id;
}else{
cout<<"该员工编号姓名均重复"<<endl;
cout<<"请重新输入该员工编号"<<endl;
cin>>id;
cout<<"请重新输入该员工姓名"<<endl;
cin>>name;
}
}
}
}
}
if (flag == false)
{
cout<<"没有该员工姓名,请核对后重新输入!!!"<<endl;
return;
}
}
break;
}
}
this->Save_Data();
cout<<"成功修改"<<modNum<<"名员工"<<endl;
}
system("pause");
system("cls");
}
}
void workManager::Find_Worker()
{
if (this->m_FileIsEmpty == true)
{
cout<<"文件不存在或文件为空!!!"<<endl;
system("pause");
system("cls");
}
cout<<"待查找员工信息标识: "<<endl;
cout<<"1.员工编号"<<endl;
cout<<"2.员工姓名"<<endl;
bool flag = false;
int choice;
cin>>choice;
switch(choice)
{
case 1:
cout<<"请输入查找员工编号"<<endl;
int id;
cin>>id;
for (int i = 0; i < this->m_EmpNum;i++)
{
if(id == this->m_EmpArray[i]->m_Id)
{
cout<<"员工编号:"<<this->m_EmpArray[i]->m_Id << " "
<<"员工姓名:"<<this->m_EmpArray[i]->m_Name<< " "
<<"员工职位等级:"<<this->m_EmpArray[i]->m_DeptId<<endl;
flag = true;
}
}
if (flag == false)
{
cout<<"没有该员工编号,请核对后重新输入!!!"<<endl;
return;
}
break;
case 2:
cout<<"请输入查找员工姓名"<<endl;
string name;
cin>>name;
for (int i = 0; i < this->m_EmpNum; i++)
{
if(name == this->m_EmpArray[i]->m_Name)
{
cout<<"员工编号:"<<this->m_EmpArray[i]->m_Id << " "
<<"员工姓名:"<<this->m_EmpArray[i]->m_Name<< " "
<<"员工职位等级:"<<this->m_EmpArray[i]->m_DeptId<<endl;
flag = true;
}
}
if (flag == false)
{
cout<<"没有该员工姓名,请核对后重新输入!!!"<<endl;
return;
}
break;
}
system("pause");
system("cls");
}
void workManager::Sort_Worker()
{
if (this->m_FileIsEmpty == true)
{
cout<<"文件不存在或文件为空!!!"<<endl;
system("pause");
system("cls");
}else{
cout<<"请选择排序方式:"<<endl;
cout<<"1.按照职工编号升序排列"<<endl;
cout<<"2.按照职工编号降序排列"<<endl;
int choice;
cin>>choice;
for (int i = 0; i < this->m_EmpNum; i++)
{
int minOriMax = i;
for (int j = i + 1; j < this->m_EmpNum; j++)
{
if (choice == 1)
{
if(this->m_EmpArray[minOriMax]->m_Id > this->m_EmpArray[j]->m_Id)
{
minOriMax = j;
}
}
else
{
if(this->m_EmpArray[minOriMax]->m_Id < this->m_EmpArray[j]->m_Id)
{
minOriMax = j;
}
}
}
if (i != minOriMax)
{
Worker * temp = this->m_EmpArray[i];
this->m_EmpArray[i] = this->m_EmpArray[minOriMax];
this->m_EmpArray[minOriMax] = temp;
}
}
cout<<"排序成功!!!"<<endl;
cout<<"排序后结果为:"<<endl;
this->Save_Data();
this->Display_Information();
}
}
void workManager::Clear_Worker()
{
if (this->m_FileIsEmpty == true)
{
cout<<"文件不存在或文件为空!!!"<<endl;
system("pause");
system("cls");
}else{
cout<<"确认将员工数据清空!!!"<<endl;
cout<<"1.确认"<<endl;
cout<<"2.返回"<<endl;
int choice;
cin>>choice;
if (choice == 1)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
Worker * temp = NULL;
this->m_EmpArray[i] = temp;
}
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
cout<<"员工数据已清空!!!"<<endl;
this->Save_Data();
system("pause");
system("cls");
}
else
{
system("pause");
system("cls");
}
}
}
workManager::~workManager()
{
if (this->m_EmpArray != NULL)
{
delete [] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
2.调用主函数
#include "workManager.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
using namespace std;
void test_fun()
{
workManager wm;
wm.Choice_Putton();
}
int main()
{
test_fun();
return 0;
}