#include<iostream>
#include<stdio.h>
#include<fstream>
#include<conio.h>
using namespace std;
const int Maxbor=5;
class Librarian //Define a librarian class  
{
public:
Librarian();
Librarian(int n,int j,char na[20],int w );
int getnumber();
int getjobage();
char *getname();
int getwages();
void resetjobage(int j);
void resetwages(int w);
void resetname(char na[20]);
private:
int number;
int jobage;
char name[20];
int wages;
};
Librarian::Librarian() //Define the default constructor
{
number=0;
char b[20]="no one";
jobage=0;
strcpy(name,b);
}
Librarian::Librarian(int n,int j,char na[20],int w) //Define a librarian member function
{
number=n;
jobage=j;
strcpy(name,na);
wages=w;
}
int Librarian::getjobage() //Define the getjobage member function, output jobage
{return jobage;}
char *Librarian::getname() //Define the getname member function, output name
{return name;}
int Librarian::getnumber() //Define the getnumber member function, output number
{return number;}
int Librarian::getwages() //Define the getwages member function, output wages
{return wages;}
void Librarian::resetjobage(int j) //Define the resetjobage member function, read in jobage
{
jobage=j;
}
void Librarian::resetname(char na[20]) //Define the resetname member function, read in a name
{
strcpy(name,na);

}
void Librarian::resetwages(int w) //Define the resetwages member function, read in w as wages
{
wages=w;
}

class reader //Define a reader class  
{
private:
int number; 
int age;
char name[20];
int borbook;//books reader has borrowed
public:
reader();
reader(int c,int a,char b[20]);
reader(int c,int a,char b[20],int e);
int getnumber();
int getage();
char *getname();
int getbor();
char* disname(){return name;}
void resetage(int j) {age=j; }
void resetname(char na[20]) {strcpy(name,na);}
void resetborbook(int a){borbook=a;}
};
reader::reader() //Define the default constructor
{
char a[20]="No body";
char c[40]="This person didn't borrow books";
number=0;
age=0;
strcpy(name,a);
borbook=0;
}
reader::reader(int c,int a,char b[20]) //Define a reader member function with no book borrowed
{
number=c;
age=a;
strcpy(name,b);
borbook=0;
}
reader::reader(int c,int a,char b[20],int e) //Define a reader member function
{
number=c;
age=a;
strcpy(name,b);
borbook=e;
}
int reader::getnumber(){return number;} //Define getnumber member function, output number
int reader::getage(){return age;} //Define getage member function, output age
char *reader::getname(){return name;} //Define getname member function, output name
int reader::getbor(){return borbook;} //Define getbor member function, output borbook

class book  //Define a book class  
{
int num;
char name[20];
char writer[20];
public:
int getnumber(){return num;}
char *getname(){return name;}
char *getwriter(){return writer;}
void resetwriter(char na[20]);
void resetname(char na[20]) ;
};

void book::resetname(char na[20]) //Define a member function, reset book name, read in name
{
strcpy(name,na);

}
void book::resetwriter(char na[20]) //Define a member function,reset book author, read in writer
{
strcpy(writer,na);
}

void aadreader()   //Function: Add a reader & record info into reader.txt
{
int number;char name[12];int n,age,borbook;
a:
ofstream fout("reader.txt",ios::app);
cout<<"Please upload reader's information:"<<endl;
cout<<"name"<<'\t'<<"age"<<'\t'<<"NO."<<'\t'<<"how many times"<<endl;
cin>>name;fout<<name<<'\t';
cin>>age;fout<<age<<'\t';
cin>>number;fout<<number<<'\t';
cin>>borbook;fout<<borbook<<'\n';
fout.close();
cout<<"Continue to upload?1.Yes 2.No"<<endl;
cin>>n;
if(n==1)
goto a;
else
{void studentland();
studentland();}
}

void outreaderinf() //Function: output all reader info from reader.txt
{
int i;
ifstream infile("reader.txt",ios::in);
char line[50];
cout<<"name"<<'\t'<<"age"<<'\t'<<"NO."<<'\t'<<"how many times"<<endl;
for(i=1;i<=10;i++)
{
infile.getline(line,50,'\n');
cout<<line<<endl;
}
infile.close();
}

 

void outbookinf() //Function: output all books info from allbook.txt
{
int i;
ifstream infile("allbook.txt",ios::in);
char line[50];
cout<<"title"<<'\t'<<"NO."<<'\t'<<"author"<<endl;
for(i=1;i<=10;i++)
{
infile.getline(line,50,'\n');
cout<<line<<endl;
}
infile.close();
}

void serchLaninf() //Function: output librarian info or output error
{
int n,i;
ifstream file("serchLaninf.txt");
if(file) { file>>n; file.close();}
else {cout<<"No librarian"; exit(0);}
ifstream is("controlloer.txt",ios_base::binary);                       
if(is)
{
Librarian *A=new Librarian[n];
for(i=0;i<n;i++)
{
is.read((char *)&A[i],sizeof(A[i]));
}
for(i=0;i<n;i++)
{
cout<<"NO."<<'\t'<<"age"<<'\t'<<"name"<<'\t'<<"wage"<<endl;
cout<<A[i].getnumber()<<"   \t"<<A[i].getjobage()<<"   \t"<<A[i].getname()<<"   \t"<<A[i].getwages()<<endl;
}
}
else
{
cout<<".txt file error'"<<endl;
}
is.close();
}

 

void libary() //Function: output library info
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"    The NUPT Library has a total area of the 33018 square meters, and 4645 reading seats inside.";
cout<<"(The library now has all kinds of books and paper collection of more than 130 copies...)"<<endl;
cout<<"    Since the implementation of computer automation management in 1990, after many years of continuous improvement and transformation, the library has a high speed network, integrated with the campus network environment."<<endl;
cout<<"    The library can hold academic activities..."<<endl;
}

 

void storelibrarian() //Function: record librarian info into ‘serchLaninf.txt’ and ‘controlloer.txt’
{
int age,n,num,wage;
char name[20];
ifstream file("serchLaninf.txt");
if(file) { file>>n; file.close();}
else { n=0;}
cout<<"The number of librarian now:";
cout<<n<<endl;
cout<<"Please enter the NO., age, name and wage for the new librarian:";
cin>>num>>age>>name>>wage;
Librarian A(num,age,name,wage);
cout<<"NO. age name wage "<<endl;
cout<<A.getnumber()<<" "<<A.getjobage()<<" "<<A.getname()<<" "<<A.getwages()<<endl;
ofstream outfile("controlloer.txt",ios_base::app );
outfile.write((char *)&A,sizeof(A));
outfile.close();
n++;
ofstream tfile("serchLaninf.txt");
tfile<<n;
tfile.close();
cout<<"Already save!";
}

 

void reLaninf() //Modify librarian info
{
int i,n,h,jobage,wage,j;
char name[20];
ifstream file("serchLaninf.txt");
if(file) { file>>n; file.close();}
else {cout<<"No librarian!";
exit(0);}
cout<<"The number of librarian now:";
cout<<n<<endl;
Librarian *A=new Librarian[n];
ifstream is("controlloer.txt",ios_base::binary);
if(is)
{
for(i=0;i<n;i++)
{
is.read((char *)&A[i],sizeof(A[i]));
}
for(i=0;i<n;i++)
{
cout<<"NO.  age  name  wage"<<endl;
cout<<A[i].getnumber()<<"    "<<A[i].getjobage()<<"    "<<A[i].getname()<<"    "<<A[i].getwages()<<endl;
}
}
else
{
cout<<"file error"<<endl;
}
is.close();
cout<<"Please select the administrator NO. you want to change:";
cin>>h;
cout<<"upload new age, name, wage:";
cin>>jobage>>name>>wage;
A[h-1].resetjobage(jobage);
A[h-1].resetname(name);
A[h-1].resetwages(wage);
cout<<"The new name is:"<<A[h-1].getname()<<" age is:"<<A[h-1].getjobage()<<" wage is:"<<A[h-1].getwages()<<endl;
ofstream tfile("controlloer.txt",ios_base::binary);
for(j=0;j<n;j++)
{
tfile.write((char *)&A[j],sizeof(A[j]));
}
tfile.close();
}

void aadbook() //Add book and record into ‘allbook.txt’
{
int num;char name[12];char writer[10];int n;
a:
ofstream fout("allbook.txt",ios::app);
cout<<"Please upload book information:"<<endl;
cout<<"name"<<'\t'<<"NO."<<'\t'<<"author"<<endl;
cin>>name;fout<<name<<'\t';
cin>>num;fout<<num<<'\t';
cin>>writer;fout<<writer<<'\n';
fout.close();
cout<<"Whether continue to upload?1.Yes 2.No"<<endl;
cin>>n;
if(n==1)
goto a;
else
{void studentland();
studentland();}
}

void delate() //Delete book record
{ int m;
cout<<"Select option:"<<endl;
cout<<" 1.delete all book information"<<endl;
cout<<" 2.delete one book information"<<endl;
cin>>m;
if(m==1)
{ofstream file("allbook.txt",ios::trunc);
cout<<"All deleted, press any key to return!"<<endl;
file.close();
getch();
system("cls");          //clear screen
void studentland();
studentland();
}
char str[10][80],buf[20];
a:
int i=0,flag=100,c=0;

ifstream fin("allbook.txt");
if(!fin){
cout<<"file error!"<<endl;
getch();
system("cls");
void studentland();
studentland();
}
cout<<"Please enter the title you want to remove:"<<endl;
cin>>buf;
strcat(buf,"\t");

while(fin.getline(str[i],80))
{
if(strncmp(str[i],buf,strlen(buf))==0)
{
flag=i;
}
i++;
}

fin.close();

if(flag==100)
{
cout<<"No book match your title! continue enter press 1, else press 0..."<<endl;
int b;
cin>>b;
if(b==1)goto a;
else c=1;
}
if(c==0){
ofstream fout("allbook.txt");
for(int j=0;j<i;j++){
if(j==flag)continue;
fout<<str[j]<<endl;
}
fout.close();
cout<<"The book information has deleted,continue press 1, else press 0..."<<endl;
int b;
cin>>b;
if(b==1)goto a;
}
cout<<"Finished, press any key to return..."<<endl;
getch();
system("cls");
void studentland();
studentland();
}

 

void searchbook() // Search book from ‘allbook.txt’
{
a:
ifstream fin;
int flag=0,b,length;
char str[20];
char f[200];char g[200];
fin.open("allbook.txt");
if(!fin)
{
cout<<"Open f1.dat error..."<<endl;
exit(1);
}
while(1)
{
cout<<"Please enter title of the book:";
cin>>str;
strcat(str,"\t");
length=strlen(str);
cout<<"------------------------------------"<<endl;
while(fin.getline(f,199))
{
strcpy(g,f);
if(strncmp(str,f,length)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"The book has been found.:"<<'\n';
cout<<"--------------------------------"<<endl;
cout<<"name"<<'\t'<<"NO."<<'\t'<<"author"<<'\t'<<endl;
cout<<g<<endl;
cout<<"--------------------------------"<<endl;
flag=0;
cout<<"continue to find press 1, else press 0:"<<endl;
cin>>b;
if(b==0)break;
}
else
{
fin.close();
cout<<"No book match the title, continue press 1, else press 0..."<<endl;
cin>>b;
if(b==0)break;
goto a;
}
}
fin.close();
cout<<"Finished, press any key to return..."<<endl;
getch();
system("cls");
void studentland();
studentland();
}

void delreader() //Delete reader record in ‘reader.txt’ or show error
{int l;
ifstream infile("reader.txt",ios::in);
char line[50];
cout<<"name"<<'\t'<<"age"<<'\t'<<"NO."<<"\t"<<"how many times"<<endl;
for(l=1;l<=10;l++)
{
infile.getline(line,50,'\n');
cout<<line<<endl;
}
infile.close();
int m;
cout<<"Select option:"<<endl;
cout<<" 1.Delete all information"<<endl;
cout<<" 2.Delete one information"<<endl;
cin>>m;
if(m==1)
{ofstream file("reader.txt",ios::trunc);
cout<<"All deleted, press any key to return!"<<endl;
file.close();
getch();
system("cls");         
void studentland();
studentland();
}
char str[10][80],buf[20];
a:
int i=0,flag=100,c=0;
ifstream fin("reader.txt");
if(!fin){
cout<<"open error!"<<endl;
getch();
system("cls");
void studentland();
studentland();
}
cout<<"Please enter the name of the reader you want to delete:"<<endl;
cin>>buf;
strcat(buf,"\t");
while(fin.getline(str[i],80))
{
if(strncmp(str[i],buf,strlen(buf))==0)
{
flag=i;
}
i++;
}

fin.close();

if(flag==100)
{
cout<<"No reader match the name! Continue to delete press 1, else press 0..."<<endl;
int b;
cin>>b;
if(b==1)goto a;
else c=1;
}
if(c==0){
ofstream fout("reader.txt");
for(int j=0;j<i;j++){
if(j==flag)continue;
fout<<str[j]<<endl;
}
fout.close();
cout<<"The reader's information has been deleted. Continue press 1, else press 0..."<<endl;
int b;
cin>>b;
if(b==1)goto a;
}
cout<<"Finished, press any key to return..."<<endl;
getch();
system("cls");
void studentland();
studentland();
}

void borrow()                     //Function: borrow book
{
ifstream fin;
int flag=0,length;
char str1[20],name[20];
char f[200];char g[200];
fin.open("allbook.txt");
if(!fin)
{
cout<<"Open f1.dat error....."<<endl;
exit(1);
}
//while(1) {
b:
cout<<"Please enter the title of the book:";
cin>>str1;
strcat(str1,"\t");
length=strlen(str1);
cout<<"------------------------------------"<<endl;
while(fin.getline(f,199))
{
strcpy(g,f);
if(strncmp(str1,f,length)==0)
{
flag=1;
break;
}
else
goto b;
}
if(flag==1)
{
cout<<"The book has been found:"<<'\n';
cout<<"--------------------------------"<<endl;
cout<<"title"<<'\t'<<"NO."<<'\t'<<"author"<<'\t'<<endl;
cout<<g<<endl;
cout<<"--------------------------------"<<endl;
flag=0;
char str[10][80];
a:
int i=0,flag1=100,c=0;
ifstream in("reader.txt");
if(!in){
cout<<"file error!"<<endl;
getch();
system("cls");
void studentland();
studentland();
}
cout<<"Please enter the reader's name:"<<endl;
cin>>name;
strcat(name,"\t");
//cin>>buf;
//strcat(buf,"\t");
while(in.getline(str[i],80))
{
if(strncmp(str[i],name,strlen(name))==0)
{
flag1=i;
}
i++;
}
in.close();
if(flag1==100)
{
cout<<"No reader match the name! Continue to find press 1, else press 0..."<<endl;
int b;
cin>>b;
if(b==1)goto a;
else{c=1;
cout<<endl;
cout<<"fail to borrow!"<<endl;
cout<<endl;
}
}
if(c==0){
         cout<<endl;
         cout<<"Success to borrow!"<<endl;
         cout<<endl;}
cout<<"Finished, press any key to return..."<<endl;
getch();
system("cls");
void studentland();
studentland();
}
}

void returnbook()                       //Function:return book info
{
ifstream fin;
int flag=0,length;
char str1[20],name[20];
char f[200];char g[200];
fin.open("allbook.txt");
if(!fin)
{
cout<<"Open f1.dat error..."<<endl;
exit(1);
}
//while(1) {
b:
cout<<"Please enter the title of the book:";
cin>>str1;
strcat(str1,"\t");
length=strlen(str1);
cout<<"------------------------------------"<<endl;
while(fin.getline(f,199))
{
strcpy(g,f);
if(strncmp(str1,f,length)==0)
{
flag=1;
break;
}
else
goto b;
}
if(flag==1)
{
cout<<"The book has been found:"<<'\n';
cout<<"--------------------------------"<<endl;
cout<<"title"<<'\t'<<"NO."<<'\t'<<"author"<<'\t'<<endl;
cout<<g<<endl;
cout<<"--------------------------------"<<endl;
flag=0;
char str[10][80];
a:
int i=0,flag1=100,c=0;
ifstream in("reader.txt");
if(!in){
cout<<"File error!"<<endl;
getch();
system("cls");
void studentland();
studentland();
}
cout<<"Please enter the reader's name:"<<endl;
cin>>name;
strcat(name,"\t");
//cin>>buf;
//strcat(buf,"\t");
while(in.getline(str[i],80))
{
if(strncmp(str[i],name,strlen(name))==0)
{
flag1=i;
}
i++;
}
in.close();
if(flag1==100)
{
cout<<"No reader match the name! Continue press 1, else press 0..."<<endl;
int b;
cin>>b;
if(b==1)goto a;
else{c=1;
cout<<endl;
cout<<"Fail to return book!"<<endl;
cout<<endl;}
}
if(c==0){
         cout<<endl;
         cout<<"Success to return book!"<<endl;
         cout<<endl;}
cout<<"Finished, press any key to return..."<<endl;
getch();
system("cls");
void studentland();
studentland();
}
}

void Librarianland() //Function: librarian login
{
int q,l=1,i,h;
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<"Confirm the identity, please press the password:";
cin>>i;
while(l)
{
if(i==00000)
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<"*******" <<" |Inquiry the information of librarian, press 1|  "<<" *******"<<endl;
cout<<" "<<"*******" <<"  |Modify the information of librarian, press 2|  "<<" *******"<<endl;
cout<<" "<<"*******" <<"|Upload the information of new librarian, press 3|"<<" *******"<<endl;
cout<<" "<<endl;
cout<<" "<<"*******" <<"      |Output information of books, press 4|  "<<"     *******"<<endl;
cout<<" "<<"*******" <<"               |Add book, press 5|"<<"                 *******"<<endl;
cout<<" "<<"*******" <<"              |Delete book, press6|"<<"                *******"<<endl;
cout<<" "<<endl;
cout<<" "<<"*******" <<"    |Output reader's information , press 7|    "<<"    *******"<<endl;
cout<<" "<<"*******" <<"              |Add reader, press 8|          "<<"      *******"<<endl;
cout<<" "<<"*******" <<"            |Delete reader, press 9|"<<"               *******"<<endl;
cout<<" "<<endl;
cout<<" "<<"*******" <<"       |Return to previous menu, press 0|"<<"          *******"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"Please press the number: ";
cin>>q;
if(q>=0&&q<10)
{
switch(q)
{
case 0:
l=0;
break;
case 1:serchLaninf();
system("pause");
break;
case 2:reLaninf();
system("pause");
break;
case 3:storelibrarian();
system("pause");
break;
case 4:outbookinf();
system("pause");
break;
case 5:aadbook();
break;
case 6:delate();
break;
case 7:outreaderinf();
system("pause");
break;
case 8:aadreader();
system("pause");
break;
case 9:delreader();
system("pause");
break;
default:
break;
}
}
else
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<"Invalid!"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
system("pause");
}
}
else
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<" Identity verification failed!"<<endl;
cout<<" "<<" Whether to reconfirm?"<<endl;
cout<<" "<<"Press 1 to reconfirm, Press 0 to give up"<<endl;
cout<<" ";
cin>>h;
if(h==0) l=0;
if(h==1)
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<"Please press the password again:";
cin>>i;
}
}

}
}

 

void studentland() //Function: student interface
{
int h;
char l='y';
while(l=='y')
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<"****** "<<"|General information about the library, press 1 | "<<"******"<<endl;
cout<<" "<<"****** "<<"      |Output books information, press 2|    "<<"     ******"<<endl;
cout<<" "<<endl;
cout<<" "<<"****** "<<"        |Search book by title, press 3| "<<"          ******"<<endl;
cout<<" "<<"****** "<<"  |Information of reader borrow books, press 4| "<<"  ******"<<endl;
cout<<" "<<"****** "<<"  |Information of reader return books, press 5| "<<"  ******"<<endl;
cout<<" "<<endl;
cout<<" "<<"****** "<<"      |Return to previous menu, press 0| "<<"         ******"<<endl;
cout<<"Please press the number:";
cin>>h;
if(h>=0&&h<7)
switch(h)
{
case 0:
l='n';
break;
case 1:libary();
system("pause");
break;
case 2: outbookinf();
system("pause");
break;
case 3:searchbook();
system("pause");
break;
case 4:borrow();
system("pause");
break;
case 5:
returnbook();
system("pause");
break;
}
else
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<"Invalid!"<<endl;
system("pause");
}
}
}

 

void main() // Main function 
{
int h,l=1;
while(l)
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<"   Books Management System"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" ------------------"<<endl;
cout<<" "<<"||Administrator login, press 1||"<<endl;
cout<<" "<<"   ||Student login, press 2||"<<endl;
cout<<" "<<"      ||Log out, press 0||"<<endl;
cout<<" -------------------"<<endl;
cout<<""<<endl;
cout<<"Please press the number:";
cin>>h;
if(h>=0&&h<3)
{
switch(h)
{
case 0:
cout<<"Welcome to Books Management System!"<<endl;
exit(0);
case 1:
Librarianland();
system("pause");
break;
case 2:
studentland();
system("pause");
break;
}
}
else
{
system("cls");

cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<"Invalid!"<<endl;
system("pause");
}
}
}