C++ | 实现员工管理系统(文件交互)

前言

该程序为员工的管理系统,分为三个阶级普通员工、经理、老板。分别实现了对数据的添加、删除、查询、修改等操作。且将员工信息存储到文件中。

一、头文件

boss.h

#include "woker.h"

class Boss : public Woker
{
public:
	Boss(int id, std::string name, int deptId);

	virtual void show_info();

	virtual std::string get_dept_name();
};

employee.h

#include "woker.h"

class Employee: public Woker
{
public:
	Employee(int id, std::string name, int deptId);

	virtual void show_info();

	virtual std::string get_dept_name();
};

manager.h

#include "woker.h"

class Manager : public Woker
{
public:
	Manager(int id, std::string name, int deptId);

	virtual void show_info();

	virtual std::string get_dept_name();
};

woker.h

#pragma once
#include <iostream>
#include <string>

class Woker
{
public:

	/** 查看职工信息 */
	virtual void show_info() = 0;
	/** 获取职位名称 */
	virtual std::string get_dept_name() = 0;

	int m_id;
	std::string m_name;
	int m_deptId; 
};

workerManager.h

#pragma once
#include <iostream>
#include <fstream>
#include "woker.h"

const std::string FILENAME = "data.txt";

class workerManager
{
public:
	int m_empNum;

	Woker **m_empArray;

	workerManager();

	/** 显示菜单 */
	void show_menu();

	/** 退出程序 */
	void quit_manager();

	/** 添加员工 */
	void addEmployee();

	/** 删除员工 */
	void delEmployee(int id);

	/** 修改员工信息 */
	void modifyEmployee(int id);

	/** 查询员工信息 */
	void searchEmployee(int id);

	/** 按编号排序 */
	void sortById(bool order = true);

	/** 清空文档 */
	void clearDos();

	/** 显示信息 */
	void PPrint();
	
	/** 信息存储 */
	void save_info();

	/** 读取信息 */
	void read_info();

	/** 字符串过滤 */
	char* str_pass(char * str);

	/** 判断部门 */
	Woker *judge_dept(int id, const char *name, int deptId);

	~workerManager();
};


二、源文件

boss.cpp

#include "boss.h"


Boss::Boss(int id, std::string name, int deptId)
{
	this->m_id = id;
	this->m_name = name;
	this->m_deptId = deptId;
}

void Boss::show_info()
{
	std::cout << "员工编号:" << this->m_id
		<< "\n员工姓名:" << this->m_name
		<< "\n员工部门编号:" << this->m_deptId << std::endl;
}

std::string Boss::get_dept_name()
{
	return std::string("老板");
}

employee.cpp

#include "employee.h"


Employee::Employee(int id, std::string name, int deptId)
{
	this->m_id = id;
	this->m_name = name;
	this->m_deptId = deptId;
}

void Employee::show_info()
{
	std::cout << "员工编号:" << this->m_id
		<< "\n员工姓名:" << this->m_name
		<< "\n员工部门编号:" << this->m_deptId << std::endl;
}

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

manager

#include "manager.h"


Manager::Manager(int id, std::string name, int deptId)
{
	this->m_id = id;
	this->m_name = name;
	this->m_deptId = deptId;
}

void Manager::show_info()
{
	std::cout << "员工编号:" << this->m_id
		<< "\n员工姓名:" << this->m_name
		<< "\n员工部门编号:" << this->m_deptId << std::endl;
}

std::string Manager::get_dept_name()
{
	return std::string("部门经理");
}

workManager.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "workerManager.h"
#include "employee.h"
#include "manager.h"
#include "woker.h"
#include "boss.h"

extern const std::string FILENAME;

workerManager::workerManager()
{
	this->m_empArray = NULL;
	this->m_empNum = 0;
	this->read_info();
}

void workerManager::show_menu(){

	std::cout << " ********************************************" << std::endl;
	std::cout << " ********* 欢迎使用职工管理系统! ************" << std::endl ;
	std::cout << " ************ 0. 退出管理程序  **************" << std::endl;
	std::cout << " ************ 1. 增加职工信息  **************" <<std::endl ;
	std::cout << " ************ 2. 显示职工信息  **************" << std::endl;
	std::cout << " ************ 3. 删除离职职工  **************" << std::endl;
	std::cout << " ************ 4. 修改职工信息  **************" << std::endl;
	std::cout << " ************ 5. 查找职工信息  **************" << std::endl;
	std::cout << " ************ 6. 按照编号排序  **************" << std::endl;
	std::cout << " ************ 7. 清空所有文档  **************"<< std::endl;
	std::cout << " ********************************************" << std::endl;
}

/** 退出程序 */
void workerManager::quit_manager()
{
	this->save_info();
	std::cout << "欢迎下次使用..." << std::endl;
	system("pause");
	exit(0);
}

/** 添加员工 */
void workerManager::addEmployee()
{
	int addNum;
	std::cout << "请输入要添加的个数:";
	std::cin >> addNum;
	if (addNum <= 0) return;
	int newsize = this->m_empNum + addNum;

	/* 创建空间  */
	Woker **newWoker = new Woker*[newsize];

	/* 将原来的数据存入心的空间 */
	if (this->m_empArray != NULL)
	{
		for (int i = 0; i < this->m_empNum; ++i)
			newWoker[i] = this->m_empArray[i];
	}
	/* 插入新的数据 */
	for (int j = 0; j < addNum; ++j)
	{
		std::cout << "----------------------------" << std::endl;
		int tmp_id;
		std::string tmp_name;
		int tmp_deptId;
		std::cout << "请输入第" << j + 1 << "个员工编号:";
		std::cin >> tmp_id;
		std::cout << "请输入第" << j + 1 << "个员工名称:";
		std::cin >> tmp_name;
		std::cout << "请输入第" << j + 1 << "个部门编号:";
		std::cin >> tmp_deptId;
		/*
		Woker *tmp_woker = NULL;

		switch (tmp_deptId)
		{
		case 1:
			tmp_woker = new Employee(tmp_id, tmp_name, tmp_deptId);
			break;
		case 2:
			tmp_woker = new Manager(tmp_id, tmp_name, tmp_deptId);
			break;
		case 3:
			tmp_woker = new Boss(tmp_id, tmp_name, tmp_deptId);
			break;
		default:
			std::cout << "没有该部门..." << std::endl;
			break;
		}
		newWoker[this->m_empNum + j] = tmp_woker;
		*/
		newWoker[this->m_empNum + j] = this->judge_dept(tmp_id, tmp_name.c_str(), tmp_deptId);

	}
	/* 更新数据 */
	delete[] this->m_empArray;
	this->m_empArray = newWoker;
	this->m_empNum = newsize;

	std::cout << "成功添加" << addNum << "个员工...." << std::endl;
}

/** 删除员工根据员工编号 */
void workerManager::delEmployee(int id)
{
	int i = 0;
	for (i = 0; i < this->m_empNum; ++i){
		if (id == this->m_empArray[i]->m_id)
			break;
	}
	if (i == this->m_empNum)
	{
		std::cout << "没有该员工信息,请输入正确的员工编号....." << std::endl;
		return;  
	}
	for (int j = i+1; j < this->m_empNum; ++j)
		this->m_empArray[j-1] = this->m_empArray[j];
	std::cout << this->m_empArray[i]->m_id << "删除成功..." << std::endl;

}

/** 修改员工信息 */
void workerManager::modifyEmployee(int id)
{
	for (int i = 0; i < this->m_empNum; ++i){
		if (id == this->m_empArray[i]->m_id)
		{
			// 打印原信息
			std::cout << "员工编号:" << this->m_empArray[i]->m_id
				<< "\n员工姓名:" << this->m_empArray[i]->m_name
				<< "\n员工部门编号:" << this->m_empArray[i]->m_deptId << std::endl;
			// 修改
			std::cout << "请输入要修改的上述员工信息:" << std::endl;
			std::cout << "请输入员工姓名:";
			std::cin >> this->m_empArray[i]->m_name;
			std::cout << "请输入员工部门编号:";
			std::cin >> this->m_empArray[i]->m_deptId;

			std::cout << this->m_empArray[i]->m_id << "信息修改成功...." << std::endl;
			return;
		}
	}

	std::cout << "没有查询到该员工,请输入正确的员工编号....." << std::endl;
}

/** 查询员工信息 */
void workerManager::searchEmployee(int id)
{
	for (int i = 0; i < this->m_empNum; ++i){
		if (id == this->m_empArray[i]->m_id)
		{
			std::cout << "员工编号:" << this->m_empArray[i]->m_id
				<< "\n员工姓名:" << this->m_empArray[i]->m_name
				<< "\n员工部门编号:" << this->m_empArray[i]->m_deptId << std::endl;
			return;
		}
	}

	std::cout << "没有查询到该员工,请输入正确的员工编号....." << std::endl;
}

/** 按编号排序 */
void workerManager::sortById(bool order)
{
	// 为true是正序
	if (order == true)
	{
		for (int i = 0; i < this->m_empNum; ++i){
			for (int j = i + 1; j < this->m_empNum; ++j)
			{
				Woker * tmp;
				if (this->m_empArray[i]->m_id < this->m_empArray[j]->m_id)
				{
					tmp = this->m_empArray[i];
					this->m_empArray[i] = this->m_empArray[j];
					this->m_empArray[j] = tmp;
				}
			}
		}
	}
	this->PPrint();
	
}

/** 清空文档 */
void workerManager::clearDos()
{
	std::ofstream fp;
	fp.open(FILENAME, std::ios::trunc);

	fp.close();
}

/** 显示信息 */
void workerManager::PPrint()
{
	std::cout << "所有员工信息:" << std::endl;
	std::cout << this->m_empNum << std::endl;
	for (int i = 0; i < this->m_empNum; ++i)
	{
		std::cout << "----------------------------" << std::endl;
		std::cout << "员工编号:" << this->m_empArray[i]->m_id
			<< "\n员工姓名:" << this->m_empArray[i]->m_name
			<< "\n员工部门编号:" << this->m_empArray[i]->get_dept_name() << std::endl;
	}
}

/** 信息存储 */
void workerManager::save_info()
{
	std::ofstream fp;
	fp.open(FILENAME, std::ios::out | std::ios::app);
	if (!fp.is_open())
	{
		std::cout << "文件打开失败..." << std::endl;
		return;
	}
	for (int i = 0; i < this->m_empNum; ++i)
		fp << "----------------------------\n" << "员工编号:" << this->m_empArray[i]->m_id
		<< "\n员工姓名:" << this->m_empArray[i]->m_name
		<< "\n员工部门编号:" << this->m_empArray[i]->m_deptId << std::endl;
	std::cout << "文件信息已存档..." << std::endl;
	fp.close();
}

/** 读取信息 */
void workerManager::read_info()
{
	std::ifstream fp;
	fp.open(FILENAME, std::ios::in);
	int count = 0, i = 0, j = 0;

	/* 判断文件是否打开成功 */
	if (!fp.is_open())
	{	
		std::cout << "文件不存在,无法加载文档..." << std::endl;
		return;
	}
	char buf[128] = { 0 };
	char *tmpData[128] = { 0 };
	std::cout << "加载文档信息..." << std::endl;
	/* 获取文件中职员个数 */
	while (fp >> buf)
	{
		tmpData[i] = new char [128];
		/* 标记个数 */ 
		if ('-' == buf[0]) 
		{
			count++;
			continue;
		}

		/* 切割信息 */  
		char* pos = strstr(buf, ":");
		strcpy(tmpData[i], pos + 1);
		i++;
	}


	/* 暂存信息 */
	Woker **tmpWorker = new Woker *[count+1];
	int tmp_id, tmp_deptId;
	char *tmp_name;
	/* 循环每个员工信息 */
	for (int x = 0; x < i / 3; ++x)
	{
		Woker *tmp_woker = NULL;
		for (j = x * 3; j < (x + 1) * 3; ++j)
		{
			if (j % 3 == 0)
				tmp_id = atoi(tmpData[j]);
			else if (j % 3 == 1)
				tmp_name = tmpData[j];
			else
				tmp_deptId = atoi(tmpData[j]);
		}

		tmpWorker[x] = this->judge_dept(tmp_id, tmp_name, tmp_deptId);
	}

	this->m_empArray = tmpWorker;
	this->m_empNum = count;					
	std::cout << "文档加载成功..." << std::endl;
	fp.close();

	delete[] * tmpData;
}

/** 判断部门 */
Woker *workerManager::judge_dept(int id, const char *name, int deptId)
{
	Woker *tmp_woker = NULL;
	switch (deptId)
	{
	case 1:
		tmp_woker = new Employee(id, name, deptId);
		break;
	case 2:
		tmp_woker = new Manager(id, name, deptId);
		break;
	case 3:
		tmp_woker = new Boss(id, name, deptId);
		break;
	default:
		std::cout << "没有该部门..." << std::endl;
		break;
	}

	return tmp_woker;
}

/** 字符串过滤 */
char* workerManager::str_pass(char * str)
{
	char buf[128] = { 0 };
	for (int i = 0; i < strlen(str) - 1; ++i)
	{
		if (str[i] == ' ')
			break;
		buf[i] = str[i];
	}

	return str;
}

workerManager::~workerManager()
{
	this->save_info();
	if (this->m_empArray != NULL)
	{
		delete[] this->m_empArray;
		this->m_empArray = NULL;
	}
}

main.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "workerManager.h"
#include "boss.h"
#include "employee.h"
#include "manager.h"
#include "woker.h"



void test()
{
	workerManager worker;

	int choice = 0, id = 0;
	while (true)
	{
		worker.show_menu();
		std::cout << "请输入选项【0-7】:";
		std::cin >> choice;
		switch (choice)
		{
		case 0:
			worker.quit_manager();
			break;
		case 1:		// 增加员工信息
			worker.addEmployee();
			break;
		case 2:		// 显示员工信息
			worker.PPrint();
			break;
		case 3:		// 删除离职员工
			std::cout << "请输入要删除的员工id:";
			std::cin >> id;
			worker.delEmployee(id);
			break;
		case 4:		// 修改职工信息
			std::cout << "请输入要删除的员工id:";
			std::cin >> id;
			worker.modifyEmployee(id);
			break;
		case 5:		// 查找职工信息
			std::cout << "请输入要删除的员工id:";
			std::cin >> id;
			worker.searchEmployee(id);
			break;
		case 6:		// 按照编号排序
			worker.sortById();
			break;
		case 7:		// 清空文档
			worker.clearDos();
			break;
		default:
			system("cls");
			std::cout << "没有该选项,请重新输入【0-7】任选一项:" << std::endl;
			break;
		}
		system("pause");
		system("cls");
	}
}


void main()
{
	test();

	system("pause");
}

在这里插入图片描述

员工管理系统】 问题描述:每个员工的信息包括:编号、姓名、性别、出生年月、学历、职务、电话、住址等。系统能够完成员工信息的查询、更新、插入、删除、排序等功能。 基本要求:排序:按不同关键字,对所有员工的信息进行排序;查询:按特定条件查找员工;更新,按编号对某个员工的某项信息进行修改;插入,加入新员工的信息;删除,按编号删除已离职的员工的信息。 选作内容:实现图形用户界面。 通过链表实现 数据结构: #include #include #include #include #include using namespace std; typedef struct workers{ char name[15];//姓名 char department[18];//单位 char gender;//性别 unsigned int age;//年龄 unsigned long long telephone;//电话 unsigned long wage;//工资 unsigned long num;//职工号 struct workers *next; }*Linklist,Lnode; void frist_print() { printf("\t\t⊙▽⊙ ⊙▽⊙ ⊙▽⊙ ⊙▽⊙ ⊙▽⊙ ⊙▽⊙ \n\n"); printf("\t\t\t欢迎进入员工管理系统\n"); } void menu() { printf("\n\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); printf("\t\t \t ◎1.创建员工信息\t \n"); printf("\t\t \t ◎2.插入员工信息\t \n"); printf("\t\t \t ◎3.修改员工信息\t \n"); printf("\t\t \t ◎4.删除员工信息\t \n"); printf("\t\t \t ◎5.查询员工信息\t \n"); printf("\t\t \t ◎6.员工信息排序\t \n"); printf("\t\t \t ◎7.显示员工信息\t \n"); printf("\t\t \t ◎8.员工工资情况\t \n"); printf("\n\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); printf("注意:输入均以回车作为结束\n"); printf("please choise 1--8:\t "); //putchar(12); } void Inset(Linklist Head){ Linklist s,L; unsigned int agee; unsigned long wagee,numm;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jxiepc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值