图书馆管理系统代码
时间: 2023-07-29 18:07:03 浏览: 58
由于图书馆管理系统是一个较为复杂的项目,代码比较长,无法在此一一列出。以下是一个简单的图书馆管理系统的代码框架,供参考:
```python
class Book:
def __init__(self, title, author, isbn, quantity):
self.title = title
self.author = author
self.isbn = isbn
self.quantity = quantity
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
self.books.remove(book)
def search_book(self, title):
for book in self.books:
if book.title == title:
return book
def display_books(self):
for book in self.books:
print(f"{book.title} by {book.author} (ISBN: {book.isbn}) - {book.quantity} copies available")
class User:
def __init__(self, name, email):
self.name = name
self.email = email
class Borrower(User):
def __init__(self, name, email):
super().__init__(name, email)
self.borrowed_books = []
def borrow_book(self, library, title):
book = library.search_book(title)
if book:
if book.quantity > 0:
self.borrowed_books.append(book)
book.quantity -= 1
print(f"{self.name} has successfully borrowed {book.title}")
else:
print(f"Sorry, {book.title} is currently out of stock")
else:
print(f"Sorry, {title} is not available in the library")
def return_book(self, library, title):
for book in self.borrowed_books:
if book.title == title:
self.borrowed_books.remove(book)
book.quantity += 1
library.add_book(book)
print(f"{self.name} has successfully returned {book.title}")
return
print(f"Sorry, {self.name} did not borrow {title}")
class Librarian(User):
def __init__(self, name, email):
super().__init__(name, email)
def add_book(self, library, book):
library.add_book(book)
print(f"{book.title} has been added to the library")
def remove_book(self, library, book):
library.remove_book(book)
print(f"{book.title} has been removed from the library")
def display_borrowed_books(self, borrower):
for book in borrower.borrowed_books:
print(f"{book.title} by {book.author}")
# example usage
library = Library()
book1 = Book("Harry Potter and the Philosopher's Stone", "J.K. Rowling", "9780747532743", 5)
book2 = Book("The Lord of the Rings", "J.R.R. Tolkien", "9780261102385", 3)
library.add_book(book1)
library.add_book(book2)
library.display_books()
borrower = Borrower("Alice", "alice@example.com")
borrower.borrow_book(library, "Harry Potter and the Philosopher's Stone")
borrower.borrow_book(library, "The Lord of the Rings")
borrower.return_book(library, "Harry Potter and the Philosopher's Stone")
librarian = Librarian("Bob", "bob@example.com")
librarian.add_book(library, Book("The Hobbit", "J.R.R. Tolkien", "9780547928227", 2))
librarian.remove_book(library, book2)
librarian.display_borrowed_books(borrower)
```
此代码框架包含了图书馆、书籍、用户、借阅者和管理员等类的定义,并提供了一些基本的操作,如添加/删除书籍、查找书籍、借阅/归还书籍等。您可以根据自己的需求修改和完善这个框架,以实现一个更完整、更实用的图书馆管理系统。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)