You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
python-project/library/library.py

125 lines
5.1 KiB

import json
import time
import hashlib
class Library():
def __init__(self):
self.loginStats = 0
self.loginUser = None
self.password = None
self.now = time.strftime('%F %X')
def hashPass(self,password):
t = hashlib.sha256(password.encode('utf-8'))
return t.hexdigest()
def write_json(self, name, dict):
with open(name, 'w') as f:
json.dump(dict, f, indent='\t', ensure_ascii=False)
def read_json(self, name):
with open(name, 'r') as f:
data = json.load(f)
return data
def regUser(self, u):
self.write_json('./data.json', u)
self.write_json(f'./{self.loginUser}', {'regTime': self.now,'book':{}})
def login(self):
data = self.read_json('data.json')
if self.loginUser in data['user']:
if data['user'][self.loginUser] == self.hashPass(self.password):
return True
def borrowBook(self, bookName):
data = self.read_json('data.json')
my_data = self.read_json(self.loginUser)
if bookName in data['book']:
del data['book'][bookName]
my_data['book'][bookName] = self.now
self.write_json('data.json', data)
self.write_json(self.loginUser, my_data)
return True
def backBook(self, bookName):
my_data = self.read_json(self.loginUser)
data = self.read_json('data.json')
if bookName in my_data['book']:
del my_data['book'][bookName]
data['book'][bookName] = self.now
self.write_json('data.json', data)
self.write_json(self.loginUser, my_data)
return True
def pushBook(self, books):
if books:
s = {}.fromkeys(books, self.now)
data = self.read_json('data.json')
data['book'].update(s)
self.write_json('data.json', data)
def main(self):
try:
while True:
num = input('''(1) 注册\n(2) 登录\n(3) 借书\n(4) 还书\n(5) 上架\n(6) 注销\n(0) 退出\n请输入编号:''')
if num == '2':
if self.loginStats==1: print('用户已登录');continue
self.loginUser, self.password = input('请输入账号:'), input('请输入密码:')
s = self.login()
if not s:
print('密码错误')
else:
print('登录成功')
self.loginStats = 1
elif num == '1':
if self.loginStats==1: print('用户已登录');continue
data = self.read_json('data.json')
self.loginUser, self.password, password = input('请输入账号:'), input('请输入密码:'), input('请在此输入密码:')
if self.loginUser in data['user']: print('用户已存在')
else: data['user'][self.loginUser] = self.hashPass(self.password);self.regUser(data);print('创建成功');self.loginStats = 1
elif num == '3':
if self.loginStats==0: print('用户未登录');continue
print('书籍列表:')
for k,v in self.read_json('data.json')['book'].items():
print(f'名称:{k}',f'时间:{v}',sep='\t\t')
bn = input('请输入书籍名称:')
if self.borrowBook(bn): print('借书成功')
else: print('输入错误,请检查')
elif num == '4':
if self.loginStats==0: print('用户未登录');continue
if not self.read_json(self.loginUser)['book']:
print('没有借阅任何书籍')
continue
print('我的书架:')
for k,v in self.read_json(self.loginUser)['book'].items():
print(f'名称:{k}',f'时间:{v}',sep='\t\t')
bn = input('请输入书籍名称:')
if self.backBook(bn): print('还书成功')
else: print('输入错误,请检查')
elif num == '5':
if self.loginUser!='admin001': print('管理员用户请自行初始化');continue
data = self.read_json('data.json')
blist = input('请输入书籍名称(多本书,请使用“,”隔开 ):').split(',')
data['book'].update({}.fromkeys(blist,self.now))
self.write_json('data.json',data)
print('上架成功')
elif num == '6':
if self.loginStats==0: print('用户未登录');continue
print(f'{self.loginUser}已注销');self.loginUser = None
elif num == '0':break
else: print('输入错误,请重新输入')
except StopIteration as e:
print(e)
finally:
print('欢迎下次光临')
if __name__ == '__main__':
t = Library()
t.main()
# 管理员用户上架图书
# t = Library()
# t.loginUser = 'admin001'
# t.main()