赞
踩
import redis
# 定义 Redis 中保存图书信息的哈希键名
BOOKS_HASH_KEY = "books"
# 创建 Redis 连接
redis_rs = redis.Redis(host='127.0.0.1', port=6379)#
#一级菜单
- def zhucaidan():
- print("-------------------------图 书 管 理 系 统-------------------------")
- while True:
- print("请输入您的选择:")
- print("1、登录")
- print("2、注册")
- print("3、退出")
-
- option = input("请输入您要执行的操作:\n")
-
- if option == "1":
- login()
-
- elif option == "2":
- register()
-
- elif option == "3":
- print("已弹出程序!!!")
- break
- else:
- print("无效选项,请重新输入!\n")
-
- # 登录模块
- def login():
- print("------------------------登录模块------------------------")
- username = input("请输入用户名:")
- password = input("请输入密码:")
- # 从 Redis 中获取保存的用户密码
- sPassword = redis_rs.hget("users", username)
- if sPassword is None:
- print("用户不存在,请先注册!")
- return
- elif password != sPassword.decode():
- print("密码不正确,请重新输入!")
- return
-
- print("登录成功,欢迎尊贵的超级VIP " + username + " 用户!")
- ER_caidan()
# 注册模块
- def register():
- print("-----注册------")
- username = input("请输入用户名:")
- password = input("请输入密码:")
- # 使用 Redis 的哈希集合功能存储用户和密码
- result = redis_rs.hsetnx("users", username, password)
-
- if result == 0:
- print("用户名已经存在,请重新输入!")
- else:
- print("注册成功!")
# 二级菜单
- def ER_caidan():
- while True:
- print()
- print("--------------------二级菜单--------------------")
- print("1. 添加图书")
- print("2. 删除图书")
- print("3. 修改图书信息")
- print("4. 显示所有图书")
-
- print("5. 借书")
- print("6. 还书")
-
- print("7. 返回上级")
- print("8. 删库跑路!!!")
- print("9. 退出")
-
- erji = input("请输入您的要执行的操作:\n")
-
- if erji == "1":
- TJ_TS()
-
- elif erji == "2":
- SC_TC()
-
- elif erji == "3":
- XG_TS_XX()
-
- elif erji == "4":
- XS_TS_XX()
-
- elif erji == "5":
- JS_XT()
-
- elif erji == "6":
- HS_XT()
-
- elif erji == "7":
- print("已返回上级菜单")
- continue
-
- elif erji == "8":
- print("你已执行销毁数据数据库!")
- redis_rs.flushall()
- break
-
- elif erji == "9":
- print("已退出程序!!!!")
- return
- else:
- print("无效选项,请重新输入!\n")
# 添加图书
- def TJ_TS():
- print("添加图书")
- ts_id = input("请输入图书ID:")
- title = input("请输入图书标题:")
- author = input("请输入图书作者:")
-
- existingBook = redis_rs.hget(BOOKS_HASH_KEY, ts_id)
-
- if existingBook is not None:
- print("该图书已经存在,请勿重复添加!")
- return
-
- redis_rs.hset(BOOKS_HASH_KEY, ts_id, title + "|" + author + "|可借阅")
- print("图书添加成功!")
# 删除图书
- def SC_TC():
- print("删除图书")
- ts_id = input("请输入要删除的图书ID:")
-
- result = redis_rs.hdel(BOOKS_HASH_KEY, ts_id)
- if result == 0:
- print("该图书不存在,请重新输入!")
- else:
- print("图书删除成功!")
# 修改图书信息
- def XG_TS_XX():
- print("修改图书信息")
- ts_id = input("请输入要修改的图书信息的图书ID:")
- existingBook = redis_rs.hget(BOOKS_HASH_KEY, ts_id)
- if existingBook is None:
- print("该图书并不存在,请重新输入!!!!")
- return
-
- print("请输入更新后的标题(空值代表不用修改!!!)")
- newTitle = input()
- print("请输入更新后的作者(空值代表不用修改!!!)")
- newAuthor = input()
- print("请输入更新后图书的可用状态(可借阅、已借出。空值代表不用修改!!!)")
- newStat = input()
-
- bookInfo = existingBook.decode().split("|")
- title = bookInfo[0]
- author = bookInfo[1]
- stat = bookInfo[2]
-
- if newTitle != "":
- title = newTitle
- if newAuthor != "":
- author = newAuthor
- if newStat != "":
- stat = newStat
-
- redis_rs.hset(BOOKS_HASH_KEY, ts_id, title + "|" + author + "|" + stat)
- print("图书信息修改成功!")
# 显示图书信息
- def XS_TS_XX():
- print("显示图书信息")
- books = redis_rs.hgetall(BOOKS_HASH_KEY)
- if not books:
- print("暂无图书信息")
- return
- for ts_id, book_info in books.items():
- book_info = book_info.decode()
- title, author, stat = book_info.split("|")
- print("图书ID: " + ts_id.decode() + ", 标题: " + title + ", 作者: " + author + ", 可用状态: " + stat)
# 借书
- def JS_XT():
- print("借书")
- ts_id = input("请输入要借阅的图书ID:")
- book_info = redis_rs.hget(BOOKS_HASH_KEY, ts_id)
- if book_info is None:
- print("该图书不存在,请重新输入!")
- return
-
- book_info = book_info.decode()
- title, author, stat = book_info.split("|")
- if stat == "可借阅":
- redis_rs.hset(BOOKS_HASH_KEY, ts_id, title + "|" + author + "|已借出")
- print("借书成功!")
- else:
- print("很抱歉,该图书已被借出!")
# 还书
- def HS_XT():
- print("还书")
- ts_id = input("请输入要归还的图书ID:")
- book_info = redis_rs.hget(BOOKS_HASH_KEY, ts_id)
- if book_info is None:
- print("该图书不存在,请重新输入!")
- return
-
- book_info = book_info.decode()
- title, author, stat = book_info.split("|")
- if stat == "已借出":
- redis_rs.hset(BOOKS_HASH_KEY, ts_id, title + "|" + author + "|可借阅")
- print("还书成功!")
- else:
- print("该图书已入库不需要归还!")
if __name__ == '__main__':
zhucaidan()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。