目录
AddBook + DeleteBook 添加书籍 删除书籍 这是管理员的功能
Register + Logout 注册 注销 用于借阅人的操作
此管理系统相比于上一篇用Java实现的功能更多了,结构更加简单了,没有用继承之类的复杂语法。逻辑实现上更加严谨了,但是并没有异常处理,以及文件读取等持久化信息的操作,这些操作需要后续的学习。
功能概览
Library.h
BookRecord
#include<vector>
#include<cassert>
#include<string>
#include<iostream>
#include<map>
#include<set>
using namespace std;
// 图书类
class BookRecord
{
public:
BookRecord(string s1, string s2, string s3, string s4, unsigned int n1, unsigned int n2) :
book_title_(s1), book_isbn_(s2), author_(s3), year_published_(s4), number_of_copies_(n1), number_of_copies_available_(n2) {}
~BookRecord() = default;
void DisplayRecord()const; // 打印
bool loan_out(); // 书籍借出函数
void AddBook(int n) { number_of_copies_ += n; number_of_copies_available_ += n; } // 增添书籍
bool is_available()const { return number_of_copies_available_ > 0; } // 判断是否有余量。
bool deletebook()const { return number_of_copies_ == number_of_copies_available_; } // 删除书籍
void ReturnBook() { ++number_of_copies_available_; } // 归还书籍
const string& isbn()const { return book_isbn_; } // 返回ISBN
const string& name()const { return book_title_; } // 返回名字
private:
string book_title_;
string book_isbn_;
string author_;
string year_published_;
unsigned int number_of_copies_;
unsigned int number_of_copies_available_;
};
存储图书的基本信息,以及一些对外接口。
Borrower
// 借阅人类
class Borrower
{
public:
Borrower(string id, string name, string tele)
:borrower_id_(id), name_(name), telephone_(tele){}
~Borrower() = default;
const string& id()const { return borrower_id_; }
const string& name()const { return name_; }
void DisplayRecord()const; // 打印函数
void LoanBook(const string& s); // 借书
bool ReturnBook(const string& s); // 还书
bool Logout()const { return book_loaned_ == 0; } // 注销
private:
string borrower_id_;
string name_;
string telephone_;
unsigned int book_loaned_ = 0;
vector<string> book_loaned_titles_ = vector<string>();
};
借阅人类,存储借阅人的基本信息,其中包括这个人借了哪些书,vector存储那些书籍的书名。
Library
// 图书馆类
class Library
{
public:
Library() = default;
~Library()
{
for (const auto& i : vec_pbook_)
delete i;
for (const auto& i : vec_pborrower_)
delete i;
}
Library(const Library&) = delete;
Library& operator=(const Library&) = delete;
void DisplayAllBook()const; // 打印全部书籍
void DisplayAllBorrower()const; // 打印全部借阅人
void DisplayBook()const; // 打印特定书籍
void DisplayBorrower()const; // 打印特定借阅人
void AddBook(); // 添加书籍
void DeleteBook(); // 删除书籍
void LoanBook(); // 借书
void ReturnBook(); // 还书
void Register(); // 注册
void Logout(); // 注销
private:
vector<Borrower*> vec_pborrower_;
vector<BookRecord*> vec_pbook_;
map<string, Borrower*> map_borrower_; // 书籍的ISBN映射到书籍对象指针
map<string, BookRe