赞
踩
(1)借书、还书
(2)实现增删查改
(3)根据作者名称或专业领域检索书籍
(4)找出借走某本书的读者
3、约束需求:
根据上述描述,可以确定以下约束需求:
(1)未借出的书籍必须能够供读者随时借阅。
(2)同一时刻,一本书不能既被借出,又可供借阅
(叠个buff:笨人普通大学生,如有不完善的地方轻喷)
首先可以肯定的是,我们需要创建一个交互界面,来实现对功能的实现,这就需要tkinter库来实现设计交互界面的功能。同时,借阅书籍需要对时间进行记录,我们导入datetime库来进行对时间的记录。
- import tkinter as tk
- from tkinter import simpledialog, messagebox, ttk
- import datetime
图书管理系统,并且拥有管理员和读者身份,需要一个User与Library,作为后续设立Library GUI的前置条件。
- class User:
- def __init__(self, name, role):
- self.name = name
- self.role = role # "admin" 或 "reader"
这样我们就建立好了使用者的名称与角色,并根据角色的不同来拥有不同的权限。
- class Library:
- def __init__(self):
- self.books = {}
- self.borrowed_books = {}
- self.max_borrow_limit = 3
- self.users = {"admin": User("admin", "admin"), "reader": User("reader", "reader")}
- self.current_user = None
在这里,我们定义了最大的借书数量是3本,并且设定了两个使用者的角色,分别是admin和reader,并且令当前用户不存在,来实现后续的登录功能。
- def login(self, username):
- if username in self.users:
- self.current_user = self.users[username]
- return True
- return False
设定一个login函数,当输入的用户名存在我们设置的self.users当中,将current_user替换为我们登录的用户,允许进行接下来的操作,否则报错。
- def logout(self):
- self.current_user = None
- return True
直接把当前用户置空,并返回真值,回到登录界面
不进行过多赘述,直接上代码(笨人会写但是讲不明白QvQ)
- def add_book(self, title, quantity, author="", field=""):
- if title not in self.books:
- self.books[title] = {"quantity": 0, "author": author, "field": field}
- self.books[title]["quantity"] += quantity
-
- def remove_book(self, title, quantity=1):
- if title in self.books and self.books[title]["quantity"] >= quantity:
- self.books[title]["quantity"] -= quantity
- if self.books[title]["quantity"] == 0:
- del self.books[title]
- return True
- return False
-
- def borrow_book(self, title, reader):
- if title in self.books and self.books[title]["quantity"] > 0:
- if len([b for b in self.borrowed_books if self.borrowed_books[b]["reader"] == reader]) >= self.max_borrow_limit:
- return False
- self.books[title]["quantity"] -= 1
- self.borrowed_books[title] = {"reader": reader, "date": datetime.date.today()}
- return True
- return False
-
- def return_book(self, title, reader):
- if title in self.borrowed_books and self.borrowed_books[title]["reader"] == reader:
- self.books[title]["quantity"] += 1
- del self.borrowed_books[title]
- return True
- return False
-
- def get_stock_info(self):
- if not self.books:
- return "当前图书库存为空。"
- message = "当前图书库存总量:\n"
- for title in self.books:
- message += f"《{title}》:{self.books[title]['quantity']} 本\n"
- return message.strip()
-
- def search_books(self, author="", field=""):
- result = {}
- for title, details in self.books.items():
- if author and details["author"].lower() == author.lower() or field and details["field"].lower() == field.lower():
- result[title] = details
- return result
-
- def get_books_by_reader(self, reader):
- return [title for title, borrow_details in self.borrowed_books.items() if borrow_details['reader'] == reader]
在这里我们要设置交互界面,并设置实现我们刚刚在Library中创建的函数,用ttk.Button来创建功能按钮。
- class LibraryGUI:
- def __init__(self, root, library):
- self.library = library
- self.root = root
- self.root.title("图书管理系统")
- self.root.geometry("800x600")
-
- self.login_screen()
-
- def login_screen(self):
- self.clear_ui()
- username = simpledialog.askstring("登录", "请输入您的用户名:")
- if username and self.library.login(username):
- self.init_ui()
- else:
- messagebox.showerror("登录失败", "用户名不正确。")
- # 登录失败后,再次显示登录界面
- self.login_screen()
-
- def init_ui(self):
- # 根据当前用户角色初始化UI
- if self.library.current_user.role == "admin":
- # 为管理员显示的按钮
- ttk.Button(self.root, text="添加图书", command=self.add_book).pack(fill=tk.X, pady=5)
- ttk.Button(self.root, text="删除图书", command=self.remove_book_gui).pack(fill=tk.X, pady=5)
- ttk.Button(self.root, text="借书", command=self.borrow_book).pack(fill=tk.X, pady=5)
- ttk.Button(self.root, text="还书", command=self.return_book).pack(fill=tk.X, pady=5)
- ttk.Button(self.root, text="库存统计", command=self.count_books).pack(fill=tk.X, pady=5)
- ttk.Button(self.root, text="显示所有借书记录", command=self.show_all_borrows).pack(fill=tk.X, pady=5)
- ttk.Button(self.root, text="退出", command=self.logout).pack(fill=tk.X, pady=5)
- # 为所有用户显示的按钮
- if self.library.current_user.role == "reader":
- ttk.Button(self.root,text="我的借书记录",command=self.show_my_borrows).pack(fill=tk.X, pady=5)
- ttk.Button(self.root, text="搜索图书", command=self.search_books).pack(fill=tk.X, pady=5)
- ttk.Button(self.root, text="退出", command=self.logout).pack(fill=tk.X, pady=5)
-
- def add_book(self):
- title = simpledialog.askstring("添加图书", "书名:")
- if title:
- author = simpledialog.askstring("添加图书", "作者:")
- field = simpledialog.askstring("添加图书", "领域:")
- quantity = simpledialog.askinteger("添加图书", "数量:", minvalue=1)
- self.library.add_book(title, quantity, author, field)
- messagebox.showinfo("成功", "成功添加图书。")
-
- def remove_book_gui(self):
- title = simpledialog.askstring("删除图书", "书名:")
- if title:
- quantity = simpledialog.askinteger("删除图书", "数量:", minvalue=1)
- if self.library.remove_book(title, quantity):
- messagebox.showinfo("成功", "图书删除成功。")
- else:
- messagebox.showerror("失败", "图书删除失败,请检查书名和数量是否正确。")
-
- def borrow_book(self):
- title = simpledialog.askstring("借书", "书名:")
- if title:
- reader = simpledialog.askstring("借书", "读者姓名:")
- if reader and self.library.borrow_book(title, reader):
- messagebox.showinfo("成功", "借书成功。")
- else:
- messagebox.showerror("失败", "借书失败。")
-
- def return_book(self):
- title = simpledialog.askstring("还书", "书名:")
- if title:
- reader = simpledialog.askstring("还书", "读者姓名:")
- if reader and self.library.return_book(title, reader):
- messagebox.showinfo("成功", "还书成功。")
- else:
- messagebox.showerror("失败", "还书失败。")
-
- def count_books(self):
- info = self.library.get_stock_info()
- messagebox.showinfo("库存统计", info)
-
- def show_all_borrows(self):
- message = "当前借书记录:\n"
- for title, details in self.library.borrowed_books.items():
- message += f"书名:{title}, 借阅者:{details['reader']}, 借阅日期:{details['date']}\n"
- messagebox.showinfo("借书记录", message or "当前没有借书记录。")
-
- def show_my_borrows(self):
- message = "当前借书记录:\n"
- for title, details in self.library.borrowed_books.items():
- message += f"书名:{title}, 借阅者:{details['reader']}, 借阅日期:{details['date']}\n"
- messagebox.showinfo("借书记录", message or "当前没有借书记录。")
-
- def search_books(self):
- title = simpledialog.askstring("搜索图书","书名(可留空):")
- author = simpledialog.askstring("搜索图书", "作者(可留空):")
- field = simpledialog.askstring("搜索图书", "领域(可留空):")
- result = self.library.search_books(author, field)
- if result:
- message = "搜索结果:\n"
- for title, details in result.items():
- message += f"《{title}》:{details['quantity']} 本,作者:{details['author']},领域:{details['field']}\n"
- messagebox.showinfo("搜索结果", message)
- else:
- messagebox.showinfo("搜索结果", "没有找到匹配的图书。")
-
- def logout(self):
- # 清除界面
- self.clear_ui()
- # 调用library的logout方法重置用户状态
- self.library.logout()
- # 重新显示登录界面
- self.login_screen()
-
- def clear_ui(self):
- # 清除当前root上的所有widget
- for widget in self.root.winfo_children():
- widget.destroy()
我们最后用logout与clear_ui函数来实现返回到登录界面,用messagebox函数来实现对话框的内容提示,simpledialog函数来获取我们输入的信息
- def main():
- root = tk.Tk()
- library = Library()
- app = LibraryGUI(root, library)
- root.mainloop()
-
- if __name__ == "__main__":
- main()
屏幕录制 2024-05-05 224631
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。