import sys
# 打印书库中的所有书的函数 def book_detail(books): for book_name, book_info in books.items(): if book_info is not None: print('bookName:{} ISBN:{} title:{} author:{} number:{} borrowNumber:{}' .format(book_name, book_info['ISBN'], book_info['title'], book_info['author'], book_info['number'],
book_info['borrow_num'])) #借阅排序 def sort_borrow(books): arr=[] i=0 for book_info in books.values(): arr.append(book_info['borrow_num']) arr1=sorted(arr,reverse=True) for book_info in books.values(): print('图书编号{},图书名{},借阅数量{}'.format(book_info['ISBN'],book_info['title'],arr1[i])) i+=1
# 检查借书情况 def check_borrow(book): for book_info in books.values(): # {}跟%s差不多的效果,你翻译的时候注意
print('{}被借出{}本'.format(book_info['title'], book_info['borrow_num'])) # 查找书籍ISBN def search_book(books): flag = 'y' # 添加书籍 while (flag == 'y'): title = input('输入你想查找的书籍的名字:') i = 0 for book_info in books.values(): i = 1 if title in book_info.values(): print('您所查找的书籍的信息如下:') print('title:{} ISBN:{}'.format(book_info['title'], book_info['ISBN'])) if i == 0: # i=0说明没有找到符合输入的书籍
print('没有该书!') # 防止出现错误输入 while (True): flag = input('''如果你想要继续查找书籍请输入'y',想退出请输入'n':''') if flag == 'y' or flag == 'n': break else: print('请输入小写的y或n!') # 添加书籍数量的函数 def add_book(books): flag = True # 添加书籍 while (flag): next_flag = True isbn = input('输入你想添加的书籍的ISBN编号或者输入 exit 退出:') if isbn == 'exit': break no_book = True for book_info in books.values(): if isbn in book_info.values(): no_book = False print("1. 修改在库数目") print('2. 修改书籍名称') print('3. 修改书籍作者') print('4. 退出') option_menu = input('书籍在库, 请输入要进行的操作:') if option_menu == '4': break elif option_menu == '1': try: add_num = eval(input('请输入添加数量:')) except Exception: print("添加书籍在库数目时请输入数字") next_flag = False break if add_num < 0: print('输入错误, 请输入正整数') next_flag = False break book_info['number'] += int(add_num) elif option_menu == '2': new_title = input('请输入需要修改成的名称:') book_info['title'] = new_title elif option_menu == '3': new_author = input('请输入需要修改成作者的名称:') book_info['author'] = new_author else: print('请输入正确的操作代码') next_flag = False break else: no_book = True if no_book: new_book_info = {} title = input('输入你想添加的书籍的名称:') author = input('输入你想添加的书籍的作者:') # 需要做异常处理,防止错误输入 try: number = eval(input('输入你想添加的书籍的在库数目:')) except Exception as e: print("添加书籍在库数目时请输入数字") next_flag = False new_book_info['ISBN'] = isbn
new_book_info['title'] = title
new_book_info['author'] = author
new_book_info['number'] = number
new_book_info['borrow_num'] = 0
key = 'book_' + str(len(books) + 1) books[key] = new_book_info
print('书籍【' + title + '】添加成功') # 防止出现错误输入 while (next_flag): flag_continue = input('''如果你想要继续添加书籍请输入'y',想退出请输入'n':''') if flag_continue.lower() == 'y': break else: flag = False break # 借书函数 def borrow_book(books): flag = 'y' # 添加书籍 while (flag == 'y'): isbn = input('输入你想借的书籍的ISBN编号:') i = 0 for book_info in books.values(): i = 1 if isbn in book_info.values(): b_num = input('请输入要借的的数量:') if book_info['number'] >= int(b_num): book_info['number'] -= int(b_num) book_info['borrow_num'] += int(b_num) else: print('对不起,库存不足!') if i == 0: # i=0说明没有找到符合输入的ISBN号
print('没有该ISBN编号!') # 防止出现错误输入 while (True): flag = input('''如果你想要继续借书籍请输入'y',想退出请输入'n':''') if flag == 'y' or flag == 'n': break else: print('请输入小写的y或n!') # 还书函数 def return_book(books): flag = 'y' # 添加书籍 while (flag == 'y'): isbn = input('Enter the ISBN number of the book you want to return') i = 0 for book_info in books.values(): i = 1 if isbn in book_info.values(): r_num = input('Please enter the amount to be returned') if book_info['borrow_num'] >= int(r_num): book_info['number'] += int(r_num) book_info['borrow_num'] -= int(r_num) else: print('''I'm sorry, there aren't so many books on loan''') if i == 0: # i=0说明没有找到符合输入的ISBN号
print('sorry we do not have this ISBN') # 防止出现错误输入 while (True): flag = input('''If you want to continue returning books, type 'y' and 'n' to exit:''') if flag == 'y' or flag == 'n': break else: print('Please enter a lowercase y or n!') def delete(books): c=input('请输入图书名称') books.pop(c) print('图书已经被删除') def duqu(books): # 获取文本文件的值
filename = r'E:\jiaoben\nmckstudy\books' with open(filename) as file_object: lines = file_object.readlines() for line in lines: a = line
books.update(eval(a)) book_detail(books) if __name__ == '__main__': # 初始图书馆图书信息,并做存储用
books = { } print(" 图书管理 ") print('===============================') print(' 1.展示图书信息 ') print(' 2.删除图书信息 ') print(' 3.增加图书信息 ') print(' 4.查询图书信息 ') print(' 5.图书借阅 ') print(' 6.图书归还 ') print(' 7.图书借阅量排序 ') print(' 8.统计在馆数量 ') print(' 9.图书信息展示 ') print(' 0.退出系统 ') print("================================") print(" 请选择1-9,0:退出 ") # 操作选项
option = input("\n请选择操作选项: ") while (option != '0'): if option == '1': print('~~~~~~展示图书信息~~~~~~') duqu(books) elif option == '2': print('删除图书信息') book_detail(books) delete(books) print("已成功删除") elif option == '3': print('增加图书信息') book_detail(books) add_book(books) book_detail(books) elif option == '4': print('查询图书信息') search_book(books) book_detail(books) elif option == '5': print('图书借阅') borrow_book(books) elif option == '6': print('图书归还') return_book(books) elif option=='7': print('图书借阅量排序') sort_borrow(books) elif option == '8': print('统计在馆数量') check_borrow(books) elif option =='9': print("图书信息展示") book_detail(books) # print('保存') else: print('不好意思,我不知道您要干什么,请重新选择') # option_continue = input("是否继续进行操作? y/n(yes/no)")