用Python语言实现需要拥有图书信息和学生信息以及能实现借阅功能和还书功能的图书管理系统
时间: 2024-01-22 16:18:05 浏览: 62
基于sqlite3和PyQt5实现的图书信息管理系统python源码+db数据+gui界面.zip
好的,下面是一个简单的Python实现,可以作为参考:
```python
# 定义一个Book类,存储图书信息
class Book:
def __init__(self, id, name, author, publisher):
self.id = id
self.name = name
self.author = author
self.publisher = publisher
self.is_borrowed = False
# 定义一个Student类,存储学生信息
class Student:
def __init__(self, id, name):
self.id = id
self.name = name
self.borrowed_books = []
# 定义一个Library类,实现图书管理系统的功能
class Library:
def __init__(self):
self.books = []
self.students = []
# 添加图书
def add_book(self, book):
self.books.append(book)
# 添加学生
def add_student(self, student):
self.students.append(student)
# 查找图书
def find_book(self, book_id):
for book in self.books:
if book.id == book_id:
return book
return None
# 查找学生
def find_student(self, student_id):
for student in self.students:
if student.id == student_id:
return student
return None
# 借书
def borrow_book(self, student_id, book_id):
student = self.find_student(student_id)
book = self.find_book(book_id)
if student and book and not book.is_borrowed:
student.borrowed_books.append(book)
book.is_borrowed = True
return True
else:
return False
# 还书
def return_book(self, student_id, book_id):
student = self.find_student(student_id)
book = self.find_book(book_id)
if student and book and book.is_borrowed:
student.borrowed_books.remove(book)
book.is_borrowed = False
return True
else:
return False
```
使用示例:
```python
# 创建一个图书馆对象
library = Library()
# 添加图书
book1 = Book(1, 'Python入门', '张三', '清华大学出版社')
book2 = Book(2, 'Java编程思想', '李四', '人民邮电出版社')
library.add_book(book1)
library.add_book(book2)
# 添加学生
student1 = Student(1, '小明')
student2 = Student(2, '小红')
library.add_student(student1)
library.add_student(student2)
# 借书
library.borrow_book(1, 1) # 小明借Python入门
library.borrow_book(2, 1) # 小红借Python入门(失败,因为该书已被借走)
# 还书
library.return_book(1, 1) # 小明还Python入门
```
阅读全文