#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义图书信息类
class BookInfo
{
public:
// 登录号
int ID;
// 书名
string name;
// 作者名
string author;
// 分类号
int category;
// 出版单位
string publisher;
// 出版时间
string publishDate;
// 价格
double price;
// 构造函数
BookInfo(int id, string n, string a, int c, string p, string pd, double pr)
{
ID = id;
name = n;
author = a;
category = c;
publisher = p;
publishDate = pd;
price = pr;
}
// 打印图书信息
void print() {
cout << "*******************" << endl;
cout << " " << endl;
cout << "登录号:" << ID << endl;
cout << " " << endl;
cout << "*******************" << endl;
cout << " " << endl;
cout << "书名:" << name << endl;
cout << " " << endl;
cout << "*******************" << endl;
cout << " " << endl;
cout << "作者名:" << author << endl;
cout << " " << endl;
cout << "*******************" << endl;
cout << " " << endl;
cout << "分类号:" << category << endl;
cout << " " << endl;
cout << "*******************" << endl;
cout << " " << endl;
cout << "出版单位:" << publisher << endl;
cout << " " << endl;
cout << "*******************" << endl;
cout << " " << endl;
cout << "出版时间:" << publishDate << endl;
cout << " " << endl;
cout << "*******************" << endl;
cout << " " << endl;
cout << "价格:" << price << endl;
cout << " " << endl;
cout << "*******************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
}
// 将图书信息转为字符串
string toString()
{
string str = to_string(ID) + "," + name + "," + author + "," + to_string(category) + "," + publisher + "," + publishDate + "," + to_string(price);
return str;
}
};
// 定义图书信息管理
class BookManager
{
private:
// 图书信息数组
vector<BookInfo> books;
// 保存图书信息的文件名
string filename;
public:
// 构造函数
BookManager(string fn)
{
filename = fn;
loadBooks();
}
// 添加图书信息
void addBook()
{
int id, category;
string name, author, publisher, publishDate;
double price;
cout << "请输入登录号:";
cout << " ";
cin >> id;
cout << "请输入书名:";
cout << " ";
cin >> name;
cout << "请输入作者名:";
cout << " ";
cin >> author;
cout << "请输入分类号:";
cout << " ";
cin >> category;
cout << "请输入出版单位:";
cout << " ";
cin >> publisher;
cout << "请输入出版时间:";
cout << " ";
cin >> publishDate;
cout << "请输入价格:";
cout << " ";
cin >> price;
BookInfo book(id, name, author, category, publisher, publishDate, price);
books.push_back(book);
saveBooks();
cout << "添加成功!" << endl;
}
// 查找图书信息
void findBookByName()
{
string name;
cout << "请输入要查找的书名:";
cin >> name;
vector<BookInfo> results;
for (int i = 0; i < books.size(); i++)
{
if (books[i].name == name)
{
results.push_back(books[i]);
}
}
if (results.size() == 0)
{
cout << "未找到该书名的图书信息!" << endl;
}
else
{
cout << "共找到" << results.size() << "本书:" << endl;
for (int i = 0; i < results.size(); i++)
{
results[i].print();
cout << endl;
}
}
}
// 查找图书信息
void findBookByAuthor()
{
string author;
cout << "请输入要查找的作者名:";
cin >> author;
vector<BookInfo> results;
for (int i = 0; i < books.size(); i++)
{
if (books[i].author == author)
{
results.push_back(books[i]);
}
}
if (results.size() == 0)
{
cout << "未找到该作者名的图书信息!" << endl;
}
else
{
cout << "共找到" << results.size() << "本书:" << endl;
for (int i = 0; i < results.size(); i++)
{
results[i].print();
cout << endl;
}
}
}
// 删除图书信息
void deleteBook()
{
int id;
cout << "请输入要删除的登录号:";
cin >> id;
for (int i = 0; i < books.size(); i++)
{
if (books[i].ID == id)
{
books.erase(books.begin() + i);
saveBooks();
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该图书信息!" << endl;
}
// 修改图书信息
void modifyBook()
{
int id;
cout << "请输入要修改的登录号:";
cin >> id;
for (int i = 0; i < books.size(); i++)
{
//重新输入书籍信息
//输入后可重新尝试查询
if (books[i].ID == id)
{
cout << "请输入新的书名:";
cout << " ";
cin >> books[i].name;
cout << "请输入新的作者名:";
cout << " ";
cin >> books[i].author;
cout << "请输入新的分类号:";
cout << " ";
cin >> books[i].category;
cout << "请输入新的出版单位:";
cout << " ";
cin >> books[i].publisher;
cout << "请输入新的出版时间:";
cout << " ";
cin >> books[i].publishDate;
cout << "请输入新的价格:";
cout << " ";
cin >> books[i].price;
saveBooks();
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该图书信息!" << endl;
cout << "请输入图书后再重新查询" << endl;
}
// 按价格排序
void sortByPrice()
{
sort(books.begin(), books.end(), [](BookInfo a, BookInfo b) {
return a.price < b.price;
});
cout << "按价格排序成功!" << endl;
}
// 显示所有图书信息
void showAllBooks()
{
for (int i = 0; i < books.size(); i++)
{
books[i].print();
cout << endl;
}
}
// 显示菜单
void showMenu()
{
cout << "_____________________________________" << endl;
cout << "|||||||||欢迎使用图书管理系统|||||||||||" << endl;
cout << "|------------------------------------|" << endl;
cout << "| |" << endl;
cout << "| 请按照以下操作执行 |" << endl;
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 loadBooks()
{
ifstream file(filename);
if (file.is_open())
{
string line;
while (getline(file, line))
{
int id = stoi(line.substr(0, line.find(",")));
//序号
line = line.substr(line.find(",") + 1);
string name = line.substr(0, line.find(","));
//名字
line = line.substr(line.find(",") + 1);
//作者
string author = line.substr(0, line.find(","));
line = line.substr(line.find(",") + 1);
//分类号
int category = stoi(line.substr(0, line.find(",")));
line = line.substr(line.find(",") + 1);
//出版单位
string publisher = line.substr(0, line.find(","));
line = line.substr(line.find(",") + 1);
//出版时间
string publishDate = line.substr(0, line.find(","));
line = line.substr(line.find(",") + 1);
//价格
double price = stod(line);
BookInfo book(id, name, author, category, publisher, publishDate, price);
books.push_back(book);
}
file.close();
}
}
// 将图书信息保存到文件
void saveBooks()
{
ofstream file(filename);
if (file.is_open())
{
for (int i = 0; i < books.size(); i++)
{
file << books[i].toString() << endl;
}
file.close();
}
}
// 运行图书信息管理系统
void run()
{
int choice;
while (true)
{
showMenu();
cout << " " << endl;
cout << " " << endl;
cout << "******************" << endl;
cout << "请输入选项:";
cin >> choice;
cout << "******************" << endl;
cout << " " << endl;
cout << " " << endl;
switch (choice)
{
case 1:
addBook();
break;
case 2:
findBookByName();
break;
case 3:
findBookByAuthor();
break;
case 4:
deleteBook();
break;
case 5:
modifyBook();
break;
case 6:
sortByPrice();
break;
case 7:
showAllBooks();
break;
case 0:
cout << "************" << endl;
cout << "谢谢使用!" << endl;
cout << "************" << endl;
return;
default:
cout << "**********************" << endl;
cout << "无效选项,请重新输入!" << endl;
cout << "**********************" << endl;
}
}
}
};
int main()
{
BookManager manager("books.txt");
manager.run();
return 0;
}