图书管理系统python 代码
时间: 2023-09-12 22:03:23 浏览: 122
这里提供一个简单的图书管理系统 Python 代码:
```python
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
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_title(self, title):
for book in self.books:
if book.title == title:
return book
return None
def search_author(self, author):
result = []
for book in self.books:
if book.author == author:
result.append(book)
return result
def search_isbn(self, isbn):
for book in self.books:
if book.isbn == isbn:
return book
return None
def display_books(self):
for book in self.books:
print("Title: {} | Author: {} | ISBN: {}".format(book.title, book.author, book.isbn))
library = Library()
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565")
book2 = Book("To Kill a Mockingbird", "Harper Lee", "9780061120084")
book3 = Book("1984", "George Orwell", "9780451524935")
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
library.display_books()
print("\nSearching for 'The Great Gatsby'...")
result = library.search_title("The Great Gatsby")
if result:
print("Title: {} | Author: {} | ISBN: {}".format(result.title, result.author, result.isbn))
else:
print("Book not found.")
print("\nSearching for books by 'Harper Lee'...")
result = library.search_author("Harper Lee")
if result:
for book in result:
print("Title: {} | Author: {} | ISBN: {}".format(book.title, book.author, book.isbn))
else:
print("No books found by this author.")
print("\nRemoving 'To Kill a Mockingbird'...")
library.remove_book(book2)
library.display_books()
```
这个代码实现了一个简单的图书管理系统,包含添加、删除、搜索和展示图书等操作。你可以根据需要进行修改和扩展。
阅读全文