#include <iostream>
using namespace std;
#include <fstream>
#include <cstring>
#include "interface.h"
#include "reader.h"
//图书管理模块
//图书类函数实现
void book::setno(int n) {
no = n;
}
int book::getno() {
return no;
}
void book::setname(char na[]) {
strcpy_s(name, na);
}
char* book:: getname() {
return name;
}
void book::borbook() {
b_flag = 2; //借书标志
}
void book::delbook() {
b_del = 2; //删除标志
}
void book::addbook(int no, char na[]) {
setno(no);
setname(na);
b_flag = 1;
b_del = 1;
}
//图书数据库类
bdatabase::bdatabase() {
book b; //建立book类对象并b
top = -1; //数组下标从0开始,此处top值置为-1可保证从数组第一个元素开始存储信息
fstream file("book.txt", ios::in);//打开文件进行读操作,若文件不存在则出错.输入输出指:内存与硬盘间的信息传递
while (1) {
file.read((char*)&b, sizeof(b));//从文件中读取b个字符到char*指向的缓存中(book类对象b的地址)
if (!file) //若读取到文件尾EOF,返回非0值(一般为1),结束书本数量的计数,关闭文件
break;
top++;
btarray[top] = b;
}
file.close();
}
bdatabase::~bdatabase() {
fstream file("book.txt", ios::out);//输出:内存->硬盘
for (int i = 0; i <= top; i++) {
if (btarray[i].b_del == 1)
file.write((char*)&btarray[i], sizeof(btarray[i]));
}
file.close();
}
void bdatabase::addbooks() {
book bk; //创建book类对象bk
int no; //书号
char bname[20]; //书名 用数组存放
cout << "请输入书号:";
cin >> no;
cout << endl << "请输入书名:";
cin >> bname;
cout << endl;
bk.b_flag = 1;
bk.addbook(no, bname); //类中成员函数addbook(),设置书号与书名 修改标志
top++;
btarray[top] = bk;
return; //返回上个操作界面
}
int bdatabase::findbooks(int suffix) {
int no;
cout << "请输入书号:";
cin >> no;
for (int i = 0; i <= top; i++) {
if (btarray[i].no == no && btarray[i].b_del == 1) {
return i;
}
}
return -1;
}
int bdatabase::findbooks() {
int no;
char value[6];
cout << "请输入书号:";
cin >> no;
for (int i = 0; i <= top; i++) {
if (btarray[i].no == no && btarray[i].b_del == 1) {
if (btarray[i].b_flag == 1)
strcpy_s(value, "在架");
if (btarray[i].b_flag == 2)
strcpy_s(value, "借出");
cout << "书号:" << btarray[i].no << " " << "书名:" << btarray[i].name << " " << "图书状态:" << value << endl;
return i;
}
}
return -1;
}
void bdatabase::editbooks() {
int cur;
cur = findbooks();
if (cur == 1) {
cout << "该书不存在" << endl;
return;
}
cout << "当前图书信息--" << "书号:" << btarray[cur].no << " " << "书名:" << btarray[cur].name << endl;
cout << endl;
cout << "请继续操作以修改图书信息" << endl;
cout << endl << "书名:";
cin >> btarray[cur].name;
return;
}
void bdatabase::delbooks() {
int cur;
cur = findbooks();
if (cur == 1) {
return;
}
if (btarray[cur].b_flag == 2) { //删除时判断图书是否借出
cout << "该图书已经借出,请归还后删除!" << endl;
return;
}
btarray[cur].b_del = 2;
return;
}
void bdatabase::listbooks() {
char value[6];
for (int i = 0; i <= top; i++) {
if (btarray[i].b_del == 1) {
if (btarray[i].b_flag == 1)
strcpy_s(value, "在架");
if (btarray[i].b_flag == 2)
strcpy_s(value, "借出");
cout << "书号:" << btarray[i].no << "书名:" << btarray[i].name << "图书状态:" << value << endl;
}
}
return;
}
void borrow_or_return(char br) { //借书、还书函数,参数br:判断 “借书”,“还书”. 1 借书 2 还书
int reader_ID = 0; //读者id
int book_ID = 0; //书的id
int rsuffix, bsuffix;
bdatabase t_bd;
rdatabase t_rd;
if (br == '1') { //借书
rsuffix = t_rd.findreaders(reader_ID);
bsuffix = t_bd.findbooks(book_ID);
if (rsuffix == -1 || bsuffix == -1)
return;
if (t_bd.btarray[bsuffix].b_flag == 2) {
cout << "图书已经借出,请选择其它图书" << endl;
return;
}
t_bd.btarray[bsuffix].b_flag = 2;
t_rd.rdarray[rsuffix].r_bor = 1;
t_rd.rdarray[rsuffix].brbook = t_bd.btarray[bsuffix];
return;
}
if (br == '2') {
rsuffix = t_rd.findreaders(reader_ID);
bsuffix = t_bd.findbooks(book_ID);
if (rsuffix == -1 || bsuffix == -1)
return;
if (t_rd.rdarray[rsuffix].brbook.no == t_bd.btarray[bsuffix].no) {
t_bd.btarray[bsuffix].b_flag = 1;
t_rd.rdarray[rsuffix].r_bor = 2;
t_rd.rdarray[rsuffix].brbook = t_bd.btarray[bsuffix];
}
else {
cout << "请重新输入,读者借阅图书书号错误" << endl;
return;
}
return;
}
}
void booksmanage() {
char in;
bdatabase bd;
do {
system("pause");
system("cls");//切换UI时,刷新缓冲区当前内容,完成下次更新
UI();
Book_Maintenance_Interface();
cin >> in;
switch (in) {
case'1':
bd.addbooks();
break;
case'2':
bd.editbooks();
break;
case'3':
bd.delbooks();
break;
case'4':
bd.findbooks();
break;
case'5':
bd.listbooks();
break;
case'6':
break;
default:
cout << "In Put Error!!! Please try again!!!" << endl;
}
} while (in != '6');
return;
}