职工管理系统
Worker
worker.h(抽象类)
#pragma once
#include<iostream>
using namespace std;
#include<cstring>
class Worker
{
public:
//显示个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string GetDeptName() = 0;
//职工编号
int m_ID;
//职工姓名
string m_name;
//职工岗位
int m_DeptId;
};
employee.h
#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"worker.h"
class Employee : public Worker
{
public:
Employee(int id , string name , int did);
//显示个人信息
void showInfo();
//获取岗位名称
string GetDeptName();
};
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
<< "\t 职工姓名:" << this->m_name
<< "\t 岗位: " << this->GetDeptName()
<< "\t 岗位职责:搬砖" << endl;
}
//获取岗位名称
string Employee::GetDeptName()
{
return string("员工");
}
manager.h
#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"worker.h"
class Manager : public Worker
{
public:
Manager(int id, string name, int did);
//显示个人信息
void showInfo();
//获取岗位名称
string GetDeptName();
//保存文件
void save();
};
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
<< "\t 职工姓名:" << this->m_name
<< "\t 岗位: " << this->GetDeptName()
<< "\t 岗位职责:看砖" << endl;
}
//获取岗位名称
string Manager::GetDeptName()
{
return string("经理");
}
boss.h
#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"worker.h"
class Boss : public Worker
{
public:
Boss(int id, string name, int did);
//显示个人信息
void showInfo();
//获取岗位名称
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("老板");
}
管理类.h
#include"管理类.h"
WorkManager::WorkManager()
{
//初始化属性
ifstream ifs;
ifs.open("系统.txt", ios::in);
if (!ifs.is_open()) //文件不存在
{
cout << "文件不存在" << endl;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
this->m_EmpArry = NULL;
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_EmpNum();
cout << "职工人数为:" << num << endl;
this->m_EmpNum = num;
//根据职工数创建数组
//开辟空间
this->m_EmpArry = new Worker * [this->m_EmpNum];
//文件数据放入数组中
this->init_Emp();
}
void WorkManager::Add()
{
cout << "请输入添加职工的数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
//添加
int newSize = this->m_EmpNum + addNum;
//开辟新空间
Worker** newSpace = new Worker * [newSize];
if (this->m_EmpArry != 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;
string name;
int dSelect;
cout << "请输入第 " << i + 1 << " 个新职工的编号:" << endl;
cin >> id;
cout << "请输入第 " << i + 1 << " 个新职工的姓名:" << endl;
cin >> name;
cout << "请选择该职工的岗位:" << endl
<< "1、普通职工" << endl
<< "2、经理" << endl
<< "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_EmpArry;
//改变空间指向
this->m_EmpArry = newSpace;
//更改职工不为空标志
this->m_FileIsEmpty = false;
//改变指针长度
this->m_EmpNum = newSize;
//添加到文件中
cout << "成功添加 " << addNum << " 名新职工" << endl;
this->Save();
}
else
{
cout << "输入数据有误!" << endl;
}
system("pause");
system("cls");
}
void WorkManager::Show_Menu()
{
cout << "——————————————————" << endl
<< "————欢迎使用职工管理系统————" << endl
<< "————— 0.退出管理系统 —————" << endl
<< "————— 1.增加职工信息 —————" << endl
<< "————— 2.显示职工信息 —————" << endl
<< "————— 3.删除离职职工 —————" << endl
<< "————— 4.修改职工信息 —————" << endl
<< "————— 5.查找职工信息 —————" << endl
<< "————— 6.按照编号排序 —————" << endl
<< "————— 7.清空所有文档 —————" << endl;
cout << endl;
}
void WorkManager::exitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
//不管是在哪个部分调用这个函数 退出程序 0 - 退出参数
}
WorkManager::~WorkManager()
{
if (this->m_EmpArry != NULL)
{
delete[]this->m_EmpArry;
this->m_EmpArry = NULL;
}
}
//保存文件
void WorkManager::Save()
{
ofstream ofs;
ofs.open("系统.txt", ios::out);
for (int i = 0; i < this->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();
}
//统计人数
int WorkManager::get_EmpNum()
{
ifstream ifs;
ifs.open("系统.txt", ios::in);
int id;
string name;
int did;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did)
//输入时有空格
{
//记录人数
num++;
}
ifs.close();
return num;
}
void WorkManager::init_Emp()
{
ifstream ifs;
ifs.open("系统.txt", 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_EmpArry[index] = worker;
index++;
}
ifs.close();
}
void WorkManager::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");
}
int WorkManager::IsExist(string name)
{
int index = -1;
for (int i = 0; i < m_EmpNum; ++i)
{
if (this->m_EmpArry[i]->m_name == name)
{
index = i;
break;
}
}
return index;
}
void WorkManager::Del_Emp() //数组数据前移(找到位置的后一位)
{
if (this->m_FileIsEmpty)
cout << "文件不存在或文件为空!" << endl;
else
{
//按照职工的姓名删除
cout << "请输入想要删除的职工名字:" << endl;
string name = "0";
cin >> name;
int index = this->IsExist(name);
if (index != -1)
{
//m_EmpNum = 1时不进入循坏
for (int i = 0; i < this->m_EmpNum - 1; ++i)
{
//数据前移
this->m_EmpArry[i] = this->m_EmpArry[i + 1];
}
//数量更新
this->m_EmpNum--;
//数据同步到文件中
this->Save();
cout << "删除成功!" << endl;
}
else
cout << "删除失败,未找到该职工!" << endl;
}
system("pause");
system("cls");
}
void WorkManager::Mod_Emp()
{
if (this->m_FileIsEmpty)
cout << "文件不存在或记录为空!" << endl;
else
{
cout << "请输入修改职工的名字:" << endl;
string name = "0";
cin >> name;
int ret = this->IsExist(name);
if (ret != -1)
{
//查找
delete this->m_EmpArry[ret];
int newId = 0;
string newName = "";
int dSelect = 0;
cout << "查到姓名为:" << name
<< "的职工,请输入新的职工号:" << endl;
cin >> newId;
cout << "请输入姓名:" << endl;
cin >> newName;
cout << "请选择该职工的岗位:" << endl
<< "1、普通职工" << endl
<< "2、经理" << endl
<< "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[ret] = worker;
cout << "修改成功!" << endl;
//保存到文件中
this->Save();
}
else
{
cout << "修改失败,查无此人" << endl;
}
}
system("pause");
system("cls");
}
void WorkManager::Find_Emp()
{
if (this->m_FileIsEmpty)
cout << "文件不存在或记录为空!" << endl;
else
{
cout << "请输入查找的方式:" << endl;
cout << "1.按职工编号查找" << endl;
cout << "2.按职工姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 2)
{
string name;
cout << "请输入查找的职工姓名:" << endl;
cin >> name;
int ret = 1;
ret = this->IsExist(name);
if (ret != -1)
{
cout << "查找成功!该职工信息如下:" << endl;
this->m_EmpArry[ret]->showInfo();
}
else
cout << "查找失败,查无此人" << endl;
}
else if (select == 1)
{
int id = 0;
cout << "请输入查找的职工编号:" << endl;
cin >> id;
//加入判断是否找到的标志
bool flag = false; //默认找不到
for (int i = 0; i < this->m_EmpNum; ++i)
{
if (this->m_EmpArry[i]->m_ID == id)
{
cout << "查找成功,职工编号为:"
<< this->m_EmpArry[i]->m_ID
<< "号职工信息如下:" << endl;
flag = true;
this->m_EmpArry[i]->showInfo();
}
}
if (flag == false)
{
cout << "查找失败,查无此人!" << endl;
}
}
else
cout << "输入选项有误!" << endl;
}
system("pause");
system("cls");
}
void WorkManager::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 < m_EmpNum; ++i)
{
int minOrMax = i;
for (int j = i + 1; j < m_EmpNum; ++j)
{
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();//展示所有职工
}
}
void WorkManager::Clam_File()
{
cout << "确认清空?" << endl;
cout << "1. 确认" << endl;
cout << "2. 返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//trunc 如果存在文件 则删除并重建
ofstream ofs("系统.txt", ios::trunc);
ofs.close();
//清空内容
if (this->m_EmpArry != NULL)
{
for (int i = 0; i < this->m_EmpNum; ++i)
{
if (this->m_EmpArry[i] != NULL)
delete this->m_EmpArry[i];
}
this->m_EmpNum = 0;
delete[]this->m_EmpArry;
this->m_EmpArry = NULL;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
main函数
#include<iostream>
using namespace std;
#include"管理类.h"
#include"worker.h"
#include"employee.h"
//提供功能接口
int main()
{
WorkManager wm;
int choice = 0;
while (true)
{
wm.Show_Menu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice)
{
case 0: //退出系统
wm.exitSystem();
break;
case 1: //添加职工
wm.Add();
break;
case 2: //显示职工
wm.Show_Emp();
break;
case 3: //删除职工
//测试 要封装
//{int ret = wm.IsExist("1");
//if (ret != -1)
// cout << "职工存在" << endl;
//else
// cout << "职工不存在" << endl;
//}
wm.Del_Emp();
break;
case 4: //修改职工
wm.Mod_Emp();
break;
case 5: //查找职工
wm.Find_Emp();
break;
case 6://排序
wm.Sort_Emp();
break;
case 7: //清空文件
wm.Clam_File();
break;
default:
system("cls"); // !!!(输入其他的)刷新屏幕
break;
}
}
return 0;
}