图书借阅管理系统python面向对象
时间: 2023-10-22 09:07:25 浏览: 137
好的,以下是一个简单的图书借阅管理系统的Python面向对象代码示例:
```python
class Book:
def __init__(self, title, author, isbn, num_copies):
self.title = title
self.author = author
self.isbn = isbn
self.num_copies = num_copies
def display(self):
print(f"Title: {self.title}")
print(f"Author: {self.author}")
print(f"ISBN: {self.isbn}")
print(f"Number of copies: {self.num_copies}")
class Library:
books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
self.books.remove(book)
def display_books(self):
for book in self.books:
book.display()
class Borrower:
def __init__(self, name):
self.name = name
self.borrowed_books = []
def borrow_book(self, library, book):
if book in library.books and book.num_copies > 0:
self.borrowed_books.append(book)
book.num_copies -= 1
print(f"{self.name} has borrowed {book.title}")
else:
print(f"{self.name} cannot borrow {book.title}")
def return_book(self, library, book):
if book in self.borrowed_books:
self.borrowed_books.remove(book)
book.num_copies += 1
print(f"{self.name} has returned {book.title}")
else:
print(f"{self.name} did not borrow {book.title}")
# Example usage
book1 = Book("Python for Data Science Handbook", "Jake VanderPlas", "978-1491912058", 2)
book2 = Book("Python Crash Course", "Eric Matthes", "978-1593279288", 3)
library = Library()
library.add_book(book1)
library.add_book(book2)
borrower1 = Borrower("Alice")
borrower1.borrow_book(library, book1)
borrower1.return_book(library, book1)
library.display_books()
```
在这个示例中,我们定义了三个类:`Book`、`Library`和`Borrower`。`Book`类表示一本书,包含书的标题、作者、ISBN和副本数量。`Library`类表示图书馆,包含已添加的书籍列表和添加/删除/显示书籍的方法。`Borrower`类表示一个借书人,包含姓名和已借书籍列表,以及借书和还书的方法。
我们可以创建一些书籍、图书馆和借书人对象,并使用它们的方法来模拟借阅过程。
阅读全文