python的图书管理系统
时间: 2023-09-11 21:04:33 浏览: 95
一个基本的图书管理系统应该包含以下功能:
1. 添加书籍:可以添加书籍信息,包括书名、作者、出版社、出版日期、ISBN等
2. 删除书籍:可以通过书籍的ISBN或者书名删除书籍
3. 查找书籍:可以通过书籍的ISBN或者书名查找书籍
4. 显示所有书籍:可以显示所有的书籍信息
5. 借出书籍:可以将某本书的状态改为已借出,并记录借书人和借书日期
6. 归还书籍:可以将某本书的状态改为已归还,并记录归还日期
基于以上需求,可以使用Python编写一个简单的图书管理系统。下面是一个示例代码:
```python
class Book:
def __init__(self, title, author, publisher, publish_date, isbn, status='available', borrower=None, borrow_date=None, return_date=None):
self.title = title
self.author = author
self.publisher = publisher
self.publish_date = publish_date
self.isbn = isbn
self.status = status
self.borrower = borrower
self.borrow_date = borrow_date
self.return_date = return_date
class Library:
books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, title_or_isbn):
for book in self.books:
if book.title == title_or_isbn or book.isbn == title_or_isbn:
self.books.remove(book)
def find_book(self, title_or_isbn):
for book in self.books:
if book.title == title_or_isbn or book.isbn == title_or_isbn:
return book
return None
def display_books(self):
for book in self.books:
print(f'{book.title} by {book.author} ({book.status})')
def borrow_book(self, title_or_isbn, borrower, borrow_date):
book = self.find_book(title_or_isbn)
if book:
if book.status == 'available':
book.status = 'borrowed'
book.borrower = borrower
book.borrow_date = borrow_date
print(f'{book.title} has been borrowed by {borrower}.')
else:
print(f'{book.title} is not available.')
else:
print('Book not found.')
def return_book(self, title_or_isbn, return_date):
book = self.find_book(title_or_isbn)
if book:
if book.status == 'borrowed':
book.status = 'available'
book.borrower = None
book.borrow_date = None
book.return_date = return_date
print(f'{book.title} has been returned.')
else:
print(f'{book.title} is not borrowed.')
else:
print('Book not found.')
if __name__ == '__main__':
lib = Library()
book1 = Book('Python for Data Science Handbook', 'Jake VanderPlas', 'O\'Reilly Media', '2016-11-21', '978-1491912058')
book2 = Book('Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow', 'Aurélien Géron', 'O\'Reilly Media', '2019-10-15', '978-1492032649')
book3 = Book('Deep Learning with Python', 'François Chollet', 'Manning Publications', '2017-10-28', '978-1617294433')
lib.add_book(book1)
lib.add_book(book2)
lib.add_book(book3)
lib.display_books()
lib.borrow_book('Python for Data Science Handbook', 'John Doe', '2021-01-01')
lib.return_book('Python for Data Science Handbook', '2021-01-15')
lib.display_books()
```
这个示例代码实现了添加、删除、查找和显示所有书籍的功能,以及借出和归还书籍的功能。您可以根据自己的需求进行适当的修改和扩展。
阅读全文