1. 程式人生 > 實用技巧 >C++實現職工管理系統(中)

C++實現職工管理系統(中)

C++實現職工管理系統(中)

大家好,今天是在部落格園的第九天,博主今天給大家帶來的是職工管理系統(C++)(中)

這次的隨筆記錄是實現(上)結語處說的幾個功能

目錄

此次要實現的功能

  • 退出管理程式:退出當前管理系統
  • 增加職工資訊:實現批量新增職工功能,將資訊錄入到檔案中,職工資訊為:職工編號、姓名、部門編號
  • 顯示職工資訊:顯示公司內部所有職工的資訊
  • 刪除離職職工:按照編號刪除指定的職工

1.先建立一個函式的類

為了方便管理相關功能的函式,建立一個WorkerInformation

的類

class WorkerInformation
{

public:

	WorkerInformation()  //利用建構函式進行相關的初始化操作
	{
		//檔案不存在的情況
		ifstream ifs1;
		ifs1.open(FILENAME, ios::in);

		if (!ifs1.is_open())
		{
			//cout << "檔案不存在" << endl;
			this->PersonNum = 0;
			this->m_PersonArr = NULL;
		    this->m_FileIsEmpty = true;
			ifs1.close();
			return;
		}

		ifs1.close();

		//檔案存在,要檢查下里面有無資料,可用下方已經註釋的程式碼進行驗證
		ifstream ifs2;
		ifs2.open(FILENAME, ios::in);
		char ch;
		ifs2 >> ch;

		if (ifs2.eof())
		{
			//cout << "檔案存在且檔案內無資料" << endl;
			this->PersonNum = 0;
			this->m_PersonArr = NULL;
			this->m_FileIsEmpty = true;
			ifs2.close();
			return;
		}
		ifs2.close();

		//檔案存在了,並且裡面有資料,我們需要知道里面的有多少人,可用下方已經註釋的程式碼進行驗證
		ifstream ifs3;
		ifs3.open(FILENAME, ios::in);
		int num = this->get_Num();
		//cout << "檔案裡有" << num << "名職工" << endl;
		this->PersonNum = num;
		ifs3.close();

		//根據人數建立一個數組
		this->m_PersonArr = new Worker * [this->PersonNum];
		//初始化陣列
		init_PArr();

	}

	~WorkerInformation()  //釋放記憶體
	{
		if (this->m_PersonArr != NULL)
		{
			delete[] this->m_PersonArr;
			this->m_PersonArr = NULL;
		}
	}

	void menu();        //選單

	void ExitSystem();  //退出
	
	void AddPerson();   //新增職工

	void save();        //儲存資料進檔案

	int PersonNum;      //需要新增的職工人數

	Worker** m_PersonArr;  //指向陣列的指標 建立進堆區進行相關的儲存操作

	bool m_FileIsEmpty;    //檢測檔案是否為空

	int get_Num();         //用來獲取檔案內的人數

	void init_PArr();      //初始化陣列

	void show_Person();    //顯示職工

	int IsExist(int id);   //通過編號判斷職工是否存在

	void Del_Person();     //刪除員工的操作
}

2.選單的建立

做一個美化的選單,可以學習下EaxyX進行美化 (目前博主還在學)

void WorkerInformation::menu()
{
	cout << "\t**********************************" << endl
	     << "\t*       歡迎來到職工管理系統        *" << endl
	     << "\t**********************************" << endl
		 << "\t****      1.增加職工資訊        ****" << endl
		 << "\t****      2.顯示職工資訊        ****" << endl
		 << "\t****      3.刪除職工資訊        ****" << endl
		 << "\t****          0.退出           ****" << endl
	     << "\t**********************************" << endl << endl;
}

3.實現退出功能

void WorkerInformation::ExitSystem()
{
	cout << "歡迎下次使用" << endl;
	exit(0);
}

3.實現增加功能

首先將寫出增加的函式

void WorkerInformation::AddPerson()
{
	cout << "請輸入需要新增的人數:" << endl;
	int AddNum = 0;
	cin >> AddNum;

	if (AddNum > 0)
	{
		int newsize = this->PersonNum + AddNum;

		Worker** newspace = new Worker * [newsize];

		if (this->m_PersonArr != NULL)
		{
			for (int i = 0; i < this->PersonNum; i++)
			{
				newspace[i] = this->m_PersonArr[i];
			}
		}

		for (int i = 0; i < AddNum; i++)
		{
			int id;
			string name;
			int did;

			cout << "請輸入第" << i + 1 << "個新職工的編號:";
			cin >> id;

			cout << "請輸入第" << i + 1 << "個新職工的名字:";
			cin >> name;


			cout << "請選擇該職工的崗位:" << endl;
			cout << "1、普通員工" << endl;
			cout << "2、經理" << endl;
			cout << "3、老闆" << endl;
			cin >> did;

			Worker* worker = NULL;
			switch (did)
			{
			case 1:
				worker = new GeneralWorker(id, name, did);
				break;
			case 2:
				worker = new Manage(id, name, did);
				break;
			case 3:
				worker = new Boss(id, name, did);
				break;
			default:
				break;
			}

			newspace[this->PersonNum + i] = worker;
		}

		delete[] this->m_PersonArr;

		this->m_PersonArr = newspace;

		this->PersonNum = newsize;

		cout << "成功新增" << AddNum << "名新職工!" << endl;

	}
	else
	{
		cout << "輸入有誤" << endl;
	}
	save();

	cout << endl;
	cout << "新增成功,稍等請按任意鍵進行清屏操作!" << endl;
	system("pause");
	system("cls");
}

接下來是儲存到檔案的函式

void WorkerInformation::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);

	for (int i = 0; i < this->PersonNum; i++)
	{
		ofs << this->m_PersonArr[i]->m_Id << " "
			<< this->m_PersonArr[i]->m_Name << " "
			<< this->m_PersonArr[i]->m_DeptId << " " << endl;
	}
	ofs.close();
	cout << "儲存成功!" << endl;
}

4.實現顯示功能

假設我們檔案裡面已經有人了,我們需要知道檔案裡面的人數,並將其放進數組裡,再一一展示出來

首先查詢檔案裡面是否為空,否則返回人數,方便下面的功能進行相關的操作,在WorkerInformation建構函式裡進行確認

bool m_FileIsEmpty;  //檢測檔案是否為空

確定人數

int WorkerInformation::get_Num()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int num = 0;

	int id;
	string name;
	int did;

	while (ifs >> id && ifs >> name && ifs >> did)
	{
		num++;
	}
	ifs.close();
	return num;
}

初始化陣列

void WorkerInformation::init_PArr()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int index = 0;
	int id;
	string name;
	int did;

	while (ifs >> id && ifs >> name && ifs >> did)
	{
		Worker* worker = NULL;

		if (did == 1)
		{
			worker = new GeneralWorker(id, name, did);
		}
		else if (did == 2)
		{
			worker = new Manage(id, name, did);
		}
		else
		{
			worker = new Boss(id, name, did);
		}
		this->m_PersonArr[index] = worker;
		index++;
	}
	ifs.close();
}

顯示資訊

void WorkerInformation::show_Person()
{
	if (this->m_FileIsEmpty)
	{
		cout << "該檔案為空或記錄為空" << endl;
	}
	else
	{
		for (int i = 0; i < this->PersonNum; i++)
		{
			this->m_PersonArr[i]->Show_WI();
		}
		cout << "顯示成功!" << endl;
	}

	cout << endl;
	cout << "稍等請按任意鍵進行清屏操作!" << endl;
	system("pause");
	system("cls");
}

5.實現刪除功能

通過編號刪除指定資訊,先確定檔案裡是否存在需要刪除的編號,再決定是否進行刪除操作

檢查檔案是否有指定人

int WorkerInformation::IsExist(int id)
{
	int num = -1;
	for (int i = 0; i < this->PersonNum; i++)
	{
		if (this->m_PersonArr[i]->m_Id == id)
		{
			num = i;
			break;
		}
	}
	return num;
}

執行刪除操作(找到指定位置,將後一位的資訊進行前移)

void WorkerInformation::Del_Person()
{
	if (this->m_FileIsEmpty)
	{
		cout << "該檔案為空或記錄為空" << endl;
	}
	else
	{
		cout << "請輸入想要刪除員工的職工編號:";
		int a;
		cin >> a;
		int ret = this->IsExist(a);

		if (ret != -1)
		{
			for (int i = ret; i < this->PersonNum - 1; i++)
			{
				this->m_PersonArr[i] = this->m_PersonArr[i+1];
			}
			this->PersonNum--;
			this->save();
			cout << "刪除成功!" << endl;
		}
		else
		{
			cout << "刪除失敗" << endl;
		}

	}
	cout << endl;
	cout << "稍等請按任意鍵進行清屏操作!" << endl;
	system("pause");
	system("cls");
}

結語

下列的功能放在下一篇的隨筆進行記錄

  • 查詢職工資訊:按照職工的編號或者職工的姓名進行查詢相關的人員資訊
  • 按照編號排序:按照職工編號,進行排序,排序規則由使用者指定(升序與降序)
  • 清空所有文件:清空檔案中記錄的所有職工資訊 (清空前需要再次確認,防止誤刪)

到訪的小夥伴們有什麼建議可以在評論區留言,大家一起學習一起進步,加油呀!