黑马--职业管理系统实践c++

跟随b站up主手写代码,改动了一点点.偷懒.如有错漏,欢迎指导.

抄作业方法:按我的命名方式分别创建h头文件以及cpp文件,并将对应代码复制粘贴即可,最后编译运行.

boss.h

#pragma once
#include "Worker.h"

class Boss :public Worker
{
public:
	Boss(int id, string name, int Deptid);
	virtual void ShowInfo();
	virtual string getDeptName();//获取职工岗位名称
};

employee.h

#pragma once
#include "Worker.h"

class Employee :public Worker
{
public:
	Employee(int id, string name, int Deptid);
	virtual void ShowInfo();
	virtual string getDeptName();//获取职工岗位名称
};

manager.h

#pragma once
#include "Worker.h"

class Manager :public Worker
{
public:
	Manager(int id, string name, int Deptid);
	virtual void ShowInfo();
	virtual string getDeptName();//获取职工岗位名称
};

worker.h

#pragma once
#include<iostream>
using namespace std;

class Worker
{
public:
	int id;//员工编号
	string name;
	int Deptid;//部门编号
	
	virtual void ShowInfo() = 0;
	virtual string getDeptName() = 0;

};

workermanger.h

#pragma once
#include<iostream>
#include <fstream>
#include "Worker.h"
#include "Employee.h"
#include "Manager.h"
#include "Boss.h"
constexpr auto FILENAME = "empfile.txt";

using namespace std;

class WorkerManger
{
public:
	int num;
	Worker** Array;
	bool FileIsEmpty;

	WorkerManger();
	void ShowMenu();
	void save();
	void Add_Emp();
	int getNum();
	void init_Emp();
	void Show_Emp();
	int IsExist(int id);
	void Del_Emp();
	void Mod_Emp();
	void Find_Emp();
	void Sort_Emp();
	void Clear_Emp();
	void ExitSystem();
	
	~WorkerManger();
};

boss.cpp

#include "Boss.h"

Boss::Boss(int id, string name, int Deptid)
{
	this->id = id;
	this->name = name;
	this->Deptid = Deptid;
}

void Boss::ShowInfo()
{
	cout << "职工姓名:" << this->name
		<< "\t职工编号:" << this->id
		<< "\t岗位:" << this->getDeptName()
		<< "\t岗位职责:管理所有事务" << endl;
}

string Boss::getDeptName()
{
	return string("总裁");
}

employee.cpp

#include "Employee.h"

Employee::Employee(int id, string name, int Deptid)
{
	this->id = id;
	this->name = name;
	this->Deptid = Deptid;
}

void Employee::ShowInfo()
{
	cout << "职工姓名:" << this->name
		<< "\t职工编号:" << this->id
		<< "\t岗位:" << this->getDeptName()
		<< "\t岗位职责:完成经理的任务" << endl;
}

string Employee::getDeptName()
{
	return string("员工");
}

manager.cpp

#include "Manager.h"

Manager::Manager(int id, string name, int Deptid)
{
	this->id = id;
	this->name = name;
	this->Deptid = Deptid;
}

void Manager::ShowInfo()
{
	cout << "职工姓名:" << this->name
		<< "\t职工编号:" << this->id
		<< "\t岗位:" << this->getDeptName()
		<< "\t岗位职责:完成老板的任务,并下发给员工" << endl;
}

string Manager::getDeptName()
{
	return string("经理");
}

workerManger.cpp

#include "workerManger.h"

WorkerManger::WorkerManger()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if (!ifs.is_open())//文件不存在,需要新建
	{
		cout << "文件不存在!" << endl;
		this->num = 0;
		this->FileIsEmpty = true;
		this->Array = nullptr;
		ifs.close();
		return;
	}

	char ch;
	ifs >> ch;
	if (ifs.eof())//文件已经存在,但没有数据
	{
		cout << "文件为空!" << endl;
		this->num = 0;
		this->FileIsEmpty = true;
		this->Array = nullptr;
		ifs.close();
		return;
	}

	this->num = this->getNum();
	this->Array = new Worker * [this->num];
	init_Emp();
}

void WorkerManger::ShowMenu()
{
	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 WorkerManger::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);

	for (int i = 0; i < this->num; i++)
	{
		ofs << this->Array[i]->id << " "
			<< this->Array[i]->name << " "
			<< this->Array[i]->Deptid << endl;
	}

	ofs.close();
}

void WorkerManger::Add_Emp()
{
	cout << "请输入职工的数量:" << endl;
	int addnum = 0;
	cin >> addnum;

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

		//将原空间下内容存放到新空间下
		if (this->Array != NULL)
		{
			for (int i = 0; i < this->num; i++)
			{
				newSpace[i] = this->Array[i];
			}
		}

		//输入新数据
		for (int i = 0; i < addnum; i++)
		{
			int id;
			string name;
			Worker* worker = nullptr;
			int dSelect;

			cout << "请输入第" << i + 1 << "名新职工编号" << endl;
			cin >> id;
			cout << "请输入第" << i + 1 << "名新职工姓名" << endl;
			cin >> name;

			cout << "请选择该职工的岗位:(1.普通职工 2.经理 3.总裁)" << endl;
			cin >> dSelect;
			switch (dSelect)
			{
			case 1:
				worker = new Employee(id, name, 1);
				break;
			case 2:
				worker = new Manager(id, name, 1);
				break;
			case 3:
				worker = new Boss(id, name, 1);
				break;
			default:
				break;
			}

			newSpace[this->num + i] = worker;
		}

		delete[] this->Array;
		this->Array = newSpace;
		this->num = newSize;
		this->FileIsEmpty = false;
		this->save();
		cout << "成功添加" << addnum << "名新职工" << endl;
	}
	else
	{
		cout << "输入不合法!请重新输入!" << endl;
	}

	cout << endl;
}

int WorkerManger::getNum()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int Deptid;

	int num = 0;
	while (ifs >> id && ifs >> name && ifs >> Deptid)
	{
		num += 1;
	}
	ifs.close();
	return num;
}

void WorkerManger::init_Emp()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int Deptid;

	int index= 0;
	while (ifs >> id && ifs >> name && ifs >> Deptid)
	{
		Worker* worker = NULL;
		if (Deptid == 1)
		{
			worker = new Employee(id, name, Deptid);
		}
		else if (Deptid == 2)
		{
			worker = new Manager(id, name, Deptid);
		}
		else
		{
			worker = new Boss(id, name, Deptid);
		}
		this->Array[index] = worker;
		index += 1;
	}
	ifs.close();
}

void WorkerManger::ExitSystem()
{
	cout << "感谢您的使用,欢迎下次再来!" << endl;
	exit(0);
}

void WorkerManger::Show_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "暂无职工,请努力赚钱,招募更多的打工人吧!" << endl;
	}
	else
	{
		for (int i = 0; i < this->num; i++)
		{
			this->Array[i]->ShowInfo();
		}
	}
}

int WorkerManger::IsExist(int id)
{
	int index = -1;
	for (int i = 0; i < this->num; i++)
	{
		if (this->Array[i]->id == id)
		{
			index = i;
			break;
		}
	}
	return index;
}

void WorkerManger::Del_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "无职工可删除!继续努力." << endl;
	}
	else
	{
		cout << "请输入想删除的职工编号:" << endl;
		int id = 0;
		cin >> id;
		int index = IsExist(id);
		if (index != -1)
		{
			for (int i = index; i < this->num - 1;i++)
			{
				this->Array[i] = this->Array[i + 1];
			}
			this->num -= 1;
			this->save();
			if (this->num == 0)
			{
				this->FileIsEmpty = true;
			}
			cout << "删除成功!" << endl;
		}
		else
		{
			cout << "该职工编号不存在!" << endl;
		}
	}
}

void WorkerManger::Mod_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "记录为空" << endl;
	}
	else
	{
		cout << "请输入想更改的职工的编号:" << endl;
		int id = 0;
		cin >> id;

		int ret = this->IsExist(id);
		if (ret != -1)
		{
			delete this->Array[ret];
			int newId;
			string newName;
			Worker* newWorker = nullptr;
			int dSelect;

			cout << "请输入新编号" << endl;
			cin >> newId;
			cout << "请输入新姓名" << endl;
			cin >> newName;
			cout << "请选择该职工的岗位:(1.普通职工 2.经理 3.总裁)" << endl;
			cin >> dSelect;
			switch (dSelect)
			{
			case 1:
				newWorker = new Employee(newId, newName, 1);
				break;
			case 2:
				newWorker = new Manager(newId, newName, 1);
				break;
			case 3:
				newWorker = new Boss(newId, newName, 1);
				break;
			default:
				break;
			}

			this->Array[ret] = newWorker;
			this->save();
			cout << "修改成功!" << endl;
		}
		else
		{
			cout << "该编号不存在!" << endl;
		}
	}
}

void WorkerManger::Find_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "记录为空!" << endl;
	}
	else
	{
		cout << "请输入想查找的职工编号:" << endl;
		int id = 0;
		cin >> id;
		int index = IsExist(id);
		if (index != -1)
		{
			cout << "查找成功!" << endl;
			this->Array[index]->ShowInfo();
		}
		else
		{
			cout << "该职工编号不存在!" << endl;
		}
	}
}

void WorkerManger::Sort_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "记录为空!" << endl;
	}
	else
	{
		cout << "请选择排序方式:1.升序排序 2.降序排序" << endl;
		int select = 0;
		cin >> select;
		if (select == 1)
		{
			for (int i = 0; i < this->num; i++)
			{
				for (int j = 0; j < this->num - i - 1; j++)
				{
					if (this->Array[j]->id > this->Array[j + 1]->id)
					{
						Worker* temp = this->Array[j + 1];
						this->Array[j + 1] = this->Array[j];
						this->Array[j] = temp;
					}
				}
			}
		}
		else
		{
			for (int i = 0; i < this->num; i++)
			{
				for (int j = 0; j < this->num - i - 1; j++)
				{
					if (this->Array[j]->id < this->Array[j + 1]->id)
					{
						Worker* temp = this->Array[j + 1];
						this->Array[j + 1] = this->Array[j];
						this->Array[j] = temp;
					}
				}
			}
		}

		this->save();
		cout << "排序完成!" << endl;
		this->Show_Emp();
		
	}
}

void WorkerManger::Clear_Emp()
{
	cout << "确认清空所有的职工信息吗?(1.确认 2.取消)" << endl;
	int select = 0;
	cin >> select;

	if (select == 1)
	{
		// ios::trunc --如果存在目标文件,删除并重新创建
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();

		if (this->Array != NULL)
		{
			for (int i = 0; i < this->num; i++)
			{
				if (this->Array[i] != NULL)
				{
					delete this->Array[i];
				}
			}
			this->num = 0;
			delete[] this->Array;
			this->Array = NULL;
			this->FileIsEmpty = true;
		}
		cout << "清空成功!" << endl;
	}
}

WorkerManger::~WorkerManger()
{
	if (this->Array != NULL)
	{
		delete[] this->Array;
		this->Array = NULL;
	}
}

职工管理系统.cpp

#pragma once
#include "WorkerManger.h"

//void test()
//{
//	Worker* 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 wm;
	int choice=0;
	while (true)
	{
		wm.ShowMenu();
		cout << "请输入您想执行操作的编号." << endl;
		cin >> choice;
		switch (choice)
		{
		case 0://退出
			wm.ExitSystem();
			break;
		case 1://添加
			wm.Add_Emp();
			break;
		case 2://显示
			wm.Show_Emp();
			break;
		case 3://删除
			wm.Del_Emp();
			break;
		case 4://修改
			wm.Mod_Emp();
			break;
		case 5://查找
			wm.Find_Emp();
			break;
		case 6://排序
			wm.Sort_Emp();
			break;
		case 7://清空
			wm.Clear_Emp();
			break;
		default:
			cout << "输入不合法,请输入0-7之间的整数!" << endl;
			system("pause");
			system("cls");
			break;
		}
		cout << endl;
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值