用python语言编写一个图书馆管理系统,并给出源代码和运行结果
时间: 2024-03-12 07:49:41 浏览: 97
基于python的图书馆管理系统源码&(python毕业设计完整源代码+LW).zip
好的,我可以为您提供一个简单的图书馆管理系统的 Python 代码示例。由于篇幅限制,我只提供核心代码部分。
```python
class Book:
def __init__(self, title, author, genre, year, quantity):
self.title = title
self.author = author
self.genre = genre
self.year = year
self.quantity = quantity
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
print("书籍已添加!")
def search_book(self, title):
for book in self.books:
if book.title == title:
return book
return None
def remove_book(self, book):
if book in self.books:
self.books.remove(book)
print("书籍已移除!")
def display_books(self):
for book in self.books:
print(f"{book.title} - {book.author} - {book.genre} - {book.year} - {book.quantity}")
def main():
library = Library()
book1 = Book("Python编程从入门到实践", "Eric Matthes", "编程", 2016, 5)
book2 = Book("流浪地球", "刘慈欣", "科幻", 2008, 3)
book3 = Book("活着", "余华", "文学", 1993, 2)
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
library.display_books()
book = library.search_book("Python编程从入门到实践")
if book:
library.remove_book(book)
library.display_books()
if __name__ == "__main__":
main()
```
运行结果如下:
```
书籍已添加!
书籍已添加!
书籍已添加!
Python编程从入门到实践 - Eric Matthes - 编程 - 2016 - 5
流浪地球 - 刘慈欣 - 科幻 - 2008 - 3
活着 - 余华 - 文学 - 1993 - 2
书籍已移除!
流浪地球 - 刘慈欣 - 科幻 - 2008 - 3
活着 - 余华 - 文学 - 1993 - 2
```
阅读全文