class Book: def init(self, title, author, isbn): self.title = title self.author = author self.isbn = isbn def str(self): return f"{self.title} by {self.author}" class Library: def init(self): self.books = [] def add_book(self, book): self.books.append(book) def search_book_by_title(self, title): results = [] for book in self.books: if title.lower() in book.title.lower(): results.append(book) return results def search_book_by_author(self, author): results = [] for book in self.books: if author.lower() in book.author.lower(): results.append(book) return results def checkout_book(self, book): if book in self.books: self.books.remove(book) return book else: return None def return_book(self, book): self.books.append(book) return book # sample usage book1 = Book('The Great Gatsby', 'F. Scott Fitzgerald', '978-0743273565') book2 = Book('To Kill a Mockingbird', 'Harper Lee', '978-0446310789') book3 = Book('1984', 'George Orwell', '978-0451524935') library = Library() library.add_book(book1) library.add_book(book2) library.add_book(book3) print(library.search_book_by_title('the')) print(library.search_book_by_author('scott fitzgerald')) book4 = Book('Pride and Prejudice', 'Jane Austen', '978-0486284736') library.add_book(book4) print(library.search_book_by_author('Jane Austen')) checked_out = library.checkout_book(book4) print(f"Book checked out: {checked_out}") returned = library.return_book(book4) print(f"Book returned: {returned}"),在程序中写上注释
时间: 2023-12-06 10:42:05 浏览: 180
# 定义一个 Book 类来表示一本书
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
def __str__(self):
return f"{self.title} by {self.author}"
# 定义一个 Library 类来表示一个图书馆
class Library:
def __init__(self):
self.books = []
# 添加一本书到图书馆中
def add_book(self, book):
self.books.append(book)
# 根据书名搜索书籍
def search_book_by_title(self, title):
results = []
for book in self.books:
if title.lower() in book.title.lower():
results.append(book)
return results
# 根据作者搜索书籍
def search_book_by_author(self, author):
results = []
for book in self.books:
if author.lower() in book.author.lower():
results.append(book)
return results
# 借出一本书
def checkout_book(self, book):
if book in self.books:
self.books.remove(book)
return book
else:
return None
# 归还一本书
def return_book(self, book):
self.books.append(book)
return book
# 示例用法
# 创建三本书
book1 = Book('The Great Gatsby', 'F. Scott Fitzgerald', '978-0743273565')
book2 = Book('To Kill a Mockingbird', 'Harper Lee', '978-0446310789')
book3 = Book('1984', 'George Orwell', '978-0451524935')
# 创建一个图书馆
library = Library()
# 将三本书添加到图书馆中
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
# 搜索标题中包含 "the" 的书籍
print(library.search_book_by_title('the'))
# 搜索作者名中包含 "scott fitzgerald" 的书籍
print(library.search_book_by_author('scott fitzgerald'))
# 添加第四本书到图书馆中
book4 = Book('Pride and Prejudice', 'Jane Austen', '978-0486284736')
library.add_book(book4)
# 搜索作者名中包含 "Jane Austen" 的书籍
print(library.search_book_by_author('Jane Austen'))
# 借出第四本书并归还
checked_out = library.checkout_book(book4)
print(f"Book checked out: {checked_out}")
returned = library.return_book(book4)
print(f"Book returned: {returned}")
阅读全文