C++ 管理系统实战
1. 目录结构
2. workClass.h
#include "workManager.h"
class Employee : public Work{
public:
Employee(int id, string name, int dept){
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dept;
}
void showWorkerInfo() override {
cout << "职工编号: " << this->m_Id
<< "\t职工姓名: " << this->m_Name
<< "\t岗位信息: " << this->getDeptName()
<< "\t岗位职责: " << this->workInfo << endl;
}
string getDeptName() override {
return "总裁";
}
private:
string workInfo = "管理公司所有事务";
};
class Manager : public Work{
public:
Manager(int id, string name, int dept){
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dept;
}
void showWorkerInfo() override {
cout << "职工编号: " << this->m_Id
<< "\t职工姓名: " << this->m_Name
<< "\t岗位信息: " << this->getDeptName()
<< "\t岗位职责: " << this->workInfo << endl;
}
string getDeptName() override {
return "经理";
}
private:
string workInfo = "管理员工";
};
class Boos : public Work{
public:
Boos(int id, string name, int dept){
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dept;
}
void showWorkerInfo() override {
cout << "职工编号: " << this->m_Id
<< "\t职工姓名: " << this->m_Name
<< "\t岗位信息: " << this->getDeptName()
<< "\t岗位职责: " << this->workInfo << endl;
}
string getDeptName() override {
return "员工";
}
private:
string workInfo = "工作";
};
3. workFunction.h
#include "workManager.h"
#include "workClass.h"
#define FILEPATH "workManager/work.txt"
void WorkManager::addWorkInfo() {
cout << "请输入添加职工的数量: " << endl;
int addMuch = 0;
cin >> addMuch;
if(addMuch > 0){
int newSize = this->m_EmpNum + addMuch;
Work **newSpace = new Work * [newSize];
if (this->pWorkArray != NULL){
for (int i = 0; i < this->m_EmpNum; ++i) {
newSpace[i] = this->pWorkArray[i];
}
}
for (int i = 0; i < addMuch; ++i) {
int id;
string name;
int dSelect;
cout << "请输入第" << i+1 << "个员工职工的ID: " << endl;
cin >> id;
cout << "请输入第" << i+1 << "职工姓名: " << endl;
cin >> name;
cout << "请选择岗位职位: " << endl;
cout << "1、普通员工: " << endl;
cout << "2、经理: " << endl;
cout << "3、老板: " << endl;
cin >> dSelect;
Work *work = NULL;
switch (dSelect) {
case 1:
work = new Employee(id, name, 1);
break;
case 2:
work = new Manager(id, name, 2);
break;
case 3:
work = new Boos(id, name, 3);
break;
default:
cout << "输入有误!" << endl;
break;
}
newSpace[this->m_EmpNum + i] = work;
}
delete [] this->pWorkArray;
this->pWorkArray = newSpace;
this->m_EmpNum = newSize;
this->fileIsHave = false;
WorkManager::saveFile();
cout << "你已成功添加 " << addMuch << " 名职工!" << endl;
}else{
cout << "输入有误!" << endl;
}
system("pause");
system("cls");
}
void WorkManager::showWorkInfo() {
if(this->fileIsHave){
cout << "文件没有数据!" << endl;
return;
}
for (int i = 0; i < this->m_EmpNum; ++i) {
for (int j = 0; j < i; ++j) {
Work *work = NULL;
if (this->pWorkArray[i]->m_Id < this->pWorkArray[j]->m_Id){
work = this->pWorkArray[i];
this->pWorkArray[i] = this->pWorkArray[j];
this->pWorkArray[j] = work;
}
}
}
for (int i = 0; i < this->m_EmpNum; ++i) {
this->pWorkArray[i]->showWorkerInfo();
}
system("pause");
system("cls");
}
void WorkManager::saveFile() {
ofstream ofs;
ofs.open(FILEPATH, ios::out);
if(!ofs.is_open()){
cout << "文件不存在或者文件为空!" << endl;
return ;
}
for (int i = 0; i < this->m_EmpNum; ++i) {
ofs << this->pWorkArray[i]->m_Id << " "
<< this->pWorkArray[i]->m_Name << " "
<< this->pWorkArray[i]->m_DeptId << endl;
}
ofs.close();
}
int WorkManager::getFileMenNum() {
ifstream ifs;
ifs.open(FILEPATH, ios::in);
int id;
string name;
int dept;
int count=0;
while (ifs >> id && ifs >> name && ifs >> dept){
count++;
}
ifs.close();
return count;
}
void WorkManager::getFileWork() {
ifstream ifs;
ifs.open(FILEPATH, ios::in);
int id;
string name;
int dept;
int count=0;
while (ifs >> id && ifs >> name && ifs >> dept){
Work *work = NULL;
if (dept == 1){
work = new Employee(id, name, dept);
}else if (dept == 2){
work = new Manager(id, name, dept);
}else{
work = new Boos(id, name, dept);
}
this->pWorkArray[count] = work;
count++;
}
ifs.close();
}
void WorkManager::deleteFileWork() {
cout << "请选择删除方式: \n 1.按编号删除\n 2.按姓名删除" << endl;
int choose;
cin >> choose;
bool status = true;
if (choose == 1){
cout << "请输入员工编号: " << endl;
int id;
cin >> id;
for (int i = 0; i < this->m_EmpNum; i++) {
if (id == this->pWorkArray[i]->m_Id){
status = false;
}
if(!status){
this->pWorkArray[i]=this->pWorkArray[i+1];
}
}
if(status) {
cout << "你输入的 ID 不存在!" << endl;
}
}else if (choose == 2){
cout << "请输入员工姓名: " << endl;
string name;
cin >> name;
for (int i = 0; i < this->m_EmpNum; ++i) {
if (name == this->pWorkArray[i]->m_Name){
status = false;
}
if(!status){
this->pWorkArray[i]=this->pWorkArray[i+1];
}
}
if(status) {
cout << "你输入的姓名不存在!" << endl;
system("cls");
return;
}
}else{
cout << "你的选择有误,请重新选择!" << endl;
system("cls");
return;
}
if (!status){
this->pWorkArray[this->m_EmpNum-1] = NULL;
this->m_EmpNum--;
this->saveFile();
cout << "删除成功!" << endl;
system("pause");
system("cls");
}
}
void WorkManager::updateWorkInfo() {
if(this->fileIsHave) {
cout << "文件不存在或者为空!" << endl;
return;
}
int id;
cout << "请输入需要修改信息的 ID: " << endl;
cin >> id;
for (int i = 0; i < this->m_EmpNum; ++i) {
if(this->pWorkArray[i]->m_Id == id){
cout << "请输入需要修改的内容: \n 1.姓名\n 2.职业" << endl;
int choose;
cin >> choose;
switch (choose) {
case 1: {
cout << "请输入修改后的姓名: " << endl;
string name;
cin >> name;
pWorkArray[i]->m_Name = name;
cout << "修改成功!" << endl;
system("pause");
system("cls");
break;
}
case 2: {
cout << "请输入修改后的部门 ID: " << endl;
cout << "1、普通员工: " << endl;
cout << "2、经理: " << endl;
cout << "3、老板: " << endl;
int dept;
cin >> dept;
pWorkArray[i]->m_DeptId = dept;
cout << "修改成功!" << endl;
system("pause");
system("cls");
break;
}
default: {
cout << "选择错误!" << endl;
return;
}
}
}else{
cout << "未找到相关 ID 的信息" << endl;
return;
}
}
this->saveFile();
}
void WorkManager::findWorkInfo() {
int id;
cout << "请输入员工ID查询: " << endl;
cin >> id;
bool status = false;
for (int i = 0; i < this->m_EmpNum; ++i) {
if(this->pWorkArray[i]->m_Id == id){
this->pWorkArray[i]->showWorkerInfo();
status = true;
}
}
if(!status){
cout << "未查询到该 ID 信息" << endl;
system("pause");
system("cls");
}
}
void WorkManager::sortWorkInfo() {
for (int i = 0; i < this->m_EmpNum; ++i) {
for (int j = 0; j < i; ++j) {
Work * work = NULL;
if(this->pWorkArray[i]->m_Id > this->pWorkArray[j]->m_Id){
work = this->pWorkArray[i];
this->pWorkArray[i] = this->pWorkArray[j];
this->pWorkArray[j] = work;
}
}
}
this->saveFile();
}
void WorkManager::deleteAllWorkInfo() {
int enterDelete = 0;
cout << "1.确认删除 \t其他任意键撤销" << endl;
cin >> enterDelete;
if(enterDelete == 1){
this->pWorkArray = NULL;
this->m_EmpNum = 0;
this->saveFile();
cout << "清空所有信息成功!" << endl;
delete [] this->pWorkArray;
this->fileIsHave = true;
}
}
4. workManager.h
#include <bits/stdc++.h>
using namespace std;
class Work;
class WorkManager {
public:
WorkManager();
int m_EmpNum;
Work ** pWorkArray;
bool fileIsHave = false ;
void addWorkInfo();
void readFile();
int getFileMenNum();
void getFileWork();
void deleteFileWork();
void updateWorkInfo();
void findWorkInfo();
void sortWorkInfo();
void deleteAllWorkInfo();
void saveFile();
void showWorkInfo();
void showMenu();
void exitSystem();
~WorkManager();
};
class Work {
public:
int m_Id;
string m_Name;
int m_DeptId;
virtual void showWorkerInfo() = 0;
virtual string getDeptName() = 0;
};
5. workManager.cpp
#include "workManager.h"
#include <bits/stdc++.h>
#include "workClass.h"
#include "workFunction.h"
#define FILEPATH "workManager/work.txt"
using namespace std;
WorkManager::WorkManager() {
ifstream ifs;
ifs.open(FILEPATH, ios::in);
if(!ifs.is_open()){
this->fileIsHave = true;
this->m_EmpNum = 0;
this->pWorkArray = NULL;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof()){
this->fileIsHave = true;
this->m_EmpNum = 0;
this->pWorkArray = NULL;
ifs.close();
return;
}
cout << this->getFileMenNum() << endl;
this->m_EmpNum = this->getFileMenNum();
this->pWorkArray = new Work *[this->m_EmpNum];
this->getFileWork();
}
void WorkManager::exitSystem() {
cout << "欢迎下次使用......" << endl;
system("pause");
exit(0);
}
WorkManager::~WorkManager() {
if(this->pWorkArray!=NULL){
delete[] this->pWorkArray;
this->pWorkArray = NULL;
}
}
void WorkManager::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 test() {
Work *work = NULL;
work = new Employee(1,"张三", 1);
work->showWorkerInfo();
delete work;
work = new Manager(2, "李四", 2);
work->showWorkerInfo();
delete work;
work = new Boos(3, "王五", 3);
work->showWorkerInfo();
delete work;
}
int main() {
WorkManager wm;
char choose = '0';
while (true) {
wm.showMenu();
cout << "请输入你的选择: " << endl;
cin >> choose;
switch (choose) {
case '0':
wm.exitSystem();
break;
case '1':
wm.addWorkInfo();
break;
case '2':
wm.showWorkInfo();
break;
case '3':
wm.deleteFileWork();
break;
case '4':
wm.updateWorkInfo();
break;
case '5':
wm.findWorkInfo();
break;
case '6':
wm.sortWorkInfo();
break;
case '7':
wm.deleteAllWorkInfo();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
感谢观看祝你生活愉快