若代码有用,请各位客官留下您宝贵的收藏,点赞和关注,谢谢啦!
前言
该系统程序适合课程设计作业,期末大作业,实训作业,适合为以后大型项目做基础和预热,值得学习!!!
一、酒店职员管理系统源代码
头文件:CEO.h
#include<iostream>
#include<string>
#include"work.h"
using namespace std;
class CEO :public work
{
public:
CEO(int id, string name, int partid,string sex,int age);
virtual void showperson();
virtual string getjob();
};
employee.h
#include<iostream>
#include<string>
#include"work.h"
using namespace std;
class employee :public work
{
public:
employee(int id,string name,int partid, string sex, int age);
virtual void showperson();
virtual string getjob();
};
manager.h
#include<iostream>
#include<string>
#include"work.h"
using namespace std;
class manager :public work
{
public:
manager(int id, string name, int partid, string sex, int age);
virtual void showperson();
virtual string getjob();
};
work.h
#include<iostream>
#include<string>
using namespace std;
class work
{
public:
virtual void showperson() = 0;
virtual string getjob() = 0;
int ID;
string Name;
int partID;
int age;
string sex;
};
WorkManager.h
#include<iostream>
#include"work.h"
#include"employee.h"
#include"manager.h"
#include"CEO.h"
#include"work.h"
#include<fstream>
#define Filename "emfile.txt"
using namespace std;
class WorkManger
{
public:
WorkManger();
void showmenu();
void showem();
void addperson();
int m_exit(int id);
void delemp();
void changeem();
void Exit();
void save();
int get_num();
void input();
void sortemp();
void findemp();
void cleanemp();
~WorkManger();
int emnum;
work ** emarry;
bool fileempty;
};
源文件:CEO.h
#include<iostream>
#include"CEO.h"
#include<string>
using namespace std;
CEO::CEO(int id, string name, int partid, string sex, int age)
{
this->ID = id;
this->Name = name;
this->partID = partid;
this->age = age;
this->sex = sex;
}
void CEO::showperson()
{
cout << "职工编号:" << this->ID
<< "\t职工姓名:" << this->Name
<< "\t职工性别:" << this->sex
<< "\t职工年龄:" << this->age
<< "\t职工岗位:" << this->getjob()
<< "\t职工职责:安排经理项目和审核酒店项目!" << endl;
}
string CEO::getjob()
{
return string("总裁");
}
employee.h
#include<iostream>
#include"employee.h"
#include<string>
using namespace std;
employee::employee(int id, string name, int partid, string sex, int age)
{
this->ID = id;
this->Name = name;
this->partID = partid;
this->age = age;
this->sex = sex;
}
void employee::showperson()
{
cout << "职工编号:" << this->ID
<< "\t职工姓名:" << this->Name
<< "\t职工性别:" << this->sex
<< "\t职工年龄:" << this->age
<< "\t职工岗位:" << this->getjob()
<<"\t职工职责:完成经理安排的任务和酒店日常的工作!" << endl;
}
string employee::getjob()
{
return string("员工");
}
manager.h
#include<iostream>
#include"manager.h"
#include<string>
using namespace std;
manager::manager(int id, string name, int partid, string sex, int age)
{
this->ID = id;
this->Name = name;
this->partID = partid;
this->age = age;
this->sex = sex;
}
void manager::showperson()
{
cout << "职工编号:" << this->ID
<< "\t职工姓名:" << this->Name
<< "\t职工性别:" << this->sex
<< "\t职工年龄:" << this->age
<< "\t职工岗位:" << this->getjob()
<< "\t职工职责:完成总裁安排的任务及管理酒店日常的事务!" << endl;
}
string manager::getjob()
{
return string("经理");
}
WorkManager.h
#include"WorkManger.h"
#include<string>
WorkManger::WorkManger()
{
//1.文件不存在
ifstream ifs;
ifs.open(Filename, ios::in);
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
this->emnum = 0;
this->emarry = NULL;
this->fileempty = true;
ifs.close();
return;
}
//2.文件存在数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空" << endl;
this->emnum = 0;
this->emarry = NULL;
this->fileempty = true;
ifs.close();
return;
}
//3.文件存在,记录存档人数
this->fileempty = false;
int num = this->get_num();
this->emnum = num;
cout << "当前存档人数为:" << num << "人" << endl;
this->emarry = new work*[emnum];
input();
}
void WorkManger::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;
cout << endl;
}
void WorkManger::addperson()
{
int addnum = 0;
cout << "请输入添加职工的人数:";
cin >> addnum;
if (addnum>0)
{
int newsize = this->emnum + addnum;
work ** newspace = new work*[newsize];
if (this->emarry != 0)
{
for (int i = 0; i < this->emnum; i++)
{
newspace[i] = this->emarry[i];
}
}
for (int i = 0; i < addnum; i++)
{
int id;
string name;
int age;
string sex;
int select;
cout << "请输入第" << i + 1 << "个职员的编号:";
cin >> id;
/*cout << endl;*/
cout << "请输入第" << i + 1 << "个职员的姓名:";
cin >> name;
cout << "请输入第" << i + 1 << "个职员的性别:";
cin >> sex;
cout << "请输入第" << i + 1 << "个职员的年龄:";
cin >> age;
/*cout << endl;*/
cout << "请选择该职工的岗位(部门编号):" << endl;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、总裁" << endl;
work* work = NULL;
cin >> select;
switch (select)
{
case 1:
work = new employee(id, name, 1,sex,age);
break;
case 2:
work = new manager(id, name, 2, sex, age);
break;
case 3:
work = new CEO(id, name, 3, sex, age);
break;
default:
break;
}
newspace[this->emnum + i] = work;
}
delete[] this->emarry;
this->emarry = newspace;
this->emnum = newsize;
cout << "成功添加" << addnum << "名职员" << endl;
this->fileempty = false;
save();
}
else
{
cout << "您输入有误!" << endl;
}
system("pause");
system("cls");
}
void WorkManger::save()
{
ofstream ofs;
ofs.open(Filename, ios::out);
for (int i = 0; i < this->emnum; i++)
{
ofs << this->emarry[i]->ID << " "
<< this->emarry[i]->Name << " "
<< this->emarry[i]->age << " "
<< this->emarry[i]->sex << " "
<< this->emarry[i]->partID << endl;
}
ofs.close();
}
int WorkManger::get_num()
{
ifstream ifs;
ifs.open(Filename, ios::in);
int id;
string name;
int job;
string sex;
int age;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> job && ifs >>sex && ifs >>age)
{
num++;
}
return num;
}
void WorkManger::input()
{
ifstream ifs;
ifs.open(Filename, ios::in);
int id;
string name;
int job;
string sex;
int age;
int innum = 0;
while (ifs >> id && ifs >> name && ifs >> job && ifs >> sex && ifs >> age)
{ work * work = NULL;
if (job == 1)
{
work = new employee(id, name, job,sex,age);
}
else if (job == 2)
{
work = new manager(id, name, job, sex, age);
}
else
{
work = new CEO(id, name, job, sex, age);
}
this->emarry[innum] = work;
innum++;
}
}
void WorkManger::delemp()
{
if (this->fileempty)
{
cout<<"文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入想要删除的职工号:" << endl;
int id = 0;
cin >> id;
int index = this->m_exit(id);
if (index != -1)
{
for (int i = index; i < this->emnum-1; i++)
{
this->emarry[i] = this->emarry[i + 1];
}
this->emnum--;
this->save(); //删除后数据同步到文件中
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,该职工号不存在!" << endl;
}
}
system("pause");
system("cls");
}
int WorkManger::m_exit(int id)
{
int index = -1;
for (int i = 0; i < this->emnum; i++)
{
if (this->emarry[i]->ID==id)
{
index = i;
break;
}
}
return index;
}
void WorkManger::changeem()
{
if (this->fileempty)
{
cout << "文件为空或不存在!" << endl;
}
else
{
int id;
cout << "请输入修改的职工号:";
cin >> id;
int et = this->m_exit(id);
if (et!=-1)
{
delete this->emarry[et];
cout << "查到此人" << endl;
int newid=0;
string newname=" ";
int jobchoose=0;
string newsex = " ";
int newage = 0;
cout << "查到第" << id << "号职工" << endl;
cout << "请输入新的职工号:";
cin >> newid;
cout << "请输入新的职工姓名:";
cin >> newname;
cout << "请输入新的职工性别:";
cin >> newsex;
cout << "请输入新的职工年龄:";
cin >> newage;
cout << "请输入新的职工部门编号:";
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、总裁" << endl;
cin >> jobchoose;
work * work = NULL;
switch (jobchoose)
{
case 1:
work = new employee(newid, newname, jobchoose,newsex,newage);
break;
case 2:
work = new manager(newid, newname, jobchoose, newsex, newage);
break;
case 3:
work = new CEO(newid, newname, jobchoose, newsex, newage);
break;
default:
break;
}
this->emarry[et] = work;
this->save();
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人" << endl;
}
}
system("pause");
system("cls");
}
void WorkManger::findemp()
{
ifstream ifs;
ifs.open(Filename, ios::in);
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空" << endl;
this->emnum = 0;
this->emarry = NULL;
this->fileempty = true;
ifs.close();
}
else if (this->fileempty)
{
cout << "文件不存在!" << endl;
}
else
{
cout << "请输入查找的方式:" << endl;
cout << "1、按职工编号查找" << endl;
cout << "2、按姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
int id;
cout << "请输入查找的职工编号:";
cin >> id;
int et = m_exit(id);
if (et != -1)
{
cout << "查找成功!" << endl;
cout << "该职工信息如下:";
this->emarry[et]->showperson();
}
else
{
cout << "查找失败或查无此人!" << endl;
}
}
else if (select == 2)
{
string name;
cout << "请输入要查找的姓名:";
cin >> name;
bool fg = false;
for (int i = 0; i < emnum; i++)
{
if (emarry[i]->Name == name)
{
cout << "查找成功:" << endl;
cout << "该职工信息如下:";
this->emarry[i]->showperson();
fg = true;
}
}
if (fg == false)
{
cout << "查找失败或查无此人!" << endl;
}
}
else
{
cout << "输入有误!" << endl;
}
}
system("pause");
system("cls");
}
void WorkManger::cleanemp()
{
int selsct;
cout << "请选择是否确定清空:";
cout << "1.确定" << "2.取消" << endl;
cin >> selsct;
if (selsct == 1)
{
//打开模式 ios::trunc 如果存在删除文件并重新创建
ofstream ofs(Filename, ios::trunc);
ofs.close();
if (/*this->*/emarry != NULL)
{
for (int i = 0; i < emnum; i++)
{
if (/*this->*/emarry[i] != NULL)
{
delete /*this->*/emarry[i];
}
}
/*this->*/emnum = 0;
delete[] /*this->*/emarry;
/*this->*/emarry = NULL;
/*this->*/fileempty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
void WorkManger::sortemp()
{
ifstream ifs;
ifs.open(Filename, ios::in);
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空" << endl;
this->emnum = 0;
this->emarry = NULL;
this->fileempty = true;
ifs.close();
}
else if (this->fileempty)
{
cout << "文件不存在!" << endl;
}
else
{
cout << "请选择排序方式: " << endl;
cout << "1、按职工号进行升序" << endl;
cout << "2、按职工号进行降序" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < emnum; i++)
{
int someone = i;
for (int j = i + 1; j < emnum; j++)
{
//升序
if (select == 1)
{
if (emarry[someone]->ID > emarry[j]->ID)
{
someone = j;
}
}
//降序
else
{
if (emarry[someone]->ID < emarry[j]->ID)
{
someone = j;
}
}
}
if (i != someone)
{
work * temp = emarry[i];
emarry[i] = emarry[someone];
emarry[someone] = temp;
this->save();
//cout << "排序完成!" << endl;
}
}
cout << "排序完成!" << endl;
}
system("pause");
system("cls");
}
void WorkManger::Exit()
{
cout << "欢迎您下次使用!!!" << endl;
system("pause");
exit(0);
}
WorkManger::~WorkManger()
{
if (this->emarry != 0)
{
delete[] this->emarry;
this->emarry = NULL;
}
}
void WorkManger::showem()
{
ifstream ifs;
ifs.open(Filename, ios::in);
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空" << endl;
this->emnum = 0;
this->emarry = NULL;
this->fileempty = true;
ifs.close();
}
else if(this->fileempty)
{
cout << "文件不存在!" << endl;
}
else
{
for (int i = 0; i < emnum; i++)
{
//利用多态调用接口
this->emarry[i]->showperson();
}
}
system("pause");
system("cls");
}
main.cpp
#include<iostream>
using namespace std;
#include"WorkManger.h"
int main()
{
WorkManger hr;
int choose = 0;
while (true)
{
system("color F1");
hr.showmenu();
cout << "请输入功能的序号:";
cin >> choose;
switch (choose)
{
case 0:
hr.Exit();//退出系统
break;
case 1: //添加职工
hr.addperson();
break;
case 2: //显示职工
hr.showem();
break;
case 3: //删除职工
hr.delemp();
break;
case 4: //修改职工
hr.changeem();
break;
case 5: //查找职工
hr.findemp();
break;
case 6: //排序职工
hr.sortemp();
break;
case 7: //清空文件
hr.cleanemp();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
总结
若代码有用,请各位客官留下您宝贵的收藏,点赞和关注,谢谢啦!