action.cpp

来自「基于控制台的图书馆管理系统。 用户级操作有按书号、书名、关键字查询」· C++ 代码 · 共 442 行

CPP
442
字号
#include "Action.hpp"
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;


string        tname;
unsigned long tnum;


bool Tbook :: operator < (const Tbook &other)
{
	return book_label < other.book_label;
}


bool judge_label(Tbook &n)
{
	return (n.Getlabel() == tnum);
}


bool judge_name(Tbook &n)
{
	return (n.Getname() == tname);
}


void OPERATION :: ADD_BOOK ()					                                // 向内存储器添加数据 
{
	book_list.push_back(now);  
}


OPERATION :: OPERATION ()                                                       // OPERATION构造函数 
{
	string        tmp;															// 用以读取数据库的标识行 
	unsigned long i;

	database_address = "database.txt";         									// 图书馆数据库文件名,实际应用中需要加密保存 

	ifstream fin(database_address.c_str());
	if (!fin)
	{
		cout << "对不起,数据库文件未找到,请修复!" << endl;
		exit(1); 
	}

	getline(fin, administrator_name);
	getline(fin, administrator_pwd);
	fin >> book_total;

	for (i = 1; i <= book_total; i++)
	{
		fin.get();
		getline(fin, tmp);
		fin >> now.book_label;
		fin.get();
		getline(fin, now.name);
		getline(fin, now.author);
		getline(fin, now.concern);
		getline(fin, now.key);
		fin >> now.status;
		if (now.status)
		{
			fin.get();
			getline(fin, now.borrower.name);
			fin >> now.borrower.phone_number;
			fin >> now.borrower.year >> now.borrower.month >> now.borrower.day;
		}
	
		ADD_BOOK();

	}
	fin.close();
}


void OPERATION :: QUERY ()					                                               // 查询图书 
{
	string keyword[5];
	int	   i, n, j, p;

	do
	{
		system("cls");
		cout << "1、按书名查询" << endl;
		cout << "2、按书号查询" << endl;
		cout << "3、按关键字查询" << endl;
		cout << "0、退出查询" << endl;
		cout << "请输入您需要的选项(1 -- 3,0):";
		cin >> n;
		switch (n)
		{
			case 1: 
				{
					cout << "请输入需要查询的书名: ";
					cin.get();
					getline(cin, tname);

					bp = find_if(book_list.begin(), book_list.end(), judge_name);
					
					if (bp == book_list.end())
					{
							cout << "对不起,书籍未找到!" << endl;
							system("pause");
							continue;
					}
					bp->prn();
					cout << "状态  :";
					if (bp->status) cout << "借出" << endl;
							   else cout << "在馆" << endl;
					cout << endl;

					system("pause");
				} break;

			case 2: 
				{
					cout << "请输入你需要借阅图书的书号: ";
					cin >> tnum;

					bp = find_if(book_list.begin(), book_list.end(), judge_label);
					
					if (bp == book_list.end())
					{
							cout << "对不起,书籍未找到" << endl;
							system("pause");
							continue;
					}
					bp->prn();
					cout << "状态  :";
					if (bp->status) cout << "借出" << endl;
							else cout << "在馆" << endl;
					cout << endl;
					
					system("pause");
				} break;
			
			case 3: 
				{
					cin.get();			
					for (p = 0; p <= 4; p++)
					{
						cout << "请输入关键词 " << p + 1 << " (Enter即结束输入):";
						getline(cin, keyword[p]);
						if (!keyword[p].size()) 
						{
							p--;
							break;
						}
					};
					if (!p) continue;
					for (bp = book_list.begin(); bp != book_list.end(); bp++)
					{
						j = 0;
						for (i = 0; i <= p; i++)
							if (bp->key.find(keyword[i]) != -1) j++;
						if (j + j > p + 1)
						{
							bp->prn();
							cout << "状态  :";
							if (bp->status) cout << "借出" << endl;
									else cout << "在馆" << endl;
							cout << endl;
						}
					}
					system("pause");
				} break;
			
			case 0: break;
			
			default:
				{
					cout << "对不起,您的输入不合法!" << endl;
					cin.clear();
				}
		}
	} while (n);
}


void OPERATION :: LEND ()					                                               // 借阅图书 
{
	char tch;

	do
	{
		cout << endl << "请输入需要借阅的书号(输入0则结束借阅): ";
		cin >> tnum;
		if (!tnum) break;

		bp = find_if(book_list.begin(), book_list.end(), judge_label);

		if (bp == book_list.end()) 
		{
			cout << "对不起,书籍未找到" << endl;
			continue;
		}
		if (bp->status)
		{
			cout << "《" << bp->Getname() << "》" << endl;
			cout << "对不起,此书已被借出" << endl;
			continue;
		}
		bp->prn();
		do
		{
			cout << "确认 Yes / 取消 No:";
			cin >> tch;
		} while (tch != 'Y' && tch != 'y' && tch != 'N' && tch != 'n');
		if (tch == 'n' || tch == 'N') continue;
	
		cin.get();
		cout << "请登记姓名:";
		getline(cin, bp->borrower.name);
		cout << "请登记电话:";
		cin >> bp->borrower.phone_number;
		cout << "请登记借阅日期(年 月 日):";
		cin >> bp->borrower.year >> bp->borrower.month >> bp->borrower.day;

		do
		{
			cout << "确认借阅 Yes / 取消操作 No:";
			cin >> tch;
		} while (tch != 'Y' && tch != 'y' && tch != 'N' && tch != 'n');
		if (tch == 'n' || tch == 'N') continue;

		bp->status = 1;
		cout << "借阅成功!" << endl;
	} while (1);
	system("pause");
}


void OPERATION :: KEY_ADD ()								                              // 键盘追加图书 
{
	char   tch;
	string ts;
	int    p;
    
	do
	{
		cout << endl << "请输入需要追加的书号(输入0则结束追加):";
		cin >> tnum;
		if (!tnum) break;

		bp = find_if(book_list.begin(), book_list.end(), judge_label);
		if (bp != book_list.end())
		{
			cout << "对不起,该书号已存在!" << endl;
			continue;
		}

		now.book_label = tnum;
		cin.get();
		cout << "请输入书名:";
		getline(cin, now.name);
		cout << "请输入作者名:";
		getline(cin, now.author);
		cout << "请输入出版社:";
		getline(cin, now.concern);

		now.status = 0;
		now.key = "";
		for (p = 1; p <= 5; p++)
		{
			cout << "请输入关键词 " << p << " (Enter即结束输入):";
			getline(cin, ts);
			if (!ts.size()) break;
			now.key = now.key + ts + "/";
		}

		do
		{
			cout << "确认添加 Yes / 重新添加 No:";
			cin >> tch;
		} while (tch != 'y' && tch != 'Y' && tch != 'n' && tch != 'N');
		if (tch == 'y' || tch == 'Y') 
		{
			ADD_BOOK ();
			book_total++;
		}
	} while (1);
	system("pause");
}


void OPERATION :: GIVEBACK()
{
	char tch;

	do
	{
		cout << "请输入需要归还的书号(输入0则结束归还): ";
		cin >> tnum;

		if (!tnum) break;

		bp = find_if(book_list.begin(), book_list.end(), judge_label);

		if (bp == book_list.end()) 
		{
			cout << "对不起,书籍未找到" << endl;
			continue;
		}
		if (!bp->status)
		{
			cout << "《" << bp->Getname() <<  "》" << endl;
			cout << "对不起,此书在库!" << endl;
			continue;
		}
		
		bp->prn();
		bp->borrower.prn();
		do
		{	
			cout << "确认 Yes / 取消 No:";
			cin >> tch;
		} while (tch != 'Y' && tch != 'y' && tch != 'N' && tch != 'n');
		if (tch == 'n' || tch == 'N') continue;

		bp->borrower.name = "";
		bp->borrower.phone_number = 0;
		bp->borrower.year = 0;
		bp->borrower.month = 0;
		bp->borrower.day = 0;
		bp->status = 0;
		cout << "归还成功!" << endl;
	} while (1);
	system("pause");
}


void OPERATION :: MANAGE ()
{
	string adn, adp;
	int	   n;
	
	cin.get();
	cout << "请输入管理员账号:";
	getline(cin, adn);
	cout << "请输入管理员密码:";
	getline(cin, adp);

  	if (adn != administrator_name || adp != administrator_pwd)
	{
		cout << "账号或密码错误!" << endl;
		system("pause");
		return;
	}

	do
	{
		system("cls");
		cout << "管理员登陆:" << endl;
		cout << "1、列出所有在库图书" << endl;
		cout << "2、列出所有借出图书" << endl;
		cout << "3、查询借阅资料" << endl;
		cout << "0、退出" << endl;
		cout << "请输入您需要的选项(1 -- 3,0):";
		
		cin >> n;
		switch (n)
		{
			case 1: 
				{
					for (bp = book_list.begin(); bp != book_list.end(); bp++)
						if (!bp->status) 
						{
							bp->prn();
							cout << endl;
						}
					system("pause");
				} break;

			case 2: 
				{
					for (bp = book_list.begin(); bp != book_list.end(); bp++)
						if (bp->status) 
						{
							bp->prn();
							cout << endl;
						}
					system("pause");
				} break;

			case 3:
				{
					for (bp = book_list.begin(); bp != book_list.end(); bp++)
						if (bp->status) 
						{
							cout << endl << "书名:" << bp->Getname() << endl;
							cout << "书号:" << bp->book_label << endl;
							bp->borrower.prn();
						}
					system("pause");
				} break;

			case 0: return;

			default:
			{
				cout << "对不起,您的输入不合法!" << endl;
				cin.clear();
			}

		}
	} while (1);
}  


OPERATION :: ~OPERATION ()
{                      
      ofstream fout(database_address.c_str());
      
      fout << administrator_name << endl;
      fout << administrator_pwd << endl;
      fout << book_total << endl;
      
      sort (book_list.begin(), book_list.end());
      
      for (bp = book_list.begin(); bp != book_list.end(); bp++)
      {
		  fout << endl;
		  fout << bp->book_label << endl;
		  fout << bp->name << endl;
		  fout << bp->author << endl;
		  fout << bp->concern << endl;
		  fout << bp->key << endl;
		  fout << bp->status << endl;
		  if (bp->status)
		  {
			  fout << bp->borrower.name << endl;
			  fout << bp->borrower.phone_number << endl;
			  fout << bp->borrower.year << " " << bp->borrower.month << " " << bp->borrower.day << endl;
		  }
	  }
	  fout.close();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?