C++ library management system

  • 2020-06-23 01:14:49
  • OfStack

Be free and at leisure, C++ to do a book management system, mainly with books, books, book management, user management and other functions, the main use of technology is containers and files, as well as class packaging


#include <iostream>
#include <list>
#include <algorithm>
#include <string.h>
#include <fstream>
#include <stdlib.h>
 
using namespace std;
class Mybook;
class Book;
 
class Book{
public:
 int id;              // Book number 
 char book_name[20];        // Title: 
 int state;            // The book state 
 char place[20];          // Book location 
 char stu_number[20];       // Student id 
 char stu_name[20];        // The student's name 
public:
 Book();
 friend class Mybook;
};
 
class User{
public:
 char stu_number[20];        // Student id 
 char stu_name[20];         // The student's name 
public:
 User()
 {
  strcpy(stu_number,"000");
  strcpy(stu_name,"0");
 }
 friend class Mybook;
};
 
class Mybook{
private:
 list<Book> link_book;       // Save book information 
 list<User> link_user;       // Save user information 
public:
 int main_menu();          // The main menu 
 void getmenu();          // Access to the menu  
 int menu();            // Library management menu 
 void getchoice();         // The input selection 
 void add_book();          // Add books 
 void display_book();        // Display book information 
 void del_book();          // Delete book information 
 void search_book();        // Search for book information 
 void update_book();        // Modify book information 
 void borrow_book();        // Borrow books 
 void return_book();        // Return the book 
 int menu_user();         // Manage user menu 
 void add_user();          // Add user 
 void del_user();          // Delete user 
 void display_user();        // To view the user 
 void update_user();        // Modify the user 
 void look_borrow();        // Check out borrowed books  
 void get_user();          // User management 
 void recv_file();         // Save the data to a file 
 void read_file();         // Read the data from the file 
 void recv_user();         // Save user information to a file 
 void read_user();         // Read user information from a file 
};
 
Book::Book()
{
 state = 0;
 strcpy(stu_number,"000");
 strcpy(stu_name,"0");
}
// Save the book information to a file 
void Mybook::recv_file()
{
 ofstream outfile("library.txt",ios::out);
 if(!outfile)
 {
 cout<<" File opening failed "<<endl;
 return ;
 }
 sleep(1);
 list<Book>::iterator p = link_book.begin();
 while(p != link_book.end())
 {
 outfile<<p->id<<endl;
 outfile<<p->book_name<<endl;
 outfile<<p->state<<endl;
 outfile<<p->place<<endl;
 outfile<<p->stu_number<<endl;
 outfile<<p->stu_name<<endl;
 p++;
 }
 cout<<" Save done! "<<endl;
 outfile.close();
}
 
void Mybook::read_file()
{
 ifstream infile("library.txt",ios::in);
 Book new_book;
 if(!infile)
 {
 cout<<"read fail"<<endl;
 return ;
 }
 char temp[20];
 while(!infile.eof())
 {
 infile.getline(temp,'\n');
 new_book.id = atoi(temp);
 memset(temp,0,20);
 infile.getline(new_book.book_name,'\n');
 infile.getline(temp,'\n');
 new_book.state = atoi(temp);
 memset(temp,0,20);
 infile.getline(new_book.place,'\n');
 infile.getline(new_book.stu_number,'\n');
 infile.getline(new_book.stu_name,'\n');
 if(infile.eof())
 {
  break;
 }
 link_book.push_back(new_book); 
 }
 infile.close();
}
// Save user information 
void Mybook::recv_user()
{
 ofstream outfile("user.txt",ios::out);
 if(!outfile)
 {
 cout<<" File opening failed "<<endl;
 return ;
 }
 sleep(1);
 list<User>::iterator p = link_user.begin();
 while(p != link_user.end())
 {
 outfile<<p->stu_number<<endl;
 outfile<<p->stu_name<<endl;
 p++;
 }
 cout<<" Save done! "<<endl;
 outfile.close();
}
// Read the data to the user 
void Mybook::read_user()
{
 ifstream infile("user.txt",ios::in);
 if(!infile)
 {
 cout<<" File opening failed! "<<endl;
 return ;
 }
 User new_user;
 while(!infile.eof())
 {
 infile.getline(new_user.stu_number,'\n');
 infile.getline(new_user.stu_name,'\n');
 if(infile.eof())
 {
  break;
 }
 link_user.push_back(new_user); 
 }
 infile.close(); 
}
// The main menu 
int Mybook::main_menu()
{
 int choice = 0;
 cout<<"\t\t\t\t===================================================="<<endl;
 cout<<"\t\t\t\t*        Welcome to the library management system         *"<<endl;
 cout<<"\t\t\t\t*--------------------------------------------------*"<<endl;
 cout<<"\t\t\t\t*         1 Borrow books,              *"<<endl;
 cout<<"\t\t\t\t*         2 Return the book,              *"<<endl;
 cout<<"\t\t\t\t*         3 Book management            *"<<endl;
 cout<<"\t\t\t\t*         4 · User management            *"<<endl;
 cout<<"\t\t\t\t*         0 , save and exit           *"<<endl;
 cout<<"\t\t\t\t*__________________________________________________*"<<endl;
 cout<<"\t\t\t\t\t Please enter options :";
 cin>>choice;
 while(!(choice >= 0 && choice <= 4))
 {
 cout<<" Input error, please retype :";
 cin>>choice;
 }
 return choice;
}
// Execute the main menu 
void Mybook::getmenu()
{
 int choice = 0;
 read_user();
 read_file();
 while(1)
 {
 system("clear");
 choice = main_menu();
 switch(choice)
 {
  case 1:
  {
  borrow_book();
  break;
  }
  case 2:
  {
  return_book();
  break;
  }
  case 3:
  {
  getchoice();
  break;
  }
  case 4:
  {
  get_user();
  break;
  }
  case 0:
  {
  cout<<" Saving, please hold on ....."<<endl;
  recv_file();
  exit(0);
  }
 }
 cout<<endl<<endl<<endl;
 cout<<" Press any key to return ...";
 getchar();
 getchar();
 }
}
// Borrow books 
void Mybook::borrow_book()
{
 int flag = 0;
 int flag1 = 0;
 int flag2 = 0;
 char id[20];
 char name[20];
 char book_name[20];
 cout<<" Please enter your student number :";
 cin>>id;
 list<User>::iterator it = link_user.begin();
 while(it != link_user.end())
 {
 if(strcmp(it->stu_number,id) == 0)
 {
  strcpy(name,it->stu_name);
  flag2 = 1;
  break;
 }
 it++;
 }
 if(flag2 == 0)
 {
 cout<<" You don't have permission to borrow books! Borrow books failure "<<endl;
 return ;
 }
 cout<<" Please enter title: ";
 cin>>book_name;
 list<Book>::iterator p = link_book.begin();
 while(p != link_book.end())
 {
 if(strcmp(p->book_name,book_name) == 0)
 {
  cout<<"======================================="<<endl;
  cout<<" Book number :"<<p->id<<endl;
  cout<<" Title: :"<<p->book_name<<endl;
  if(p->state == 0)
  {
  cout<<" Status of books: not available !"<<endl;
  }
  else
  {
  cout<<" Status of books: borrowed !"<<endl;
  }
  cout<<" Where the bookshelf "<<p->place<<endl;
  flag1 = 1;
 }
 p++;
 }
 if(flag1 == 0)
 {
 cout<<" The book you borrowed does not exist, fail to borrow the book! ";
 return ;
 }
 p = link_book.begin();
 cout<<" The book has been found, please enter the ISbn for borrowing: ";
 int book_id;
 cin>>book_id;
 while(p != link_book.end())
 {
 if(strcmp(p->book_name,book_name) == 0 && p->id == book_id && p->state == 0)
 {
  strcpy(p->stu_number,id);
  strcpy(p->stu_name,name);
  p->state = 1;
  cout<<" Success in borrowing books! "<<endl;
  flag = 1;
  break;
 }
 p++;
 }
 if(flag == 0)
 {
 cout<<" The book has been borrowed and failed! ";
 }
 
}
// Return the book 
void Mybook::return_book()
{
 char stu_id[20];
 cout<<" Please enter your student number ";
 cin>>stu_id;
 int flag = 0;
 int flag1 = 0;
 list<Book>::iterator p = link_book.begin();
 while(p != link_book.end())
 {
 if(strcmp(p->stu_number,stu_id) == 0)
 {
  cout<<"==========================================="<<endl;
  cout<<" Call number: "<<p->id<<endl;
  cout<<" Title: "<<p->book_name<<endl;
  flag = 1;
 }
 p++;
 }
 if(flag == 0)
 {
 cout<<" No, you don't have any books "<<endl;
 return ;
 }
 else if(flag == 1)
 {
 cout<<" The books you borrowed have been inquired, please enter the isbn for return :"<<endl;
 }
 int id;
 cin>>id;
 p = link_book.begin();
 while(p != link_book.end())
 {
 if(strcmp(p->stu_number,stu_id) == 0&&p->id == id)
 {
  strcpy(p->stu_name ,"000");
  strcpy(p->stu_number , "0");
  p->state = 0;
  cout<<" Return book success! "<<endl;
  flag1 = 1;
  break;
 }
 p++;
 }
 if(flag1 == 0)
 {
 cout<<" Input isbn error, return the book failed! "<<endl;
 }
}
// User management menu 
int Mybook::menu_user()
{
 int choice = 0;
 cout<<"\t\t\t\t=========================================="<<endl;
 cout<<"\t\t\t\t*        User management           *"<<endl;
 cout<<"\t\t\t\t*----------------------------------------*"<<endl;
 cout<<"\t\t\t\t*     1 , add users           *"<<endl;
 cout<<"\t\t\t\t*     2 , display user           *"<<endl;
 cout<<"\t\t\t\t*     3 , delete users           *"<<endl;
 cout<<"\t\t\t\t*     4 , modify the user           *"<<endl;
 cout<<"\t\t\t\t*     5 , display book borrowing information       *"<<endl;
 cout<<"\t\t\t\t*     6 , save and return 1 layer       *"<<endl;
 cout<<"\t\t\t\t*----------------------------------------*"<<endl;
 cout<<"\t\t\t\t\t Please enter options :";
 cin>>choice;
 while(!(choice >= 1 && choice <= 6))
 {
 cout<<" Input error, please retype :";
 cin>>choice;
 }
 return choice;
 
}
// Perform user administration 
void Mybook::get_user()
{
 int choice = 0;
 while(1)
 {
 system("clear");
 choice = menu_user();
 system("clear");
 switch(choice)
 {
  case 1:
  {
  add_user();
  break;
  }
  case 2:
  {
  display_user();
  break;
  }
  case 3:
  {
  del_user();
  break;
  }
  case 4:
  {
  update_user();
  break;
  }
  case 5:
  {
  look_borrow();
  break;
  }
  case 6:
  {
  recv_user();
  return ;
  }
 }
 cout<<endl<<endl<<endl;
 cout<<" Press any key to return ...";
 getchar();
 getchar();
 }
}
// Add user 
void Mybook::add_user()
{
 User new_user;
 cout<<" Please enter your student number: ";
 cin>>new_user.stu_number;
 cout<<" Please enter your name: ";
 cin>>new_user.stu_name;
 
 link_user.push_back(new_user);
 cout<<" Added successfully! ";
 cout<<" Whether to continue to add (y/n)";
 char ch;
 cin>>ch;
 while(!(ch == 'Y'||ch == 'y'||ch == 'N'||ch == 'n'))
 {
 cout<<" Incorrect input, please re-enter: ";
 cin>>ch;
 }
 if(ch == 'Y'||ch == 'y')
 {
 system("clear");
 add_user(); 
 }
}
// According to the user 
void Mybook::display_user()
{
 list<User>::iterator it = link_user.begin();
 while(it != link_user.end())
 {
 cout<<"====================================="<<endl;
 cout<<" Student number: "<<it->stu_number<<endl;
 cout<<" Name: "<<it->stu_name<<endl<<endl;
 it++;
 }
}
// Delete user 
void Mybook::del_user()
{
 char id[20];
 cout<<" Please enter the student id you want to delete: ";
 cin>>id;
 int flag = 0;
 list<User>::iterator it = link_user.begin();
 while(it != link_user.end())
 {
 if(strcmp(it->stu_number,id) == 0)
 {
  link_user.erase(it);
  flag = 1;
  break;
 }
 it++;
 }
 if(flag == 1)
 {
 cout<<" Delete successful! "<<endl;
 }
 else
 {
 cout<<" You delete the user does not exist, delete failed! "<<endl;
 }
}
// Modify user information 
void Mybook::update_user()
{
 cout<<" Please enter the student number you want to modify: "<<endl;
 char number[20];
 cin>>number;
 int flag = 0;
 list<User>::iterator it = link_user.begin();
 while(it != link_user.end())
 {
 if(strcmp(it->stu_number,number) == 0)
 {
  cout<<" Please update your student id: ";
  cin>>it->stu_number;
  cout<<" Please update your name: ";
  cin>>it->stu_name;
  flag = 1;
  break;
 }
 it++;
 }
 if(flag == 1)
 {
 cout<<" Modified successfully! "<<endl;
 }
 else
 {
 cout<<" Modification failed! "<<endl;
 }
}
// Check out the books you have borrowed 
void Mybook::look_borrow()
{
 int flag = 0;
 list<Book>::iterator p = link_book.begin();
 while(p != link_book.end())
 {
 if(p->state == 1)
 {
  cout<<"==================================================="<<endl;
  cout<<" Name: "<<p->stu_name<<endl;
  cout<<" Student number: "<<p->stu_number<<endl;
  cout<<" Title: "<<p->book_name<<endl;
  cout<<" Shelf number: "<<p->place<<endl;
  flag = 1;
 }
 p++;
 }
 if(flag == 1)
 {
 cout<<" Has inquired the book related borrowing information "<<endl;
 }
 else
 {
 cout<<" No relevant books are available at present !"<<endl;
 }
}
// Library management menu 
int Mybook::menu()
{
 int choice = 0;
 cout<<"\t\t\t\t\t============================================"<<endl;
 cout<<"\t\t\t\t\t*        Books management       *"<<endl;
 cout<<"\t\t\t\t\t*------------------------------------------*"<<endl;
 cout<<"\t\t\t\t\t* 1 , add book information    2 , display book information    *"<<endl;
 cout<<"\t\t\t\t\t*------------------------------------------*"<<endl;
 cout<<"\t\t\t\t\t* 3 , delete book information    4 , search for book information    *"<<endl;
 cout<<"\t\t\t\t\t*------------------------------------------*"<<endl;
 cout<<"\t\t\t\t\t* 5 Update the book information    6 , return on 1 layer     *"<<endl;
 cout<<"\t\t\t\t\t*------------------------------------------*"<<endl;
 cout<<"\t\t\t\t\t Please enter options :";
 cin>>choice;
 while(!(choice >= 1 && choice <= 6))
 {
 cout<<" Input error, please retype :";
 cin>>choice;
 }
 return choice;
 
}
// Implement library management 
void Mybook::getchoice()
{
 int choice = 0;
 while(1)
 {
 system("clear");
 choice = menu();
 system("clear");
 switch(choice)
 {
  case 1:
  {
  add_book();
  break;
  }
  case 2:
  {
  display_book();
  break;
  }
  case 3:
  {
  del_book();
  break;
  }
  case 4:
  {
  search_book();
  break;
  }
  case 5:
  {
  update_book();
  break;
  }
  case 6:
  {
  return ;
  }
 }
 cout<<endl<<endl<<endl;
 cout<<" Press any key to return ....."<<endl;
 getchar();
 getchar();
 }
}
// Increase the book 
void Mybook::add_book()
{
 Book new_book;
 cout<<" Please enter the ISbn: ";
 cin>>new_book.id;
 cout<<" Please enter title: ";
 cin>>new_book.book_name;
 cout<<" Please enter the book shelf: ";
 cin>>new_book.place;
 
 link_book.push_back(new_book);
 cout<<" Added successfully! ";
 cout<<" Whether to continue to add (y/n)";
 char ch;
 cin>>ch;
 while(!(ch == 'Y'||ch == 'y'||ch == 'N'||ch == 'n'))
 {
 cout<<" Incorrect input, please re-enter: ";
 cin>>ch;
 }
 if(ch == 'Y'||ch == 'y')
 {
 system("clear");
 add_book(); 
 }
}
// Display book information 
void Mybook::display_book()
{
 list<Book>::iterator p = link_book.begin();
 while(p != link_book.end())
 {
 cout<<"======================================="<<endl;
 cout<<" Book number :"<<p->id<<endl;
 cout<<" Title: :"<<p->book_name<<endl;
 if(p->state == 0)
 {
  cout<<" Status of books: not available !"<<endl;
 }
 else
 {
  cout<<" Status of books: borrowed !"<<endl;
 }
 cout<<" Where the bookshelf "<<p->place<<endl<<endl;
 p++;
 }
}
// Delete book information 
void Mybook::del_book()
{
 list<Book>::iterator p = link_book.begin();
 int num = 0;
 int flag = 0;
 cout<<" Please enter the isbn you want to delete: ";
 cin>>num;
 while(p != link_book.end())
 {
 if(p->id == num)
 {
  link_book.erase(p);
  flag = 1;
  break;
 }
 p++;
 }
 if(flag == 1)
 {
 cout<<"\n\n Deleting completed !";
 }
 else
 {
 cout<<"\n\n The book does not exist, delete failed! "<<endl;
 }
}
// Search for book information 
void Mybook::search_book()
{
 list<Book>::iterator p = link_book.begin();
 char book_name[20];
 int flag = 0;
 cout<<" Please enter the title of the book you are looking for: ";
 cin>>book_name;
 while(p != link_book.end())
 {
 if(strcmp(p->book_name,book_name) == 0)
 {
  cout<<"======================================="<<endl;
  cout<<" Book number :"<<p->id<<endl;
  cout<<" Title: :"<<p->book_name<<endl;
  if(p->state == 0)
  {
  cout<<" Status of books: not available !"<<endl;
  }
  else
  {
  cout<<" Status of books: borrowed !"<<endl;
  }
  cout<<" Where the bookshelf "<<p->place<<endl;
  flag = 1;
 }
 p++;
 }
 if(flag == 1)
 {
 cout<<"\n\n Find the complete !";
 }
 else
 {
 cout<<"\n\n This book is not available in the library! "<<endl;
 }
}
// Modify book information 
void Mybook::update_book()
{
 list<Book>::iterator p = link_book.begin();
 int num = 0;
 int flag = 0;
 cout<<" Please enter the isbn you want to update: ";
 cin>>num;
 while(p != link_book.end())
 {
 if(p->id == num)
 {
  cout<<" Please enter the title of the book ";
  cin>>p->book_name;
  cout<<" Please enter the book shelf number :";
  cin>>p->place;
  flag = 1;
 }
 p++;
 }
 if(flag == 1)
 {
 cout<<"\n\n Update complete !";
 }
 else
 {
 cout<<"\n\n The book does not exist, update failed! "<<endl;
 }
}
int main()
{
  Mybook b;
 b.getmenu();
 
 return 0;
}

For more information, please pay attention to the topic management System Development.


Related articles: