职工管理系统
职工管理系统的需求分析: 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() ) { this ->m_emparray = NULL ; this ->m_num = 0 ; this ->m_fileisempty = true ; ifs.close(); return ; } char ch; ifs>>ch; if (ifs.eof()) { this ->m_emparray = NULL ; this ->m_num = 0 ; this ->m_fileisempty = true ; ifs.close(); return ; } int num = this ->get_num(); this ->m_num = num; this ->m_emparray = new worker*[this ->m_num+1 ]; init_emp(); } 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 ) { 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 () { 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; 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) ; 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 ; }
实现!