书管理系统代码设计

设计一个书管理系统涉及多个方面,包括图书的添加、删除、查询,借阅和归还管理,用户信息管理等。

python
class Book: def __init__(self, title, author, isbn, available_copies): self.title = title self.author = author self.isbn = isbn self.available_copies = available_copies class User: def __init__(self, user_id, name): self.user_id = user_id self.name = name class Library: def __init__(self): self.books = [] self.users = [] self.borrowed_books = {} def add_book(self, title, author, isbn, available_copies): book = Book(title, author, isbn, available_copies) self.books.append(book) def remove_book(self, isbn): for book in self.books: if book.isbn == isbn: self.books.remove(book) break def display_books(self): for book in self.books: print(f"{book.title} by {book.author} (ISBN: {book.isbn}), Available Copies: {book.available_copies}") def add_user(self, user_id, name): user = User(user_id, name) self.users.append(user) def borrow_book(self, user_id, isbn): for book in self.books: if book.isbn == isbn and book.available_copies > 0: if user_id not in self.borrowed_books: self.borrowed_books[user_id] = [] self.borrowed_books[user_id].append(book) book.available_copies -= 1 print(f"{book.title} borrowed successfully by user {user_id}") break else: print("Book not available for borrowing.") def return_book(self, user_id, isbn): if user_id in self.borrowed_books and len(self.borrowed_books[user_id]) > 0: for book in self.borrowed_books[user_id]: if book.isbn == isbn: book.available_copies += 1 self.borrowed_books[user_id].remove(book) print(f"{book.title} returned successfully by user {user_id}") break else: print("User has not borrowed this book.") else: print("User has not borrowed any books.") # 示例用法 library = Library() library.add_book("The Catcher in the Rye", "J.D. Salinger", "978-0-316-76948-0", 5) library.add_book("To Kill a Mockingbird", "Harper Lee", "978-0-06-112008-4", 3) library.add_user(1, "Alice") library.add_user(2, "Bob") library.display_books() library.borrow_book(1, "978-0-316-76948-0") library.borrow_book(2, "978-0-316-76948-0") library.display_books() library.return_book(1, "978-0-316-76948-0") library.return_book(2, "978-0-316-76948-0") library.display_books()

python
class Library: # ... (之前的代码) def search_books(self, keyword): found_books = [] for book in self.books: if keyword.lower() in book.title.lower() or keyword.lower() in book.author.lower(): found_books.append(book) if found_books: for book in found_books: print(f"{book.title} by {book.author} (ISBN: {book.isbn}), Available Copies: {book.available_copies}") else: print("No matching books found.") def view_user_info(self, user_id): for user in self.users: if user.user_id == user_id: print(f"User ID: {user.user_id}, Name: {user.name}") borrowed_books = self.borrowed_books.get(user_id, []) if borrowed_books: print("Borrowed Books:") for book in borrowed_books: print(f"{book.title} by {book.author} (ISBN: {book.isbn})") else: print("No books borrowed.") break else: print("User not found.") def view_borrow_history(self, user_id): borrowed_books = self.borrowed_books.get(user_id, []) if borrowed_books: print(f"Borrow History for User {user_id}:") for book in borrowed_books: print(f"{book.title} by {book.author} (ISBN: {book.isbn})") else: print("No borrowing history for this user.") # 示例用法 library.search_books("Mockingbird") library.view_user_info(1) library.view_borrow_history(1)

这个扩展示例包括了图书搜索、查看用户信息和查看借阅历史记录等功能。你可以根据具体需求进一步调整和扩展这个系统。