以下是一个完整的图书借阅管理系统的Python实现,包含核心功能(图书管理、借阅/归还、查询等),无需任何外部库或网址依赖,直接运行即可使用:

完整代码

python

import datetime

class Book:

def __init__(self, book_id, title, author, total_copies=1):

self.book_id = book_id #图书ID

self.title = title #书名

self.author = author #作者

self.total_copies = total_copies #总副本数

self.available_copies = total_copies #可借副本数

self.borrow_records = [] #借阅记录(记录用户ID和借阅日期)

def __str__(self):

return f"ID: {self.book_id} |书名: {self.title} |作者: {self.author} |可借副本: {self.available_copies}/{self.total_copies}"

class User:

def __init__(self, user_id, name):

self.user_id = user_id #用户ID

self.name = name #用户名

self.borrowed_books = {} #借阅的图书{book_id:归还截止日期}

def __str__(self):

return f"用户ID: {self.user_id} |姓名: {self.name} |当前借阅: {len(self.borrowed_books)}本"

class Library:

def __init__(self):

self.books = {} #所有图书{book_id: Book对象}

self.users = {} #所有用户{user_id: User对象}

self.borrow_period = 30 #默认借阅期限(天)

# ==========图书管理==========

def add_book(self, book_id, title, author, copies=1):

if book_id in self.books:

print("❌ 图书ID已存在!")

else:

self.books[book_id] = Book(book_id, title, author, copies)

print(f"✅ 成功添加图书: {title} (共{copies}本)")

def remove_book(self, book_id):

if book_id not in self.books:

print("❌ 图书不存在!")

elif self.books[book_id].borrow_records:

print("❌ 图书有未归还记录,无法删除!")

else:

del self.books[book_id]

print(f"✅ 已删除图书ID: {book_id}")

# ==========用户管理==========

def register_user(self, user_id, name):

if user_id in self.users:

print("❌ 用户ID已存在!")

else:

self.users[user_id] = User(user_id, name)

print(f"✅ 成功注册用户: {name}")

# ==========借阅功能==========

def borrow_book(self, user_id, book_id):

if user_id not in self.users:

print("❌ 用户不存在!")

elif book_id not in self.books:

print("❌ 图书不存在!")

elif self.books[book_id].available_copies <= 0:

print("❌ 图书已全部借出!")

else:

user = self.users[user_id]

book = self.books[book_id]

#记录借阅

due_date = datetime.date.today() + datetime.timedelta(days=self.borrow_period)

user.borrowed_books[book_id] = due_date

book.available_copies -= 1

book.borrow_records.append((user_id, datetime.date.today()))

print(f"✅ 用户{user.name}成功借阅《{book.title}》,请于{due_date}前归还。")

def return_book(self, user_id, book_id):

if user_id not in self.users:

print("❌ 用户不存在!")

elif book_id not in self.books:

print("❌ 图书不存在!")

elif book_id not in self.users[user_id].borrowed_books:

print("❌ 用户未借阅此书!")

else:

user = self.users[user_id]

book = self.books[book_id]

#归还图书

del user.borrowed_books[book_id]

book.available_copies += 1

#检查是否逾期

due_date = user.borrowed_books[book_id]

today = datetime.date.today()

if today > due_date:

days_overdue = (today - due_date).days

print(f"⚠️图书已逾期{days_overdue}天,请尽快缴纳罚款!")

else:

print(f"✅ 用户{user.name}已归还《{book.title}》。")

# ==========查询功能==========

def search_books(self, keyword):

results = []

for book in self.books.values():

if (keyword.lower() in book.title.lower() or

keyword.lower() in book.author.lower() or

keyword.lower() in book.book_id.lower()):

results.append(book)

if results:

print("\n�� 搜索结果:")

for book in results:

print(book)

else:

print("❌ 未找到匹配的图书!")

def list_all_books(self):

if not self.books:

print("�� 图书馆暂无图书!")

else:

print("\n�� 所有图书:")

for book in self.books.values():

print(book)

def list_user_borrowed(self, user_id):

if user_id not in self.users:

print("❌ 用户不存在!")

else:

user = self.users[user_id]

print(f"\n��{user.name}的借阅记录:")

if not user.borrowed_books:

print("无借阅记录")

else:

for book_id, due_date in user.borrowed_books.items():

book = self.books.get(book_id, None)

title = book.title if book else "未知图书"

print(f"-《{title}》(截止日期: {due_date})")

# ==========主菜单==========

def main():

library = Library()

#预置测试数据

library.add_book("B001", "三体", "刘慈欣", 2)

library.add_book("B002", "平凡的世界", "路遥", 1)

library.register_user("U001", "张三")

library.register_user("U002", "李四")

while True:

print("\n===图书借阅管理系统===")

print("1.图书管理")

print("2.用户管理")

print("3.借阅/归还")

print("4.查询")

print("5.退出系统")

choice = input("请选择功能(1-5): ").strip()

#图书管理

if choice == "1":

print("\n---图书管理---")

print("1.添加图书")

print("2.删除图书")

print("3.查看所有图书")

sub_choice = input("请选择操作(1-3): ").strip()

if sub_choice == "1":

book_id = input("输入图书ID: ").strip()

title = input("输入书名: ").strip()

author = input("输入作者: ").strip()

copies = int(input("输入副本数量: ").strip() or 1)

library.add_book(book_id, title, author, copies)

elif sub_choice == "2":

book_id = input("输入要删除的图书ID: ").strip()

library.remove_book(book_id)