职工管理系统

职工管理系统的需求分析:

1.

公司职工分为三类: 普通员工,经理,老板,显示信息时,需要显示职工编号,职工岗位,部门编号,以及职责。

2.

管理系统中需要实现的功能如下

  • 退出管理程序:退出当前的管理系统。
  • 增加职工信息:实现批量添加职工,将信息录入到文件当中,职工信息为:职工编号,职工姓名,部门编号。
  • 显示职工信息:显示公司内部所有的员工信息。
  • 删除离职职工:按照编号删除职工。
  • 修改职工信息:按照编号修改职工信息。
  • 查找职工信息:按照职工编号或职工姓名查找职工信息。
  • 按照编号排序:按照职工的编号对职工进行升序或降序排列。
  • 清空所以文档:清空文件中的所有职工信息。(清空前需确认是否删除,防止误删。)

实现

1. 创建worker类,之后普通员工、经理、老板类均基于此实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include <iostream>
using namespace std;

class worker
{
public:
// 显示信息
virtual void showinfo () = 0;
// 获取岗位名称
virtual string getdepidname () = 0;

int m_id;// 职工编号

string m_name ; // 职工姓名

int m_depid ; // 部门编号
};

2.分别创建普通职工、经理、老板类
普通职工头文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include <iostream>
#include "worker.h"
using namespace std;

class employee :public worker
{
public:
// 构造函数
employee(int id,string name,int depid );
// 显示信息
virtual void showinfo () ;
// 获取岗位名称
virtual string getdepidname () ;
};
普通职工
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "employee.h"
#include "worker.h"
using namespace std;

// 构造函数
employee::employee(int id,string name,int depid )
{
this->m_id = id;
this->m_depid = depid;
this->m_name = name;
}
// 显示信息
void employee::showinfo()
{
cout<<"这个员工的编号为: "<<this->m_id<<"\t";
cout<<"这个员工的名字为: "<<this->m_name<<"\t";
cout<<"这个员工的部门为: "<<getdepidname()<<"\t";
cout<<"这个员工的职责是: \"完成经理交给的任务\""<<endl;

}
// 获取岗位名称
string employee::getdepidname ()
{
return string("员工");
}

经理、老板类 类似
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include <iostream>

#include "worker.h"

class boss:public worker
{
public:
// 构造函数
boss (int id,string name,int depid );
// 显示信息
virtual void showinfo ();
// 获取岗位名称
virtual string getdepidname () ;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include "boss.h"
#include "worker.h"

// 构造函数
boss::boss (int id,string name,int depid )
{
this->m_id = id;
this->m_depid = depid;
this->m_name = name;
}
// 显示信息
void boss::showinfo ()
{
cout<<"这个员工的编号为: "<<this->m_id<<"\t";
cout<<"这个员工的名字为: "<<this->m_name<<"\t";
cout<<"这个员工的部门为: "<<getdepidname()<<"\t";
cout<<"这个员工的职责是: \"安排给经理任务\""<<endl;
}
// 获取岗位名称
string boss::getdepidname ()
{
return string("老板");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include "worker.h"
#include <iostream>

class manager :public worker
{
public:
// 构造函数
manager (int id,string name,int depid );
// 显示信息
virtual void showinfo ();
// 获取岗位名称
virtual string getdepidname () ;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include "manager.h"
#include "worker.h"

// 构造函数
manager::manager(int id, string name, int depid)
{
this->m_id = id;
this->m_depid = depid;
this->m_name = name;
}
// 显示信息
void manager::showinfo()
{
cout<<"这个员工的编号为: "<<this->m_id<<"\t";
cout<<"这个员工的名字为: "<<this->m_name<<"\t";
cout<<"这个员工的部门为: "<<getdepidname()<<"\t";
cout<<"这个员工的职责是: \"完成老板下发的任务,并分配给员工任务。\""<<endl;
}
// 获取岗位名称
string manager::getdepidname()
{
return string("经理");
}

3.创建workerManager类,之后函数的调用均有此类对象实现
workerManager头文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#include <iostream>
#include "worker.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
#include <fstream>
#define FILENAME "empFile.txt"

using namespace std;

class workmanager
{
public:
workmanager ();
// 展示菜单
void shoumenu ();

// 退出系统
void exitsystem ();

// 记录职工数
int m_num;

// 职工数组指针
worker ** m_emparray;

// 添加员工
void addemp();

// 写进文件
void save();

// 判断文件是否存在
bool m_fileisempty;

// 得到员工的数量
int get_num();

// 初始化职工
void init_emp();

// 显示员工信息
void show_emp();

// 删除职工
void del_emp();

// 判断员工是否存在
int is_exist(int id);

// 修改职工信息
void mod_emp();

// 查找员工信息
void find_emp();

// 对员工编号进行排序
void sort_emp();

// 清空文件
void clean_file();


~workmanager();
};

功能实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include "workManager.h"

// 构造函数
workmanager::workmanager()
{
ifstream ifs;
ifs.open(FILENAME,ios::in);
if ( !ifs.is_open() )// 如果文件不存在
{
// cout<<"文件不存在"<<endl;
// 初始化
this->m_emparray = NULL;
this->m_num = 0;
this->m_fileisempty = true;
ifs.close();
return ;
}
// 文件存在
// 1. 文件为空
char ch;
ifs>>ch;
if (ifs.eof())
{
// cout<<"文件为空"<<endl;
// 初始化
this->m_emparray = NULL;
this->m_num = 0;
this->m_fileisempty = true;
ifs.close();
return ;
}

// 当文件存在且存在数据
int num = this->get_num();
// 测试当前员工数量
//cout<<"当前的员工数量为 :"<<num<<endl;
// 可行
this->m_num = num;

// 存储
// 开辟空间 将文件中的数据存到数组中
this->m_emparray = new worker*[this->m_num+1];
init_emp();

// 测试
/*for (int i = 0; i < this->m_num; i++)
{
cout<<"员工编号: "<<this->m_emparray[i]->m_id<<" ";
cout<<"员工姓名: "<<this->m_emparray[i]->m_name<<" ";
cout<<"部门编号: "<<this->m_emparray[i]->m_depid<<endl;
}*/
// 测试成功

}

// 展示菜单
void workmanager::shoumenu (){
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::exitsystem()
{
cout<<"欢迎下次使用"<<endl;
exit(0);
}

// 添加员工
void workmanager::addemp()
{
cout<<"请输出添加职工的数量: "<<endl;
int addnum = 0;
cin >> addnum;
if (addnum > 0)
{
// 计算当前下的空间大小
int newsize = this->m_num + addnum ;
// 开辟一个所需要的大小
worker ** newspace = new worker*[newsize];
if (this->m_emparray != NULL)
{
for (int i = 0; i < this->m_num; 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<<
"1 . 普通员工"<<endl<<
"2 . 经理 "<<endl<<
"3 . 老板 "<<endl;
cin>>dselect;
// 不明确具体的职位 利用多态
worker * lworker = NULL;
switch (dselect)
{
case(1):
{
lworker = new employee(id,name,dselect);
break;
}
case(2):
{
lworker = new manager(id,name,dselect);
break;
}
case(3):
{
lworker = new boss(id,name,dselect);
break;
}
default:
break;
}
newspace[this->m_num+i] = lworker;

}
// 释放掉原来的数组空间
delete [] this->m_emparray;

// 把新的数组指针赋给原来得
this->m_emparray = newspace;

// 更新新的员工数量
this->m_num = newsize ;

// 更新职工文件情况
this->m_fileisempty = false;
cout << "成功添加"<< addnum << "名员工" << endl;
this->save();
}
else
{
cout << "输入有误" << endl ;
}

}

// 写进文件
void workmanager::save()
{
ofstream ofs;
ofs.open(FILENAME,ios::out);// 用输出的方式打开文件 即写文件
// 将每个人的信息写入到文件
for (int i = 0; i < this->m_num; i++)
{
ofs<<this->m_emparray[i]->m_id<<"\t";//职工编号
ofs<<this->m_emparray[i]->m_name<<"\t";//职工姓名
ofs<<this->m_emparray[i]->m_depid<<endl;//职工部门编号
}
// 关闭文件
ofs.close();
}

// 得到员工的数量
int workmanager::get_num()
{
ifstream ifs;
ifs.open(FILENAME,ios::in);
int id;
string name;
int depid;
int num = 0; // 记录员工数量
while (ifs >> id && ifs >> name && ifs >> depid)
{
num++;
}

ifs.close();

return num;
}

// 初始化职工
// 文件中的员工写入到数组中
void workmanager::init_emp()
{
ifstream ifs;
ifs.open(FILENAME,ios::in);

int id;
string name;
int depid;

int index = 0;
while (ifs >> id && ifs >> name && ifs >> depid)
{
worker * lworker = NULL;
switch (depid)
{
case 1:
{
lworker = new employee(id,name,depid);
}
case 2:
{
lworker = new manager (id,name,depid);
}
case 3:
{
lworker = new boss (id,name,depid);
}
default:
break;

}
this->m_emparray[index] = lworker;
index++;
}

// 关闭文件
ifs.close();
}

// 显示员工信息
void workmanager::show_emp()
{
if (this->m_fileisempty||this->m_num==0) // 默认文件为空 true
{
cout<<"没有员工 !"<<endl;
}
else
{
for (int i = 0; i < this->m_num; i++)
{
// 利用多态
this->m_emparray[i]->showinfo();
}
}
}

// 删除职工
void workmanager::del_emp()
{

if (this->m_fileisempty)
{
cout<<"文件为空!"<<endl;
}
else // 文件存在
{
int id;
cout<<"请输入需要删除的员工编号: "<<endl;

cin>>id;
int index = this->is_exist(id);
if (index == -1)
{
cout<<"删除失败,员工编号不存在!"<<endl;
}
else
{
for (int i = index; i < this->m_num - 1; i++)
{
// 将数据前移
this->m_emparray[i] = this->m_emparray[i+1];
}
this->m_num--;

// 文件中的数据同步
this->save();
cout<<"删除成功"<<endl;
}
}

}

// 判断员工是否存在
int workmanager::is_exist(int id)
{
int index = -1;
for (int i = 0; i < this->m_num; i++)
{
if (id == this->m_emparray[i]->m_id)
{
index = i;
break;

}
}
return index;
}

// 修改职工信息
void workmanager::mod_emp()
{
// 判断文件是否存在
if (this->m_fileisempty||this->m_num == 0)
{
cout<<"没有员工可修改!"<<endl;
}
else
{
cout<<"请输入需要修改的员工编号:"<<endl;
int id;
cin>>id;
int ret = this->is_exist(id);
if (ret != -1)
// 员工存在
{
// 删除掉原来该位置上的信息
delete this->m_emparray[ret];
int lid = 0;
string name = "";
int depid = 0;
worker* lworker = NULL;
cout<<"修改后的编号为:"<<endl;
cin>>lid;
cout<<"修改后的姓名为:"<<endl;
cin>>name;
cout<<"修改后的部门标号为:"<<endl;
cout<<"1. 普通员工"<<endl;
cout<<"2. 经理 "<<endl;
cout<<"1. 老板 "<<endl;
cin>>depid;
switch (depid)
{
case 1:
{
lworker = new employee(lid,name,depid);
break;
}
case 2:
{
lworker = new manager(lid,name,depid);
break;
}
case 3:
{
lworker = new boss (lid,name,depid);
break;
}

{
default:
break;
}
}
this->m_emparray[ret] = lworker;
this->save();
cout<<"修改成功!"<<endl;
}
else
{
cout<<"编号不存在!"<<endl;
}
}
}

// 查找员工信息
void workmanager::find_emp()
{
if (this->m_fileisempty || this->m_num == 0)
{
cout<<"无任何员工信息"<<endl;
}
else
// 有员工
{
int a;
cout<<"请输入查找的方式:"<<endl;
cout<<"1. 按编号查找"<<endl;
cout<<"2. 按姓名查找"<<endl;
cin>>a;
if (a == 1)
{
int id;
cout<<"请输入查找的编号:"<<endl;
cin>>id;
int ret = this->is_exist(id);
if (ret == -1)
{
cout<<"员工编号不存在"<<endl;
}
else
{
cout<<"编号:"<<this->m_emparray[ret]->m_id<<"\t"
<<"姓名:"<<this->m_emparray[ret]->m_name<<"\t"
<<"部门编号:"<<this->m_emparray[ret]->m_depid<<endl;
}
}
else if( a==2 )
{
string name;
cout<<"请输入需要查找的姓名:"<<endl;
cin>>name;
for (int i = 0; i < this->m_num; i++)
{
if (name == this->m_emparray[i]->m_name)
{
cout<<"编号:"<<this->m_emparray[i]->m_id<<"\t"
<<"姓名:"<<this->m_emparray[i]->m_name<<"\t"
<<"部门编号:"<<this->m_emparray[i]->m_depid<<endl;
break;
}
else
{
cout<<"查找的员工不存在!"<<endl;
}

}

}
else
{
cout<<"请重新输入!"<<endl;
}

}
}

// 对员工编号进行排序
void workmanager::sort_emp()
{
// 1. 选择排序
if (this->m_fileisempty || this->m_num == 0)
{
cout<<"没有员工"<<endl;
}
else
{
cout<<"请选择排序的方式:"<<endl;
cout<<"1. 升序"<<endl;
cout<<"2. 降序"<<endl;
int select = 0;
cin>>select;

for(int i = 0 ; i<this->m_num-1;i++)
{
int mindex = i;// 初始化最小的坐标为i
for (int j = i + 1; j < this->m_num; j++)
{
if (select == 1)// 升序:则每一次选出来的数是最小的
{

if(this->m_emparray[j]->m_id < this->m_emparray[mindex]->m_id)
{
mindex = j;
continue;
}
}
if (select == 2)// 降序: 即每一次选出来的数是最大的

{
if(this->m_emparray[j]->m_id > this->m_emparray[mindex]->m_id)
{
mindex = j;
continue;
}
}
}
if (i != mindex) // 交换顺序
{
// 创建一个中间变量
worker * ltemp = this->m_emparray[i];
this->m_emparray[i] = this->m_emparray[mindex];
this->m_emparray[mindex] = ltemp;
}
}
cout<<"排序成功!"<<endl;
this->save();
this->show_emp();
}
}

// 清空文件
void workmanager::clean_file()
{
cout<<"是否确定清空数据?"<<endl;
cout<<"1. 确定 "<<endl;
cout<<"2. 返回 "<<endl;
int select = 0;
cin>>select;
if (select == 1)// 确定清空文件
{
// 文件清除
ofstream ofs(FILENAME,ios::trunc);
// 打开模式 ios::trunc 如果存在 删除并重新创建
ofs.close();

// 数组数据删除
if (this->m_emparray != NULL)
{
for (int i = 0; i < this->m_num; i++)
{
if (this->m_emparray[i] != NULL)
{
delete this->m_emparray[i];
}
}
this->m_num = 0;
delete[] this->m_emparray;
this->m_emparray = NULL;
this->m_fileisempty = true;
// 为空
}
cout<<"清除成功!"<<endl;


}
}


workmanager::~workmanager()
{
if (this->m_emparray != NULL)
{
for (int i = 0; i < this->m_num; i++)
{
if (this->m_emparray[i] != NULL)
{
delete this->m_emparray[i];
}
}
delete []this->m_emparray;
this->m_emparray = NULL;
}
}

系统实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>

#include <stdlib.h>

#include "workManager.h"
#include "worker.h"
#include "employee.h"

using namespace std;



int main()
{
workmanager wm;
int choice = 0;
while (1)
{
wm.shoumenu();
cout<<"请输入你的选择: "<<endl;
cin>>choice;
switch (choice)
{
case(0):// 退出
{
wm.exitsystem();
break ;
}
case(1):// 添加
{
wm.addemp();
break ;
}
case(2):// 显示
{
wm.show_emp();
break ;
}
case(3):// 删除
{
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.clean_file();
break ;
}
default:
cout<<"输入的数字有误,请重新输入!"<<endl;
break;
}
system("pause");
system("cls");
}

system("pause");

return 0;
}

实现!