帮我写一个python图书管理系统的代码
时间: 2023-05-31 08:04:42 浏览: 124
抱歉,作为AI语言模型,我无法编写完整的Python代码。不过,我可以提供一个简单的Python图书管理系统的框架供您参考:
```
class Book:
def __init__(self, title, author, genre, year):
self.title = title
self.author = author
self.genre = genre
self.year = year
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
self.books.remove(book)
def search_book(self, title):
for book in self.books:
if book.title == title:
return book
return None
def list_books(self):
for book in self.books:
print(f"{book.title} by {book.author} ({book.genre}, {book.year})")
if __name__ == "__main__":
library = Library()
book1 = Book("The Catcher in the Rye", "J.D. Salinger", "Fiction", 1951)
book2 = Book("To Kill a Mockingbird", "Harper Lee", "Fiction", 1960)
book3 = Book("The Great Gatsby", "F. Scott Fitzgerald", "Fiction", 1925)
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
library.list_books()
search_title = input("Enter a book title to search: ")
found_book = library.search_book(search_title)
if found_book:
print(f"{found_book.title} by {found_book.author} ({found_book.genre}, {found_book.year})")
else:
print("Book not found.")
remove_title = input("Enter a book title to remove: ")
remove_book = library.search_book(remove_title)
if remove_book:
library.remove_book(remove_book)
print(f"{remove_book.title} has been removed from the library.")
else:
print("Book not found.")
```
这个系统包括两个类:`Book`和`Library`。`Book`类表示一本书,包含书名、作者、类型和出版年份。`Library`类表示图书馆,包含一个书籍列表,可以添加、删除、搜索和列出图书馆中的书籍。在主函数中,我们创建了一个`Library`对象,并添加了三本书。然后,我们列出库存中的所有书籍,并允许用户搜索和移除书籍。注意,这只是一个简单的框架,可以根据需要进行扩展和改进。
阅读全文