写一个学生管理系统
test.py
from admin import Admin
admin = Admin()
admin.begin_win()
admin.py
import json
import time
class Student:
def __init__(self):
self.name = None
self.age = None
self.phone = None
class Admin:
def __init__(self):
self.id = None
self.name = None
self.psw = None
self.adm_in = None
# self.students = []
self.stu = Student()
def begin_win(self):
print('=========================学生管理系统界面=========================')
print('\t\t1.登录')
print('\t\t2.注册')
print('\t\t3.退出')
print('==================================================================')
try:
self.adm_in = int(input('请输入您的选择:'))
self.begin_choices()
except ValueError:
print("输入有误,请重新输入••••••")
time.sleep(1)
self.begin_win()
def begin_choices(self):
while True:
if self.adm_in == 1:
self.adm_log()
self.draw_func_win()
elif self.adm_in == 2:
with open('admins.json', 'r', encoding='utf-8') as f:
content = f.read()
data = json.loads(content)
print('waiting••••••')
time.sleep(1)
admin_name2 = input('您的名字(2~4):').replace(' ', '')
name2_len = len(admin_name2)
if not 2 <= name2_len <= 4:
print("名字在2~4,请重试••••••")
time.sleep(1)
self.begin_choices()
elif admin_name2 in data:
print("该用户存在,请重新输入••••••")
time.sleep(1)
self.begin_choices()
adm_pwd2 = input('输入密码(5~10):').replace(' ', '')
pwd2_len = len(adm_pwd2)
if not 5 <= pwd2_len <= 10:
print("密码在5~10位,请重试••••••")
time.sleep(1)
self.begin_choices()
adm_re_pwd2 = input('再次确认:').replace(' ', '')
if adm_pwd2 != adm_re_pwd2:
print('密码不一致,请重试••••••')
time.sleep(1)
self.begin_choices()
data[admin_name2] = {}
data[admin_name2]["pwd"] = adm_pwd2
with open('admins.json', 'w', encoding='utf-8') as f1:
data_json = json.dumps(data)
f1.write(data_json)
print('注册成功(* ̄︶ ̄),返回开始界面••••••')
time.sleep(1)
self.begin_win()
elif self.adm_in == 3:
print('exit..')
else:
print('输入命令错误,请重新输入••••••')
time.sleep(1)
self.begin_win()
break
def adm_log(self):
with open('admins.json', 'r', encoding='utf-8') as f:
content = f.read()
admins_users = json.loads(content)
is_loop = True
while is_loop:
self.name = input('请输入用户名:').replace(' ', '')
self.psw = input('请输入密码:').replace(' ', '')
if self.name in admins_users:
if self.psw == admins_users[self.name]["pwd"]:
print('登陆成功!')
is_loop = False
else:
print('密码不正确••••••')
time.sleep(1)
back = input('是否退出?y:退出').replace(' ', '')
if back == 'y':
self.begin_win()
else:
print('用户名错误....')
time.sleep(1)
back = input('是否退出?y:退出').replace(' ', '')
if back == 'y':
self.begin_win()
def draw_func_win(self):
while True:
print('=========================学生管理=========================')
print('❀❀❀欢迎', self.name, ':')
print('\t\t1.添加学生')
print('\t\t2.查看学生')
print('\t\t3.修改学生信息')
print('\t\t4.删除学生')
print('\t\t5.返回')
print('==========================================================')
try:
self.adm_in = int(input('您想进行的操作是:'))
self.stu_operate()
break
except ValueError:
print('输入错误,请重输••••••')
time.sleep(1)
continue
def stu_operate(self):
if self.adm_in == 1:
self.add_stu()
elif self.adm_in == 2:
self.search_stu()
elif self.adm_in == 3:
self.change_info()
elif self.adm_in == 4:
self.del_stu()
elif self.adm_in == 5:
self.begin_win()
else:
print('输入命令错误,请重新输入••••••')
time.sleep(1)
self.draw_func_win()
def add_stu(self):
while True:
self.stu.name = input('请输入学生的姓名:').replace(' ', '')
try:
self.stu.age = int(input('请输入学生的年龄:'))
except ValueError:
print('输入错误,重新输入')
continue
self.stu.phone = input('请输入学生的电话号码:').replace(' ', '')
if len(self.stu.phone) != 11:
print('手机号输入错误,请重输入••••••')
time.sleep(1)
continue
now_time = time.time()
a_stu = {'name': self.stu.name, 'age': self.stu.age, 'phone': self.stu.phone, "time": now_time}
with open('admins.json', 'r', encoding='utf-8') as f:
content = f.read()
admins_users = json.loads(content)
if "add" not in admins_users[self.name]:
admins_users[self.name]["add"] = [a_stu]
else:
admins_users[self.name]["add"] += [a_stu]
with open('admins.json', 'w', encoding='utf-8') as f:
data_json = json.dumps(admins_users)
f.write(data_json)
print('添加成功!')
self.go_back()
if self.adm_in == 2:
break
elif self.adm_in == 1:
continue
self.draw_func_win()
def search_stu(self):
with open('admins.json', 'r', encoding='utf-8') as f:
content = f.read()
admins_users = json.loads(content)
stu_len = len(admins_users[self.name]["add"])
count = 0
if "add" not in admins_users[self.name]:
print('暂时还没有学生╮(╯▽╰)╭')
elif not stu_len:
print("暂时还没有学生╮(╯▽╰)╭")
else:
for student in admins_users[self.name]["add"]:
count += 1
print('第', count, '名学生:', end='')
print('学生姓名:', student['name'], end='\t\t')
print('学生年龄:', student['age'], end='\t\t')
print('电话号码:', student['phone'])
if "alter" not in admins_users[self.name]:
print('暂时无修改的数据╮(╯▽╰)╭')
else:
try:
alter_nums = len(admins_users[self.name]["alter"])
print("您修改学生数据{}次,最新修改的数据是:\n{}"
.format(alter_nums, admins_users[self.name]["alter"][-1]))
except IndexError:
print('暂时无修改的数据╮(╯▽╰)╭')
if "del" not in admins_users[self.name]:
print('暂时无删除过数据╮(╯▽╰)╭')
else:
try:
del_nums = len(admins_users[self.name]["del"])
print("您删除了{}条数据,最新删除的数据是:\n{}"
.format(del_nums, admins_users[self.name]["del"][-1]))
except IndexError:
print('暂时无删除过数据╮(╯▽╰)╭')
input('按任意键退出')
self.draw_func_win()
def change_info(self):
while True:
try:
count = int(input('您要修改第几组数据:'))
except ValueError:
print('输入错误,请重新输入••••••')
time.sleep(1)
continue
with open('admins.json', 'r', encoding='utf-8') as f:
content = f.read()
admins_users = json.loads(content)
stu_len = len(admins_users[self.name]["add"])
if not 0 < count <= stu_len:
print('超出界限,请重输••••••')
time.sleep(1)
continue
print('1.修改名字')
print('2.修改年龄')
print('3.修改号码')
try:
key_stu = int(input('请选择:'))
except ValueError:
print('输入的不是数字,请重新输入••••••')
time.sleep(1)
continue
if key_stu == 1:
new_name = input('学生的姓名为:').replace(' ', '')
now_time = time.time()
admins_users[self.name]["add"][count - 1]["name"] = new_name
admins_users[self.name]["add"][count - 1]["time"] = now_time
if "alter" not in admins_users[self.name]:
admins_users[self.name]["alter"] = [{"name": new_name, "time": now_time}]
else:
admins_users[self.name]["alter"] += [{"name": new_name, "time": now_time}]
with open('admins.json', 'w', encoding='utf-8') as f:
data_json = json.dumps(admins_users)
f.write(data_json)
print('修改成功!')
elif key_stu == 2:
try:
new_age = int(input('学生的年龄为:'))
except ValueError:
print('输入错误,请重新输入••••••')
time.sleep(1)
continue
admins_users[self.name]["add"][count - 1]["age"] = new_age
now_time = time.time()
admins_users[self.name]["add"][count - 1]["time"] = now_time
if "alter" not in admins_users[self.name]:
admins_users[self.name]["alter"] = [{"age": new_age, "time": now_time}]
else:
admins_users[self.name]["alter"] += [{"age": new_age, "time": now_time}]
with open('admins.json', 'w', encoding='utf-8') as f:
data_json = json.dumps(admins_users)
f.write(data_json)
print('修改成功!')
elif key_stu == 3:
new_phone = input('学生的电话号码为:').replace(' ', '')
now_time = time.time()
admins_users[self.name]["add"][count - 1]["phone"] = new_phone
admins_users[self.name]["add"][count - 1]["time"] = now_time
if "alter" not in admins_users[self.name]:
admins_users[self.name]["alter"] = [{"phone": new_phone, "time": now_time}]
else:
admins_users[self.name]["alter"] += [{"phone": new_phone, "time": now_time}]
with open('admins.json', 'w', encoding='utf-8') as f:
data_json = json.dumps(admins_users)
f.write(data_json)
print('修改成功!')
else:
print('输入命令错误,请重新选择••••••')
time.sleep(1)
continue
self.go_back()
if self.adm_in == 1:
continue
elif self.adm_in == 2:
break
self.draw_func_win()
def del_stu(self):
while True:
try:
count = int(input('您要删除第几组数据(如果清空请输入 -1):'))
except ValueError:
print('输入错误,请重输••••••')
time.sleep(1)
continue
with open('admins.json', 'r', encoding='utf-8') as f:
content = f.read()
admins_users = json.loads(content)
stu_len = len(admins_users[self.name]["add"])
if not stu_len:
print('暂时没有学生,无法删除,退出••••••')
time.sleep(1)
self.adm_in = 2
elif count == -1:
if "del" not in admins_users[self.name]:
admins_users[self.name]["del"] = [admins_users[self.name]["add"]]
else:
admins_users[self.name]["del"] += admins_users[self.name]["add"]
admins_users[self.name]["add"].clear()
with open('admins.json', 'w', encoding='utf-8') as f:
data_json = json.dumps(admins_users)
f.write(data_json)
self.adm_in = 2
print('删除成功!')
elif not 0 < count <= stu_len:
print('超出界限,请重输••••••')
time.sleep(1)
continue
else:
if "del" not in admins_users[self.name]:
admins_users[self.name]["del"] = [admins_users[self.name]["add"][count - 1]]
else:
admins_users[self.name]["del"] += admins_users[self.name]["add"][count - 1]
del admins_users[self.name]["add"][count - 1]
with open('admins.json', 'w', encoding='utf-8') as f:
data_json = json.dumps(admins_users)
f.write(data_json)
print('删除成功!')
self.go_back()
if self.adm_in == 1:
continue
elif self.adm_in == 2:
break
self.draw_func_win()
def go_back(self):
while True:
try:
self.adm_in = int(input('输入(1.继续\t2.退出):'))
except ValueError:
print('输入错误,请重试')
continue
if self.adm_in == 1:
print('continue••••••')
time.sleep(1)
break
elif self.adm_in == 2:
break
else:
print('输入错误命令,请重输••••••')
time.sleep(1)
admins.json(数据自动生成)
{"a0": {"pwd": "12345", "add": [], "alter": [{"name": "stuoo", "time": "Tue Aug 6 23:09:14 2019"}, {"name": "stu0", "time": "Tue Aug 6 23:10:39 2019"}, {"name": "444", "time": "Tue Aug 6 23:12:17 2019"}, {"name": "4", "time": "Tue Aug 6 23:16:21 2019"}, {"name": "147", "time": "Tue Aug 6 23:18:51 2019"}, {"age": 22, "time": "Tue Aug 6 23:30:51 2019"}, {"phone": "11111111111", "time": "Tue Aug 6 23:31:17 2019"}, {"name": "stu233", "time": "Wed Aug 7 00:21:47 2019"}, {"age": 16, "time": "Wed Aug 7 00:22:34 2019"}, {"age": 17, "time": "Wed Aug 7 00:22:47 2019"}], "del": [{"name": "stu1", "age": 14, "phone": "45879654126", "time": "Tue Aug 6 22:45:40 2019"}, "name", "age", "phone", "time", {"name": "12", "age": 45, "phone": "12365417895", "time": "Tue Aug 6 22:49:34 2019"}, {"name": "222", "age": 24, "phone": "14587965487", "time": "Tue Aug 6 23:18:31 2019"}, {"name": "stuaa", "age": 18, "phone": "14785946123", "time": "Wed Aug 7 00:21:09 2019"}]}, "144": {"pwd": "45678"}}