用python写一个完整的图书管理系统
时间: 2023-02-06 19:04:26 浏览: 101
好的,下面是一个完整的图书管理系统的示例代码:
```
# 定义图书类
class Book:
def __init__(self, title, author, publisher, pages):
self.title = title
self.author = author
self.publisher = publisher
self.pages = pages
self.borrowed = False
self.borrowed_by = None
# 定义图书馆类
class Library:
def __init__(self):
self.books = []
# 添加图书
def add_book(self, book):
self.books.append(book)
# 借出图书
def borrow_book(self, title, borrower):
for book in self.books:
if book.title == title and not book.borrowed:
book.borrowed = True
book.borrowed_by = borrower
return True
return False
# 归还图书
def return_book(self, title):
for book in self.books:
if book.title == title and book.borrowed:
book.borrowed = False
book.borrowed_by = None
return True
return False
# 创建图书馆对象
library = Library()
# 创建图书对象并添加到图书馆
book1 = Book("Python入门", "John Smith", "Python Press", 250)
book2 = Book("Java入门", "Jane Smith", "Java Press", 300)
book3 = Book("C++入门", "Bob Smith", "C++ Press", 200)
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
# 借出图书
if library.borrow_book("Python入门", "张三"):
print("借书成功")
else:
print("借书失败")
# 归还图书
if library.return_book("Python入门"):
print("还书成功")
else:
print("还书失败")
```
在这个示例代码中,我们定义了两个类:`Book` 类和 `Library` 类。`Book` 类用于表示图书,包含图书的标题、作者、出版社、页数、是否被借出以
阅读全文