使用Python写一个图书管理系统
时间: 2024-03-06 14:25:00 浏览: 107
python编写的图书管理系统
5星 · 资源好评率100%
好的,以下是一个简单的图书管理系统的Python代码:
```
class Book:
def __init__(self, title, author, genre, year, num_copies):
self.title = title
self.author = author
self.genre = genre
self.year = year
self.num_copies = num_copies
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 display_books(self):
for book in self.books:
print("Title:", book.title)
print("Author:", book.author)
print("Genre:", book.genre)
print("Year:", book.year)
print("Number of Copies:", book.num_copies)
print()
def main():
library = Library()
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "Classic", 1925, 5)
book2 = Book("To Kill a Mockingbird", "Harper Lee", "Classic", 1960, 3)
book3 = Book("1984", "George Orwell", "Science Fiction", 1949, 2)
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
library.display_books()
book_to_remove = library.search_book("1984")
library.remove_book(book_to_remove)
print("After removing 1984:")
library.display_books()
if __name__ == '__main__':
main()
```
以上代码实现了一个简单的图书管理系统,包括添加图书、删除图书、搜索图书和展示图书信息等功能。用户可以根据需要自行添加其他功能。
阅读全文