day12-作业

写一个学生管理系统

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"}}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352

推荐阅读更多精彩内容

  • 学生管理系统 主界面 fileManager文件
    略略略_29fd阅读 179评论 0 0
  • import json 注册 def sign_up():with open('accounts.txt', 'r...
    Shyerjouniewin阅读 220评论 0 0
  • 5.写一个类,封装所有和数学运算相关的功能 5-160.6666666666666666208 redblue美国...
    晓晓的忍儿阅读 416评论 0 14
  • 1.声明一个电脑类: 属性:品牌、颜色、内存大小方法:打游戏、写代码、看视频 a.创建电脑类的对象,然后通过对象点...
    木偶演员阅读 198评论 0 4
  • 学生管理系统
    ______n___阅读 205评论 0 0