题目: 图书借阅管理系统
一:题目内容
1 系统的基本功能
本课题要求编写 Python 程序实现对图书信息录入、图书信息查询、图书借阅等方面的管理。一个综合的图书借阅管理系统,要求能够管理图书的基本信息(包含新图书入库、读者图书查询借阅等),需要实现以下功能:读取以数据文件形式存储的图书信息;管理员可以增加、修改、删除图书的信息;读者可以按照图书名、作者名、索书号等查询图书,并可通过该系统实现对图书的借阅、续借和归还;读者还可查询自己所借图书的信息(是否归还、归还日期等)
系统内的所有信息必须以文件的方式存储在硬盘中,图书信息文件,存放了图书的索 书号、书名、作者、出版社、类别、库存总量、可借本数。格式如下:
O141.4/3-6
数学模型 《数学模型》编写组编 华南理工大学出版社 数学 10 8
T311.5/3-1 Python程序设计 丁亚涛 中国水利水电出版社 计算机 5 3
I247.5/2-3 天龙八部壹(2
版
)
金庸 广州出版社 文学 3 2
……
2 要求及提示
2.1 基本要求
(1)系统内的相关信息文件由程序设计人员预先从键盘上录入,文件中的数据记录不得少于 20 条
(2)设计并实现系统的相关界面,提供良好的交互界面;
(3)登录时输入帐号以区分读者和管理员;
(4)读者信息查询:
图书查询借阅功能:输入一个书名或索书号、作者等其他信息,查出相关图书的基本信息并显示输出,同时提示是否需要借阅该图书;
“我的”功能:显示个人图书借阅历史,显示所借图书的状态(是否归还、
归还日期)、并选择是否归还或续借。
(5)管理端信息查询:
管理员可以增加、修改、删除图书的信息。
2.2 选做要求
(1)设计一个基于读者借阅历史数据的推荐算法,向读者推荐可能喜欢的图书。
(2)仿照“豆瓣读书”,设计实现一个书评功能,提供读者对图书进行评分和评论的操作。
(3)使用 Tkinter 或其他
GUI
函数库,为本课题设计一个可视化的界面,要求界面美观、布局合理、功能正确以及对用户的错误操作能够进行友好提示。
2.3 提示
(1)程序的总体框图如下:
图
1
图书借阅管理系统总体框图
(2)数据结构:
依据给定的图书信息,定义图书类,设计内容如下:
class Book(object):
def __init__(self, id, name, author, press, type, amount, available):
self.id = id #索书号
self.name = name #书名
self.author = author #作者
self.press = press #出版社
self.type = type #图书类别
self.amount = amount #库存总量
self.available = available #可借本书
2.4 其他要求
(1)在上述功能要求的基础上,为了提高本课程的成绩,可以和任课教师沟通,为程 序设计题添加一些额外的功能。
(2)变量、方法命名符合规范。
(3)注释详细:每个变量都要求有注释说明用途;方法有注释说明功能,对参数、返 回值也要以注释的形式说明用途;关键的语句段要求有注释解释。
(4)程序的层次清晰,可读性强。
3 开发环境
开发环境使用 Python3 以上版本,开发工具可以选择
IDLE
或者
PyCharm
等集成开发工具
二:核心代码(仅提供核心代码部分)
class Book:
def __init__(self, id, name, author, press, type, amount, available):
self.id = id
self.name = name
self.author = author
self.press = press
self.type = type
self.amount = amount
self.available = available
class BorrowRecord:
def __init__(self, book_id, user_id, borrow_date, due_date, return_date=None):
self.book_id = book_id
self.user_id = user_id
self.borrow_date = borrow_date
self.due_date = due_date
self.return_date = return_date
def create_files():
if not os.path.exists("books.txt"):
with open("books.txt", "w") as f:
f.write(json.dumps([]))
if not os.path.exists("user.txt"):
with open("user.txt", "w") as f:
f.write(json.dumps({"user": {"username": "123", "password": "123"},
"admin": {"username": "abo", "password": "abo"}}))
if not os.path.exists("borrow_records.txt"):
with open("borrow_records.txt", "w") as f:
f.write(json.dumps([]))
def read_file(file_name):
with open(file_name, "r") as f:
return json.loads(f.read())
def write_file(file_name, data):
with open(file_name, "w") as f:
f.write(json.dumps(data))
def login(user_type):
users = read_file("user.txt")
username = input("请输入用户名: ")
password = input("请输入密码: ")
if users.get(user_type).get("username") == username and users.get(user_type).get("password") == password:
print(f"{user_type}登陆成功")
return True
else:
print("用户名或密码错误")
return False
def change_password(user_type):
users = read_file("user.txt")
username = input("请输入用户名: ")
old_password = input("请输入旧密码: ")
new_password = input("请输入新密码: ")
if users.get(user_type).get("username") == username and users.get(user_type).get("password") == old_password:
users[user_type]["password"] = new_password
write_file("user.txt", users)
print("密码修改成功")
else:
print("用户名或密码错误")
def add_book():
books = read_file("books.txt")
id = input("请输入图书编号: ")
name = input("请输入图书名称: ")
author = input("请输入作者名称: ")
press = input("请输入出版社名称: ")
type = input("请输入图书类型: ")
amount = input("请输入图书总量: ")
available = input("请输入可借数量: ")
book = Book(id, name, author, press, type, amount, available).__dict__
books.append(book)
write_file("books.txt", books)
print("图书信息添加成功")
def delete_book():
books = read_file("books.txt")
id = input("请输入要删除的图书编号: ")
for book in books:
if book.get("id") == id:
books.remove(book)
write_file("books.txt", books)
print("图书信息删除成功")
break
else:
print("未找到该图书信息")
def update_book():
books = read_file("books.txt")
id = input("请输入要修改的图书编号: ")
for book in books:
if book.get("id") == id:
book["name"] = input(f"请输入新的图书名称({book.get('name')}): ") or book.get("name")
book["author"] = input(f"请输入新的作者名称({book.get('author')}): ") or book.get("author")
book["press"] = input(f"请输入新的出版社名称({book.get('press')}): ") or book.get("press")
book["type"] = input(f"请输入新的图书类型({book.get('type')}): ") or book.get("type")
book["amount"] = input(f"请输入新的图书总量({book.get('amount')}): ") or book.get("amount")
book["available"] = input(f"请输入新的可借数量({book.get('available')}): ") or book.get("available")
write_file("books.txt", books)
print("图书信息修改成功")
break
else:
print("未找到该图书信息")
def search_books():
books = read_file("books.txt")
keyword = input("请输入关键字: ")
result = []
for book in books:
if keyword in book.get("name") or keyword in book.get("author") or keyword in book.get("press") or keyword in book.get("type"):
result.append(book)
if not result:
print("未找到相关图书信息")
else:
print("搜索结果如下:")
for book in result:
print(f"编号:{book['id']}")
print(f"书名:{book['name']}")
print(f"作者:{book['author']}")
print(f"出版社:{book['press']}")
print(f"类型:{book['type']}")
print(f"总量:{book['amount']}")
print(f"可借数量:{book['available']}")
print("")
def borrow_book(user_id):
books = read_file("books.txt")
borrow_records = read_file("borrow_records.txt")
id = input("请输入要借阅的图书编号: ")
for book in books:
if book.get("id") == id:
if int(book.get("available")) > 0:
book["available"] = str(int(book.get("available")) - 1)
write_file("books.txt", books)
borrow_date = datetime.now().strftime("%Y-%m-%d")
due_date = (datetime.now() + timedelta(days=7)).strftime("%Y-%m-%d")
borrow_record = BorrowRecord(id, user_id, borrow_date, due_date).__dict__
borrow_records.append(borrow_record)
write_file("borrow_records.txt", borrow_records)
print("图书借阅成功")
else:
print("该图书已全部借出")
break
else:
print("未找到该图书信息")
def view_borrow_history(user_id):
borrow_records = read_file("borrow_records.txt")
result = []
for record in borrow_records:
if record.get("user_id") == user_id:
result.append(record)
if not result:
print("您还没有借阅过图书")
else:
print("您的借阅记录如下:")
for record in result:
book = read_file("books.txt")
book_name = ""
for b in book:
if b.get("id") == record.get("book_id"):
book_name = b.get("name")
break
print(f"图书名称:{book_name}")
print(f"借阅日期:{record.get('borrow_date')}")
print(f"应还日期:{record.get('due_date')}")
if record.get("return_date"):
print(f"归还日期:{record.get('return_date')}")
print("")
def return_or_renew_book(user_id):
borrow_records = read_file("borrow_records.txt")
id = input("请输入要归还或续借的图书编号: ")
for record in borrow_records:
if record.get("book_id") == id and record.get("user_id") == user_id:
if not record.get("return_date"):
print("1. 归还图书")
print("2. 续借图书")
choice = input("请选择操作: ")
if choice == "1":
record["return_date"] = "2023-05-16"
write_file("borrow_records.txt", borrow_records)
books = read_file("books.txt")
for book in books:
if book.get("id") == id:
book["available"] = str(int(book.get("available")) + 1)
write_file("books.txt", books)
break
print("图书归还成功")
elif choice == "2":
record["due_date"] = "2023-05-30"
write_file("borrow_records.txt", borrow_records)
print("图书续借成功")
else:
print("无效的选择,请重新输入")
else:
print("该图书已归还,无法续借")
break
else:
print("未找到该借阅记录")
def view_all_books():
books = read_file("books.txt")
if not books:
print("当前没有图书信息")
else:
print("图书信息如下:")
for book in books:
print(f"编号:{book['id']}")
print(f"书名:{book['name']}")
print(f"作者:{book['author']}")
print(f"出版社:{book['press']}")
print(f"类型:{book['type']}")
print(f"总量:{book['amount']}")
print(f"可借数量:{book['available']}")
print("")
三:代码运行
(运行截图过多,仅截几张)
ps:
1.上述内容仅供参考,如需相关(代码,报告)可留言,后续将上传在csdn中。
2.定制可私信我。