图书管理系统基本功能
"""__ author__= 小孩子 """
import json
# 装饰器,对在个人界面写入文件及界面功能的处理
def file_ecorator(fn):
def test(*args, **kwargs):
book_dict = fn(*args, **kwargs)
with open('../files/user_book.txt', 'w', encoding='utf-8') as file_book_name:
file_book_name.write(json.dumps(book_dict))
print('1.继续')
print('2.返回')
while True:
case = int(input('请选择1~2:'))
if case == 1:
test(book_dict, args[1])
elif case == 2:
interface(book_dict, args[1])
else:
print('抱歉选择有误,请重新选择')
return test
def file_read():
with open('../files/user_book.txt', 'r', encoding='utf-8') as file_user_book:
books_dict = json.loads(file_user_book.read())
return books_dict
# 主界面
def main_interface(user_dict: dict):
with open('../files/page.txt', 'r', encoding='utf-8') as page_file:
interface_page = page_file.read()
print(interface_page)
while True:
case = int(input('请选择1 - 3'))
if case == 1:
user_login(user_dict)
elif case == 2:
user_register(user_dict)
elif case == 3:
quits()
else:
print('抱歉选择有误,请重新选择')
print(interface_page)
# 登录功能
def user_login(user_login_dict: dict):
user_name = input('请输入用户名:')
user_password = input('请输入密码:')
while True:
# 对用户名和密码的判断,若正确则进入个人界面,并将起用户图书传入个人界面
if user_name in user_login_dict and user_login_dict[user_name] == user_password:
print('登录成功')
interface(file_read(), user_name)
break
elif user_name not in user_login_dict:
print('用户名错误,请重新输入用户名和密码')
user_name = input('用户名:')
user_password = input('密码:')
elif user_name in user_login_dict and user_login_dict[user_name] != user_password:
print('密码错误,请重新输入用户名和密码')
user_name = input('用户名:')
user_password = input('密码:')
# 注册界面
def user_register(user_register_dict: dict):
user_name = input('用户名:')
user_password = input('密码:')
while True:
if 3 <= len(user_name) <= 5:
if user_name not in user_register_dict:
if len(user_password) > 6:
# 将满足条件的用户及密码写入user.txt文件
user_register_dict[user_name] = user_password
with open('../files/user.txt', 'w', encoding='utf-8') as file_user:
file_user.write(json.dumps(user_register_dict))
# 读入user_book.txt ,将其保存在字典user_books_dict
with open('../files/user_book.txt', 'r', encoding='utf-8') as file_user_book:
user_books_dict = json.loads(file_user_book.read())
# 为该字典添加对应的用户并写入user_book中
user_books_dict[user_name] = []
with open('../files/user_book.txt', 'w', encoding='utf-8') as file_user:
file_user.write(json.dumps(user_books_dict))
main_interface(user_register_dict)
break
else:
print('密码长度不足六位,请重新注册')
user_name = input('用户名:')
user_password = input('密码:')
else:
print('该用户名已被注册,请重新注册')
user_name = input('用户名:')
user_password = input('密码:')
else:
print('该用户名长度不满足条件,请重新注册')
user_name = input('用户名:')
user_password = input('密码:')
# 退出函数
def quits():
exit()
# 个人界面
def interface(books: dict, user_name: str):
print('==============================')
print('❀❀欢迎{}来到您的个人界面'.format(user_name))
print('❤ 1.添加图书')
print('❤ 2.查询图书')
print('❤ 3.修改图书信息')
print('❤ 4.删除图书')
print('❤ 5.返回')
print()
print('==============================')
while True:
case = int(input('请选择1-5:'))
if case == 1:
add(books, user_name)
elif case == 2:
select(books, user_name)
elif case == 3:
modify(books, user_name)
elif case == 4:
delete(books, user_name)
elif case == 5:
backtrack(books)
else:
print('抱歉选择有误,请重新选择')
interface(books, user_name)
# 添加功能
@file_ecorator
def add(book: dict, user_name: str):
book_name = input('请输入图书的名字:')
book[user_name].append(book_name)
print('添加成功')
return book
# 查询功能
def select(book: dict, user_name: str):
print('{}有以下书籍:'.format(user_name))
for book_se in book[user_name]:
print(book_se)
print('\n')
print('查询成功')
print('1.返回')
case = int(input('是否返回:'))
if case == 1:
interface(book, user_name)
# 修改功能
@file_ecorator
def modify(book: dict, user_name: str):
length = len(book[user_name])
# 查询
print('{}有以下书籍:'.format(user_name))
for book_se in book[user_name]:
print(book_se)
print('\n')
# 修改
input_name = input('请输入您要修改的图书名:')
for i in range(length):
if book[user_name][i] == input_name:
modify_name = input('请修改图书名:')
book[user_name][i] = modify_name
print('修改成功')
for book_se in book[user_name]:
print(book_se)
print('\n')
break
else:
print('该书不存在')
return book
# 删除功能
@file_ecorator
def delete(book: dict, user_name: str):
input_name = input('请输入您要删除的图书名:')
for delete_name in book[user_name]:
if delete_name == input_name:
book[user_name].remove(input_name)
print('删除成功')
for book_se in book[user_name]:
print(book_se)
print('\n')
break
else:
print('该书不存在')
return book
# 返回个人界面
def backtrack(book: dict):
main_interface(book)
def main():
with open('../files/user.txt', 'r', encoding='utf-8') as user_file:
user = json.loads(user_file.read())
main_interface(user)
# 入口
if __name__ == '__main__':
main()
图书与用户结构
用户,密码结构