20190318-使用類做一個簡單的圖書館管理系統


要求:使用類的形式做一個圖書館管理系統,實現借書,入庫,還書,查書等功能。

設計思路:

第一步:先寫一個書的類,來存儲圖書館中最重要的組成部分書的信息管理,包括書名,書作者,書的所屬類別,書的價格等

class Book():
    '''書類,存儲書名,作者,分類,價格等信息'''
    def __init__(self,name,author,classify,price):
        self.name = name
        self.author = author
        self.classify = classify
        self.price = price
    def get_bookinfo(self):
        print('書名:%s\n作者:%s\n分類:%s\n價格:%s'%(self.name,self.author,self.classify,self.price))
    def set_book_name(self,new_name):
        self.name = new_name
    def get_book_name(self):#獲取書名
        return self.name
    def get_book_author(self):#獲取書作者
        return self.author
    def get_book_classify(self):#獲取書分類
        return self.classfiy
    def get_book_price(self):#獲取書的價格
        return self.price

第二步:因為單獨一個類管理書籍信息,因此現在寫一個圖書館主體類,寫3個類變量,分別存儲圖書館所有的圖書(list)、尚未被借出的圖書(list)、已被借出的圖書(dict),並且使用序列化來存儲信息編寫算法如下:

class Library():
    '''圖書館,實現借書,入庫,還書,查書等功能,使用序列化存儲書的實例'''
    import pickle as p
    with open(r'C:\Users\何發奮\Desktop\Python 習題\instance\book_library.txt','rb') as f:#序列化信息存儲地址
        try:
            book_total_library = p.load(f)#存儲所有的書
            book_in_library =p.load(f)#存儲未被借出的書
            out_library = p.load(f)#存儲被借出的書的信息,包括借書人以及借書日期
        except:
            book_total_library = []
            book_in_library =[]
            out_library = {}
    def __init__(self,name,location):
        '''初始化圖書館'''
        self.name = name
        self.location = location

第三步:寫借書,入庫,還書,查書等功能

3.1 入庫

def add():
        '''加書'''
        import pickle as p
        book_name = input('請輸入書名:')
        author_name = input('請輸入author:')
        classify = input('請輸入classify:')
        price = input('請輸入price:')
        book = Book(book_name,author_name,classify,price)
        Library.book_total_library.append(book)
        Library.book_in_library.append(book)
        #將書籍加入本館總庫以及在庫圖書列表
        with open(r'C:\Users\何發奮\Desktop\Python 習題\instance\book_library.txt','wb') as fp:
            p.dump(Library.book_total_library,fp)
            p.dump(Library.book_in_library,fp)
            p.dump(Library.out_library,fp)
        #將修改的數據記錄入序列化

3.2借書,借書的邏輯是

  step1:遍歷本館書庫,查看是否有該書籍

  step2:遍歷本館可借書單,查看書籍是否已被借出

  step3:如果本館有此書,並且未被借出,則出借該書,記錄出借記錄,否則提示本館無此書,或者此書已經被出借

def borrow(self,username,book_name):
        '''借書'''
        import pickle as p
        import time
        current_date = time.strftime('%Y-%m-%d',time.localtime())
        #print(Library.book_in_library)
        #print(Library.out_library)
        for book_object1 in Library.book_total_library:
            if book_object1.get_book_name() == book_name:
                for book_object2 in Library.book_in_library:
                    #print(book_object2)
                    #print(book_object2.get_book_name())
                    if book_object2.get_book_name() ==book_name:
                        print('object2:',book_object2)
                        #查找圖書在本館,且未被借出
                        Library.out_library[book_object2] = (username,current_date)
                        #記錄圖書已經被借出,登記出借信息
                        Library.book_in_library.remove(book_object2)
                        #print(Library.book_in_library)
                        #print(Library.out_library)
                        #修改在庫圖書記錄,記錄當前圖書已被借出,不在庫
                        print('借書成功,請及時歸還')
                        with open(r'C:\Users\何發奮\Desktop\Python 習題\instance\book_library.txt','wb') as fp:
                            p.dump(Library.book_total_library,fp)
                            p.dump(Library.book_in_library,fp)
                            p.dump(Library.out_library,fp)
                            #序列化記錄修改記錄
                        print()
                        return
                else:#本館有此書,但是已被借走
                    print('您要的書已被借走,您可通過book_info查看已被借閱的信息')
                    print()
                    return
                #如果圖書在本館,但是已被借出,提示已被借走
        else:
            print('您所要借閱的書本館尚無,請去其他圖書館看看')
            print()
            #如果本館無此圖書,提示

3.3還書,還書的邏輯為先查看出借記錄,查看是否有對應的出借記錄,如果有則收回書籍,修改在庫記錄,否則提示報錯

def lend(self,book_name):
        '''還書'''
        import pickle as p
        for book_object in Library.out_library.keys():
            print(book_object.get_book_name())
            if book_name ==book_object.get_book_name():
                Library.book_in_library.append(book_object)
                del Library.out_library[book_object]#刪除出借記錄
                with open(r'C:\Users\何發奮\Desktop\Python 習題\instance\book_library.txt','wb') as fp:
                    p.dump(Library.book_total_library,fp)
                    p.dump(Library.book_in_library,fp)
                    p.dump(Library.out_library,fp)
                print('還書成功!')
                print()
                return
                #如果查到對應的借出記錄,則記錄歸還記錄
        else:
            print('未查到借閱記錄,請確認是否是在本館借的書')
            print()
            #如果未查到對應的借出記錄不做操作

3.4查詢功能,查書功能分3種,查詢本館所有的圖書;查詢本館在庫圖書;查詢本館已被出借圖書

def info_all(self):#所有書籍
        import pickle as p
        if Library.book_total_library:
            print('*'*80)
            for item in Library.book_total_library:
                item.get_bookinfo()
                print('-'*80)
            print(' '*80)
    def info_ready(self):#在庫書籍
        if Library.book_in_library:
            print('-'*80)
            for item in Library.book_in_library:
                item.get_bookinfo()
                print('-'*80)
            print(' '*80)
        else:
            print('當前無可借書籍')
            print(' '*80)
    def info_borrow(self):#已被出借書籍
        if Library.out_library:
            print('*'*80)
            for item in Library.out_library:
                item.get_bookinfo()
                print('-'*80)
            print(' '*80)
        else:
            print('無被借閱記錄')
            print(' '*80)

最后寫程序的主體,也就是操作命令如下:

if __name__ == '__main__':
    #import operator
    while 1:
        command = input('歡迎光臨小何圖書館請輸入命令:\nadd:增加圖書\nborrow:借閱圖書\nlend:還書\ninfo_all:查看庫中圖書列表\ninfo_ready:查看在庫圖書列表\ninfo_borrow:查看借出圖書列表\nsearch:查找圖書\nexit:退出\n')
        if command.strip().lower() == 'add':
            exec('xh_library.add()')
        elif command.strip().lower() == 'borrow':
            user_name = input('請輸入用戶名:')
            book_name = input('請輸入書名:')
            temp = 'xh_library.borrow(%s,%s)'%(user_name,book_name)
            print(temp)
            exec('xh_library.borrow("%s","%s")'%(user_name,book_name))
        elif command.strip().lower() == 'lend':
            book_input = input('請輸入要還的書名:')
            exec('xh_library.lend("%s")'% book_input)
        elif command.strip().lower() == 'info_all':
            exec('xh_library.info_all()')
        elif command.strip().lower() == 'info_ready':
            exec('xh_library.info_ready()')
        elif command.strip().lower() == 'info_borrow':
            #print('catch')
            exec('xh_library.info_borrow()')
        elif command.strip().lower() == 'exit':
            break

尚未完全完善,待后續更新!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM