C++ 酒店管理系统

主要功能:

1.添加员工信息

2.显示员工信息

3.删除员工信息

4.修改员工信息

5.查找员工信息

6.员工信息排序

7.清空数据

(1)显示数据

(2)修改数据

(3)查找数据

(4)信息排序

部分代码展示:workerManager.cpp。需要完整代码可以留邮箱,有时间就发

#include "stdafx.h"
#include "workerManager.h"

WorkerManager::WorkerManager()
{
	//1.文件不存在
	ifstream ifs;
	ifs.open(FILENAME, ios::in);//读文件
	if (!ifs.is_open())
	{
		//cout << "文件不存在" << endl;
		this->m_staffNum = 0;
	    this->m_staffArray = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//2.文件存在,数据为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//cout << "文件为空!" << endl;
		this->m_staffNum = 0;
		this->m_staffArray = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//3.文件存在数据
	int num = this->get_staffNum();
	this->m_staffNum = num;
	//开辟空间
	this->m_staffArray = new Worker*[this->m_staffNum];
	
	//将文件中的数据存在数组中
	this->init_staff();
}


void WorkerManager::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 WorkerManager::exitSystem()
{
	cout << "系统退出" << endl;
	system("pause");
	exit(0);
}

void WorkerManager::Add_staff()
{
	cout << "输入添加员工的数量:" << endl;
	int  addNum = 0;  //保存员工的数量

	cin >> addNum;
	if (addNum > 0)
	{
		//计算新空间的大小
		int newSize = this->m_staffNum + addNum;
		//开辟新空间
		Worker ** newSpace=new Worker *[newSize];

		//将原始数据拷贝到新空间中
		if (this->m_staffArray != NULL)
		{
			for (int i = 0; i < this->m_staffNum; i++)
			{
				newSpace[i] = this->m_staffArray[i];
			}
		}
		for (int i = 0; i < addNum; i++)
		{
			int id;
			string name;
			int dId;
			cout << "请输入第" << i + 1 << "个员工的编号:" << endl;
			cin >> id;
			//判断该编号是否存在
			int ret = this->IsExist(id);  
			if (ret != -1)
			{
				while (1)
				{
					cout << "该编号已存在,请重新输入编号:" << endl;
					cin >> id;
				    ret = this->IsExist(id);
					if (ret == -1)
					{
						break;
					}
				}
			}
			
			cout << "请输入第" << i + 1 << "个员工的姓名:" << endl;
			cin >> name;
			cout << "请输入该员工的岗位:" << endl;
			cout << "1.普通员工" << endl;
			cout << "2.经理" << endl;
			cout << "3.老板" << endl;
			cin >> dId;

			Worker *worker = NULL;
			switch (dId)
			{
			case 1:
				worker = new staff(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_staffNum + i] = worker;
		}
		//释放原有空间
		delete[] this->m_staffArray;
		//更改新空间的指向
		this->m_staffArray = newSpace;
		//更新新的员工数量
		this->m_staffNum = newSize;
		//提示添加成功
		this->m_FileIsEmpty = false;//文件不为空
		cout << "成功添加" << addNum << "个员工" << endl;
		this->save();
	}
	else
	{
		cout << "输入有误!" << endl;
	}
	//按任意键回到上级目录
	system("pause");
	system("cls");
}

void WorkerManager::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);  //写文件
	for (int i = 0; i < this->m_staffNum; i++)  
	{
		ofs << this->m_staffArray[i]->m_Id << " "
			<< this->m_staffArray[i]->m_Name << " "
			<< this->m_staffArray[i]->m_dId << endl;
	}
	//关闭文件
	ofs.close();
}

int WorkerManager::get_staffNum()
{
	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::init_staff()
{
	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 staff(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_staffArray[index] = worker;
		index++;
	}
	ifs.close();
}

//显示员工数据
void WorkerManager::show_staff()   
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < m_staffNum; i++)
		{
			this->m_staffArray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}
//
int WorkerManager::IsExist(int id)
{
	int index = -1;
	for (int i = 0; i < this->m_staffNum; i++)
	{
		if (this->m_staffArray[i]->m_Id == id)
		{
			index = i;
			break;
		}
	}
	return index;
}


//删除员工
void WorkerManager::Del_staff()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者为空" << endl;
	}
	else
	{
		cout << "请输入删除员工的的编号:" << endl;
		int id = 0;
		cin >> id;

		int index = this->IsExist(id);
		if (index != -1)  //员工存在,删除
		{
			for (int i = index; i < this->m_staffNum - 1; i++)
			{
				this->m_staffArray[i] = this->m_staffArray[i + 1];
			}
			this->m_staffNum--; //更新员工的数量
			//文件数据同步
			this->save();
			cout << "删除成功" << endl;
		}
		else
		{
			cout << "该员工不存在" << endl;
		}
		//清屏操作
		system("pause");
		system("cls");
	}
}

//修改员工数据
void WorkerManager::Mod_staff()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者为空" << endl;
	}
	else
	{
		cout << "请输入修改的职工编号:" << endl;
		int id;
		cin >> id;
		int ret=this->IsExist(id);
		if (ret != -1)
		{
			delete this->m_staffArray[ret];
			int newId = 0;
			string newName = " ";
			int newdId = 0;
			cout << "查找到" << id << "号职工,请输入新职工编号:" << endl;
			cin >> newId;
			cout << "请输入新的名字:" << endl;
			cin >> newName;
			cout << "请输入新的岗位" << endl;
			cout << "1.清洁员" << endl;
			cout << "2.经理" << endl;
			cout << "3.老板" << endl;
			cin >> newdId;
			Worker * worker = NULL;

			switch (newdId)
			{
			case 1:
				worker = new staff(newId, newName, newdId);
				break;
			case 2:
				worker = new manager(newId, newName, newdId);
				break;
			case 3:
				worker = new boss(newId, newName, newdId);
				break;
			default:
				break;
			}
			//更新数据到数组中
			this->m_staffArray[ret] = worker;
			cout << "修改成功" << endl;

			//保存到文件中
			this->save();
		}
		else
		{
			cout << "修改失败,查无此人" << endl;
		}
	}
	system("pause");
	system("cls");
}


//查找员工
void WorkerManager::Find_staff()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者数据为空";
	}
	else
	{
		cout << "请输入查找方式:" << endl;
		cout << "1.按编号查找" << endl;
		cout << "2.按姓名查找" << endl;
		int select = 0;
		cin >> select;
		if (select == 1)
		{
			int id;
			cout << "请输入查找的职工编号:" << endl;
			cin >> id;

			int ret = IsExist(id);
			if (ret != -1)
			{
				cout << "查找成功!该员工信息如下:" << endl;
				this->m_staffArray[ret]->showInfo();
			}
			else
			{
				cout << "查无此人" << endl;
			}
		}
		else if (select == 2)
		{
			string name;
			cout << "请输入查找的职工名字:" << endl;
			cin >> name;
			//判断是否查到
			bool flag = false;//默认未找到
			for (int i = 0; i < m_staffNum; i++)
			{
				if (this->m_staffArray[i]->m_Name == name)
				{
					cout << "查找成功,职工编号为:"
						<< this->m_staffArray[i]->m_Id
						<< "该员工信息如下:" << endl;
					flag = true;
					this->m_staffArray[i]->showInfo();
				}
			}
			if (flag == false)
			{
				cout << "查无此人" << endl;
			}
		}
		else
		{
			cout << "查无此人" << endl;
		}
	}
	system("pause");
	system("cls");
}
//对员工编号进行排序
void WorkerManager::Sort_staff()  
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请选择排序方式:" << endl;
		cout << "1.升序排列" << endl;
		cout << "2.降序排序" << endl;
		int select = 0;
		cin >> select;
		for (int i = 0; i < m_staffNum; i++)
		{
			int minOrMax = i;  //最小值或者对大值的下标
			for (int j = i + 1; j < m_staffNum; j++)
			{
				if (select == 1) //升序
				{
					if (this->m_staffArray[minOrMax]->m_Id > this->m_staffArray[j]->m_Id)
					{
						minOrMax = j;
					}
				}
				else //降序
				{
					if (this->m_staffArray[minOrMax]->m_Id < this->m_staffArray[j]->m_Id)
					{
						minOrMax = j;
					}
				}
			}
			if (i != minOrMax)
			{
				Worker * temp = this->m_staffArray[i];
				this->m_staffArray[i] = this->m_staffArray[minOrMax];
				this->m_staffArray[minOrMax] = temp;
			}
		}
		cout << "排序成功!" << endl;
		this->show_staff();
		this->save();
	}
}

void WorkerManager::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_staffArray != NULL)
		{
			//删除堆区的每个对象
			for (int i = 0; i < this->m_staffNum; i++)
			{
				delete this->m_staffArray[i];
				this->m_staffArray[i] = NULL;
			}
			//删除堆区数组指针
			delete[] this->m_staffArray;
			this->m_staffArray = NULL;
			this->m_staffNum = 0;
			this->m_FileIsEmpty = true;
		}
		cout << "清空成功" << endl;
	}
	system("pause");
	system("cls");
}
WorkerManager::~WorkerManager()  //释放
{
	if (this->m_staffArray != NULL)
	{
		for (int i = 0; i < m_staffNum; i++)
		{
			delete this->m_staffArray[i];
			this->m_staffArray[i] = NULL;
		}
		delete[]this->m_staffArray;
		this->m_staffArray = NULL;
	}
}

 

评论 113
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酷小川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值