赞
踩
图书管理系统的编写:
图书类Book:
属性:书名name 作者author 是否借出isborrow 书籍ID bookID 书籍类型category
注意:书籍ID不能重复
图书管理系统BookManager类
存放图书的工具使用列表
方法:
1.添加图书
2.借书 (根据图书名字借书)要检验图书是否存在、图书是否已经借出
3.还书
4.查询书籍 (根据名字查询, 根据类别查询)
5.修改书籍信息(根据书籍ID修改)
6.本地化保存数据信息(json格式)
import json import os class Book(object): ''' 创建Book类,以此存储单个书的信息 ''' def __init__(self, name, author, bookID, category, isborrow): self.name = name self.author = author self.bookID = bookID self.category = category self.isborrow = isborrow class BookManager(object): ''' 创建BookManager类,用来存储图书管理系统的一系列书和相关方法 ''' def __init__(self): ''' 在BookManager类的初始化init方法中创建图书列表,用来存储所有书的信息。 书库.txt:此文件用来本地化存储对象信息 --若书库.txt不存在则新建文件 --若文件存在,则查看文件中是否已有书籍信息,存在则将信息读取到图书列表中 ''' self.book_list = list() self.bookID_set = set() base_path = os.path.dirname(os.path.abspath(__file__)) file_path = os.path.join(base_path, '数据') if not os.path.exists(file_path): os.makedirs(file_path) if not os.path.exists(os.path.join(file_path, '书库.txt')): with open(os.path.join(file_path, '书库.txt'), mode='w'): pass with open(os.path.join(file_path, '书库.txt'), mode='r+', encoding='utf-8') as file: for line in file.readlines(): date = json.loads(line) book = Book(date['name'], date['author'], date['bookID'], date['category'], date['isborrow']) self.book_list.append(book) self.bookID_set.add(book.bookID) def add(self): ''' 在已有图书列表中添加书籍,图书ID不可重复 --若图书ID已存在则提示用户 --图书ID不存在则添加剩余书籍信息 添加功能执行完毕后保存信息 ''' while True: bookID = input("请输入图书ID,Q或q退出:") if bookID.upper() == "Q": break elif bookID in self.bookID_set: print("图书ID已存在,请重新输入") continue else: self.bookID_set.add(bookID) try: name, author, category = input("请输入书名、作者、图书类别(用空格隔开):").split() except Exception as e: print('输入错误,请按提示输入') self.bookID_set.remove(bookID) continue book = Book(name, author, bookID, category, False) self.book_list.append(book) self.save() def borrow(self): ''' 实现借书功能 完成借书后保存 ''' while True: ans_2 = input('请输入所借书名,Q或q退出:') if ans_2.upper() == 'Q': break elif not any([i.name == ans_2 for i in self.book_list]): print('查无此书') continue else: num = -1 for i in self.book_list: if i.name == ans_2 and i.isborrow == False: i.isborrow = True print('借出成功') num = self.book_list.index(i) break if num == -1: print('此书已被借出') self.save() def bock(self): ''' 实现还书功能 完成还书后保存 ''' while True: ans_3 = input('请输入所还书名,Q或q退出:') if ans_3.upper() == 'Q': break elif not any([i.name == ans_3 for i in self.book_list]): print('查无此书') continue else: num = -1 for i in self.book_list: if i.name == ans_3 and i.isborrow == True: i.isborrow = False print('还书成功') num = self.book_list.index(i) break if num == -1: print('此书未被借出') self.save() def inquire(self): ''' 实现查询功能 --按照书名查找,存在则返回书籍信息,不存在则提示 --按照书籍类别查找,存在则返回相关信息,不存在则提示 ''' def inq_name(): while True: name_set = set() for i in self.book_list: name_set.add(i.name) ans_4_1 = input("请输入书名,Q或q退出:\n") if ans_4_1.upper() == 'Q': break elif ans_4_1 not in name_set: print("查无此书") continue else: for i in self.book_list: if i.name == ans_4_1: print(i.__dict__) def inq_category(): while True: category_set = set() for i in self.book_list: category_set.add(i.category) ans_4_2 = input("请输入书籍类别,Q或q退出:\n") if ans_4_2.upper() == 'Q': break elif ans_4_2 not in category_set: print("查无此类别") continue else: for i in self.book_list: if i.category == ans_4_2: print(i.__dict__) options = {"1": inq_name, "2": inq_category} while True: text = "\n请输入需求(按提示输入1-2),Q或q退出\n\t1——书名查询\t2——类别查询\t\n" ans_4 = input(text) if ans_4.upper() == 'Q': break elif not ans_4.isdecimal() or not (1 <= int(ans_4) <= 2): print("输入错误,请重新输入") continue else: options.get(ans_4)() def save(self): ''' 将数据保存到本地 ''' base_path = os.path.dirname(os.path.abspath(__file__)) file_path = os.path.join(base_path, '数据') with open(os.path.join(file_path, '书库.txt'), mode='w+', encoding='utf-8') as file: for i in self.book_list: text = json.dumps(i.__dict__) + '\n' file.write(text) print("保存完毕") def run(): print("进入图书管理系统") library = BookManager() while True: home = {"1": library.add, "2": library.borrow, "3": library.bock, "4": library.inquire, "5": library.save} text = "\t1——添加书籍\t2——借书\t3——还书\t4——查询\t5——保存\n" choice = input("请输入需求(按提示输入1-6),Q或q退出\n" + text) if choice.upper() == "Q": break elif not choice.isdecimal() or not (1 <= int(choice) <= 5): print("输入错误,请按提示输入") continue else: home.get(choice)() if __name__ == "__main__": run()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。