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

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

posted @ 2019-03-18 14:34  何发奋  阅读(1113)  评论(0编辑  收藏  举报