library management system simulation(first)

来源:互联网 发布:1password mac 下载 编辑:程序博客网 时间:2024/06/05 02:15
//**************************************************************//                     customer_exception.h //  ************************************************************#ifndef CUSTOMER_EXCEPTION_H_#define CUSTOMER_EXCEPTION_H_#include<stdexcept>#include<string>class customerException :public std::logic_error{public:customerException(const std::string &message=""):logic_error(message.c_str()){}};#endif


 

//***********************************************************//                      book_exception.h//***********************************************************               #ifndef BOOK_EXCEPTION_H_#define BOOK_EXCEPTION_H_#include<stdexcept>#include<string>class bookException:public std::logic_error{public:bookException(const std::string &message=""):logic_error(message.c_str()){}};#endif


 

//********************************************************//        book_item.h implemention file---book_item.cpp//********************************************************        #ifndef BOOK_ITEM_H_#define BOOK_ITEM_H_#include<string>#include<iostream>#include<fstream>using namespace std;class bookItem{private:string title;string author;string ISBN;string publisher;string category;string custID;float  price;int    pages;//construtor functionpublic:bookItem(const bookItem&_item);bookItem();void createBookItem();string getTitle()const;string getAuthor()const;string getISBN()const;string getPublisher()const;string getCategory()const;string getCustID()const;bool   getStatus()const;void   setCustID(const string &_custID);        bookItem & getPtr();                void display()const;friend ifstream & operator>>(ifstream &fin,bookItem &_item);friend ofstream & operator<<(ofstream &fout,const bookItem &_item);};#endif


 

//*****************************************************************//   book_item.cpp     book's information//*****************************************************************   #include"book_item.h"#include<cstddef>using namespace std;bookItem::bookItem(const bookItem &_item){title=_item.title;author=_item.author;ISBN=_item.ISBN;publisher=_item.publisher;category=_item.category;custID=_item.custID;price=_item.price;pages=_item.pages;}bookItem::bookItem(){title="";author="";ISBN="";publisher="";category="";custID="";price=0.0;pages=0;}void bookItem::createBookItem(){bool isok=false;char ch;while(!isok){cout<<"\t\t\t    create book item !"<<endl<<endl<<endl;cout<<"   \t\t\tenter the book's title :";getline(cin,title);cout<<"\n   \t\t\tenter the book's author :";getline(cin,author);cout<<"\n   \t\t\tenter the book's ISBN :";getline(cin,ISBN);cout<<"\n    \t\t\tenter the book's publisher :";getline(cin,publisher);cout<<"\n    \t\t\tenter the book's category :";getline(cin,category);cout<<"\n    \t\t\tenter the book's price :";cin>>price;while(cin.get()!='\n');cout<<"\n    \t\t\tenter the book's pages :";cin>>pages;while(cin.get()!='\n');cout<<endl<<endl<<endl;cout<<"    \t\t\tbook information is ok !"<<endl;cout<<"   \t\t\tS for Save And R for Reset :";cin>>ch;while(cin.get()!='\n');if(ch=='S'||ch=='s')isok=true;}}string bookItem::getTitle()const{return title;}string bookItem::getAuthor()const{return author;}string bookItem::getISBN()const{return ISBN;}string bookItem::getPublisher()const{return publisher;}string bookItem::getCategory()const{return category;}string bookItem::getCustID()const{return custID;}bool bookItem::getStatus()const{if(custID.empty())return false;elsereturn true;}void bookItem::setCustID(const string &_custID){custID=_custID;}bookItem  & bookItem::getPtr(){return *this;}void bookItem::display()const{ cout<<"********************************************************************************"<<endl<<endl;cout<<"**\t\tbook's title :    "<<title<<endl;cout<<"**\t\tbook's author :    "<<author<<endl;cout<<"**\t\tbook's ISBN :    "<<ISBN<<endl;cout<<"**\t\tbook's publisher :    "<<publisher<<endl;cout<<"**\t\tbook's category :    "<<category<<endl;cout<<"**\t\tbook's price :    $ "<<price<<endl;cout<<"**\t\tbook's pages :    "<<pages<<endl;if(getStatus())cout<<"\t\t\t\tthis book has been borrowed !"<<endl;elsecout<<"\t\t\t\tthis book is still in lirary !"<<endl;cout<<"\n********************************************************************************"<<endl<<endl;}ifstream &operator>>(ifstream &fin,bookItem &_item){if(fin.peek()=='\n')fin.ignore(1);getline(fin,_item.title,'~');getline(fin,_item.author,'~');getline(fin,_item.ISBN,'~');        getline(fin,_item.publisher,'~');getline(fin,_item.category,'~');        if(fin.peek()=='~'){fin.ignore(1);_item.custID="";}elsegetline(fin,_item.custID,'~');fin>>_item.price>>_item.pages;return fin;}ofstream &operator<<(ofstream &fout,const bookItem &_item){fout<<_item.title<<'~'<<_item.author<<'~'<<_item.ISBN<<'~'<<_item.publisher<<'~'<<_item.category<<'~'<<_item.custID<<'~'<<_item.price<<' '<<_item.pages<<endl;return fout;}


 

//************************************************************//       customer_item.h implemention file--customer_item.cpp//************************************************************#ifndef CUSTOMER_ITEM_H_#define CUSTOMER_ITEM_H_#include<string>#include<iostream>#include<fstream>#include"book_tree.h"#include"book_item.h"using namespace std;class customerItem{private:static const int MAX=5;string name;string ID;string unit;string password;int    numb;bookItem *books[MAX];public:customerItem();        customerItem(const customerItem &_item);~customerItem();void createCustomerItem();customerItem &operator=(const customerItem &_item);bookItem*& operator[](int index);void setPassword(const string &_str);string getPassword()const;string getID()const;void display()const;//attentionvoid borrow(bookItem &_item);void revert(const string &_ISBN);ifstream& readCitem(ifstream &fin,bookTree &btree);friend ofstream &operator<<(ofstream &fout, customerItem &_item);};#endif


 

//***************************************************************//            customer_item.cpp  customer information //**************************************************************            #include"customer_item.h"#include"book_item.h"customerItem::customerItem(){name="";ID="";unit="";password="";numb=0;for(int i=0;i<MAX;i++){books[i]=NULL;}}customerItem::customerItem(const customerItem &_item){name=_item.name;ID=_item.ID;unit=_item.unit;        password=_item.password;numb=_item.numb;for(int i=0;i<numb;i++)books[i]=_item.books[i];}customerItem::~customerItem(){}void customerItem::createCustomerItem(){bool isok=false;char ch;while(!isok){cout<<"\t\t\tcreate new customer information !"<<endl<<endl<<endl;cout<<"\t\t\tenter the customer's name :";getline(cin,name);cout<<"\n\t\t\tenter the customer's ID :";getline(cin,ID);cout<<"\n\t\t\tenter the customer's unit :";getline(cin,unit);cout<<"\a\n\n\t\t\t   customer information is ok  "<<endl<<endl;cout<<"\t\t          S for Save or R for Reset :";cin>>ch;while(cin.get()!='\n');        if(ch=='s'||ch=='S')isok=true;}}customerItem &customerItem::operator=(const customerItem &_item){if(this == &_item)return *this;else{name=_item.name;             ID=_item.ID;        unit=_item.unit;                password=_item.password;         numb=_item.numb;        for(int i=0;i<numb;i++)      books[i]=_item.books[i];}return *this;}void customerItem::setPassword(const string &_str){password=_str;}string customerItem::getPassword()const{return password;}string customerItem::getID()const{return ID;}void customerItem::display()const{cout<<"********************************************************************************"<<endl<<endl;cout<<"**\t\tthe customer's name :"<<name<<endl;cout<<"**\t\tthe customer's ID :"<<ID<<endl;cout<<"**\t\tthe customer's unit :"<<unit<<endl<<endl;if(numb==0)cout<<"\t\t\t    you has borrowed 0 book !"<<endl;else{cout<<"\t\t\t    you has borrow "<<numb<<" books ."<<endl;cout<<"\t\t\t\t  AS follows :"<<endl;for(int i=0;i<numb;i++)books[i]->display();}}void customerItem::borrow(bookItem &_item){if(numb>=MAX){cout<<"\a\a\a\t\t      more than "<<MAX<<" books you have borrowed !\n";cout<<"\t\t\t      borrowing is canceled "<<endl<<endl;}else{_item.setCustID(ID);books[numb]=&(_item.getPtr());numb++;cout<<"\t\t\t      borrowing success !"<<endl<<endl;}}void customerItem::revert(const string &_ISBN){      int index=0;while(books[index]->getISBN()!=_ISBN && index<numb){index++;}if(books[index]->getISBN()!=_ISBN)cout<<"\t\t\t you didnot borrow this book !"<<endl<<endl;else{int i;string tem="";                books[index]->setCustID(tem);for(i=index;i<numb-1;i++){books[i]=books[i+1];}books[i]=NULL;numb--;cout<<"\t\t\t     revert book success !"<<endl<<endl;}}ofstream &operator<<(ofstream &fout, customerItem &_item){fout<<_item.name<<'~'<<_item.ID<<'~'<<_item.unit<<'~'<<_item.password<<'~'<<_item.numb<<'~';for(int i=0;i<_item.numb;i++){fout<<_item[i]->getISBN()<<'~';}fout<<endl;return fout;}ifstream& customerItem::readCitem(ifstream &fin, bookTree &_tree){getline(fin,name,'~');getline(fin,ID,'~');getline(fin,unit,'~');getline(fin,password,'~');fin>>numb;        if(fin.peek()=='~')fin.ignore(1);for(int i=0;i<numb;i++){       string temp;getline(fin,temp,'~');books[i]=&(_tree.search(temp).getPtr());}while(fin.get()!='\n');return fin;}bookItem *&customerItem::operator[](int index){return books[index];}


 

//************************************************************//      custoemr_node.h implemention file---customer_node.cpp//************************************************************              #ifndef CUSTOMER_NODE_H_#define CUSTOEMR_NODE_H_#include"customer_item.h"class customerNode{private:customerItem item;customerNode *leftchild;customerNode *rightchild;customerNode();customerNode(const customerItem &_item,customerNode *leftPtr=NULL,customerNode *rightPtr=NULL);~customerNode();friend class customerTree;};#endif


 

//*****************************************************************//              customer_node.cpp binary's customer node//*****************************************************************       #include"customer_node.h"customerNode::customerNode(){item=customerItem();leftchild=rightchild=NULL;}customerNode::customerNode(const customerItem &_item,customerNode *leftPtr,customerNode *rightPtr):item(_item),leftchild(leftPtr),rightchild(rightPtr){}customerNode::~customerNode(){}


 

//*****************************************************************//          book_node.h implemention file---book_node.cpp//*****************************************************************       #ifndef BOOK_NODE_H_#define BOOK_NODE_H_#include"book_item.h"class bookNode{private:bookItem item;bookNode *leftchild;bookNode *rightchild;        explicit bookNode();bookNode(const bookItem &_item,bookNode *leftPtr=NULL,bookNode *rightPtr=NULL);~bookNode();friend class bookTree;};#endif

 

 

//****************************************************************//   book_node.cpp  binary's book node//****************************************************************   #include"book_node.h"bookNode::bookNode(){item=bookItem();leftchild=NULL;rightchild=NULL;}bookNode::bookNode(const bookItem &_item,bookNode *leftPtr,bookNode *rightPtr):item(_item),leftchild(leftPtr),rightchild(rightPtr){}bookNode::~bookNode(){}

 

//**********************************************************//             book_tree.h implemention file---book_tree.cpp//**********************************************************#ifndef BOOK_TREE_H_#define BOOK_TREE_H_#include<fstream>#include<iostream>#include<cstddef>#include<string>#include"book_node.h"#include"book_exception.h"using namespace std;typedef void (*Function)(const bookItem &_item);class bookTree{private:bookNode *root;int amount;public:bookTree();bookTree(const bookItem &_item);~bookTree();bool isEmpty()const;int getAmount()const{return amount;}void insert(const bookItem &newitem)throw(bookException);                void remove(const string &_ISBN)throw(bookException);void search(const string &_key,char & _type)const;bookItem & search(const string &_ISBN)throw(bookException);void traverse(Function  visit);void write(ofstream &fout);void read(ifstream &fin,int n);protected:void insertItem(bookNode*& treePtr,const bookItem &newItem)throw(bookException);void removeItem(bookNode *& treePtr,const string &_ISBN)throw(bookException);void removeNode(bookNode*& treePtr);void processLeft(bookNode*& treePtr,bookItem &_item);void destory(bookNode *  &_treePtr);void retrieveInorde(bookNode *const & treePtr,const string &_key,char &_type)const;bookItem & retrieveTree(bookNode * & treePtr,const string &_ISBN)throw(bookException);        void traverseItem(bookNode * & treePtr,Function  visit);void  writeItem(bookNode * & treePtr,ofstream &fout);void readItem(bookNode *& treePtr,ifstream &fin,int n);};#endif

 

 

//**************************************************************//          book_tree.cpp  search binary tree storing book items     
//**************************************************************          #include<cassert>#include<cstdlib>#include<cstddef>#include"book_tree.h"bookTree::bookTree(){root=NULL;amount=0;}bookTree::bookTree(const bookItem &_item){root=new bookNode(_item,NULL,NULL);assert(root!=NULL);amount=1;}bookTree::~bookTree(){destory(root);}bool bookTree::isEmpty()const{return (root==NULL);}void bookTree::insert(const bookItem &newitem)throw(bookException){try{insertItem(root,newitem);}catch(bookException &bexvept){throw;}}void bookTree::remove(const string &_ISBN)throw(bookException){try{removeItem(root,_ISBN);}catch(bookException &bexcept){throw;}}void bookTree::search(const string &_key,char & _type)const{retrieveInorde(root,_key,_type);} bookItem & bookTree::search(const string &_ISBN)throw(bookException){try{return(retrieveTree(root,_ISBN));}catch(bookException &cexcept){throw;}}void bookTree::traverse(Function visit){traverseItem(root,visit);}void bookTree::write(ofstream &fout){             writeItem(root,fout);}void bookTree::read(ifstream &fin,int n){ readItem(root,fin,n);}void bookTree::insertItem(bookNode *&treePtr,const bookItem &newitem)                     throw(bookException){if(treePtr==NULL){treePtr=new bookNode(newitem,NULL,NULL);if(treePtr==NULL)throw bookException("bookException :can't allocate memory in insert function !\a\a\a\a\a");amount++;}else if((treePtr->item.getISBN())>newitem.getISBN())insertItem(treePtr->leftchild,newitem);elseinsertItem(treePtr->rightchild,newitem);}void bookTree::removeItem(bookNode *& treePtr,const string &_ISBN)throw(bookException){if(treePtr==NULL)throw bookException("bookException :delete the key book failed in remove function !\a\a\a\a\a");else if(treePtr->item.getISBN()>_ISBN)removeItem(treePtr->leftchild,_ISBN);else if(treePtr->item.getISBN()==_ISBN){removeNode(treePtr);amount--;}elseremoveItem(treePtr->rightchild,_ISBN);}void bookTree::removeNode(bookNode *& treePtr){bookNode *delPtr;bookItem tempItem;       if(treePtr->leftchild==NULL&&treePtr->rightchild==NULL)       {       delete treePtr;       treePtr=NULL;       }       else if(treePtr->leftchild==NULL)       {       delPtr=treePtr;       treePtr=treePtr->rightchild;       delPtr->rightchild=NULL;       delete delPtr;       }       else if(treePtr->rightchild==NULL)       {       delPtr=treePtr;       treePtr=treePtr->leftchild;       delPtr->leftchild=NULL;       delete delPtr;       }       else       {       processLeft(treePtr->rightchild,tempItem);       treePtr->item=tempItem;       }}void bookTree::processLeft(bookNode *&treePtr,bookItem &_item){if(treePtr->leftchild==NULL){_item=treePtr->item;bookNode *tempPtr=treePtr;treePtr=treePtr->rightchild;tempPtr->rightchild=NULL;delete tempPtr;}elseprocessLeft(treePtr->leftchild,_item);}void bookTree::destory(bookNode *&_treePtr){if(_treePtr!=NULL){         destory(_treePtr->leftchild);         destory(_treePtr->rightchild);         delete _treePtr;_treePtr=NULL;amount--;}}void bookTree::retrieveInorde(bookNode *const & treePtr,const string &_key,char & _type)const{        if(treePtr!=NULL){if(_type=='T'||_type=='t'){       retrieveInorde(treePtr->leftchild,_key,_type);       if(treePtr->item.getTitle()==_key)       {       treePtr->item.display();       }       retrieveInorde(treePtr->rightchild,_key,_type);}else if(_type=='A'||_type=='a'){retrieveInorde(treePtr->leftchild,_key,_type);if(treePtr->item.getAuthor()==_key){treePtr->item.display();}retrieveInorde(treePtr->rightchild,_key,_type);}else if(_type=='I'||_type=='i'){retrieveInorde(treePtr->leftchild,_key,_type);if(treePtr->item.getISBN()==_key){treePtr->item.display();}retrieveInorde(treePtr->rightchild,_key,_type);}else if(_type=='P'||_type=='p'){retrieveInorde(treePtr->leftchild,_key,_type);if(treePtr->item.getPublisher()==_key){treePtr->item.display();}retrieveInorde(treePtr->rightchild,_key,_type);}else if(_type=='C'||_type=='c'){retrieveInorde(treePtr->leftchild,_key,_type);if(treePtr->item.getCategory()==_key){treePtr->item.display();}retrieveInorde(treePtr->rightchild,_key,_type);}}}bookItem &bookTree::retrieveTree(bookNode *&treePtr,const string &_ISBN)throw(bookException){if(treePtr==NULL)throw bookException("bookException :seek the key book failed in search function !\a\a\a\a\a");else if(treePtr->item.getISBN()==_ISBN)return treePtr->item;else if(treePtr->item.getISBN()>_ISBN)retrieveTree(treePtr->leftchild,_ISBN);elseretrieveTree(treePtr->rightchild,_ISBN);}void bookTree::traverseItem(bookNode *&treePtr,Function visit){if(treePtr!=NULL){traverseItem(treePtr->leftchild,visit);visit(treePtr->item);traverseItem(treePtr->rightchild,visit);}}void bookTree::writeItem(bookNode *&treePtr,ofstream &fout){if(treePtr!=NULL){writeItem(treePtr->leftchild,fout);fout<<treePtr->item;writeItem(treePtr->rightchild,fout);}}void bookTree::readItem(bookNode *&treePtr ,ifstream &fin,int n){if(n>0){treePtr=new bookNode();readItem(treePtr->leftchild,fin,n/2);fin>>treePtr->item;amount++;readItem(treePtr->rightchild,fin,(n-1)/2);}}


 

//****************************************************************//         customer_tree.h   implemention file---customer_tree.cpp    
//*******************************************************************#ifndef CUSTOMER_TREE_H_#define CUSTOMER_TREE_H_#include<string>#include<fstream>#include<iostream>#include<cstddef>#include"customer_node.h"#include"customer_exception.h"using namespace std;typedef void (*FunctionType)(const customerItem &_item);class customerTree{private:customerNode *root;int amount;public:customerTree();customerTree(const customerItem &_item);~customerTree();int getAmount()const{return amount;}bool isEmpty()const;void insert(const customerItem &newitem)throw(customerException);void remove(const string &_ID)throw(customerException);customerItem & search(const string &_ID)throw(customerException);                void traverse(FunctionType  visit);void write(ofstream &fout);void read(ifstream &fin,int n, bookTree &_tree);protected:void insertItem(customerNode *&treePtr,const customerItem &newitem)throw(customerException);void removeItem(customerNode *&treePtr,const string &_ID)throw(customerException);void removeNode(customerNode *&treePtr);void processLeft(customerNode *&treePtr,customerItem &_item);void destory(customerNode *&treePtr);customerItem & retrieveTree(customerNode *&treePtr,const string &_ID)throw(customerException);void traverseItem(customerNode *&treePtr,FunctionType  visit);void writeItem(customerNode *&treePtr,ofstream &fout);void readItem(customerNode *&treePtr,ifstream &fin,int n,bookTree &_tree);};#endif

//************************************************************************//            customer_tree.cpp  search binary tree storing customer items//************************************************************************#include<cassert>#include<cstddef>#include"customer_tree.h"#include"book_tree.h"customerTree::customerTree(){root=NULL;amount=0;}customerTree::customerTree(const customerItem &_item){root=new customerNode(_item,NULL,NULL);assert(root!=NULL);amount=1;}customerTree::~customerTree(){destory(root);}bool customerTree::isEmpty()const{return (root==NULL);}void customerTree::insert(const customerItem &newitem)throw(customerException){try{insertItem(root,newitem);}catch(customerException &cExcept){throw;}}void customerTree::remove(const string &_ID)throw(customerException){try{removeItem(root,_ID);}catch(customerException &cexcept){throw;}}customerItem &customerTree::search(const string &_ID)throw(customerException){try{return retrieveTree(root,_ID);}catch(customerException &cexcept){throw;}}void customerTree::traverse(FunctionType visit){traverseItem(root,visit);}void customerTree::read(ifstream &fin,int n,bookTree &_tree){ readItem(root,fin,n,_tree);}void customerTree::write(ofstream &fout){ writeItem(root,fout);}void customerTree::writeItem(customerNode *&treePtr,ofstream &fout){if(treePtr!=NULL){writeItem(treePtr->leftchild,fout);fout<<treePtr->item;writeItem(treePtr->rightchild,fout);}}void customerTree::readItem(customerNode *& treePtr,ifstream &fin,int n,bookTree &_tree){if(n>0){treePtr=new customerNode();readItem(treePtr->leftchild,fin,n/2,_tree);treePtr->item.readCitem(fin,_tree);amount++;readItem(treePtr->rightchild,fin,(n-1)/2,_tree);}}void customerTree::insertItem(customerNode *&treePtr,const customerItem &newitem)throw(customerException){if(treePtr==NULL){treePtr= new customerNode(newitem,NULL,NULL);if(treePtr==NULL)throw customerException("customerException :allocate memory failed in insert function !\a\a\a\a\a");amount++;}else if(treePtr->item.getID()>newitem.getID())insertItem(treePtr->leftchild,newitem);elseinsertItem(treePtr->rightchild,newitem);}void customerTree::removeItem(customerNode *&treePtr,const string &_ID)throw(customerException){if(treePtr==NULL)throw customerException("customerException :delete failed in remove function !\a\a\a\a\a");else if(treePtr->item.getID()>_ID)removeItem(treePtr->leftchild,_ID);else if(treePtr->item.getID()==_ID){removeNode(treePtr);amount--;}elseremoveItem(treePtr->rightchild,_ID);}void customerTree::removeNode(customerNode *&treePtr){customerNode *delPtr;customerItem tempItem;if(treePtr->leftchild==NULL &&treePtr->rightchild==NULL){delete treePtr;treePtr=NULL;}else if(treePtr->leftchild==NULL){delPtr=treePtr;treePtr=treePtr->rightchild;delPtr->rightchild=NULL;delete delPtr;}else if(treePtr->rightchild==NULL){delPtr=treePtr;treePtr=treePtr->leftchild;delPtr->leftchild=NULL;delete delPtr;}else{processLeft(treePtr->rightchild,tempItem);treePtr->item=tempItem;}}void customerTree::processLeft(customerNode *&treePtr,customerItem &_item){if(treePtr->leftchild==NULL){_item=treePtr->item;customerNode *delPtr=treePtr;treePtr=treePtr->rightchild;delPtr->rightchild=NULL;delete delPtr;}elseprocessLeft(treePtr->leftchild,_item);}void customerTree::destory(customerNode *&treePtr){if(treePtr!=NULL){destory(treePtr->leftchild);destory(treePtr->rightchild);delete treePtr;treePtr=NULL;}}customerItem &customerTree::retrieveTree(customerNode *&treePtr,const string &_ID)throw(customerException){if(treePtr==NULL)throw customerException("customerException :seek the key customer failed in search function !\a\a\a\a\a");else if(treePtr->item.getID()>_ID)retrieveTree(treePtr->leftchild,_ID);else if(treePtr->item.getID()==_ID)return treePtr->item;elseretrieveTree(treePtr->rightchild,_ID);}void customerTree::traverseItem(customerNode *&treePtr,FunctionType visit){if(treePtr!=NULL){traverseItem(treePtr->leftchild,visit);visit(treePtr->item);      traverseItem(treePtr->rightchild,visit);}}


 

	
				
		
原创粉丝点击