#include
#include
#include//引入头文件
using namespace std;
#define MAX 1000//系统最大储存书籍数量
int COUNT = 0;//记录添加书籍数量
class Date {
public:
int year;
int month;
int day;
};//日期类
class User {
private:
string id = "admin";//账号
string passWord = "0000";//密码
public:
void check(string id, string passWord);//密码账号验证
};
class Book {
private:
class Date {
public:
int year;
int month;
int day;
};//日期类
string name;
string writer;
string typeNumber;
string publisher;
double price;
Date date;//类组合
//私有成员对数据进行保护
public:
Book();//默认构造器
void setInfo(string name, string writer, string typeNumber, double price, string publisher, int year, int month, int day);//添加数据
//获取整个信息
string getName();
string getWriter();
string getTypeNumber();
string getPublisher();
double getPrice();
int getDateYear();
int getDateMonth();
int getDateDay();
void setName(string name);
void setWriter(string writer);
void setTypeNumber(string typeNumber);
void setPublisher(string publisher);
void setPrice(double price);
void setYear(int year);
void setMonth(int month);
void setDay(int day);
//set,get函数访问私有成员
};
Book book[MAX];//类数组定义
void addBookMessage(Book book[MAX]);//添加图书
void showBookMessage(Book book[MAX]);//展示图书
void searchBookMessageName(Book book[MAX]);//查找书籍-书名
void searchBookMessageWriter(Book book[MAX]);//查找书籍-类型号
void reviseBookMessageName(Book book[MAX]);//修改书籍
void deleteBookMessageName(Book book[MAX]);//删除图书-书名
void saveFile(Book book[MAX]);//保存文件
void readFile(Book book[MAX]);//读取文件
void exitSystem();//退出
void searchMenu(Book book[MAX]);//查询图书主菜单
void mainUserMenu();//用户进入主菜单
void mainBookMenu();//书籍管理主菜单
//修改书籍的部分函数
void changeName(Book book[MAX], int i);
void changeWriter(Book book[MAX], int i);
void changeTypeNumber(Book book[MAX], int i);
void changePublisher(Book book[MAX], int i);
void changePrice(Book book[MAX], int i);
void changeYear(Book book[MAX], int i);
void changeMonth(Book book[MAX], int i);
void changeDay(Book book[MAX], int i);
Book::Book() {
this->name = "待添加";
this->writer = "带添加";
this->typeNumber = "带添加";
this->price = 0.0000000;
this->publisher = "带添加";
this->date.year = 0;
this->date.month = 0;
this->date.day = 0;
}//默认构造,将参数初始化
void Book::setInfo(string name, string writer, string typeNumber, double price, string publisher, int year, int month, int day) {
this->name = name;
this->writer = writer;
this->typeNumber = typeNumber;
this->price = price;
this->publisher = publisher;
this->date.year = year;
this->date.month = month;
this->date.day = day;
}
void Book::setName(string name) {
this->name = name;
}
void Book::setWriter(string writer) {
this->writer = writer;
}
void Book::setTypeNumber(string typeNumber) {
this->typeNumber = typeNumber;
}
void Book::setPublisher(string publisher) {
this->publisher = publisher;
}
void Book::setPrice(double price) {
this->price = price;
}
void Book::setYear(int year) {
this->date.year = year;
}
void Book::setMonth(int month) {
this->date.month = month;
}
void Book::setDay(int day) {
this->date.day = day;
}
string Book::getName() {
return this->name;
}
string Book::getWriter() {
return this->writer;
}
string Book::getTypeNumber() {
return this->typeNumber;
}
string Book::getPublisher() {
return this->publisher;
}
double Book::getPrice() {
return this->price;
}
int Book::getDateYear() {
return this->date.year;
}
int Book::getDateMonth() {
return this->date.month;
}
int Book::getDateDay() {
return this->date.day;
}
//在类外对get,set函数重写
void User::check(string id, string passWord) {
if (this->id == id && this->passWord == passWord) {
mainBookMenu();
}
else if (this->id != id && this->passWord == passWord) {
system("cls");
cout << "tttt账号错误,请重新输入!" << endl;
Sleep(1000);
mainUserMenu();
}
else if (this->id == id && this->passWord != passWord) {
system("cls");
cout << "tttt密码错误,请重新输入!" << endl;
Sleep(1000);
mainUserMenu();
}
else if (this->id != id && this->passWord != passWord) {
system("cls");
cout << "tttt重新输入!" << endl;
Sleep(1000);
mainUserMenu();
}
}
//用户验证函数
void addBookMessage(Book book[MAX]) {
//比较已加入图书数量和最大存书比较
if (COUNT < MAX) {
string name;
string writer;
string typeNumber;
double price;
string publisher;
int year;
int month;
int day;
system("cls");
cout << "tttt----------------------------------" << endl;
cout << "tttt请输入书名:";
cin >> name;
cout << "tttt请输入作者";
cin >> writer;
cout << "tttt请输入类型号";
cin >> typeNumber;
cout << "tttt请输入价格";
cin >> price;
cout << "tttt请输入出版社";
cin >> publisher;
cout << "tttt请输入出版日期";
cin >> year >> month >> day;
book[COUNT].setInfo(name, writer, typeNumber, price, publisher, year, month, day);
COUNT++;//添加一个后往后计数加一
}
else {
cout << "书库已满,无法继续添加书籍" << endl;
Sleep(1000);
mainBookMenu();
}
mainBookMenu();
}
void showBookMessage(Book book[MAX]) {
if (COUNT == 0) {
system("cls");
cout << "tttt----------------------------------" << endl;
cout << "tttt书库中木有书,请添加书籍" << endl;
cout << "tttt----------------------------------" << endl;
Sleep(1000);
mainBookMenu();
}
else {
system("cls");
cout << "tt书库中书籍数量:" << COUNT << endl;
cout << "tt-----------------------------------------------------------------------" << endl;
cout << "tt书名" << "t 作者" << "t 类型号" << "t 价格" << "t出版社" << "t出版日期" << endl;
for (int i = 0; i < COUNT; i++) {
cout << "tt" << book[i].getName() << "t " << book[i].getWriter() << "t " << book[i].getTypeNumber() << "t " << book[i].getPrice() << "t " << book[i].getPublisher() << "t " << book[i].getDateYear() << "-" << book[i].getDateMonth() << "-" << book[i].getDateDay() << endl;
}
cout << "tt-----------------------------------------------------------------------" << endl;
}
system("pause");
}
void searchBookMessageName(Book book[MAX]) {
string nameSearch;
int number = 0;
if (COUNT == 0) {
system("cls");
cout << "tttt----------------------------------" << endl;
cout << "tttt书库中木有书,请添加书籍" << endl;
cout << "tttt----------------------------------" << endl;
Sleep(1000);
mainBookMenu();
}
else {
system("cls");
cout << "tttt请输入你想查询书的书名:";
cin >> nameSearch;
for (int i = 0; i < COUNT; i++) {
if (book[i].getName() == nameSearch) {
number++;
cout << "tt-----------------------------------------------------------------------" << endl;
cout << "tt书名" << "t 作者" << "t 类型号" << "t 价格" << "t出版社" << "t出版日期" << endl;
cout << "tt" << book[i].getName() << "t " << book[i].getWriter() << "t " << book[i].getTypeNumber() << "t " << book[i].getPrice() << "t " << book[i].getPublisher() << "t " << book[i].getDateYear() << "-" << book[i].getDateMonth() << "-" << book[i].getDateDay() << endl;
}
}
if (!number) {
system("cls");
cout << "tttt未找到任何相图书" << endl;
}
}
system("pause");
}
void searchBookMessageWriter(Book book[MAX]) {
string writerSearch;
int number = 0;
if (COUNT == 0) {
system("cls");
cout << "tttt----------------------------------" << endl;
cout << "tttt书库中木有书,请添加书籍" << endl;
cout << "tttt----------------------------------" << endl;
Sleep(1000);
mainBookMenu();
}
else {
system("cls");
cout << "tttt请输入你想查询书的作者:";
cin >> writerSearch;
for (int i = 0; i < COUNT; i++) {
if (book[i].getWriter() == writerSearch) {
number++;
cout << "tt-----------------------------------------------------------------------" << endl;
cout << "tt书名" << "t 作者" << "t 类型号" << "t 价格" << "t出版社" << "t出版日期" << endl;
cout << "tt" << book[i].getName() << "t " << book[i].getWriter() << "t " << book[i].getTypeNumber() << "t " << book[i].getPrice() << "t " << book[i].getPublisher() << "t " << book[i].getDateYear() << "-" << book[i].getDateMonth() << "-" << book[i].getDateDay() << endl;
}
}
}
if (!number) {
system("cls");
cout << "tttt未找到任何相图书" << endl;
}
system("pause");
}
void changeName(Book book[MAX], int i) {
string name;
cout << "输入修改书名:";
cin >> name;
book[i].setName(name);
cout << "修改成功!";
system("pause");
}
void changeWriter(Book book[MAX], int i) {
string writer;
cout << "输入修改作者:";
cin >> writer;
book[i].setWriter(writer);
cout << "修改成功!";
system("pause");
}
void changeTypeNumber(Book book[MAX], int i) {
string typeNumber;
cout << "输入修改类型号:";
cin >> typeNumber;
book[i].setTypeNumber(typeNumber);
cout << "修改成功!";
system("pause");
}
void changePublisher(Book book[MAX], int i) {
string publish;
cout << "输入修改出版社:";
cin >> publish;
book[i].setPublisher(publish);
cout << "修改成功!";
system("pause");
}
void changePrice(Book book[MAX], int i) {
double price;
cout << "输入修改价格:";
cin >> price;
book[i].setPrice(price);
cout << "修改成功!";
system("pause");
}
void changeYear(Book book[MAX], int i) {
int year;
cout << "输入修改年份:";
cin >> year;
book[i].setYear(year);
cout << "修改成功!";
system("pause");
}
void changeMonth(Book book[MAX], int i) {
int month;
cout << "输入修改月分:";
cin >> month;
book[i].setMonth(month);
cout << "修改成功!";
system("pause");
}
void changeDay(Book book[MAX], int i) {
int day;
cout << "输入修改日:";
cin >> day;
book[i].setDay(day);
cout << "修改成功!";
system("pause");
}
void reviseBookMessageName(Book book[MAX]) {
string nameSearch;
int number = 0;
int choiceSearch;
if (COUNT == 0) {
system("cls");
cout << "tttt----------------------------------" << endl;
cout << "tttt书库中木有书,请添加书籍" << endl;
cout << "tttt----------------------------------" << endl;
Sleep(1000);
mainBookMenu();
}
else {
system("cls");
cout << "请输入你想更改书的书名:";
cin >> nameSearch;
for (int i = 0; i < COUNT; i++) {
if (book[i].getWriter() == nameSearch) {
cout << "tt书名" << "t 作者" << "t 类型号" << "t 价格" << "t出版社" << "t出版日期" << endl;
cout << "tt" << book[i].getName() << "t " << book[i].getWriter() << "t " << book[i].getTypeNumber() << "t " << book[i].getPrice() << "t " << book[i].getPublisher() << "t " << book[i].getDateYear() << "-" << book[i].getDateMonth() << "-" << book[i].getDateDay() << endl;
cout << "tttt1.书名" << endl;
cout << "tttt2.作者" << endl;
cout << "tttt3.类型号" << endl;
cout << "tttt4.价格" << endl;
cout << "tttt5.出版社" << endl;
cout << "tttt6.出版年" << endl;
cout << "tttt7.出版月" << endl;
cout << "tttt8.出版日" << endl;
cout << "请选择修改的项目:";
cin >> choiceSearch;
switch (choiceSearch) {
case 1:changeName(book, i);
break;
case 2:changeWriter(book, i);
break;
case 3:changeTypeNumber(book, i);
break;
case 4:changePublisher(book, i);
break;
case 5: changePrice(book, i);
break;
case 6:changeYear(book, i);
break;
case 7:changeMonth(book, i);
break;
case 8:changeDay(book, i);
break;
}
number++;
}
}
}
if (!number) {
system("cls");
cout << "tttt未找到任何相图书" << endl;
}
system("pause");
}
void deleteBookMessageName(Book book[MAX]) {
string nameDelete;
int number = 0;
if (COUNT == 0) {
system("cls");
cout << "tttt----------------------------------" << endl;
cout << "tttt书库中木有书,请添加书籍" << endl;
cout << "tttt----------------------------------" << endl;
Sleep(1000);
mainBookMenu();
}
cout << "tttt请输入你要删除的书名:";
cin >> nameDelete;
for (int i = 0; i < COUNT; i++) {
if (book[i].getName() == nameDelete) {
for (int j = i; j < COUNT; j++) {
book[j] = book[j + 1];
COUNT--;
number++;
}
}
}
if (!number) {
system("cls");
cout << "tttt未找到任何相图书" << endl;
}
}
void saveFile(Book book[MAX]) {
system("cls");
ofstream ofs, ofs1;
ofs1.open("图书管理.txt", ios::out);
ofs1 << COUNT;
ofs.open("图书管理_data.txt", ios::binary);
if (!ofs) {
cerr << "open error!" << endl;
return;
}
for (int i = 0; i < COUNT; i++) {
ofs.write((char*)&book[i], sizeof(book[i]));
}
ofs.close();
cout << "tttt保存成功!" << endl;
Sleep(1000);
mainBookMenu();
}
void readFile(Book book[MAX]) {
system("cls");
ifstream ifs, ifs1;
ifs1.open("图书管理.txt", ios::in);
ifs1 >> COUNT;
ifs.open("图书管理_data.txt", ios::binary);
if (!ifs) {
cerr << "open error!" << endl;
return;
}
for (int i = 0; i < COUNT; i++) {
ifs.read((char*)&book[i], sizeof(book[i]));
}
ifs.close();
cout << "tttt读取成功!" << endl;
Sleep(1000);
mainBookMenu();
}
void searchMenu(Book book[MAX]) {
int choiceSearch;
system("cls");
cout << "tttt1.书名查询" << endl;
cout << "tttt2.作者查询" << endl;
cout << "tttt选择那种查询方式:";
cin >> choiceSearch;
switch (choiceSearch) {
case 1:searchBookMessageName(book);
break;
case 2:searchBookMessageWriter(book);
break;
}
}
void exitSystem() {
system("cls");
cout << "tttt----------------------------------" << endl;
cout << "tttt| |感谢使用!!| |" << endl;
cout << "tttt----------------------------------" << endl;
exit(0);
}
void mainBookMenu() {
int choice = 0;
do {
system("cls");
cout << "tttt----------------------------------" << endl;
cout << "tttt| | 图书管理系统 | |" << endl;;
cout << "tttt| | 制作者李世豪 | |" << endl;
cout << "tttt----------------------------------" << endl;
cout << "tttt| |1.图书信息录入| |" << endl;
cout << "tttt| |2.图书信息浏览| |" << endl;
cout << "tttt| |3.图书信息查找| |" << endl;
cout << "tttt| |4.图书信息删除| |" << endl;
cout << "tttt| |5.图书信息修改| |" << endl;
cout << "tttt| |6. 保存 | |" << endl;
cout << "tttt| |7. 读取 | |" << endl;
cout << "tttt| |8. 退出 | |" << endl;
cout << "tttt----------------------------------" << endl;
cout << "tttt输入序号选择:";
cin >> choice;
if (choice >= 1 && choice <= 8) {
switch (choice) {
case 1:addBookMessage(book);
break;
case 2:showBookMessage(book);
break;
case 3:searchMenu(book);
break;
case 4:deleteBookMessageName(book);
break;
case 5:reviseBookMessageName(book);
break;
case 6:saveFile(book);
break;
case 7:readFile(book);
break;
case 8:exitSystem();
break;
}
}
else {
system("cls");
cout << "输入序号有误" << endl;
Sleep(1000);
mainBookMenu();
}
} while (choice != 8);
}
void mainUserMenu() {
system("cls");
string checkId;
string checkPassWord;
User user;
cout << "tttt----------------------------------" << endl;
cout << "tttt| | 用户验证 | |" << endl;
cout << "tttt----------------------------------" << endl;
cout << "tttt输入登入账号:";
cin >> checkId;
cout << "tttt输入登入密码:";
cin >> checkPassWord;
user.check(checkId, checkPassWord);
}
int main() {
mainUserMenu();
return 0;
}