设计图书管理信息系统,使之能提供以下功能: 1、图书信息录入功能 2、图书信息文件读取功能 3、图书信息文件保存功能网 4、图书信息浏览功能 5、查询功能: 1)按ID查询2)按类型查询6修改功能: 根据ID修改相应信息; 7、删除图书功能 删除相应ID的图书信息。 8、排序功能(由用户指定按照图书类型或ID;指定升序或降序) 9、退出
时间: 2023-12-04 20:04:16 浏览: 76
好的,我可以给您提供一个简单的 Python 代码实现:
```python
import json
class Book:
def __init__(self, book_id, book_name, author_name, publisher, price, category, quantity, location, purchase_date):
self.book_id = book_id
self.book_name = book_name
self.author_name = author_name
self.publisher = publisher
self.price = price
self.category = category
self.quantity = quantity
self.location = location
self.purchase_date = purchase_date
class BookManagementSystem:
def __init__(self):
self.books = []
def add_book(self):
book_id = input("请输入图书ID:")
book_name = input("请输入书名:")
author_name = input("请输入作者姓名:")
publisher = input("请输入出版社:")
price = input("请输入价格:")
category = input("请输入图书类别:")
quantity = input("请输入数量:")
location = input("请输入上架位置:")
purchase_date = input("请输入购买日期:")
book = Book(book_id, book_name, author_name, publisher, price, category, quantity, location, purchase_date)
self.books.append(book)
print("图书信息录入成功!")
def load_books(self, file_path):
try:
with open(file_path, "r") as f:
self.books = json.load(f)
print("图书信息文件读取成功!")
except FileNotFoundError:
print("文件不存在!")
def save_books(self, file_path):
with open(file_path, "w") as f:
json.dump(self.books, f)
print("图书信息文件保存成功!")
def view_books(self):
for book in self.books:
print(f"图书ID:{book.book_id},书名:{book.book_name},作者姓名:{book.author_name},出版社:{book.publisher},价格:{book.price},图书类别:{book.category},数量:{book.quantity},上架位置:{book.location},购买日期:{book.purchase_date}")
if not self.books:
print("暂无图书信息!")
def query_by_id(self):
book_id = input("请输入要查询的图书ID:")
for book in self.books:
if book.book_id == book_id:
print(f"图书ID:{book.book_id},书名:{book.book_name},作者姓名:{book.author_name},出版社:{book.publisher},价格:{book.price},图书类别:{book.category},数量:{book.quantity},上架位置:{book.location},购买日期:{book.purchase_date}")
break
else:
print("未找到该图书!")
def query_by_category(self):
category = input("请输入要查询的图书类别:")
for book in self.books:
if book.category == category:
print(f"图书ID:{book.book_id},书名:{book.book_name},作者姓名:{book.author_name},出版社:{book.publisher},价格:{book.price},图书类别:{book.category},数量:{book.quantity},上架位置:{book.location},购买日期:{book.purchase_date}")
else:
print("未找到该类别的图书!")
def modify_book(self):
book_id = input("请输入要修改信息的图书ID:")
for i in range(len(self.books)):
if self.books[i].book_id == book_id:
book_name = input("请输入新的书名:")
author_name = input("请输入新的作者姓名:")
publisher = input("请输入新的出版社:")
price = input("请输入新的价格:")
category = input("请输入新的图书类别:")
quantity = input("请输入新的数量:")
location = input("请输入新的上架位置:")
purchase_date = input("请输入新的购买日期:")
self.books[i] = Book(book_id, book_name, author_name, publisher, price, category, quantity, location, purchase_date)
print("图书信息修改成功!")
break
else:
print("未找到该图书!")
def delete_book(self):
book_id = input("请输入要删除的图书ID:")
for i in range(len(self.books)):
if self.books[i].book_id == book_id:
del self.books[i]
print("图书信息删除成功!")
break
else:
print("未找到该图书!")
def sort_books(self):
sort_by = input("请输入排序方式(id或category):")
ascending = input("请输入排序顺序(asc或desc):")
if sort_by == "id":
self.books.sort(key=lambda x: x.book_id, reverse=(ascending == "desc"))
elif sort_by == "category":
self.books.sort(key=lambda x: x.category, reverse=(ascending == "desc"))
else:
print("排序方式错误!")
return
print("图书信息排序成功!")
def exit_system(self):
print("感谢使用图书管理信息系统!")
return
if __name__ == "__main__":
bms = BookManagementSystem()
while True:
print("=" * 30)
print("欢迎使用图书管理信息系统!")
print("1. 录入图书信息\n2. 读取图书信息文件\n3. 保存图书信息文件\n4. 浏览图书信息\n5. 按ID查询图书信息\n6. 按类型查询图书信息\n7. 修改图书信息\n8. 删除图书信息\n9. 排序图书信息\n10. 退出系统")
choice = input("请选择操作:")
if choice == "1":
bms.add_book()
elif choice == "2":
file_path = input("请输入文件路径:")
bms.load_books(file_path)
elif choice == "3":
file_path = input("请输入文件路径:")
bms.save_books(file_path)
elif choice == "4":
bms.view_books()
elif choice == "5":
bms.query_by_id()
elif choice == "6":
bms.query_by_category()
elif choice == "7":
bms.modify_book()
elif choice == "8":
bms.delete_book()
elif choice == "9":
bms.sort_books()
elif choice == "10":
bms.exit_system()
break
else:
print("请输入正确的操作!")
```
这个程序实现了您所要求的所有功能,您可以根据需要进行修改和完善。程序使用了一个 `Book` 类来表示图书信息,使用一个 `BookManagementSystem` 类来管理所有图书信息,并且提供了各种操作的方法。
阅读全文