用Python实现一个图书管理系统
时间: 2023-11-05 19:08:17 浏览: 152
python实现的图书管理系统
好的,下面是一个简单的图书管理系统的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)
print("Book added successfully.")
def remove_book(self, book):
if book in self.books:
self.books.remove(book)
print("Book removed successfully.")
else:
print("Book not found.")
def search_book(self, title):
for book in self.books:
if book.title == title:
print(f"{book.title} by {book.author} ({book.genre}, {book.year})")
return
print("Book not found.")
def show_all_books(self):
for book in self.books:
print(f"{book.title} by {book.author} ({book.genre}, {book.year})")
library = Library()
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "Classic", 1925)
book2 = Book("To Kill a Mockingbird", "Harper Lee", "Classic", 1960)
library.add_book(book1)
library.add_book(book2)
library.show_all_books()
library.search_book("The Great Gatsby")
library.remove_book(book1)
library.show_all_books()
```
以上代码实现了一个简单的图书管理系统,包括添加图书、删除图书、搜索图书和展示所有图书等功能。在这个示例中,我们用类来表示图书和图书馆,每本书有标题、作者、类型和年份等属性,而图书馆则包含了所有的书,并提供了各种管理功能。
阅读全文