软件测试最全C++入门案例——基于多态的职工管理系统 & 控制台项目_c+(3),成功入职网易月薪35K

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

{
public:
// 构造函数
Manager(int id, string name, int dId);

// 方法, virtual 可删可不删
virtual void showInfo();
virtual string getDeptment();

};


### 类的实现


![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/e8c171f359af4673b29a834dd14ba9d8.png)



#include “manager.h”

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

void Manager::showInfo()
{
cout << “职工编号:” << this->id
<< “\t职工姓名:” << this->name
<< “\t岗位:” << this->getDeptment()
<< “\t岗位职责:服从老板,安排员工干活”
<< endl;
}

string Manager::getDeptment()
{
return string(“经理”);
}


## 主程序


### 头文件中定义接口


在头文件中定义显示菜单的页面接口,员工管理相关的显示员工,增删改查等相关操作,另外包括文件交互,把员工的相关信息存储到txt文本文件中。


![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/83d48097f91a4065948bb8d3585a0f52.png)



#pragma once // 防止头文件重复包含
#include // 包含输入和输出流的头文件
using namespace std; // 使用标准命名空间

#include “worker.h”
#include “emp.h”
#include “manager.h”
#include “boss.h”

#include
#define FILENAME “workerNo.txt”

class WorkerManager
{
public:
// 构造函数
WorkerManager();

// 展示菜单
void ShowMenu();

// 显示员工
void ShowEmp();

// 退出程序
void exitSystem();

// 记录职工人数
int workerNum;

// 职工的数组
Worker \*\* workerArray;

void addWorker();

// 保存文件,持久化
void save();

bool isFileEmpty;

// 统计人数
int getWorkerNum();

// 初始化员工
void initWorker();

// 删除职工
void delEmp();

// 判断职工是否存在
int isExist(int id);

// 修改员工
void updateEmp();

// 查找员工
void findEmp();

// 按照编号排序
void sortEmp();

// 清空文件
void cleanFile();

// 析构函数
~WorkerManager();

};


### cpp文件中具体实现



#include “workerManage.h”

WorkerManager::WorkerManager()
{
// 文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in); // 读取文件
if (!ifs.is_open())
{
cout << “文件不存” << endl;
this->workerNum = 0;
this->workerArray = NULL;
this->isFileEmpty = true; // 是否为空
ifs.close();
return;
}
// 文件存在,数据为空
char ch;
ifs >> ch;
if (ifs.eof()) // 代表文件为空
{
cout << “文件为空” << endl;
this->workerNum = 0;
this->workerArray = NULL;
this->isFileEmpty = true; // 是否为空
ifs.close();
return;
}

// 当文件存在,且记录有数据
int num = this->getWorkerNum();
cout << "职工人数为:" << num << endl;
this->workerNum = num;
this->workerArray = new Worker \* [this->workerNum];
// 将文件中数据存到数组中
this->initWorker();

}

void WorkerManager::ShowMenu()
{
cout << “=" << endl;
cout << “职工管理系统” << endl;
cout << “1、添加职工” << endl;
cout << “2、显示职工” << endl;
cout << “3、删除职工” << endl;
cout << “4、修改职工” << endl;
cout << “5、查找职工” << endl;
cout << “6、排序职工” << endl;
cout << “7、清空文件” << endl;
cout << “0、退出系统” << endl;
cout << "
=” << endl;
}

void WorkerManager::ShowEmp()
{
// 判断文件是否为空
if (this->isFileEmpty)
{
cout << “文件不存在” << endl;
}
else
{
// 输出原有的员工
for (int i = 0; i < this->workerNum; i++)
{
workerArray[i]->showInfo(); // 利用多态调用程序接口
}
}
system(“pause”);
system(“cls”);
}

void WorkerManager::exitSystem()
{
cout << “欢迎下次使用” << endl;
system(“pause”);
exit(0); // 直接退出
}

void WorkerManager::addWorker()
{
cout << “输入添加的职工数量” << endl;
int addNum = 0;
cin >> addNum;
if (addNum>0)
{
// 计算添加的空间大小
int newSize = this->workerNum + addNum;

	// 开辟新空间 写入newSpace时缓存区溢出
	Worker \*\* newSpace = new Worker\* [newSize];

	// 原来是不是空的
	if (this->workerArray !=NULL)
	{
		for (int i = 0; i < this->workerNum; i++)
		{
			newSpace[i] = this->workerArray[i];
		}
	}
	// 添加新的数据
	for (int i = 0; i < addNum; i++)
	{
		int id; // 职工编号
		string name; // 职工姓名
		int dId; // 部分编码
		cout << "输入第" << i+1 << " 个新职工的编号" << endl;
		cin >> id;
		cout << "输入第" << i + 1 << " 个新职工的姓名" << endl;
		cin >> name;
		cout << "选择第" << i + 1 << " 个新职工的岗位" << endl;
		cout << "1普通,2经理,3老板" << endl;
		cin >> dId;
		
		Worker \* worker = NULL;
		switch (dId)
		{
		case 1:
			worker = new Emp(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->workerNum + i] = worker;
	}

	// 释放原有空间
	delete[] this->workerArray;

	// 更改新空间的指向
	this->workerArray = newSpace;

	this->workerNum = newSize;

	cout << "添加了" << addNum << " 个员工" << endl;
	this->isFileEmpty = false; // 是否为空
	// 保存数据到文件中
	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->workerNum; i++)
{
ofs << this->workerArray[i]->id << " "
<< this->workerArray[i]->name << " "
<< this->workerArray[i]->dep << endl;
}
}

int WorkerManager::getWorkerNum()
{
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::initWorker()
{
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 Emp(id, name, dId);
	}
	else if (dId == 2) // 经理
	{
		worker = new Manager(id, name, dId);
	}
	else
	{
		worker = new Boss(id, name, dId);
	}
	this->workerArray[index] = worker;
	index++;
}

// 关闭文件
ifs.close();

}

void WorkerManager::delEmp()
{
if (this->isFileEmpty)
{
cout << “文件不存在或者为空” << endl;
}
else {
// 输入要删除的员工的id
cout << “输入要删除的员工的id” << endl;
int id = 0;
cin >> id;
if (this->isExist(id) != -1) // 存在
{
for (int i = id; i < this->workerNum -1; i++)
{
this->workerArray[i] = this->workerArray[i + 1];
}
this->workerNum–; // 更新人员数量
// 更新文件
this->save();
}
else {
cout << “未找到职工” << endl;
}
}
system(“pause”);
system(“cls”);
}

int WorkerManager::isExist(int id)
{
int index = -1;
for (int i = 0; i < this->workerNum; i++)
{
if (this->workerArray[i]->id == id )
{
index = i; // 找到了职工
break;
}
}
return index;
}

void WorkerManager::updateEmp()
{
if (this->isFileEmpty)
{
cout << “文件为空,或者不存在” << endl;
}
else
{
cout << “输入要修改的员工编号” << endl;
int id;
cin >> id;
int index = this->isExist(id);
if (index != -1)
{
delete this->workerArray[index];
int newId = 0;
string newName = “”;
int dId = 0;
cout << “输入第职工的新编号” << endl;
cin >> newId;
cout << “输入职工的新姓名” << endl;
cin >> newName;
cout << “选择职工的新岗位” << endl;
cout << “1普通,2经理,3老板” << endl;
cin >> dId;

		Worker\* worker = NULL;
		switch (dId)
		{
		case 1:
			worker = new Emp(newId, newName, dId);
			break;
		case 2:
			worker = new Manager(newId, newName, dId);
			break;
		case 3:
			worker = new Boss(newId, newName, dId);
			break;
		default:
			break;
		}

		// 更新到数组中
		this->workerArray[index] = worker;
		cout << "修改成功" << endl;
		this->save();
	}
	else
	{
		cout << "修改失败,查无此人" << endl;
	}
}
system("pause");
system("cls");

}

void WorkerManager::findEmp()
{
if (this->isFileEmpty)
{
cout << “文件为空,或者不存在” << endl;
}
else {
cout << “输入查找方式,1.按照编号,2.按照姓名” << endl;
int flag = 0;
cin >> flag;
if (flag == 1)
{
// 按照编号查询
int id = 0;
cout << “输入查找编号” << endl;
cin >> id;
int find = this->isExist(id);
if (find != -1)
{
cout << “查找成功” << endl;
this->workerArray[find]->showInfo();
}
else
{
cout << “查无此人” << endl;
}
}
else if (flag == 2) {
// 按照姓名查询

		string name;
		cout << "输入查找的姓名" << endl;
		cin >> name;
		// 是否查到的标志
		bool findFlag = false;

		for (int i = 0; i < this->workerNum; i++)
		{
			if (this->workerArray[i]->name == name) {
				cout << " 查询成功,职工编号为: "
					<< this->workerArray[i]->id
					<<  "职工信息如下:" << endl;
				// 调用显示
				this->workerArray[i]->showInfo();

				findFlag = true;
			}
		}

		if (!findFlag)
		{
			cout << "查找失败" << endl;
		}
	}
	else
	{
		cout << "输入有误" << endl;
	}
}	
system("pause");
system("cls");

}

void WorkerManager::sortEmp()
{
if (this->isFileEmpty)
{
cout << “文件为空,或者不存在” << endl;
system(“pause”);
system(“cls”);
}
else
{
cout << “输入排序方式,1.序号升序,2.序号降序” << endl;
int flag = 0;
cin >> flag;

	if (flag == 1 || flag ==2)
	{
		// 升序
		for (int i = 0; i < this->workerNum; i++)
		{

			int minOrMax = i; // 生命最大值或最小值下标
			for (int j = i+1; j < this->workerNum; j++)
			{
				if (flag == 1) // 升序
				{
					// 如果是升序,当前假设的最大值
					if (this->workerArray[minOrMax]->id > this->workerArray[j]->id)
					{
						minOrMax = j;
					}
				}
				else // 降序
				{
					if (this->workerArray[minOrMax]->id < this->workerArray[j]->id)
					{
						minOrMax = j;
					}
				}

			}
			// 判断一开始认定的 最大或最小值 是不是 计算得到的最大或最小值,如果不是,交换数据
			if (i != minOrMax)
			{
				Worker \* temp = this->workerArray[i];
				this->workerArray[i] = this->workerArray[minOrMax];
				this->workerArray[minOrMax] = temp;
			}
		}
		cout << "排序成功,排序后的结果为:" << endl;
		this->save();
		this->ShowEmp();
	}
	else
	{
		cout << "输入有误" << endl;
	}
}

}

void WorkerManager::cleanFile()
{

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

rray[minOrMax];
this->workerArray[minOrMax] = temp;
}
}
cout << “排序成功,排序后的结果为:” << endl;
this->save();
this->ShowEmp();
}
else
{
cout << “输入有误” << endl;
}
}

}

void WorkerManager::cleanFile()
{

[外链图片转存中…(img-tPX4gcbV-1715550077612)]
[外链图片转存中…(img-MAJ5ul74-1715550077612)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值