图书管理系统C++(包含管理员登录、用户注册、租借图书、归还图书等一系列功能)

 systemManage.cpp

#include"systemManage.h"
#include <fstream>
#include<vector>
#include<conio.h>

//构造函数
SystemManage::SystemManage()
{
	this->m_Index = 0;
	this->m_Error = 0;
}

//菜单
void SystemManage::showMenu()
{
	cout << "                      ******************************************" << endl;
	cout << "                      ***      欢迎使用图书管理系统          ***" << endl;
	cout << "                      ******************************************" << endl;
	cout << "                      ***  1.租借图书        2.归还图书      ***" << endl;
	cout << "                      ***  3.图书丢失        4.查看所有图书  ***" << endl;
	cout << "                      ***  5.查找图书        6.用户注册      ***" << endl;
	cout << "                      ***  7.管理员特权      8.借书须知      ***" << endl;
	cout << "                      ******************************************" << endl;
}
//管理员骨架
void SystemManage::guanLiYuanGuJia()
{
	//登录
	this->managerLogOn();

	//登录失败 
	if (this->m_Index == -1)//障碍
	{
		//退回
		this->m_Index = 0;//初始化

		return;//返回至图书管理系统主页面
	}

		//登录成功
		while (1)
		{
			this->managerShowMenu();

			switch (getchar())
			{
			case '1'://添加图书
				this->addBook();
				break;

			case '2'://下架图书
				this->outBook();
				if (this->m_Error == -1)
				{
					this->m_Error = 0;
					break;
				}
				this->saveBook(this->m_shengXiaV);
				break;

			case '3'://查看借书人信息
				this->lookBorrowerData();
				break;

			case '4'://返回菜单
				return;//返回上一级调用函数guanLiYuanGuJia()
				break;
			case '5':
				
				this->changeMima();
				if (this->m_Error == -1)
				{
					this->m_Error = 0;
					break;
				}
				break;
			case '0'://退出系统
				cout << "欢迎下次使用" << endl;
				exit(0);
				break;

			default:
				std::system("cls");
				break;

			}
	
	}
	

	std::system("pause");

}

//管理员登录
void SystemManage::managerLogOn()
{
	//清空界面
	std::system("cls");
	cout << "                      ******************************************" << endl;
	cout << "                      ***      欢迎来到管理员登录页面        ***" << endl;
	cout << "                      ******************************************" << endl;

	//读取文件的数据

	ifstream ifs; 
	ifs.open("ManageData.txt",ios::in);
	if (!ifs.is_open())
	{
		cout << "文件打开失败!" << endl;
		std::system("pause");
		return;
	}

	//文件打开成功
	string data;

	//存放子串
	vector<string>v;

	while (ifs >> data)
	{
		//cout << data << endl;大聪明,123456,
		int pos = -1;
		int start = 0;
		while (1)
		{
			pos = data.find(",", start);
			if (pos == -1)
			{
				break;
				//跳出
			}
			string target=data.substr(start, pos - start);

			//放到容器中
			v.push_back(target);

			start = pos + 1;//更新起始点 跳到123456
		}
	}
	ifs.close();

	//登录输入

	string name;//姓名
	char words[50];//密码
	cout << "请输入管理员姓名:" << endl;
	cin >> name;

	//密码输入
	int i = 0;
	cout << "请输入管理员密码:" << endl;

	while (i < 9 && (words[i] = _getch()) && words[i] != '\r')//如果输入超过9次 或者 遇到回车符就跳出循环
	{
		printf("*");//掩饰密码
			i++;
	}
	cout << endl;
	words[i] = 0;//字符串结束标志
	string mima(words);//转为字符串型

	//对比
	if (name == v[0] && mima == v[1])
	{
		cout << "登录成功!" << endl;
		std::system("pause");
		return;
	}
	else {
		cout << "登录失败!" << endl;
		std::system("pause");
		this->m_Index = -1;
		return;
	}

	
}

//管理员菜单
void SystemManage::managerShowMenu()
{
	std::system("cls");
	cout << "                      ******************************************" << endl;
	cout << "                      ***      欢迎来到管理员页面            ***" << endl;
	cout << "                      ******************************************" << endl;
	cout << "                      ***  1.添加图书          2.下架图书    ***" << endl;
	cout << "                      ***  3.查看借书人信息    4.返回主菜单  ***" << endl;
	cout << "                      ***  5.修改管理员密码    0.退出系统    ***" << endl;
	cout << "                      ******************************************" << endl;
}

//图书丢失
void SystemManage::reduceBook()
{
	std::system("cls");
	cout << "                      ******************************************" << endl;
	cout << "                      ***              图书丢失              ***" << endl;
	cout << "                      ******************************************" << endl;
	cout << endl;
	cout << "                      *** 丢失图书请联系图书丢失管理员:小黄  ***" << endl;
	cout << endl;
	cout << "                      *** 赔偿金添加小黄微信进行微信转账赔偿 ***" << endl;
	cout << endl;
	cout << endl;
	cout << endl;
	cout << "小黄电话:10086" << endl;
	cout << "小黄微信:XH123456789" << endl;
	std::system("pause");
	std::system("cls");
}

//用户注册
void SystemManage::addUser()
{
	//显示注册界面
	std::system("cls");
	cout << "                      ******************************************" << endl;
	cout << "                      ***              用户注册              ***" << endl;
	cout << "                      ******************************************" << endl;

	//输入身份证
	cout << "请输入身份证号码:" << endl;
	string idCard;
	cin >> idCard;

	//判断 18 0-9 idCard[17]->A-Z
	for (int i = 0; i < 18; i++)
	{
		if (idCard.size() != 18 || idCard[i]>'9'||idCard[i]<'0'||(idCard[17]>'Z'&&idCard[17]<'A') || (idCard[17]>'9' && idCard[17] < '0'))//不符合条件判断
		{
			cout << "输入的账号不符合规范!" << endl;
			std::system("pause");
			return;
		}
	}

	//创建密码
	string mima1;
	string mima2;
	cout << "请输入密码:" << endl;
	cin >> mima1;
	cout << "请再此输入密码:" << endl;
	cin >> mima2;

	//比对密码
	if (mima1 != mima2)
	{
		cout << "你再次输入的密码不匹配!" << endl;
		cout << " 请重新进入页面!" << endl;
		std::system("pause");
		std::system("cls");
		return;
	}

	//打开文件
	ifstream ifs;//输入流
	ifs.open("User.txt", ios::out);
	if (!ifs.is_open())
	{
		cout << "文件打开失败!" << endl;
		std::system("pause");
		return;
	}
	else
	{
		//解析数据
		string userData;
		while (ifs >> userData)
		{
			int pos = -1;
			int start = 0;
			while (1) {
				pos = userData.find(",", start);
				if (pos == -1)
				{
					break;
				}
				else {
					string target = userData.substr(start, pos - start);
					if (target == idCard)
					{
						cout << "账号已存在!《无法再次注册》" << endl;
						std::system("pause");
						std::system("cls");
						return;
					}
				}

				//更改截取的起始点
				start = pos + 1;
			}

		}
	}




	//注册成功后 将数据传入文件中
	ofstream ofs;
	ofs.open("User.txt", ios::in | ios::app);//输入数据 可追加数据
	if (!ofs.is_open())//打开失败
	{
		cout << "文件打开失败!" << endl;
		std::system("pause");
		return;
	}

	//打开成功
	ofs << idCard << "," << mima1 << ",";

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

	cout << "注册成功!" << endl;
	std::system("pause");
}

//借书须知
void SystemManage::borrowBookNotice()
{
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***              借书须知                   ***" << endl;
	cout << "                      ***********************************************" << endl;
	cout << "                      ***             系统尚未健全                ***" << endl;
	cout << "                      ***                                         ***" << endl;
	cout << "                      *** 图书归还一次只能归还一本(按照租借顺序)***" << endl;
	cout << "                      ***                                         ***" << endl;
	cout << "                      ***          租借图书请知晓以上情况!       ***" << endl;
	cout << "                      ***********************************************" << endl;
	std::system("pause");
	std::system("cls");
}

//添加图书
void SystemManage::addBook()
{
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***              添加图书                   ***" << endl;
	cout << "                      ***********************************************" << endl;
	int num = 0;//添加图书数量
	string bookName;//书名
	int id;//编号
	int type;//类型 1 2 3
	Books b;//图书
	 
	cout << "请输入本次添加图书的数量:" << endl;
	cin >> num;

	for (int i = 0; i < num; i++)
	{
		cout << "请输入第" << i + 1 << "本图书的书名:" << endl;
		cin >> bookName;
		cout << "请输入第" << i + 1 << "本图书的编号:" << endl;
		cin >> id;
		cout << "请输入第" << i + 1 << "本图书的类型:" << endl;
		cout << "1--哲学" << endl;
		cout << "2--小说" << endl;
		cout << "3--儿童读物" << endl;
		while (1)
		{
			cin >> type;
			if (type<1&&type>3)
			{
				cout << "输入有误,请重新输入图书类型!" << endl;

			}
			else {
		  break;
			}
		}
		b.m_Name = bookName;
		b.m_Type = type;
		//把用户添加的图书信息保存到系统类的一个容器中
		this->m_Bookm.insert(pair<int,Books>(id, b));
	}

	//保存容器里的数据
	this->saveData();

	this->m_Bookm.clear();

	cout << "成功添加" << num <<"本图书!" << endl;
	std::system("pause");
	std::system("cls");
}


//保存添加图书信息(添加图书专用)
void SystemManage::saveData()
{
	ofstream ofs;
	ofs.open("系统图书.txt", ios::out | ios::app);
	if (!ofs.is_open())
	{
		cout << "文件打开失败!" << endl;
		std::system("pause");
		std::system("cls");
		return;
	}
	for (map<int, Books>::iterator it = this->m_Bookm.begin(); it != this->m_Bookm.end(); it++)
	{
		ofs << it->first << "," << it->second.m_Name << ",";
		if (it->second.m_Type == 1)
		{
			ofs << "哲学," << endl;
		}
		if (it->second.m_Type == 2)
		{
			ofs << "小说," << endl;
		}
		if (it->second.m_Type == 3)
		{
			ofs << "儿童读物," << endl;
		}
	}
	ofs.close();
}

//查看所有图书
void SystemManage::lookOverBook()
{
	//展示所有图书界面
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***             展示所有图书                ***" << endl;
	cout << "                      ***********************************************" << endl;

	//创建输入流文件对象
	ifstream ifs;

	//打开文件
	ifs.open("系统图书.txt", ios::in);

	//将文件内所有图书信息放到临时容器中
	vector<string>tempV;
	string bookData;
	while (ifs >> bookData)
	{
		int pos = -1;
		int start = 0;
		
		while (1)
		{
			pos = bookData.find(",", start);
			if (pos == -1)
			{
				break;
			}
			string target = bookData.substr(start, pos - start);
			tempV.push_back(target);

			//起始点改变
			start = pos + 1;
	     }

	}

	//通过迭代器将数据显示至窗口
	int count = 0;
	for (vector<string>::iterator it = tempV.begin(); it != tempV.end(); it++)
	{
		count++;
		if (count == 1)
		{
			cout << "图书编号:" << setw(8) << left << *it << " ";
		}
		if (count == 2)
		{
			cout << "图书名称:" << setw(30) << left << *it << " ";
		}
		if (count == 3)
		{
			cout << "图书类型:" << setw(10) << left << *it << " ";
			cout << endl;
			count = 0;
		}
	}
 
	std::system("pause");
	std::system("cls");
}

//查找图书
void SystemManage::findBook()
{
	//显示界面
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***                查找图书                 ***" << endl;
	cout << "                      ***********************************************" << endl;

	//读文件到项目内  string data
	ifstream ifs;
	ifs.open("系统图书.txt", ios::out);

	//1.空   
	char ch;
	ifs >> ch;
	if (ch == ifs.eof())//文件为空
	{
		cout << "系统里没有图书!" << endl;
		ifs.close();
		std::system("pause");
		return;
	}

	//读回来
	ifs.putback(ch);

	vector<string>v;//存放图书的数据

	string data;
	while (ifs >> data)
	{
		int pos = -1;
		int start = 0;
		while (1)
		{
			pos =data.find(",", start);

			//判断
			if (pos == -1)
			{
				break;
			}
			string target=data.substr(start, pos - start);
			v.push_back(target);

			//改变起始点位置
			start = pos + 1;
		}
		//cout << data << endl;
	}

	//非空 解析到容器里
	cout << "请输入查找的方式:" << endl;
	cout << "1-------输入图书编号进行查找" << endl;
	cout << "2-------输入图书名称进行查找" << endl;

	int index = 0;
	char select = '0';
	while (1)
	{
		cin >> select;
		switch (select)
		{
		case '1'://图书编号
			this->findById(v);
			index = 1;
			break;
		case '2'://图书名称
			this->findByBookName(v);
			index = 1;
			break;
		default://一直循环
			cout << "你的输入有误,请重新输入!" << endl;
			std::system("pause");
			break;
		}
		if (index == 1)//退出循环
		{
			break;
		}
	}
	


	ifs.close();
}

//按图书编号查找
void SystemManage::findById(const vector<string>& v)
{
	string id;
	cout << "请输入图书编号:" << endl;
	cin >> id;
	for (vector<string>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		if (*it == id)
		{
			cout << "系统存在你要查找的图书!" << endl;
			cout << endl;
			cout << "图书编号:" << setw(8) << left << *it;
			it++;
			cout << "图书名称:" << setw(30) << left << *it;
			it++;
			cout << "图书类型:" << setw(10) << left << *it<< endl;
			cout << endl;
			cout << "可以租借这本书!" << endl;
			cout << endl;
			std::system("pause");
			std::system("cls");
			return;
		}


	}

	//没找到该图书
	cout << endl;
	cout << "系统内部没有该书信息!" << endl;
	cout << endl;
	std::system("pause");
	
}

//按图书名称查找
void SystemManage::findByBookName(const vector<string>& v)
{

	string name;
	cout << "请输入图书名称:" << endl;
	cin >> name;
	for (vector<string>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		if (*it == name)
		{
			cout << "系统存在你要查找的图书!" << endl;
			cout << endl;
			it--;
			cout << "图书编号:" << setw(8) << left << *it;
			it++;
			cout << "图书名称:" << setw(30) << left << *it;
			it++;
			cout << "图书类型:" << setw(10) << left << *it << endl;
			cout << endl;
			cout << "可以租借这本书!" << endl;
			cout << endl;
			std::system("pause");
			std::system("cls");
			return;
		}


	}

	//没找到该图书
	cout << endl;
	cout << "系统内部没有该书信息!" << endl;
	cout << endl;
	std::system("pause");
	
}

//下架图书
void SystemManage::outBook()
{
	//展示页面
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***                下架图书                 ***" << endl;
	cout << "                      ***********************************************" << endl;

	//提示输入要下架的图书
	cout << "请输入要下架的图书名称:" << endl;
	string bookName;
	cin >> bookName;

	//打开系统文件
	ifstream ifs;
	ifs.open("系统图书.txt", ios::in);

	//把子串放到一个临时的容器
	vector<string>v_temp;

	//拿到文件的所有数据 字符串
	string bookData1;
	while (ifs>>bookData1)
	{
		//解析字符串
		int pos = -1;
		int start = 0;
		while (1)
		{
			pos = bookData1.find(",", start);
			if (pos == -1)
			{
				break;
			}
			string target = bookData1.substr(start, pos - start);
			v_temp.push_back(target);

			start = pos + 1;
		}
	}
	//cout << v_temp.size() << endl;

	ifs.close();
	
	int count1 = 0;
	//把容器里要下架的图书删除掉
	for (vector<string>::iterator it = v_temp.begin(); it != v_temp.end(); it++)
	{
		count1++;
		if (bookName == *it)
		{
			it--;
			v_temp.erase(it, it + 3);//删除数据
			this->m_shengXiaV = v_temp;// 系统类的成员 将剩下的书赋值给v_temp

			break;
		}
		if (count1 == v_temp.size())
		{
			cout << "系统内部不存在该图书!" << endl;
			std::system("pause");
			this->m_Error = -1;
			return;
		}
	}

	//添加容器 用于储存剩余的图书

	//清空文件
	ofstream ofs;
	ofs.open("系统图书.txt", ios::trunc);
	ofs.close();

	//重新向文件导入数据(剩下的图书)




	cout << "成功下架" << bookName <<"!" << endl;
	std::system("pause");
	std::system("cls");
}

//保存剩下图书
void SystemManage::saveBook(const vector<string>& v2)
{
	ofstream ofs1;
	ofs1.open("系统图书.txt", ios::out);
	for (vector<string>::const_iterator it = this->m_shengXiaV.begin(); it != this->m_shengXiaV.end(); it++)
	{
		ofs1 << *it << ",";
	}
	ofs1.close();
	this->m_shengXiaV.clear();//清空
}

//租借图书
void SystemManage::borrowBook()
{
	//展示页面
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***                租借图书                 ***" << endl;
	cout << "                      ***********************************************" << endl;


	//输入信息
	string name;
	cout << "请输入你的姓名:" << endl;
	cin >> name;

	string phone;
	cout << "请输入你的手机号码:" << endl;
	cin >> phone;

	string id;
	cout << "请输入你的账号:" << endl;
	cin >> id;

	string mima;
	cout << "请输入你的密码:" << endl;
	cin >> mima;

	//用户注册  User.txt 解析
	ifstream ifs2;
	ifs2.open("User.txt", ios::in);

	vector<string>tempV1;
	string borrowerData;
	while (ifs2 >> borrowerData)
	{
		//cout << borrowerData << endl;
		int pos = -1;
		int start = 0;
		while (1)
		{
			pos = borrowerData.find(",", start);
			if (pos == -1)
			{
				break;
			}
			string target = borrowerData.substr(start, pos - start);
			tempV1.push_back(target);

			start = pos + 1;
		}
	}

	ifs2.close();

	//cout << tempV1.size() << endl;
	
	//将用户信息跟解析到容器的信息对比   匹配失误 return

	int num = 0;
	for (vector<string>::iterator it = tempV1.begin(); it != tempV1.end(); it++)
	{
		if (id == *it)
		{
			num++;
			it++;
			if (*it == mima)
			{

				break;
			}
			else
			{
				cout << "密码输入错误!" << endl;
				std::system("pause");
				std::system("cls");
				this->m_Error = -1;
				return;
			}
		}
	}

	if (num == 0)
	{
		cout << "该账号还没有注册!" << endl;
		std::system("pause");
		std::system("cls");
		this->m_Error = -1;
		return;
	}

	//cout << "可以借书了!" << endl;
	
	//开始借书
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***                租借图书                 ***" << endl;
	cout << "                      ***********************************************" << endl;
	cout << "所有系统图书如下:" << endl;

	//展示系统所有图书
	this->showOnBorrow();

	//输入一本想借的图书
	cout << "请输入想租借的图书的书名: " << endl;
	string bookName2;
	cin >> bookName2;

	//打开系统图书文件 解析数据到容器中
	ifstream ifs5;
	ifs5.open("系统图书.txt", ios::in);

	vector<string>v3;
	string bookData3;
	while (ifs5 >> bookData3)
	{
		int pos3 = -1;
		int start3 = 0;
		while (1)
		{
			pos3 = bookData3.find(",", start3);
				if (pos3 == -1)
				{
					break;
				}
				string target = bookData3.substr(start3, pos3 - start3);

				v3.push_back(target);

				start3 = pos3 + 1;
		}
	}
	ifs5.close();

	//没有匹配到 return
	
	int count2 = 0;
	for (vector<string>::iterator it = v3.begin(); it != v3.end(); it++)
	{
		count2++;
		if (bookName2 == *it)
		{
			break;
		}
		if (count2 == v3.size())
		{
			cout << "系统内没有该图书!" << endl;
			std::system("pause");
			std::system("cls");
			this->m_Error = -1;
			return;
		}
	}

	//对比 遍历匹配
	
	vector<string>temp_v;//借走的图书
	for (vector<string>::iterator it = v3.begin(); it != v3.end(); it++)
	{
		if (*it == bookName2)
		{
			//1.把租借的图书放到一个容器中
			it--;
			temp_v.assign(it, it + 3);
			v3.erase(it, it + 3);//清空
			break;
		 }
	}

	this->m_shengXiaV = v3;//将剩下的数据传到专用容器中



	//借书人信息容器  6 名字 身份证 手机号 一本图书(3)
	for (int i = 0; i < 4; i++)
	{
		if (i == 0)
		{
			this->m_BorrowV.push_back(name);
		}
		if (i == 1)
		{
			this->m_BorrowV.push_back(phone);
		}
		if(i == 2)
		{
			this->m_BorrowV.push_back(id);
		}
		if (i == 3)
		{
			for (vector<string>::iterator it = temp_v.begin(); it != temp_v.end(); it++)
			{
				this->m_BorrowV.push_back(*it);
			}
		}
	}
	
	
	//通过容器将数据导入  借书人信息.txt
	ofstream ofs2;
	ofs2.open("借书人信息.txt", ios::out|ios::app);

	for (int i = 0; i < 6; i++)
	{
		ofs2 << this->m_BorrowV[i] << ",";
	}
	ofs2.close();

	m_BorrowV.clear();
	
	//清空原来的系统图书
	ofstream ofs;
	ofs.open("系统图书.txt", ios::trunc);
	ofs.close();

	cout << "成功租借" << bookName2 << endl;

	std::system("pause");
	std::system("cls");
}

//展示系统图书(租借图书专用)
void SystemManage::showOnBorrow()
{
	ifstream ifs3;
	ifs3.open("系统图书.txt", ios::in);

	vector<string>v1;//解析到的字符串
	string bookData2;
	while (ifs3 >> bookData2)
	{
		int pos2 = -1;
		int start2 = 0;
		while (1)
		{
			pos2 = bookData2.find(",", start2);
			if (pos2 == -1)
			{
				break;
			}
			string target = bookData2.substr(start2, pos2 - start2);
			v1.push_back(target);

			start2 = pos2 + 1;
		}
	}

	//cout << v1.size() << endl;
	//std::system("pause");

	ifs3.close();

	int count = 0;
	for (vector<string>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		count++;
		if (count == 1)
		{
			cout << "图书编号:" << setw(8) << left << *it << " ";
		}
		if (count == 2)
		{
			cout << "图书名称:" << setw(30) << left << *it << " ";
		}
		if (count == 3)
		{
			cout << "图书类型:" << setw(10) << left << *it << " ";
			cout << endl;
			count = 0;
		}
	}
}

//保存剩下图书   图书系统.txt
void SystemManage::saveShengXia(const vector<string>& v3)
{
	//剩下的系统图书
	ofstream ofs;
	ofs.open("系统图书.txt", ios::out);
	for (vector<string>::const_iterator it = v3.begin(); it != v3.end(); it++)
	{
		ofs << *it << ",";
	}

	this->m_shengXiaV.clear();
	ofs.close();
}


//查看借书人信息
void SystemManage::lookBorrowerData()
{

	//显示界面
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***              图书租借情况               ***" << endl;
	cout << "                      ***********************************************" << endl;


	//从借书人信息.txt文件解析数据放到容器中
	ifstream ifs;
	ifs.open("借书人信息.txt", ios::in);

	string data1;
	vector<string>tempv;
	while (ifs >> data1)
	{
		int pos = -1;
		int start = 0;
		while (1)
		{
			pos = data1.find(",", start);
			if (pos == -1)
			{
				break;
			}
			string target = data1.substr(start, pos - start);

			tempv.push_back(target);

			start = pos + 1;
		}
	}


	ifs.close();
	//借书人信息  姓名 手机号码 账号 图书编号 图书姓名 图书类型

	if (tempv.size() == 0)
	{
		cout << "未有借书信息!" << endl;
		std::system("pause");
		return;
	}

	//遍历容器
	cout << endl;
	cout << setw(13) << left << "姓名" << setw(18) << left << "手机号码"
		<< setw(24) << left << "身份证号" << setw(20) << left << "图书编号"
		<< setw(24) << left << "图书名称" << setw(6) << left << "图书类型" << endl;
	cout << endl;

	int count = 0;
	for (vector<string>::iterator it = tempv.begin(); it != tempv.end(); it++)
	{
		count++;
		if (count == 1)
		{
			cout << setw(13) << left << *it;
		}
		if (count == 2)
		{
			cout << setw(18) << left << *it;
		}
		if (count == 3)
		{
			cout << setw(24) << left << *it;
		}
		if (count == 4)
		{
			cout << setw(20) << left << *it;
		}
		if (count == 5)
		{
			cout << setw(24) << left << *it;
		}
		if (count == 6)
		{
			cout << setw(6) << left << *it << endl;
			count = 0;
		}
	}
	cout << endl;


	std::system("pause");
}

//归还图书
void SystemManage::backBook()
{
	//显示界面
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***                归还图书                 ***" << endl;
	cout << "                      ***********************************************" << endl;


	//输入信息
	string name;
	cout << "请输入你的姓名:" << endl;
	cin >> name;

	string phone;
	cout << "请输入你的手机号码:" << endl;
	cin >> phone;

	string id;
	cout << "请输入你的账号:" << endl;
	cin >> id;

	string mima;
	cout << "请输入你的密码:" << endl;
	cin >> mima;



	//解析 用户注册信息 
	ifstream ifs;
	ifs.open("User.txt", ios::in);

	string data;
	vector<string>userData_v;
	while (ifs >> data)
	{
		int pos = -1;
		int start = 0;
		while (1)
		{
			pos = data.find(",", start);

			if (pos == -1)
			{
				break;
			}

			string target = data.substr(start, pos - start);

			userData_v.push_back(target);
			start = pos + 1;
		}
	}
	ifs.close();
	//cout << userData_v.size() << endl;
	

	//检查账号 密码 是否正确
	bool isIdRight = false;
	for (vector<string>::iterator it = userData_v.begin(); it != userData_v.end(); it++)
	{
		if (*it == id)
		{
			isIdRight = true;
			it++;
			if (*it == mima)
			{
				break;
			}
			else
			{
				cout << "密码输入错误,请重新输入!" << endl;
				std::system("pasue");
				this->m_Error = -1;
				return;
			}
		 }
	}

	//2.失败 return
	if (isIdRight == false)
	{
		cout << "账号或密码输入错误,请重新输入!" << endl;
		std::system("pasue");
		this->m_Error = -1;
 		return;
	}
	//1.成功 开始归还
	ifstream ifs2;
	ifs2.open("借书人信息.txt", ios::in);

	string borrow_Data;
	vector<string>v;
	while (ifs2 >> borrow_Data)
	{
		int pos = -1;
		int start = 0;

		while (1)
		{
			pos = borrow_Data.find(",", start);

			if (pos == -1)
			{
				break;
			}

			string target = borrow_Data.substr(start, pos - start);

			v.push_back(target);

			start = pos + 1;
		}
	}
	//cout << v.size() << endl;
	ifs2.close();

	//正式开始归还
	int count = 0;
	for (vector<string>::iterator it = v.begin(); it != v.end(); it++)
	{
		count++;
      //判断用户是否有借书信息
		if (*it == name)
		{
			break;
		}
		  
		if (count == v.size())
		{
			cout << "没有你的借书信息!" << endl;
			std::system("pause");
			this->m_Error = -1;
			return;
		}
		
	}


	vector<string>tempv;
	//删除一整条借书人信息  &&  保存被删除信息里的信息(容器)
	for (vector<string>::iterator it = v.begin(); it != v.end(); it++)
	{
		if (*it == name)
		{
			//1.保存图书信息 
			tempv.push_back(*(it + 3));
			tempv.push_back(*(it + 4));
			tempv.push_back(*(it + 5));

			// 2.删除一整条信息
			v.erase(it, it + 6);

			break;
		}
	}
	
	ofstream ofs1;
	ofs1.open("系统图书.txt", ios::out|ios::app);

	ofs1 << tempv[0] << "," << tempv[1] << "," << tempv[2] << ",";

	string bookName = tempv[1];

	tempv.clear();
	ofs1.close();



	this->m_shengXiaBorrowerV = v;
	
	//清空掉借书人信息.txt
	ifstream ifs3;
	ifs3.open("借书人信息.txt", ios::trunc);
	ifs3.close();
	

	//3.归还图书(追加 导入系统图书.txt)

	//借书人信息文件 先清空  剩下的借书人信息导入
	cout <<bookName << "已经归还!" << endl;
	std::system("pause");
	std::system("cls");
}

//保存剩下的借书人信息
void SystemManage::saveShengXiaBorrower(const vector<string>& v)
{
	ofstream ofs("借书人信息.txt", ios::out);
	for (vector<string>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		ofs << *it << ",";
	}
	ofs.close();
	this->m_shengXiaBorrowerV.clear();
}

//修改密码
void SystemManage::changeMima()
{

	//显示界面
	std::system("cls");
	cout << "                      ***********************************************" << endl;
	cout << "                      ***              修改管理员密码             ***" << endl;
	cout << "                      ***********************************************" << endl;

	string p1, p2, p3;
	cout << "请输入原密码:";
	cin >> p1;

	
	ifstream ifs2;
	ifs2.open("ManageData.txt", ios::in);

	string Manage_Data;
	vector<string>v;
	while (ifs2 >> Manage_Data)
	{
		int pos = -1;
		int start = 0;

		while (1)
		{
			pos = Manage_Data.find(",", start);

			if (pos == -1)
			{
				break;
			}

			string target = Manage_Data.substr(start, pos - start);

			v.push_back(target);

			start = pos + 1;
		}
	}
	//cout << v.size() << endl;
	ifs2.close();

	for (vector<string>::iterator it = v.begin(); it != v.end(); it++)
	{
	
		if (p1 == *++it)
		{
			cout << "请输入新的密码:";
			cin >> p2;

			cout << "请再次输入密码:";
			cin >> p3;

			if (p2 != p3)
			{
				cout << "密码错误!.";
				std::system("pause");
				return;
			}

			ofstream outfile("ManageData.txt", ios::out);
			outfile << "大聪明" <<"," << p2 <<",";
			outfile.close();

			cout << "管理员密码已修改.";
		}
		else
			cout << "密码错误!.";
		cout << endl;
	}

	std::system("pause");
	std::system("cls");
}

 main.cpp

#include<iostream>
#include"systemManage.h"
using namespace std;



int main()
{
	//菜单
	SystemManage sm;
	while (1) 
	{
		sm.showMenu();

		switch (getchar())
		{
		case '1'://租借图书
			sm.borrowBook();
			if (sm.m_Error == -1)
			{
				sm.m_Error = 0;
				break;
			}

			//保存系统剩下的图书  
			sm.saveShengXia(sm.m_shengXiaV);

			break;
		case '2'://归还图书
			sm.backBook();
			if (sm.m_Error == -1)
			{
				sm.m_Error = 0;
				break;
			}
			//保存剩下的借书人信息
			sm.saveShengXiaBorrower(sm.m_shengXiaBorrowerV);
			break;
		case '3'://图书丢失
			sm.reduceBook();
			break;
		case '4'://查看所有图书
			sm.lookOverBook();
			break;
		case '5'://查找图书
			sm.findBook();
			break;
		case '6'://用户注册
			sm.addUser();
			break;
		case '7'://管理员特权
			sm.guanLiYuanGuJia();
			break;
		case '8'://借书须知 
			sm.borrowBookNotice();
			break;
		default:
			system("cls");
			break;
		}
	}
	
	system("pause");
	return 0;
}

books.h 

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

class Books
{
public:

	//书名
	string m_Name;

	//图书类型
	int m_Type;
};

​

 systemManage.h

#pragma once
#include<iostream>
#include"books.h"
#include<map>
#include<iomanip>
#include<vector>
using namespace std;

class SystemManage
{
public:
	int m_Index;

	int m_Error;

	vector<string>m_shengXiaBorrowerV;//用于保存剩下借书人信息容器

	vector<string>m_BorrowV;//专门用来储存借书人信息的容器

	map<int, Books>m_Bookm;//添加图书进入系统的一个中间容器

	vector<string>m_shengXiaV; //储存剩下的图书(下架图书专用)
	//构造函数
	SystemManage();

	//菜单
	void showMenu();

	//管理员骨架
	void guanLiYuanGuJia();

	//管理员登录
	void managerLogOn();

	//管理员菜单
	void managerShowMenu();

	//图书丢失
	void reduceBook();

	//用户注册
	void addUser();

	//借书须知
	void borrowBookNotice();

	//添加图书
	void addBook();

	//保存添加图书信息(添加图书专用)
	void saveData();

	//查看所有图书
	void lookOverBook();

	//查找图书
	void findBook();

	//查找图书专用
	//按图书编号查找
	void findById(const vector<string> &v);

	//按图书名称查找
	void findByBookName(const vector<string>& v);

	//下架图书
	void outBook();

	//保存剩下图书
	void saveBook(const vector<string>&v2);

	//租借图书
	void borrowBook();

	//展示系统图书(租借图书专用)
	void showOnBorrow();

	//保存剩下图书
	void saveShengXia(const vector<string>& v3);

	//查看借书人信息
	void lookBorrowerData();

	//归还图书
	void backBook();

	//保存剩下的借书人信息
	void saveShengXiaBorrower(const vector<string>& v);

	//修改密码
	void changeMima();

};

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个包括学生和管理员登录系统的C++图书管理系统类链表的示例代码。以下是一个基本的实现: ```cpp #include <iostream> #include <string> using namespace std; // 图书类 class Book { private: string title; string author; int year; public: Book(string title, string author, int year) { this->title = title; this->author = author; this->year = year; } string getTitle() { return title; } string getAuthor() { return author; } int getYear() { return year; } }; // 学生类 class Student { private: string name; int id; public: Student(string name, int id) { this->name = name; this->id = id; } string getName() { return name; } int getId() { return id; } }; // 链表节点类 class Node { public: Book* book; Node* next; Node(Book* book) { this->book = book; next = nullptr; } }; // 链表类 class LinkedList { private: Node* head; public: LinkedList() { head = nullptr; } // 添加图书到链表 void addBook(Book* book) { Node* newNode = new Node(book); if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } // 显示链表的所有图书 void displayBooks() { Node* current = head; if (current == nullptr) { cout << "链表为空" << endl; return; } cout << "图书列表:" << endl; while (current != nullptr) { cout << "书名:" << current->book->getTitle() << endl; cout << "作者:" << current->book->getAuthor() << endl; cout << "出版年份:" << current->book->getYear() << endl; cout << "-------------------------" << endl; current = current->next; } } }; // 图书管理系统类 class LibrarySystem { private: LinkedList bookList; public: // 添加图书到系统 void addBook(Book* book) { bookList.addBook(book); } // 显示系统的所有图书 void displayBooks() { bookList.displayBooks(); } // 学生登录 void studentLogin(Student* student) { cout << "学生登录成功!" << endl; cout << "欢迎," << student->getName() << "!" << endl; // 进行学生相关操作,例如借书、还书等 } // 管理员登录 void adminLogin() { string password; cout << "请输入管理员密码:"; cin >> password; if (password == "admin123") { cout << "管理员登录成功!" << endl; // 进行管理员相关操作,例如添加图书、删除图书等 } else { cout << "密码错误,登录失败!" << endl; } } }; int main() { // 创建图书对象 Book book1("C++ Primer", "Stanley B. Lippman", 2012); Book book2("Effective C++", "Scott Meyers", 2005); Book book3("The C++ Programming Language", "Bjarne Stroustrup", 2013); // 创建学生对象 Student student("张三", 12345); // 创建图书管理系统对象 LibrarySystem librarySystem; // 添加图书到系统 librarySystem.addBook(&book1); librarySystem.addBook(&book2); librarySystem.addBook(&book3); // 显示系统的所有图书 librarySystem.displayBooks(); // 学生登录 librarySystem.studentLogin(&student); // 管理员登录 librarySystem.adminLogin(); return 0; } ``` 这是一个简单的图书管理系统,包括学生和管理员登录系统,并使用链表来存储图书对象。您可以根据需要进行修改和扩展。希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值