用Python语言实现图书管理系统并要求实现以下功能: (1) 分别输入图书信息和学生信息并将其分别保存到两个文件 (2)分别从文件中读出图书信息和学生信息 (3)实现借阅功能:输入学号和书号,如果借阅成功(学号所对应的学生在学生信息中并且书号所对应的图书在图书信息中且库存大于等于1,每个学生最多借阅5本书),修改图书信息和借阅信息,并将修改后的数据分别保存到文件1和文件2 (4)实现还书功能:从借阅信息中该学生的借阅信息中修改还书日期,修改图书信息的库存信息,并保存到对应的文件
时间: 2024-01-22 22:19:12 浏览: 94
以下是一个简单的图书管理系统的 Python 代码,实现了上述功能:
```python
import json
from datetime import datetime
# 定义文件名
book_file = 'book.json'
student_file = 'student.json'
borrow_file = 'borrow.json'
# 读取文件并返回数据
def read_file(file_name):
try:
with open(file_name, 'r') as f:
data = json.load(f)
except FileNotFoundError:
data = []
return data
# 保存数据到文件
def save_data(data, file_name):
with open(file_name, 'w') as f:
json.dump(data, f)
# 输入图书信息并保存到文件
def input_book():
book_list = read_file(book_file)
book = {}
book['id'] = input('请输入图书编号:')
book['name'] = input('请输入图书名称:')
book['author'] = input('请输入作者:')
book['publisher'] = input('请输入出版社:')
book['total'] = int(input('请输入总数量:'))
book['stock'] = book['total']
book_list.append(book)
save_data(book_list, book_file)
# 输入学生信息并保存到文件
def input_student():
student_list = read_file(student_file)
student = {}
student['id'] = input('请输入学生编号:')
student['name'] = input('请输入学生姓名:')
student_list.append(student)
save_data(student_list, student_file)
# 借阅图书
def borrow_book():
borrow_list = read_file(borrow_file)
book_list = read_file(book_file)
student_list = read_file(student_file)
student_id = input('请输入学生编号:')
book_id = input('请输入图书编号:')
# 判断学生是否存在
student = None
for s in student_list:
if s['id'] == student_id:
student = s
break
if student == None:
print('学生不存在')
return
# 判断图书是否存在
book = None
for b in book_list:
if b['id'] == book_id:
book = b
break
if book == None:
print('图书不存在')
return
# 判断是否库存足够
if book['stock'] < 1:
print('库存不足')
return
# 判断学生是否借阅过5本书
count = 0
for borrow in borrow_list:
if borrow['student_id'] == student_id:
count += 1
if count >= 5:
print('每个学生最多借阅5本书')
return
# 借阅成功,修改图书和借阅信息
book['stock'] -= 1
borrow = {}
borrow['student_id'] = student_id
borrow['book_id'] = book_id
borrow['borrow_date'] = datetime.now().strftime('%Y-%m-%d')
borrow_list.append(borrow)
save_data(book_list, book_file)
save_data(borrow_list, borrow_file)
# 还书
def return_book():
borrow_list = read_file(borrow_file)
book_list = read_file(book_file)
student_list = read_file(student_file)
student_id = input('请输入学生编号:')
book_id = input('请输入图书编号:')
# 判断学生是否存在
student = None
for s in student_list:
if s['id'] == student_id:
student = s
break
if student == None:
print('学生不存在')
return
# 判断图书是否存在
book = None
for b in book_list:
if b['id'] == book_id:
book = b
break
if book == None:
print('图书不存在')
return
# 判断学生是否借阅过这本书
borrow = None
for b in borrow_list:
if b['student_id'] == student_id and b['book_id'] == book_id:
borrow = b
break
if borrow == None:
print('学生没有借阅这本书')
return
# 修改借阅信息和图书库存
borrow['return_date'] = datetime.now().strftime('%Y-%m-%d')
book['stock'] += 1
save_data(borrow_list, borrow_file)
save_data(book_list, book_file)
# 选择功能菜单
while True:
print('1. 输入图书信息')
print('2. 输入学生信息')
print('3. 借阅图书')
print('4. 还书')
print('5. 退出')
choice = input('请选择功能:')
if choice == '1':
input_book()
elif choice == '2':
input_student()
elif choice == '3':
borrow_book()
elif choice == '4':
return_book()
elif choice == '5':
break
else:
print('无效选择')
```
此代码需要在相同目录下创建三个 JSON 文件分别用于保存图书信息、学生信息和借阅信息。对于借阅信息,除了学生编号、图书编号和借阅日期外,还包括还书日期,如果还书日期为空表示该书还未归还。
阅读全文