作业需求:
# 1. 员工信息表程序,实现增删改查操作:
# 2. 可进行模糊查询,语法至少支持下面3种:
# select name,age from staff_table where age > 22
# select * from staff_table where dept = "IT"
# select * from staff_table where enroll_date like "2013"
# 查到的信息,打印后,最后面还要显示查到的条数
# 3. 可创建新员工纪录,以phone做唯一键,staff_id需自增
# 4. 可删除指定员工信息纪录,输入员工id,即可删除
# 5. 可修改员工信息,语法如下:
# UPDATE staff_table SET dept="Market" where dept = "IT"
# 6. 注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码
import json
import os
#打印框架
def kuang_jia(dress):
print('员工信息表'.center(90))
print('输入相应的序号获得相应功能: ', '1.查看'.center(5), '2.增加'.center(5), '3.删除'.center(5), '4.修改'.center(5),
'5.查找'.center(5), '6.退出'.center(5))
print('-'*90)
print('|', 'Number'.center(10), 'Id'.center(10), 'Name'.center(10), 'Age'.center(10),
'Phone'.center(20), 'Dept'.center(10), 'Enorll_date'.center(10), '|')
try:
with open(dress, 'r', encoding='utf-8') as f:
for line in f:
ret = json.loads(line)
print('|', ret[0].center(10), ret[1].center(10), ret[2].center(10),
ret[3].center(10), ret[4].center(20), ret[5].center(10), ret[6].center(10), '|')
except FileNotFoundError:
f = open(dress, 'w', encoding='utf-8')
f.close()
print('='*90)
print('输入相应的序号获得相应功能: ', '1.查看'.center(5), '2.增加'.center(5), '3.删除'.center(5), '4.修改'.center(5),
'5.查找'.center(5), '6.退出'.center(5))
print('=' * 90)
#增加
def add(dress):
while 1:
print('输入q退出[增加]功能!')
l = []
id = input('Id: ')
if id == 'q':
break
name = input('Name: ')
if name == 'q':
break
age = input('Age: ')
if age == 'q':
break
phone = input('Phone: ')
if phone == 'q':
break
dept = input('Dept: ')
if dept == 'q':
break
enorll_date = input('Enorll_date: ')
if enorll_date == 'q':
break
with open(dress, 'r', encoding='utf-8') as f:
lines = f.readlines()
l.append(str(len(lines)))
l.append(id)
l.append(name)
l.append(age)
l.append(phone)
l.append(dept)
l.append(enorll_date)
with open(dress, 'a', encoding='utf-8') as f:
ret = json.dumps(l)
f.write(ret + '\n')
#删除
def dele(dress):
number = input('Number: ')
with open(dress, 'r', encoding='utf-8') as f, open('file.bak', 'w', encoding='utf-8') as f1:
for line in f:
ret = json.loads(line)
if number != ret[0]:
f1.write(line)
os.remove(dress)
os.rename('file.bak', dress)
#修改
def changes(dress):
with open(dress, 'r', encoding='utf-8') as f, open('file.bak', 'w', encoding='utf-8') as f1:
number = input('Number: ')
for line in f:
ret = json.loads(line)
if number == ret[0]:
print('|', 'Number'.center(10), 'Id'.center(10), 'Name'.center(10), 'Age'.center(10),
'Phone'.center(20), 'Dept'.center(10), 'Enorll_date'.center(10), '|')
print('|', ret[0].center(10), ret[1].center(10), ret[2].center(10),
ret[3].center(10), ret[4].center(20), ret[5].center(10), ret[6].center(10), '|')
while 1:
print('输入q退出[修改]')
can_shu = input('输入要修改的参数: ').lower().strip()
info = input('您希望修改成: ')
if can_shu == 'id':
ret[1] = info
break
elif can_shu == 'name':
ret[2] = info
break
elif can_shu == 'age':
ret[3] = info
break
elif can_shu == 'phone':
ret[4] = info
break
elif can_shu == 'dept':
ret[5] = info
break
elif can_shu == 'enorll_date':
ret[6] = info
break
elif can_shu == 'q':
break
else:
print('输入有误,请重新输入!')
continue
f1.write(json.dumps(ret))
else:
f1.write(line)
os.remove(dress)
os.rename('file.bak', dress)
#查找
def find(dress):
while 1:
can_shu = input('输入您要查询的参数: ').lower().strip()
if can_shu not in 'nameageidphonedeptenroll_date':
print('输入有误,请重新输入!')
continue
else:
break
info = input('输入您要查询的参数值: ')
with open(dress, 'r', encoding='utf-8') as f:
num = 0
print('|', 'Number'.center(10), 'Id'.center(10), 'Name'.center(10), 'Age'.center(10),
'Phone'.center(20), 'Dept'.center(10), 'Enorll_date'.center(10), '|')
for line in f:
ret = json.loads(line)
if can_shu == 'id':
if info == ret[1]:
print('|', ret[0].center(10), ret[1].center(10), ret[2].center(10),
ret[3].center(10), ret[4].center(20), ret[5].center(10), ret[6].center(10), '|')
num += 1
elif can_shu == 'name':
if info == ret[2]:
print('|', ret[0].center(10), ret[1].center(10), ret[2].center(10),
ret[3].center(10), ret[4].center(20), ret[5].center(10), ret[6].center(10), '|')
num += 1
elif can_shu == 'age':
if info == ret[3]:
print('|', ret[0].center(10), ret[1].center(10), ret[2].center(10),
ret[3].center(10), ret[4].center(20), ret[5].center(10), ret[6].center(10), '|')
num += 1
elif can_shu == 'phone':
if info == ret[4]:
print('|', ret[0].center(10), ret[1].center(10), ret[2].center(10),
ret[3].center(10), ret[4].center(20), ret[5].center(10), ret[6].center(10), '|')
num += 1
elif can_shu == 'dept':
if info == ret[5]:
print('|', ret[0].center(10), ret[1].center(10), ret[2].center(10),
ret[3].center(10), ret[4].center(20), ret[5].center(10), ret[6].center(10), '|')
num += 1
elif can_shu == 'enorll_date':
if info == ret[6]:
print('|', ret[0].center(10), ret[1].center(10), ret[2].center(10),
ret[3].center(10), ret[4].center(20), ret[5].center(10), ret[6].center(10), '|')
num += 1
print('共找到{}条相关信息。'.format(num))
print('输入相应的序号获得相应功能: ', '1.查看'.center(5), '2.增加'.center(5), '3.删除'.center(5), '4.修改'.center(5),
'5.查找'.center(5), '6.退出'.center(5))
#自动编号
def bian_hao(dress):
with open(dress, 'r', encoding='utf-8') as f, open('file.bak', 'w', encoding='utf-8') as f1:
lines = f.readlines()
f.seek(0)
for line in f:
ret = json.loads(line)
ret[0] = str(lines.index(line))
f1.write(json.dumps(ret) + '\n')
os.remove(dress)
os.rename('file.bak', dress)
#主程序
def main():
dress = input('输入相对位置下需要导入的文件名: ').strip()
bian_hao(dress)
kuang_jia(dress)
while 1:
want = input('输入你想要进行的操作: ').strip()
if want == '1':
kuang_jia(dress)
elif want == '2':
add(dress)
kuang_jia(dress)
elif want == '3':
dele(dress)
bian_hao(dress)
kuang_jia(dress)
elif want == '4':
changes(dress)
kuang_jia(dress)
elif want == '5':
find(dress)
elif want == '6':
break
main()