File size: 6,123 Bytes
e28c9e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#This program will save your information for quicker catching
#The data will only save locally.
import ensure_package_exist
ensure_package_exist.ensure_package_exist()
import json,re
from type_defs import Course, CourseData, UserData


def save_user_date(user_data:UserData):
    with open("user_data.json",mode="w",encoding="utf-8") as file:
        json.dump(user_data.to_dict(),file,indent=2)

def main()->UserData:
    user_data = UserData(
        std_id=input("Input your Student ID: "),
        password=input("Input your password: ")
    )

    while True:
        source=""
        while source not in ["a","b","q"]:
            source = input("输入课程来源(a.方案选课/b.自由选课/q.停止添加)(输入a或b即可): ")
        if(source == "q"):
            break
        course_id = input("输入课程号: ")
        course_index = input("输入课序号(必须为两位数,如:01、33): ")
        try:
            int(course_id),int(course_index)
            if len(course_index) != 2:
                raise Exception
            new_course = Course(course_id=course_id, course_index=course_index)
            if source == "a":
                user_data.course.a.append(new_course)
            else:
                user_data.course.b.append(new_course)
        except:
            print("Invaild course!Failed to add the course!")
            continue

    save_user_date(user_data)
    print("Done!")
    return user_data

def show_user_data(user_data:UserData) -> None:
    print("User information:")
    print("Student ID:%s"%(user_data.std_id),"Password:%s"%(user_data.password),sep="\n")
    print("Your courses:")
    print("-"*20)
    print("Plan courses:")
    print("\n".join(map(lambda x:"Course %s_%s"%(x.course_id,x.course_index),user_data.course.a)))
    print("-"*20)
    print("Free courses:")
    print("\n".join(map(lambda x:"Course %s_%s"%(x.course_id,x.course_index),user_data.course.b)))
    print("-"*20)

def update(user_data:UserData) -> UserData:
    show_user_data(user_data)

    operate = ""
    while True:
        while operate not in ["i","p","a","b","q"]:
            operate = input("请输入更新指令(i.更改学号/p.更改密码/a.更改方案选课/b.更改自由选课/s.展示当前信息/q.保存并退出)(输入前面的字母即可):")
        if operate == "q":
            return user_data
        elif operate == "i":
            user_data.std_id = input("Input the new std_id:")
            print("Student ID has been updated!")
        elif operate == "p":
            new_passwd = input("Input the new password:")
            if new_passwd == input("Input the new password again:"):
                user_data.password = new_passwd
                print("Password has been updated!")
            else:
                print("The passwords entered did not match.Failed to update the password!")
        elif operate in ["a","b"]:
            order,args = "",[]
            name = "方案选课" if operate == "a" else "自由选课"
            en_name = "Plan courses" if operate == "a" else "Free courses"
            course_list = user_data.course.a if operate == "a" else user_data.course.b
            while True:
                print("%s:"%(en_name))
                print("\n".join(map(lambda x:"%d.%s"%(x[0],x[1]),enumerate(map(lambda x:"Course %s_%s"%(x.course_id,x.course_index),course_list),1))))
                while order not in ["a","d","q"]:
                    manage = input("请输入更改%s的指令(a [课程号] [课序号].添加新的课程/d [序号].删除第[序号]条课程/q.退出方案选课更新)(输入指令并带上参数):"%(name))
                    order = manage.split(" ")[0]
                    args = re.findall(r"\d+",manage)
                if order == "q":
                    break
                elif order == "a":
                    if len(args) == 2:
                        try:
                            numbers = [int(i) for i in args]
                            if len(args[1]) != 2:
                                raise Exception
                            new_course = Course(course_id=args[0], course_index=args[1])
                            if not new_course in course_list:
                                course_list.append(new_course)
                                print("Successfully added Course %s_%s!"%(args[0],args[1]))
                            else:
                                print("Course %s_%s has already existed!"%(args[0],args[1]))
                        except:
                            print("Invaild arguments type!Failed to add the course!")
                    else:
                        print("Invaild number of arguments!Failed to add the course!")
                elif order == "d":
                    if len(args) == 1:
                        try:
                            pos = int(args[0])
                            try:
                                if pos <= 0:
                                    raise Exception
                                target = course_list[pos-1]
                                del course_list[pos-1]
                                print("Successfully deleted the Course %s_%s!"%(target.course_id,target.course_index))
                            except:
                                print("Invaild position!Failed to delete the course!")
                        except:
                            print("Invaild argument type!Failed to delete the course!")
                    else:
                        print("Invaild number of arguments!Failed to add the course!")
                order,args = "",[]
        elif operate == "s":
            show_user_data(user_data)
        operate = ""

if __name__ == "__main__":
    try:
        with open("user_data.json",mode="r") as file:
            new_json = update(UserData.from_dict(json.load(file)))
        save_user_date(new_json)
        print("Update finished!")
    except:
        main()