写了一个 图书馆 程序,可序列化,实现图书的增、删、借、还

写了一个 图书馆 程序,可序列化,实现图书的增、删、借、还:

#coding=gbk
'''
1、重新写了一个图书馆程序
2、Library 图书馆 Book书 CommandList命令
'''
import pickle as p
import os
import traceback
from datetime import datetime

#图书馆类,管理图书的增、查、借阅、归还
class Library():
    
    def __init__(self,name,location):
        self.__name=name
        self.__location=location
        self.__booklist=[]
        self.__borrowlist=[]
    
    def add_book(self,book):
        self.__booklist.append(book)
        print('%s图书新增成功'%book.get_bookname())
    
    def query_book(self,book_name):
        for i in self.__booklist:
            if i.get_bookname()==book_name:
                print('图书 %s 找到了。'%i.get_bookname())
                return i.get_bookinfo()
        else:
            print('%s图书不存在!'%book_name)
            return None

    def borrow_book(self,book_name):
        for i in self.__booklist:
            if i.get_bookname()==book_name:
                i.update_borrow_returntime(datetime.now(),1)
                self.__borrowlist.append(i)
                print('图书%s 借阅成功,借阅时间:%s'%(book_name,i.get_bookinfo()[3]))
                self.__booklist.remove(i)
                
                break
        else:
            print('未找到该图书%s!'%book_name)

    def return_book(self,book_name):
        for i in self.__borrowlist:
            if i.get_bookname()==book_name:
                i.update_borrow_returntime(datetime.now(),0)
                self.__booklist.append(i)
                print('图书%s 借阅归还成功!借阅时长共计:%s天%s秒)'%(book_name,i.get_period()[0],i.get_period()[1]))
                self.__borrowlist.remove(i)
                
                break
        else:
            print('%s图书不在借阅名单,请确认图书名称!'%book_name)
    def list_book(self):
        b_list=[]
        bb_list=[]
        for i in self.__booklist:
            b_list.append(i.get_bookname())
        print('图书馆存书有:',b_list)
        
        for i in self.__borrowlist:
            bb_list.append(i.get_bookname())
        print('图书馆借出去的书有:',bb_list)

#图书类,管理图书的基本信息
class Book():
    def __init__(self,name,author,tag):
        self.__name=name
        self.__author=author
        self.__tag=tag
        self.__borrowtime=datetime.now()
        self.__returntime=datetime.now()
    
    def get_bookname(self):
        return self.__name

    def update_borrow_returntime(self,modify_time,flag):
        '''flag=1 更新借阅时间,flag=0 更新归还时间'''
        if flag ==1:
            self.__borrowtime=datetime.now()
        elif flag==0:
            self.__returntime=datetime.now()
    def get_period(self):
        period=self.__returntime-self.__borrowtime
        return period.days,period.total_seconds()
        
    
    def get_bookinfo(self):
        return self.__name,self.__author,self.__tag,self.__borrowtime,self.__returntime

#命令类,根据用户命令 调用图书馆的相关图书管理操作
class CommandList():
    def __init__(self):
        pass
    
    def listinfo(self,library):
        if library==None:
            print('''
            create:新建图书馆
            listbooks:图书馆的当前图书展示
            add:新增图书
            query:查询图书
            borrow:借阅图书
            returnbook:归还图书
            exit:退出程序
            ''')
        else:
            print('''
            listbooks:图书馆的当前图书展示
            add:新增图书
            query:查询图书
            borrow:借阅图书
            returnbook:归还图书
            exit:退出程序
            ''')
    
    def create_library(self,name,location):
        print('%s图书馆创建成功!'%name)
        return Library(name,location)
    
    def use_library(self,data_file):
        if os.path.exists(data_file):
            try:
                with open(data_file,'rb') as fp:
                    library=p.load(fp)
                    print('数据文件加载成功!')
            except:
                print('数据文件加载失败,错误信息如下:')
                traceback.print_exc()
        else:
            print("Warning: library数据文件不存在,请输入'create'命令创建library")
            library=None
        return library

    def add(self,library):

        while 1:
            book_name=input('请输入要添加书的书名:').strip()
            book_author=input('请输入要添加书的作者:').strip()
            book_tag=input('请输入要添加书的类型:').strip()
            if book_name=='' or book_author=='' or book_tag=='':
                print('书名、作者、类型均不能为空!')
                continue
            else:
                book=Book(book_name,book_author,book_tag)
                library.add_book(book)
            flag=input('是否继续新增图书 y 是 n 否').strip().lower()
            if flag=='y':
                continue
            else:
                break
    
    def query(self,library):
        book_name=input('请输入要查询的图书名称:').strip()
        s=library.query_book(book_name)
        if s:
            print('图书名称:%s\n图书作者:%s\n图书类型:%s\n'%(s[0],s[1],s[2]))

    def borrow(self,library):
        book_name=input('请输入要借阅的图书名称:').strip()
        library.borrow_book(book_name)
    
    def returnbook(self,library):
        book_name=input('请输入要归还的图书名称:').strip()
        library.return_book(book_name)
    
    def get_library_book_info(self,library):
        library.list_book()
        
    
    def exit(self,library):
        if library==None:
            return None

        flag=input('请确认当前数据是否需要保存,y 保存,n 不保存').strip()
    
        if flag=='y':
            file_name=input('请输入保存数据的文件名称\(全路径\):')
            try:
                with open(file_name,'wb') as fp:
                    p.dump(library,fp)
                    print('保存数据成功!')
            except:
                traceback.print_exc()
        else:
            print('程序退出成功!')
            

#初始调用语句    
if __name__=='__main__':
    #'f:\\pylianxi\\library_data.txt'

    comm=CommandList()

    data_file=input('请输入要加载的数据文件路径:')
    library=comm.use_library(data_file)
    
    while 1:
        comm.listinfo(library)
        command=input('请输入要操作的命令:')
        command=command.strip()
        if library==None and command!='create' and command!='exit':
            print("Warning: library尚未创建,请输入'create'命令创建library!")
            continue

        if command=='create':
            library_name=input('请输入要创建library的名字:').strip()
            library_location=input('请输入要创建library的地理位置:').strip()
            library=comm.create_library(library_name,library_location)


        elif command=='add':
            
            comm.add(library)

        elif command=='query':
            comm.query(library)

        elif command=='borrow':
            comm.borrow(library)
        elif command=='returnbook':
            comm.returnbook(library)
        
        elif command=='exit':
            comm.exit(library)
            break
        elif command=='listbooks':
            comm.get_library_book_info(library)
        else:
            print('命令输入错误,请重新输入!')
        
                
            

            
    
        
        
                
            

 

posted @ 2019-01-17 10:26  小金儿  阅读(453)  评论(0编辑  收藏  举报