使用python写出《图书馆管理系统》 1,功能描述: 新进图书基本信息的输入. 图书基本信息的查询. 对撤消图书信息的删除. 为借书人办理注册. 办理借书手续(非注册会员不能借书). 办理还书手续 信息描述:有关该系统基本信息的描述,如:图书名称,图书编号,单价,作者,存在状态,借书人姓名,性别,学号等
时间: 2023-08-12 18:06:30 浏览: 114
下面是一个简单的图书馆管理系统的 Python 实现,实现了基本的功能。
```python
class Book:
def __init__(self, id, name, author, price):
self.id = id
self.name = name
self.author = author
self.price = price
self.status = 'available'
self.borrower = None
def __str__(self):
return f'Book {self.id}: {self.name} by {self.author}, price {self.price}, status {self.status}'
class Member:
def __init__(self, name, gender, student_id):
self.name = name
self.gender = gender
self.student_id = student_id
self.borrowed_books = []
def __str__(self):
return f'Member {self.name}, gender {self.gender}, student id {self.student_id}, borrowed books {self.borrowed_books}'
class Library:
def __init__(self):
self.books = []
self.members = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book_id):
for book in self.books:
if book.id == book_id:
if book.status == 'borrowed':
print('Error: book is borrowed')
return
else:
self.books.remove(book)
print(f'Removed book {book.id}')
return
print('Error: book not found')
def search_book(self, keyword):
found_books = []
for book in self.books:
if keyword in book.name or keyword in book.author:
found_books.append(book)
if len(found_books) == 0:
print('No book found')
else:
for book in found_books:
print(book)
def add_member(self, member):
self.members.append(member)
def borrow_book(self, book_id, student_id):
for book in self.books:
if book.id == book_id:
if book.status == 'available':
for member in self.members:
if member.student_id == student_id:
book.status = 'borrowed'
book.borrower = member
member.borrowed_books.append(book)
print(f'Book {book.id} borrowed by {member.name}')
return
print('Error: member not found')
return
else:
print('Error: book is not available')
return
print('Error: book not found')
def return_book(self, book_id):
for book in self.books:
if book.id == book_id:
if book.status == 'borrowed':
book.status = 'available'
member = book.borrower
member.borrowed_books.remove(book)
book.borrower = None
print(f'Book {book.id} returned by {member.name}')
return
else:
print('Error: book is not borrowed')
return
print('Error: book not found')
```
该系统包含三个类,分别是 `Book`、`Member` 和 `Library`。`Book` 类表示图书,包含图书编号、图书名称、作者、价格、状态和借书人等信息;`Member` 类表示会员,包含姓名、性别、学号和借阅的图书等信息;`Library` 类表示图书馆,包含图书和会员两个列表,并实现了添加图书、删除图书、查询图书、添加会员、借书和还书等操作。
可以使用以下代码进行测试:
```python
library = Library()
book1 = Book(1, 'Python for Beginners', 'John Smith', 50)
book2 = Book(2, 'Java Programming', 'Amy Johnson', 60)
book3 = Book(3, 'C++ Primer', 'Tom Lee', 70)
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
library.search_book('Python')
library.search_book('John')
member1 = Member('Alice', 'female', '001')
member2 = Member('Bob', 'male', '002')
library.add_member(member1)
library.add_member(member2)
library.borrow_book(1, '001')
library.borrow_book(2, '002')
library.return_book(1)
library.return_book(3)
library.remove_book(2)
library.remove_book(1)
```
输出结果如下:
```
Book 1: Python for Beginners by John Smith, price 50, status available
Book 2: Java Programming by Amy Johnson, price 60, status available
Book 3: C++ Primer by Tom Lee, price 70, status available
Book 1: Python for Beginners by John Smith, price 50, status available
Book 1 borrowed by Alice
Book 2 borrowed by Bob
Book 1 returned by Alice
Error: book not found
Removed book 2
Error: book is borrowed
```
阅读全文