公司员工分为3种类型,老板,经理,普通员工,使用C++面向对象特性针对该3种类型员工进行抽象,实现对员工的基本管理,包括增、删、改、查等常规操作,并将数据保存到本地时间数据持久化。
************************************
********欢迎使用职工管理系统!********
********** 0. 退出管理程序 **********
********** 1. 增加职工信息 **********
********** 2. 显示职工信息 **********
********** 3. 删除职工信息 **********
********** 4. 修改职工信息 **********
********** 5. 查找职工信息 **********
********** 6. 按照编号排序 **********
********** 7. 清空所有信息 **********
************************************
输入对应操作后执行相应操作。
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Worker {
public:
int mId;
string mName;
int mDepId;
virtual void showInfo()=0;
virtual string getDepName()=0;
};
#pragma once
#include<iostream>
#include<string>
#include "worker.h"
using namespace std;
class Boss: public Worker{
public:
Boss(int id, string name, int dId);
Boss();
void showInfo() override;
string getDepName();
};
#include "boss.h"
string Boss::getDepName() {
return "经理";
}
void Boss::showInfo() {
cout << "职工编号:" << this->mId
<< "\t职工姓名:" << this->mName
<< "\t岗位:" << this->getDepName()
<< "\t岗位职责:管理公司所有的事务。" << endl;
}
Boss::Boss(int id, string name, int dId) {
this->mId = id;
this->mName = name;
this->mDepId = dId;
}
#pragma once
#include<iostream>
#include<string>
#include "worker.h"
using namespace std;
class Manager: public Worker{
public:
Manager(int id, string name, int dId);
Manager();
void showInfo() override;
string getDepName();
};
#include "manager.h"
string Manager::getDepName() {
return "经理";
}
void Manager::showInfo() {
cout << "职工编号:" << this->mId
<< "\t职工姓名:" << this->mName
<< "\t岗位:" << this->getDepName()
<< "\t岗位职责:完成老板交给的任务。" << endl;
}
Manager::Manager(int id, string name, int dId) {
this->mId = id;
this->mName = name;
this->mDepId = dId;
}
#pragma once
#include<iostream>
using namespace std;
#include "worker.h"
#include<string>
class Employee: public Worker{
public:
Employee(int id, string name, int dId);
void showInfo() override;
string getDepName() override;
};
#include "employee.h"
void Employee::showInfo() {
cout<<"职工编号:"<<this->mId
<<"\t职工姓名:"<<this->mName
<<"\t岗位:"<<this->getDepName()
<<"\t岗位职责:完成经理交给的任务。"<<endl;
}
string Employee::getDepName() {
return "员工";
}
Employee::Employee(int id, string name, int dId){
this->mId = id;
this->mName = name;
this->mDepId = dId;
}
#pragma once
#include<iostream>
#include "worker.h"
using namespace std;
class WorkerManager {
public:
WorkerManager();
~WorkerManager();
void showMenu();
void exitSystem();
void AddEmp();
static int getEmpNum();
void initEmp() const;
void save() const;
void showEmp();
void delEmp();
void modEmp();
void findEmp();
void sortEmp();
int isExist(int id);
void cleanFile();
int mEmpNum;
Worker **empArray;
bool mFileIsEmpty;
};
#include "workerManager.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
#include<fstream>
#define FILENAME "emp.txt"
WorkerManager::WorkerManager() {
ifstream ifs;
// 文件不存在
ifs.open(FILENAME, ios::in);
if (!ifs.is_open()) {
//cout << "文件不存在" << endl;
this->mEmpNum = 0;
this->empArray = nullptr;
this->mFileIsEmpty = true;
ifs.close();
return;
}
// 文件存在,但是数据为空
char ch;
ifs >> ch;
if (ifs.eof()) {
// cout << "文件存在,且为空" << endl;
this->mEmpNum = 0;
this->empArray = nullptr;
this->mFileIsEmpty = true;
ifs.close();
return;
}
// 当文件存在,且存在记录
int num = WorkerManager::getEmpNum();
// cout << "职工人数为:" << num << endl;
this->mEmpNum = num;
this->mFileIsEmpty = false;
this->empArray = new Worker *[this->mEmpNum];
this->initEmp();
/* for (int i = 0; i < this->mEmpNum; i++) {
cout << "员工编号:" << this->empArray[i]->mId << "姓名:" << this->empArray[i]->mName << "部门:"
<< this->empArray[i]->mDepId << endl;
}*/
}
WorkerManager::~WorkerManager() {
if (this->empArray != nullptr) {
delete[] this->empArray;
this->empArray = nullptr;
}
}
void WorkerManager::showMenu() {
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;
}
void WorkerManager::exitSystem() {
cout << "欢迎下次使用!" << endl;
exit(0);
}
void WorkerManager::AddEmp() {
cout << "请输入添加员工的数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0) {
int newSize = this->mEmpNum + addNum;
Worker **newSpace = new Worker *[newSize];
//将原空间中的数据,拷贝到新空间下
if (this->mEmpNum != 0) {
for (int i = 0; i < this->mEmpNum; i++) {
newSpace[i] = this->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;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> dSelect;
Worker *worker = nullptr;
switch (dSelect) {
case 1:
worker = new Employee(id, name, dSelect);
break;
case 2:
worker = new Manager(id, name, dSelect);
break;
case 3:
worker = new Boss(id, name, dSelect);
break;
default:
break;
}
newSpace[this->mEmpNum + i] = worker;
}
delete[] this->empArray;
this->empArray = newSpace;
this->mEmpNum = newSize;
this->mFileIsEmpty = false;
cout << "成功添加" << addNum << "名新员工" << endl;
this->save();
} else {
cout << "输入有误,请核实!" << endl;
}
system("clear");
}
void WorkerManager::save() const {
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < this->mEmpNum; i++) {
ofs << this->empArray[i]->mId << " "
<< this->empArray[i]->mName << " "
<< this->empArray[i]->mDepId << endl;
}
ofs.close();
}
int WorkerManager::getEmpNum() {
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 WorkerManager::initEmp() const {
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int dId;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> dId) {
Worker *worker = nullptr;
// 普通员工
if (dId == 1) {
worker = new Employee(id, name, id);
} else if (dId == 2) {
worker = new Manager(id, name, id);
} else if (dId == 3) {
worker = new Boss(id, name, id);
} else {
}
this->empArray[index] = worker;
index++;
}
ifs.close();
}
void WorkerManager::showEmp() {
if (this->mFileIsEmpty) {
cout << "文件不存在或者为空!" << endl;
} else {
for (int i = 0; i < mEmpNum; i++) {
this->empArray[i]->showInfo();
}
}
system("clear");
}
void WorkerManager::delEmp() {
if (this->mFileIsEmpty) {
cout << "文件不存在或记录为空" << endl;
} else {
cout << "请输入要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->isExist(id);
if (index != -1) {
for (int i = index; i < this->mEmpNum - 1; i++) {
this->empArray[i] = this->empArray[i + 1];
}
this->mEmpNum--;
this->save();
cout << "删除成功!" << endl;
} else {
cout << "删除失败,未找到该职工!" << endl;
}
}
system("clear");
}
int WorkerManager::isExist(int id) {
int index = -1;
for (int i = 0; i < this->mEmpNum; i++) {
if (this->empArray[i]->mId == id) {
index = i;
break;
}
}
return index;
}
void WorkerManager::modEmp() {
if (this->mFileIsEmpty) {
cout << "文件不存在或者记录为空!" << endl;
} else {
cout << "请输入修改职工的编号:" << endl;
int id;
cin >> id;
int ret = this->isExist(id);
if (ret != -1) {
delete this->empArray[ret];
int newId = 0;
string newName = "";
int dSelect = 0;
cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
cin >> newId;
cout << "请输入新的姓名:" << endl;
cin >> newName;
cout << "请输入岗位:" << endl;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> dSelect;
Worker *worker = nullptr;
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->empArray[ret] = worker;
cout << "员工信息修改成功!" << endl;
this->save();
} else {
cout << "查无此人!修改失败!" << endl;
}
}
system("clear");
}
void WorkerManager::findEmp() {
if (this->mFileIsEmpty) {
cout << "文件不存在或者记录为空!" << endl;
} else {
cout << "请输入查找的方式:" << endl;
cout << "1. 按照职工的编号查找" << endl;
cout << "2. 按照职工的姓名查找" << endl;
int select = 0;
cin >> select;
bool flag = false;
switch (select) {
case 1: {
int id;
cout << "请输入员工的ID!" << endl;
cin >> id;
int ret = isExist(id);
if (ret != -1) {
cout << "查找成功!改职工的信息如下:" << endl;
this->empArray[ret]->showInfo();
}
flag = true;
break;
}
case 2: {
string name;
cout << "请输入员工的姓名:" << endl;
cin >> name;
for (int i = 0; i < this->mEmpNum; i++) {
if (this->empArray[i]->mName == name) {
cout << "查找成功,该员工的编号为:" << this->empArray[i]->mId << ",信息如下" << endl;
flag = true;
this->empArray[i]->showInfo();
}
}
break;
}
default:
cout << "查找方式不正确!" << endl;
break;
}
if (!flag) {
cout << "查找失败!" << endl;
}
}
}
void WorkerManager::sortEmp() {
if (this->mFileIsEmpty) {
cout << "文件不存在或者为空!" << endl;
system("clear");
} else {
cout << "请输入排序的方式:" << endl;
cout << "1. 按照职工的编号升序" << endl;
cout << "2. 按照职工的编号降序" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < this->mEmpNum; i++) {
int minOrMax = i;
for (int j = i + 1; j < this->mEmpNum; j++) {
if (select == 1) {
if (this->empArray[i]->mId > this->empArray[j]->mId) {
minOrMax = j;
}
} else {
if (this->empArray[i]->mId < this->empArray[j]->mId) {
minOrMax = j;
}
}
}
if (i != minOrMax) {
Worker *temp = this->empArray[i];
this->empArray[i] = this->empArray[minOrMax];
this->empArray[minOrMax] = temp;
}
}
cout << "排序成功!排序后的结果为:" << endl;
this->save();
this->showEmp();
}
}
void WorkerManager::cleanFile() {
cout << "确定清空文件?" << endl;
cout << "Y/y. 确定" << endl;
cout << "N/n. 返回" << endl;
char select = 0;
cin >> select;
switch (select) {
case 'Y' :
case 'y' : {
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->empArray != nullptr) {
for (int i = 0; i < this->mEmpNum; i++) {
delete this->empArray[i];
this->empArray[i] = nullptr;
}
delete[] this->empArray;
this->empArray = nullptr;
this->mEmpNum = 0;
this->mFileIsEmpty = true;
}
cout << "清空成功!" << endl;
system("clear");
break;
}
case 'N' :
case 'n' : {
break;
}
default:
cout << "输入无效!";
break;
}
}
#include<iostream>
using namespace std;
#include "workerManager.h"
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
int main() {
WorkerManager wm;
int choice = 0;
while (true) {
wm.showMenu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice) {
case 0:
wm.exitSystem();
break;
case 1:
wm.AddEmp();
break;
case 2:
wm.showEmp();
break;
case 3:
wm.delEmp();
break;
case 4:
wm.modEmp();
break;
case 5:
wm.findEmp();
break;
case 6:
wm.sortEmp();
break;
case 7:
wm.cleanFile();
break;
default:
system("clear");
break;
}
}
}
3.1 本次实战主要实践了C++多态的特性; 3.2 文件操作; 3.3 指向指针的指针。