面向对象编程自学笔记整理
十三、面向对象编程综合案例:职工信息管理系统
职工管理系统制作.cpp
#include<iostream>
#include"workerManger.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
using namespace std;
void test01()
{
worker * worker = NULL;
worker = new Employee(1, "张三", 1);
worker->showInfo();
delete worker;
worker = new Manager(2, "李四", 2);
worker->showInfo();
delete worker;
worker = new Boss(3, "王五", 3);
worker->showInfo();
delete worker;
}
int main()
{
workerManger wkmaner;
int choice = 0;
while (true)
{
wkmaner.Show_Menu();
cout << "请输入您的选择:";
cin >> choice;
switch (choice)
{
case 0:
wkmaner.exitSystem();
break;
case 1:
wkmaner.Add_emp();
break;
case 2:
wkmaner.show_emp();
break;
case 3:
wkmaner.Del_emp();
break;
case 4:
wkmaner.Mod_emp();
break;
case 5:
wkmaner.Find_emp();
break;
case 6:
wkmaner.Sort_emp();
break;
case 7:
wkmaner.Clean_file();
break;
default:
break;
}
}
return 0;
}
workerManger.cpp
#include"workerManger.h"
workerManger::workerManger()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
this->m_Empnum = 0;
this->m_Fileisempty = true;
this->m_Emparry = NULL;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
this->m_Empnum = 0;
this->m_Fileisempty = true;
this->m_Emparry = NULL;
ifs.close();
return;
}
int num = this->get_Empnum();
this->m_Empnum = num;
this->m_Emparry = new worker * [this->m_Empnum]; \
init_emp();
for (int i = 0; i < m_Empnum; i++)
{
}
}
void workerManger::Show_Menu()
{
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 workerManger::exitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
void workerManger::Add_emp()
{
cout << "请输入添加职工数目:" ;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
int newSize = this->m_Empnum + addNum;
worker** newspace = new worker * [newSize];
if (this->m_Emparry != NULL)
{
for (int i = 0; i < this->m_Empnum; i++)
{
newspace[i] = this->m_Emparry[i];
}
}
for (int i = 0; i < addNum; i++)
{
int id;
string name;
int dselect;
cout << "请输入" << i + 1 << "个新员工编号:";
cin >> id;
cout << "请输入" << i + 1 << "个新职工姓名:";
cin >> name;
cout << "请选择该职工岗位:" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.总裁" << endl;
cin >> dselect;
worker* Worker =NULL;
switch (dselect)
{
case 1:
Worker = new Employee(id, name, 1);
break;
case 2:
Worker = new Manager(id, name, 2);
break;
case 3:
Worker = new Boss(id, name, 3);
break;
default:
break;
}
newspace[this->m_Empnum + i] = Worker;
}
delete[] this->m_Emparry;
this->m_Emparry = newspace;
this->m_Empnum = newSize;
this->m_Fileisempty = false;
cout << "成功添加" << addNum << "名新员工!" << endl;
this->save();
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
system("pause");
system("cls");
}
void workerManger::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < this->m_Empnum;i++)
{
ofs << this->m_Emparry[i]->m_ID << " "
<< this->m_Emparry[i]->m_name << " "
<< this->m_Emparry[i]->m_deptid << endl;
}
ofs.close();
}
int workerManger::get_Empnum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
num++;
}
ifs.close();
return num;
}
void workerManger::init_emp()
{
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 = NULL;
if (did == 1)
{
worker = new Employee(id, name, did);
}
else if (did == 2)
{
worker = new Manager(id, name, did);
}
else if (did == 3)
{
worker = new Boss(id, name, did);
}
this->m_Emparry[index] = worker;
index++;
}
}
void workerManger::show_emp()
{
if (this->m_Fileisempty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
for (int i = 0; i < m_Empnum; i++)
{
this->m_Emparry[i]->showInfo();
}
}
system("pause");
system("cls");
}
int workerManger::Isexist(int ID)
{
int index = -1;
for (int i = 0; i < this->m_Empnum; i++)
{
if (this->m_Emparry[i]->m_ID == ID)
{
index = i;
break;
}
}
return index;
}
void workerManger::Del_emp()
{
if (this->m_Fileisempty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请输入要删除的职工编号:";
int id = 0;
cin >> id;
int index = this->Isexist(id);
if (index != -1)
{
for (int i = index; i < this->m_Empnum - 1; i++)
{
this->m_Emparry[i] = this->m_Emparry[i + 1];
}
this->m_Empnum--;
this->save();
cout << "删除成功!"<<endl;
}
else
{
cout << "删除失败,未找到该职工" << endl;
}
}
system("pause");
system("cls");
}
void workerManger::Mod_emp()
{
if (this->m_Fileisempty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请输入要修改职工的编号:";
int id;
cin >> id;
int ret = this->Isexist(id);
if (ret != -1)
{
delete this->m_Emparry[ret];
int newid = 0;
string newname = "";
int dselect = 0;
cout << "查到" << id << "号职工,请输入新职工号:";
cin >> newid;
cout << "请输入新姓名:";
cin >> newname;
cout << "请输入岗位:" << endl
<< "1.普通职工" << endl
<< "2.经理" << endl
<< "3.总裁" << endl;
cin >> dselect;
worker* Worker = NULL;
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->m_Emparry[ret] = Worker;
cout << "修改成功" << this->m_Emparry[ret]->m_deptid << endl;
this->save();
}
else
{
cout << "查找失败,查无此人" << endl;
}
}
system("pause");
system("cls");
}
void workerManger::Find_emp()
{
if (this->m_Fileisempty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请输入查找方式:" << endl
<< "1.按职工编号查找" << endl
<< "2.按姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
int id;
cout << "请输入要查找的职工编号:";
cin >> id;
int ret = Isexist(id);
if (ret != -1)
{
cout << "查找成功,该职工信息如下:"<<endl;
this->m_Emparry[ret]->showInfo();
}
else
{
cout << "查找失败,查无此人" << endl;
}
}
else if (select == 2)
{
string name;
cout << "请输入要查找的姓名:" ;
cin >> name;
bool flag = false;
for (int i = 0; i < m_Empnum; i++)
{
if (m_Emparry[i]->m_name == name)
{
cout << "查找成功,职工编号为:"
<< m_Emparry[i]->m_ID
<< "号的信息如下:" << endl;
flag = true;
this->m_Emparry[i]->showInfo();
}
}
if (flag == false)
{
cout << "查找失败,查无此人" << endl;
}
}
else
{
cout << "输入选项有误" << endl;
}
}
system("pause");
system("cls");
}
void workerManger::Sort_emp()
{
if (this->m_Fileisempty)
{
cout << "文件不存在或记录为空" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" << endl
<< "1.按职工号进行升序!" << endl
<< "2.按职工号进行降序!" << endl;
int select =0 ;
cin >> select;
for (int i = 0; i < m_Empnum; i++)
{
int minorMAX = i;
for (int j = i+1; j < m_Empnum; j++)
{
if (select == 1)
{
if (this->m_Emparry[minorMAX]->m_ID > this->m_Emparry[j]->m_ID)
{
minorMAX = j;
}
}
else
{
if (this->m_Emparry[minorMAX]->m_ID < this->m_Emparry[j]->m_ID)
{
minorMAX = j;
}
}
}
if (i != minorMAX)
{
worker* temp = this->m_Emparry[i];
this->m_Emparry[i] =this->m_Emparry[minorMAX];
this->m_Emparry[minorMAX] = temp;
}
}
cout << "排次成功,排序后的结果为:" << endl;
this->save();
this->show_emp();
}
}
void workerManger::Clean_file()
{
cout << "确认清空?" << endl
<< "1.确认" << endl
<< "2.返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->m_Emparry != NULL)
{
for (int i = 0; i < this->m_Empnum; i++)
{
if (this->m_Emparry[i] != NULL)
{
delete this->m_Emparry[i];
this->m_Emparry[i] = NULL;
}
}
this->m_Empnum = 0;
delete[] this->m_Emparry;
this->m_Emparry = NULL;
this->m_Fileisempty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
workerManger::~workerManger()
{
if (this->m_Emparry != NULL)
{
for (int i = 0; i < this->m_Empnum; i++)
{
if (this->m_Emparry[i] != NULL)
{
delete this->m_Emparry[i];
}
}
delete[] this->m_Emparry;
this->m_Emparry = NULL;
}
}
workerManger.h
#pragma once
#include<iostream>
#include<fstream>
#include"worker.h"
#include"employee.h"
#include"boss.h"
#include"manager.h"
using namespace std;
#define FILENAME "empfile.txt"
class workerManger
{
public:
workerManger();
void Show_Menu();
void exitSystem();
void Add_emp();
int m_Empnum;
worker ** m_Emparry;
void save();
bool m_Fileisempty;
int get_Empnum();
void init_emp();
void show_emp();
void Del_emp();
int Isexist(int ID);
void Mod_emp();
void Find_emp();
void Sort_emp();
void Clean_file();
~workerManger();
};
worker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class worker
{
public:
virtual void showInfo() = 0;
virtual string getDeptname() = 0;
int m_ID;
string m_name;
int m_deptid;
};
manager.cpp
#include"manager.h"
Manager::Manager(int id, string name, int did)
{
this->m_ID = id;
this->m_name = name;
this->m_deptid = did;
}
void Manager::showInfo()
{
cout << "职工编号:" << this->m_ID;
cout << "\t职工姓名:" << this->m_name;
cout << "\t职工岗位:" << this->getDeptname();
cout << "\t岗位职责:完成老板交代任务,并下发任务给员工" << endl;
}
string Manager::getDeptname()
{
return string("经理");
}
manager.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Manager :public worker
{
public:
Manager(int id, string name, int did);
virtual void showInfo();
virtual string getDeptname();
};
employee.cpp
#include"employee.h"
Employee::Employee(int id, string name, int did)
{
this->m_ID = id;
this->m_name = name;
this->m_deptid = did;
}
void Employee::showInfo()
{
cout << "职工编号:" << this->m_ID;
cout << "\t职工姓名:" << this->m_name;
cout << "\t职工岗位:" << this->getDeptname();
cout << "\t岗位职责:完成经理交代任务" << endl;
}
string Employee::getDeptname()
{
return string("员工");
}
employee.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Employee :public worker
{
public:
Employee(int id, string name, int did);
virtual void showInfo();
virtual string getDeptname();
};
boss.cpp
#include"boss.h"
Boss::Boss(int id, string name, int did)
{
this->m_ID = id;
this->m_name = name;
this->m_deptid = did;
}
void Boss::showInfo()
{
cout << "职工编号:" << this->m_ID;
cout << "\t职工姓名:" << this->m_name;
cout << "\t职工岗位:" << this->getDeptname();
cout << "\t岗位职责:管理公司所有任务" << endl;
}
string Boss::getDeptname()
{
return string("老板");
}
boss.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Boss :public worker
{
public:
Boss(int id, string name, int did);
virtual void showInfo();
virtual string getDeptname();
};