用类实现图书管理系统包括调用目录
时间: 2024-03-10 07:43:12 浏览: 49
好的,我可以为您提供一个用类实现的图书管理系统示例,并包括调用目录功能。示例代码如下:
```python
class Book:
def __init__(self, name, author, publisher, price):
self.name = name
self.author = author
self.publisher = publisher
self.price = price
self.is_borrowed = False
def borrow_book(self):
self.is_borrowed = True
def return_book(self):
self.is_borrowed = False
def display_info(self):
print(f"书名:{self.name},作者:{self.author},出版社:{self.publisher},价格:{self.price},状态:{'已借出' if self.is_borrowed else '未借出'}")
class Library:
def __init__(self):
self.books = []
def add_book(self):
name = input("请输入书名:")
author = input("请输入作者:")
publisher = input("请输入出版社:")
price = input("请输入价格:")
book = Book(name, author, publisher, price)
self.books.append(book)
print("添加成功!")
def remove_book(self):
name = input("请输入要删除的书名:")
for book in self.books:
if book.name == name:
self.books.remove(book)
print("删除成功!")
return
print("未找到该书!")
def search_book(self):
name = input("请输入要查询的书名:")
for book in self.books:
if book.name == name:
book.display_info()
return
print("未找到该书!")
def borrow_book(self):
name = input("请输入要借阅的书名:")
for book in self.books:
if book.name == name:
if book.is_borrowed:
print("该书已被借出!")
else:
book.borrow_book()
print("借阅成功!")
return
print("未找到该书!")
def return_book(self):
name = input("请输入要归还的书名:")
for book in self.books:
if book.name == name:
if not book.is_borrowed:
print("该书未被借出!")
else:
book.return_book()
print("归还成功!")
return
print("未找到该书!")
def display_all_books(self):
for book in self.books:
book.display_info()
def display_menu():
print("欢迎使用图书管理系统!")
print("1. 添加书籍")
print("2. 删除书籍")
print("3. 查询书籍")
print("4. 借阅书籍")
print("5. 归还书籍")
print("6. 显示所有书籍")
print("0. 退出系统")
def main():
library = Library()
while True:
display_menu()
choice = input("请选择操作:")
if choice == "1":
library.add_book()
elif choice == "2":
library.remove_book()
elif choice == "3":
library.search_book()
elif choice == "4":
library.borrow_book()
elif choice == "5":
library.return_book()
elif choice == "6":
library.display_all_books()
elif choice == "0":
break
else:
print("无效操作!")
if __name__ == "__main__":
main()
```
在主函数中,我们创建了一个 Library 类的实例,并通过调用其方法实现了添加、删除、查询、借阅、归还和显示所有书籍的功能。调用目录功能可以在 display_menu() 函数中实现。
注意:上面的示例代码只是一个简单的实现,您可以根据具体需求进行修改和完善,例如添加用户注册和登录功能、使用数据库等。
阅读全文