一、介绍
程序为对一个公司的所有职工信息管理系统,程序提供了对于职工信息的增删改排等功能。并使用多态的技术,大大提高了程序的维护性和扩展性。
二、功能详述
主要工作类:WorkerManager
交互界面——showmenu:显示对职工的增删改查操作选项 文件交互
void WorkerManager::showmenu() {
cout << endl;
cout << "\t" << "\t" << "欢迎使用职工管理系统!" << endl;
cout << "\t" << "\t" << "0 退出管理系统" << endl;
cout << "\t" << "\t" << "1 增加职工信息" << endl;
cout << "\t" << "\t" << "2 显示职工信息" << endl;
cout << "\t" << "\t" << "3 删除离职职工" << endl;
cout << "\t" << "\t" << "4 修改职工信息" << endl;
cout << "\t" << "\t" << "5 查找职工信息" << endl;
cout << "\t" << "\t" << "6 按照编号排序" << endl;
cout << "\t" << "\t" << "7 清空所有文档" << endl;
cout << endl;
}
利用多态分类添加职工
首先建立抽象类 Worker:
三个公共属性:职工编号、职工姓名、部门编号
两个纯虚函数:showinof、getdeptname
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Worker
{
public:
virtual void showinof() = 0;//显示信息
virtual string getdeptname() = 0;//获取部门信息
int m_id;
string m_name;
int m_deptid;
};
建立老板、经理、普通员工三个类,并分别都继承Worker
并重写纯虚函数,添加构造函数初始化三个属性
用一个数组保存指向每一个职工对象的Worker指针,并在WorkerManager中创建数组元素个数、数组指针两个成员变量
在构造函数中初始化两个变量
WorkerManager::WorkerManager() {
//文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (! ifs.is_open())
{
//cout << "文件不存在" << endl;
this->m_number = 0;
this->m_worker = NULL;
this->m_fileEmpty = true;
ifs.close();
return;
}
//文件存在且为空
char c;
ifs >> c;
if (ifs.eof())
{
//cout << "文件为空" << endl;
this->m_number = 0;
this->m_worker = NULL;
this->m_fileEmpty = true;
ifs.close();
return;
}
//文件存在但不为空
this->m_number = this->get_empNum();
this->m_worker = new Worker * [this->m_number];
this->init_filearry();
}
建立txt文件保存职员信息
在WorkerManager中设置判断文件是否为空的成员属性m_fileEmpty、读文件成员函数get_empNum、保存文件成员函数save
void WorkerManager::save() {
ofstream ofs;
ofs.open(FILENAME, ios::out);
//写入数据
for (int i = 0; i < this->m_number; i++)
{
ofs << this->m_worker[i]->m_id << " "
<< this->m_worker[i]->m_name << " "
<< this->m_worker[i]->m_deptid << endl;
}
ofs.close();
}
int WorkerManager::get_empNum() {
ifstream ifs;
ifs.open(FILENAME, ios::in);
int num = 0;
int id;
string name;
int did;
while (ifs >> id && ifs >> name && ifs >> did)
{
++num;
}
ifs.close();
return num;
}
void WorkerManager::init_filearry() {
ifstream ifs;
ifs.open(FILENAME, ios::in);
int index = 0;
int id;
string name;
int did;
while (ifs >> id && ifs >> name && ifs >> did)
{
Worker* p2 = NULL;
switch (did)
{
case 1:
p2 = new Boss(id, name, did);
break;
case 2:
p2 = new Manager(id, name, did);
break;
case 3:
p2 = new employee(id, name, did);
}
this->m_worker[index] = p2;
index++;
}
this->m_number = index;
ifs.close();
}
显示职工信息:在WorkerManager中设置show_empinfo成员函数
批量增加职工:add函数
删除离职职工信息:
在WorkerManager中设置IsExist_emp成员函数根据输入的职工编号找到离职职工数组下标
设置delete_emp成员函数进行数据覆盖
修改职工信息:根据职工编号关键字查找所要修改信息的职工
排序:按照编号排序,管理员可以选择升序或降序方式。程序采用简单排序方法。
清空:当管理员确定选择清空文件时,文件内容被删除。成员属性被还原。
void WorkerManager::add() {
cout << "请输入要录入的职工人数:";
int val;
cin >> val;
if (val > 0)
{
int arrsize = m_number + val;
Worker** p = new Worker * [arrsize];
if (m_worker != NULL)
{
for (int i = 0; i < m_number; i++)
{
p[i] = m_worker[i];
}
}
//输入信息
for (int i = 0; i < val; i++)
{
int id;
string name;
int deptid;
cout << "请输入第" << i + 1 << "位职工编号:" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "位职工姓名:" << endl;
cin >> name;
cout << "请选择该职工的部门编号: 1、老板 2、经理 3、普通员工" << endl;
cin >> deptid;
Worker* p1 = NULL;
switch (deptid)
{
case 1:
p1 = new Boss(id, name, deptid);
break;
case 2:
p1 = new Manager(id, name, deptid);
break;
case 3:
p1 = new employee(id, name, deptid);
}
p[m_number + i] = p1;
}
delete[] m_worker;
m_worker = p;
m_number = arrsize;
this->save();
this->m_fileEmpty = false;
cout << "已成功录入" << val << "名新职工" << endl;
}
else
{
cout << "输入错误,请输入一个大于0的整数" << endl;
}
}
void WorkerManager::show_empinfo() {
if (this->m_fileEmpty == true)
{
cout << "系统中无任何职工信息!" << endl;
}
else
{
for (int i = 0; i < this->m_number; i++)
{
this->m_worker[i]->showinof();
}
}
}
int WorkerManager::IsExist_emp(int id) {
int index = -1;
for (int i = 0; i < this->m_number; i++)
{
if (this->m_worker[i]->m_id == id)
{
index = i;
}
}
return index;
}
void WorkerManager::delete_emp() {
if (this->m_fileEmpty)
{
cout << "文件不存在!" << endl;
}
else
{
int id;
cout << "请输入离职职工的职工编号:";
cin >> id;
if (this->IsExist_emp(id) != -1)
{
for (int i = this->IsExist_emp(id); i < this->m_number; i++)
{
this->m_worker[i] = this->m_worker[i + 1];
}
this->m_number--;
cout << "职工信息删除成功!" << endl;
this->save();
}
else
{
cout << "系统内无该职工编号,删除失败!" << endl;
}
}
}
void WorkerManager::modify_emp() {
cout << "请输入要修改的职工编号:";
int id;
cin >> id;
if (this->IsExist_emp(id) != -1)
{
int id;
string name;
int deptid;
cout << "请重新输入职工编号:" << endl;
cin >> id;
cout << "请重新输入职工姓名:" << endl;
cin >> name;
cout << "请重新选择该职工的部门编号: 1、老板 2、经理 3、普通员工" << endl;
cin >> deptid;
Worker* p1 = NULL;
switch (deptid)
{
case 1:
p1 = new Boss(id, name, deptid);
break;
case 2:
p1 = new Manager(id, name, deptid);
break;
case 3:
p1 = new employee(id, name, deptid);
}
this->m_worker[this->IsExist_emp(id)] = p1;
this->save();
cout << "修改成功!" << endl;
}
else
{
cout << "系统内无该职工编号!" << endl;
}
}
void WorkerManager::search_emp() {
cout << "请输入职工姓名:";
string name;
cin >> name;
bool exist = false;
if (this->m_fileEmpty)
{
cout << "系统内无任何职工信息!" << endl;
}
else
{
for (int i = 0; i < this->m_number; i++)
{
if (this->m_worker[i]->m_name == name)
{
exist = true;
cout << "查找成功!信息如下:" << endl;
this->m_worker[i]->showinof();
}
}
if (exist == false)
{
cout << "系统内无" << name << "信息" << endl;
}
}
}
void WorkerManager::sort_emp() {
if (this->m_fileEmpty)
{
cout << "文件不存在!" << endl;
}
else
{
int choice;
cout << "请选择排序方式:" << endl;
cout << "1 升序" << endl;
cout << "2 降序" << endl;
cin >> choice;
if (choice == 1)
{
for (int i = 0; i < this->m_number; i++)
{
int min = i;
for (int j = i + 1; j < this->m_number; j++)
{
if (this->m_worker[min]->m_id > this->m_worker[j]->m_id)
{
min = j;
}
}
Worker* p = NULL;
if (min != i)
{
p = this->m_worker[min];
this->m_worker[min] = this->m_worker[i];
this->m_worker[i] = p;
}
}
save();
cout << "升序已完成!" << endl;
}
else if (choice == 2)
{
for (int i = 0; i < this->m_number; i++)
{
int max = i;
for (int j = i + 1; j < this->m_number; j++)
{
if (this->m_worker[max]->m_id < this->m_worker[j]->m_id)
{
max = j;
}
}
Worker* p = NULL;
if (max != i)
{
p = this->m_worker[max];
this->m_worker[max] = this->m_worker[i];
this->m_worker[i] = p;
}
}
save();
cout << "降序已完成!" << endl;
}
else
{
cout << "输入错误,请选择输入1或2!" << endl;
sort_emp();
}
}
}
void WorkerManager::destory_file() {
if (this->m_fileEmpty)
{
cout << "文件不存在!" << endl;
}
else
{
cout << "是否确定清空文件" << endl;
cout << "1 确定 2 返回" << endl;
int choice;
cin >> choice;
if (choice == 1)
{
ofstream ofs;
ofs.open(FILENAME, ios::trunc);
ofs.close();
if (this->m_worker != NULL)
{
for (int i = 0; i < this->m_number; i++) {
delete this->m_worker[i];
this->m_worker[i] = NULL;
}
delete[] this->m_worker;
this->m_worker = NULL;
this->m_number = 0;
this->m_fileEmpty = true;
}
cout << "清空成功!" << endl;
}
}
system("pause");
system("cls");
}
三、完整代码
http://链接:https://pan.baidu.com/s/1_9_u62McABtwXDRSidbaTA?pwd=8hpo 提取码:8hpo
四、结尾
如有bug,可评论区留言!谢谢!