写一个图书馆管理系统代码
时间: 2024-09-19 14:00:16 浏览: 31
c++管理系统图书馆管理系统代码.zip
创建一个简单的图书管理系统,我们可以考虑用Python编写一个基础版本。这里将展示一个基本的命令行界面,包括添加书籍、查询书籍、借阅和归还书籍的功能。请注意,这只是一个简化版的示例,并未包含数据库连接等复杂功能。
```python
class Book:
def __init__(self, title, author, available):
self.title = title
self.author = author
self.available = available
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def search_book(self, title):
for book in self.books:
if book.title == title:
return book
return None
def borrow_book(self, title):
book = self.search_book(title)
if book and book.available:
book.available = False
print(f"Book '{title}' borrowed.")
else:
print("Book not found or already borrowed.")
def return_book(self, title):
book = self.search_book(title)
if book and not book.available:
book.available = True
print(f"Book '{title}' returned.")
else:
print("Invalid action.")
# 创建图书馆实例
library = Library()
# 添加书籍
book1 = Book("Python Programming", "John Doe", True)
book2 = Book("Data Structures", "Jane Smith", True)
library.add_book(book1)
library.add_book(book2)
# 操作书籍
library.borrow_book("Python Programming")
library.return_book("Data Structures")
```
阅读全文