dataProcessing.py --> 数据读写方法的封装
infoManage.py --> 信息管理(增删查改)方法的封装
Interface.py --> 所需页面的封装
log --> 登录与注册功能的封装
main --> 程序主入口即对各个方法的调用
stuInfo --> 保存数据的json文件
按书写顺序贴出代码:
1、main.py
# -*- coding: utf-8 -*-
# @Time : 2018/7/28 8:39
# @Author : G.Hope
# @Email : 1638327522@qq.com
# @File : main.py
# @Software: PyCharm
"""
1.分析数据结构
学生管理系统管理学生 ---> 管理的是多个学生 ---> 需要容器储存学生 ---> 考虑使用那种容器
---> 列表?字典? ---> 系统对应的大的容器是字典 --->字典中的某一个key对应的值是所有的学生(用列表作为容器)
--->每一个学生是一个字典
[[学生],'GHope','123456',123,'描述']
{'student':[学生],'user_name':'GHope','pass_word':'123456'}
{'student':[学生1,学生2,学生3...],''}
添加多个用户管理功能:每一个登录的用户只能查看自己录入保存的学生信息,改变数据结构。
最外层依然为一个大字典,里面包含多个字典,每一个字典对应一位用户。使用用户名作为key值。
此时需要满足用户名唯一的设置,最外层除了用户信息外,还应该包括版本信息、帮助文档等一些帮助用户使用系统的相关文档。
所以大致数据结构设置如下:
{'user_name':{'pwd':'root','stu_name':'Tom','stu_id':'py180501','sex':'男’,’score':{'Python':56,'C语言':67,
'国学基础':78,'体育':89},'user_name1':{'pwd':'123456','stu_name':'Lucy','stu_id':'py180502','sex':'女’,
’score':{'Python':66,'C语言':57,'国学基础':98,'体育':69},'version_info':'V2018.1.1.0','help':''}
"""
"""
因为需要用到的key值比较多,所以在需要用到的相关模块中分别声明为全局变量。
在此只需导入模块即可。
"""
import Interface
import log
import infoManage
while True:
Interface.interface_login()
log_control = int(input('\t请输入你的选择:'))
if log_control == 1:
log_result = log.login()
if log_result:
while True: # while循环的嵌套可以实现多个用户之间的切换
Interface.interface_main()
main_control = int(input('\t请输入你的选择:'))
if main_control == 1:
Interface.interface_main_find()
infoManage.stu_inquire(log_result)
elif main_control == 2:
infoManage.stu_add(log_result)
elif main_control == 3:
Interface.interface_main_update()
infoManage.stu_update(log_result)
elif main_control == 4:
Interface.interface_main_del()
infoManage.stu_del(log_result)
elif main_control == 5:
Interface.interface_main_version()
# infoManage.stu_version()
elif main_control == 6:
Interface.interface_main_help()
# infoManage.stu_help()
elif main_control == 7:
break
else:
print("请根据提示,输入系统可以辨认的值!!!")
else:
break
if log_control == 2:
if log.logon():
continue
break
if log_control == 3:
break
2、Interface.py
# -*- coding: utf-8 -*-
# @Time : 2018/7/28 9:08
# @Author : G.Hope
# @Email : 1638327522@qq.com
# @File : Interface.py
# @Software: PyCharm
#相关界面设计
def interface_login():
print("++++++++++++++++++++++++++++++++++++++++++++++")
print("+ 学生信息管理系统 +")
print("+ 【1】登陆 +")
print("+ 【2】注册 +")
print("+ 【3】退出 +")
print("+++++++++++++++++++++++++++++++++++++++++++++++\n")
def interface_main():
print("++++++++++++++++++++++++++++++++++++++++++++++")
print("+ 学生信息管理系统 +")
print("+ 【1】查询学生信息 +")
print("+ 【2】添加学生信息 +")
print("+ 【3】修改学生信息 +")
print("+ 【4】删除学生信息 +")
print("+ 【5】查看版本信息 +")
print("+ 【6】浏览帮助文档 +")
print("+ 【7】退出登陆 +")
print("++++++++++++++++++++++++++++++++++++++++++++++\n")
def interface_main_find():
print("+++++++++++++++++++++++++++++++++++++++++++++++")
print("+ 学生信息管理系统 +")
print("+ 【1】按姓名查找 +")
print("+ 【2】按学号查找 +")
print("+ 【3】查看全部 +")
print("+ 【4】返回上一级 +")
print("+++++++++++++++++++++++++++++++++++++++++++++++\n")
def interface_main_update():
print("++++++++++++++++++++++++++++++++++++++++++++++")
print("+ 学生信息管理系统 +")
print("+ 【1】按学号修改 +")
print("+ 【2】按姓名修改 +")
print("+ 【3】返回上一级 +")
print("++++++++++++++++++++++++++++++++++++++++++++++\n")
def interface_main_del():
print("++++++++++++++++++++++++++++++++++++++++++++++")
print("+ 学生信息管理系统 +")
print("+ 【1】按学号删除 +")
print("+ 【2】按姓名删除 +")
print("+ 【3】返回上一级 +")
print("++++++++++++++++++++++++++++++++++++++++++++++\n")
def interface_main_version():
print("++++++++++++++++++++++++++++++++++++++++++++++")
print("+ 学生信息管理系统版本信息 +")
print("+ 创建时间:2018.7-2018.8 +")
print("+ 创建人:GHope +")
print("+ 版本号:V2018.1.1.0 +")
print("++++++++++++++++++++++++++++++++++++++++++++++\n")
emm = input("请输入任意键返回上一层")
def interface_main_help():
print("++++++++++++++++++++++++++++++++++++++++++++++++")
print("+ 学生信息管理系相关说明 +")
print("+ 本系统仅供学习参考使用,如需转载,请注明出处。 +")
print("+ 关于数据结构及其功能的简单说明:1、本系统为多用+")
print("+户管理系统。每个用户仅能看到自己录入的信息。且自己+")
print("+录入的信息不被其它人所共享。系统中共享信息仅包括系统+")
print("+版本信息及此说明文档。关于数据结构的信息在代码注释中+")
print("+有相关解释。如有疑问,请阅读相关代码。如不能解决,请+")
print("+通过1638327522@qq.com联系作者,期待你的反馈。 +")
print("+++++++++++++++++++++++++++++++++++++++++++++++++\n")
emm = input("请输入任意键返回上一层")
if __name__ == '__main__':
pass
3、dataProcessing.py
# -*- coding: utf-8 -*-
# @Time : 2018/7/28 10:51
# @Author : G.Hope
# @Email : 1638327522@qq.com
# @File : dataProcessing.py
# @Software: PyCharm
import json
def data_read(read_path):
try:
with open(read_path, 'r', encoding='utf-8') as f:
result = json.load(f)
# print(result)
return result
except FileNotFoundError:
print('文件不存在!!!')
def data_write(write_path, write_body):
with open(write_path, 'w', encoding='utf-8') as f:
json.dump(write_body, f)
# print('本地信息录入成功!!!')
# data_read( './stuInfo')
# 注释代码为测试模块功能所使用
if __name__ == '__main__':
pass
4、log.py
# -*- coding: utf-8 -*-
# @Time : 2018/7/28 8:43
# @Author : G.Hope
# @Email : 1638327522@qq.com
# @File : log.py
# @Software: PyCharm
import dataProcessing
path = './stuInfo'
def login(): # 登录功能实现
date = dataProcessing.data_read(path)
if not date:
print("暂无用户,请注册!!!")
return
else:
user_name = input('请输入用户名:')
count = 5
while True:
pwd = input("请输入密码:")
if user_name not in date:
print("输入的用户不存在")
break
elif pwd == date[user_name]["pwd"]:
print('登录成功,请开始你的操作!')
return user_name
else:
count -= 1
if count > 0:
print("密码错误,请重新输入。还能输入%d次" % count)
else:
break
def logon(): # 注册功能实现
data = dataProcessing.data_read(path)
if not data:
data = {}
while True:
user_name = input("请输入用户名:")
if user_name not in data:
key = input("请输入密码:")
key2 = input("请再次输入密码:")
if key == key2 and key != "":
data[user_name] = {"pwd": key,"body":{}}
dataProcessing.data_write(path, data)
print("注册成功")
break
elif key == "":
print("密码不能为空,请重新输入")
else:
print("两次密码不相同,请重新输入密码")
else:
print("该用用户名已经被注册,请重新输入")
return True
if __name__ == '__main__':
pass
5、infoManage.py
# -*- coding: utf-8 -*-
# @Time : 2018/7/28 10:05
# @Author : G.Hope
# @Email : 1638327522@qq.com
# @File : infoManage.py
# @Software: PyCharm
import dataProcessing
path = './stuInfo'
data_all = dataProcessing.data_read(path)
def stu_inquire(user_name):
while True:
inquire_control = int(input('请输入你的选择:'))
if inquire_control == 4:
break
data_current = data_all[user_name]
if inquire_control == 1:
inquire_name = input("请输入你想要查找的姓名:")
for stu_info in data_current["body"]:
if inquire_name == stu_info["stu_name"]:
print(stu_info)
elif inquire_control == 2:
inquire_id = input('请输入你想查找的学号:')
for stu_info in data_current["body"]:
if inquire_id == stu_info["stu_id"]:
print(stu_info)
elif inquire_control == 3:
print(data_current)
else:
print('请输入合法的操作提示:')
def stu_add(user_name):
data_current = data_all[user_name]
name = input("请输入添加学生的姓名:")
id = input("请输入添加学生的学号:")
sex = input("请输入添加学生的性别:")
score = {}
py = input("请输入该生的python成绩:")
c = input("请输入该生的C语言成绩:")
Chinaese_base = input("请输入该生的国学基础成绩:")
pe = input("请输入该生的体育成绩:")
data_current["body"][id] = {"姓名": name, "学号": id, "性别": sex, "成绩": score}
data_current["body"][id][score] = {"python成绩":py,"C语言成绩":c,"国学基础成绩":Chinaese_base,"体育成绩":pe}
dataProcessing.data_write(path,data_current)
print('恭喜你,添加成功!!!')
def stu_update(user_name):
while True:
update_control = int(input('请输入你的选择:'))
if update_control == 3:
break
data_current = data_all[user_name]
if update_control == 1:
update_name = input("请输入你想要修改的姓名:")
for stu_info in data_current["body"]:
if update_name == stu_info["stu_name"]:
print(stu_info)
if input("你确定要修改这个学生的信息吗?确认按y,取消按任意键") == "y":
name = input("请输入添加学生的姓名:")
id = input("请输入添加学生的学号:")
sex = input("请输入添加学生的性别:")
score = {}
py = input("请输入该生的python成绩:")
c = input("请输入该生的C语言成绩:")
Chinaese_base = input("请输入该生的国学基础成绩:")
pe = input("请输入该生的体育成绩:")
data_current["body"][update_name] = {"姓名": name, "学号": id, "性别": sex, "成绩": score}
data_current["body"][update_name][score] = {"python成绩": py, "C语言成绩": c, "国学基础成绩": Chinaese_base,
"体育成绩": pe}
dataProcessing.data_write(path, data_current)
print('恭喜你,修改成功!!!')
elif update_control == 2:
update_id = input('请输入你想要修改的学号:')
for stu_info in data_current["body"]:
if update_id == stu_info["stu_id"]:
if input("你确定要修改这个学生的信息吗?确认按y,取消按任意键") == "y":
name = input("请输入添加学生的姓名:")
id = input("请输入添加学生的学号:")
sex = input("请输入添加学生的性别:")
score = {}
py = input("请输入该生的python成绩:")
c = input("请输入该生的C语言成绩:")
Chinaese_base = input("请输入该生的国学基础成绩:")
pe = input("请输入该生的体育成绩:")
data_current["body"][update_id] = {"姓名": name, "学号": id, "性别": sex, "成绩": score}
data_current["body"][update_id][score] = {"python成绩": py, "C语言成绩": c, "国学基础成绩": Chinaese_base,
"体育成绩": pe}
dataProcessing.data_write(path, data_current)
print('恭喜你,修改成功!!!')
def stu_del(user_name):
while True:
del_control = int(input('请输入你的选择:'))
if del_control == 3:
break
data_current = data_all[user_name]
if del_control == 1:
del_name = input("请输入你想要删除的姓名:")
for stu_info in data_current["body"]:
if del_name == stu_info["stu_name"]:
print(stu_info)
if input("你确定要删除这个学生的信息吗?确认按y,取消按任意键") == "y":
del data_current["body"][del_name]
dataProcessing.data_write(path, data_current)
print('恭喜你,修改成功!!!')
elif del_control == 2:
del_id = input('请输入你想要修改的学号:')
for stu_info in data_current["body"]:
if del_id == stu_info["stu_id"]:
if input("你确定要删除这个学生的信息吗?确认按y,取消按任意键") == "y":
del data_current["body"][del_id]
dataProcessing.data_write(path, data_current)
print('恭喜你,删除成功!!!')
# def stu_version():
# n = input("请输入任意键返回上一层!")
# def stu_help():
# m = input("请输入任意键返回上一层!")
if __name__ == '__main__':
pass
总结:整体感觉还是对数据的操作还是不太熟悉,逻辑多了感觉有些绕。代码写的少了吧!