def view_classroom(self, class_id): sc_table = DatabasesCls("student_classroom") classroom_table = DatabasesCls("classroom") course_table = DatabasesCls("courses") school_table = DatabasesCls("schools") school_data = school_table.open() sc_data = sc_table.open() classroom_data = classroom_table.open() course_data = course_table.open() if class_id in sc_data: while True: print("_" * 100) print("班级ID\t\t学生ID\t\t\t讲师ID\t\t\t班级名称\t\t\t\t课程名称\t\t学校") print("-" * 100) student_list = sc_data[class_id] for student in student_list: teacher_id = classroom_data[class_id]["teacher_id"] class_name = classroom_data[class_id]["class_name"] course_id = classroom_data[class_id]["course_id"] course_name = course_data[course_id]["name"] school_id = classroom_data[class_id]["school_id"] school_name = school_data[school_id]["name"] print("%s\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t%s" % \ (class_id,student,teacher_id,class_name,course_name,school_name)) print("-" * 100) choice = input("[B|b返回]: ") if choice == "B" or choice == "b": break else: print("该班级还没有学员!!") time.sleep(1)
def student_list(self): print("学员列表--") student_pc_table = DatabasesCls("student_purchase_course") course_table = DatabasesCls("courses") data = student_pc_table.open() course_data = course_table.open() print(data) while True: print("_" * 100) print("学员ID\t\t\t购买的课程\t\t\t课程名称\t\t\t购买时间\t\t\t\t\t班级关联状态") print("-" * 100) for k, v in data.items(): for i, j in v.items(): student_id = k course_id = i course_name = course_data[course_id]["name"] purchase_time = j["purchase_time"] status = j["status"] status_com = "" if status == 0: status_com = "未关联" elif status == 1: status_com = "已关联" print("%s\t\t\t%s\t\t\t\t\t%s\t\t\t%s\t\t\t%s" % (student_id, course_id, course_name, purchase_time, status_com)) print("-" * 100) choice = input("请输入选项[A关联班级|B|b返回]: ").strip() if choice == "B" or choice == "b": break elif choice == "A" or choice == "a": Admin.join_student(self)
def classroom_list(self): classroom_data = ClassroomStaticCls.classroom_table.open() course_data = CourseStaticCls.course_table.open() teacher_data = TeacherStaticCls.teacher_table.open() school_data = SchoolStaticCls.school_table.open() sc_table = DatabasesCls("student_classroom") sc_data = sc_table.open() print(sc_data) while True: print("_" * 130) print("-" * 130) for k, v in classroom_data.items(): class_id, class_name, course_id, school_id, teacher_id = \ k, v["class_name"], v["course_id"], v["school_id"], v["teacher_id"] course_name = course_data[course_id]["name"] school_name = school_data[school_id]["name"] teacher_name = teacher_data[teacher_id]["name"] count = len(sc_data[class_id]) print("班级ID: %s\t\t班级名字: %s\t\t课程ID: %s\t\t课程名字: %s\t\t所在学校: %s\t\t授课讲师: %s\t\t学生人数: %s" % \ (class_id, class_name, course_id, course_name, school_name, teacher_name,count)) print("-" * 130) choice = input("输入班级ID查看班级学员(B返回): ") if choice == "B" or choice == "b": break School.view_classroom(self, choice)
def update(self): school_table = DatabasesCls("schools") school_list = school_table.open() data = self.teacher_table.open() if self.id in data and self.id.isalnum(): if self.password != "" and self.name != "" and self.phone != "" and self.teacher_title \ != "" and self.teacher_salary != "": if self.teacher_school in school_list: try: self.teacher_obj["password"] = self.password self.teacher_obj["name"] = self.name self.teacher_obj["phone"] = self.phone self.teacher_obj["school_id"] = self.teacher_school self.teacher_obj["title"] = self.teacher_title self.teacher_obj["salary"] = self.teacher_salary self.teacher_obj["email"] = self.teacher_email data[self.id] = self.teacher_obj self.teacher_table.save(data) return 0 #保存成功 except BaseException: return 3 #保存失败 else: return 3 #学校代码不存在 else: return 2 #账号密码、名字、手机、职务、薪资不能为空 else: return 1 #讲师账号已存在或输入错误
def my_course(self): print("我的课程--") user_name = user_data["user_id"] sc_table = DatabasesCls("student_classroom") cr_table = DatabasesCls("classroom") courses_table = DatabasesCls("courses") spc_table = DatabasesCls("student_purchase_course") spc_data = spc_table.open() courses_data = courses_table.open() sc_data = sc_table.open() cr_data = cr_table.open() print(sc_data) print(cr_data) print(courses_data) print(spc_data) while True: print("_" * 50) print("课程ID\t\t\t课程名称\t\t\t课程大纲") print("-" * 50) course_dict = spc_data[user_name] for id, value in course_dict.items(): course_id = id course_name = courses_data[course_id]["name"] course_outline = courses_data[course_id]["outline"] print("%s\t\t\t\t%s\t\t\t%s" % (course_id, course_name, course_outline)) print("-" * 50) choice = input("选择课程ID进入学习[B返回]: ") print(course_dict) if choice == "B" or choice == "b": break try: choice = int(choice) except: pass if choice in course_dict: course = courses_data[choice]["name"] try: course_dict[choice]["count"] += 1 except: course_dict[choice]["count"] = 0 finally: spc_data[user_name] = course_dict spc_table.save(spc_data) print("正在学习课程%s, 已学习%s次..." % (course, course_dict[choice]["count"])) time.sleep(1)
def join_student(self): student_table = DatabasesCls("students") classroom_table = DatabasesCls("classroom") student_classroom = DatabasesCls("student_classroom") student_pc_table = DatabasesCls("student_purchase_course") student_data = student_table.open() classroom_data = classroom_table.open() sc_data = student_classroom.open() pc_data = student_pc_table.open() print("关联班级--") print("/" * 15 + "学员关联班级 (*选项为必填)" + "/" * 15) student_id = input("*请输入学员的ID: ").strip() student_class_id = input("*请输入该学员关联的班级ID: ").strip() print(sc_data) if student_id in student_data: if student_id in pc_data: if student_class_id in classroom_data: try: s_list = sc_data[student_class_id] except: s_list = [] if student_id not in s_list: s_list.append(student_id) print(s_list) sc_data[student_class_id] = s_list student_classroom.save(sc_data) course_id = classroom_data[student_class_id][ "course_id"] pc_data[student_id][course_id]["status"] = 1 student_pc_table.save(pc_data) print("关联成功!") time.sleep(1) else: print("学员%s已经关联到该班级." % student_id) print(sc_data) else: print("班级ID不存在!!") else: print("该学员未购买该课程!!") else: print("学员ID不存在!!")
def update(self): school_table = DatabasesCls("schools") course_table = DatabasesCls("courses") teacher_table = DatabasesCls("teachers") class_data = self.classroom_table.open() course_data = course_table.open() school_data = school_table.open() teacher_data = teacher_table.open() if self.class_id in class_data: if self.class_name != "": if self.course_id in course_data: if self.school_id in school_data: if self.teacher_id in teacher_data: try: enroll_date = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()) self.classroom_obj[ "class_name"] = self.class_name self.classroom_obj[ "course_id"] = self.course_id self.classroom_obj[ "teacher_id"] = self.teacher_id self.classroom_obj[ "school_id"] = self.school_id self.classroom_obj["enroll_date"] = str( enroll_date) class_data[self.class_id] = self.classroom_obj self.classroom_table.save(class_data) return 0 # 保存成功 except BaseException: return 1 # 保存失败 else: return 2 # 讲师ID不存在 else: return 6 # 学校不存在 else: return 3 # 课程ID不存在 else: return 4 # 班级名字为空 else: return 5 # 班级ID不存在
def view_grade(self): print("查询成绩--") user_name = user_data["user_id"] spc_table = DatabasesCls("student_purchase_course") courses_table = DatabasesCls("courses") spc_data = spc_table.open() course_data = courses_table.open() while True: print("_" * 50) print("课程ID\t\t\t课程名称\t\t\t学习成绩") print("-" * 50) course_dict = spc_data[user_name] for course_id, value in course_dict.items(): course_name = course_data[course_id]["name"] study_grade = value["grade"] print("%s\t\t\t\t%s\t\t\t%s分" % (course_id, course_name, study_grade)) print("-" * 50) choice = input("[B|b返回]: ") if choice == "B" or choice == "b": break
def my_classroom(self): print("我的班级--") user_name = user_data["user_id"] sc_table = DatabasesCls("student_classroom") cr_table = DatabasesCls("classroom") sc_data = sc_table.open() cr_data = cr_table.open() while True: print("_" * 50) print("班级ID\t\t\t班级名称\t\t\t\t讲师") print("-" * 50) print(sc_data) for class_id, student_list in sc_data.items(): if user_name in student_list: class_name = cr_data[class_id]["class_name"] teacher = cr_data[class_id]["teacher_id"] print("%s\t\t\t%s\t\t\t%s" % (class_id, class_name, teacher)) print("-" * 50) choice = input("[B|b返回]: ") if choice == "B" or choice == "b": break
def study_record(self): print("上课记录--") user_name = user_data["user_id"] spc_table = DatabasesCls("student_purchase_course") courses_table = DatabasesCls("courses") spc_data = spc_table.open() course_data = courses_table.open() while True: print("_" * 50) print("课程ID\t\t\t课程名称\t\t\t学习次数") print("-" * 50) course_dict = spc_data[user_name] for course_id, value in course_dict.items(): course_name = course_data[course_id]["name"] try: study_count = value["count"] except: study_count = 0 print("%s\t\t\t\t%s\t\t\t%s" % (course_id, course_name, study_count)) print("-" * 50) choice = input("[B|b返回]: ") if choice == "B" or choice == "b": break
def create_classroom(self): print("/" * 15 + "创建班级 (*选项为必填)" + "/" * 15) class_id = input("*输入要创建的班级ID(如:1801): ").strip() class_name = input("*输入要创建的班级名字: ").strip() course_id = input("*输入该班级开设(关联)的课程ID: ").strip() school_id = input("*输入该班级所在的学校ID: ").strip() teacher_id = input("*输入该班级的授课(关联)讲师的ID帐号: ").strip() try: course_id = int(course_id) except ValueError: pass classroom = ClassroomStaticCls(class_id, class_name, course_id, school_id, teacher_id) # 初始化 对象中的数据 new_classroom = classroom.create() # 在初始化数据后 调用 create方法 #============================================== # 增加一个为空的班级对应学生的信息的存放着 sc_table = DatabasesCls("student_classroom") sc_data = sc_table.open() #读取学生班级信息 sc_data[class_id] = [] sc_table.save(sc_data) if new_classroom == 0: print("创建成功!") elif new_classroom == 1: print("创建失败! 无法保存.") elif new_classroom == 2: print("讲师不存在或讲师ID错误!") elif new_classroom == 3: print("课程不存在或课程ID错误!") elif new_classroom == 4: print("班级名字不能为空!") elif new_classroom == 5: print("班级ID已存在!") elif new_classroom == 6: print("学校不存在!") else: print("创建失败!") print(classroom.classroom_obj)
def view_classroom(self, teacher_id): print("查看班级学员--") cr_table = DatabasesCls("classroom") sc_table = DatabasesCls("student_classroom") course_table = DatabasesCls("courses") spc_table = DatabasesCls("student_purchase_course") cr_data = cr_table.open() sc_data = sc_table.open() course_data = course_table.open() spc_data = spc_table.open() classes = [] while True: print("_" * 120) print( "班级ID\t\t班级名称\t\t\t\t讲师ID\t\t\t课程ID\t\t课程名称\t\t\t学校ID\t\t班级人数\t\t\t创建时间" ) print("-" * 120) for class_id, class_value in cr_data.items(): teacher = class_value["teacher_id"] if teacher == teacher_id: classes.append(class_id) class_name = class_value["class_name"] course_id = class_value["course_id"] course_name = course_data[course_id]["name"] school_id = class_value["school_id"] count = len(sc_data[class_id]) enroll_date = class_value["enroll_date"] print("%s\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t\t%s\t\t\t%s" % \ (class_id,class_name,teacher,course_id,course_name,school_id,count,enroll_date)) print("-" * 120) choice = input("输入选项: \n" "1 查看学员\n" "2 开始上课\n" "3 返回\n" "Enter your choice: ") if choice == "3": break if choice == "1": print("查看班级学员--") class_id2 = input("请输入班级ID: ") if class_id2 in classes: while True: print("_" * 80) print("班级ID\t\t学生ID\t\t\t课程名称\t\t\t学习次数\t\t\t成绩") print("-" * 80) student_list = sc_data[class_id2] if class_id2 in cr_data: course_id = cr_data[class_id2]["course_id"] course_name = course_data[course_id]["name"] for student_id in student_list: if student_id in spc_data: study_count = spc_data[student_id][ course_id]["count"] study_grade = spc_data[student_id][ course_id]["grade"] print( "%s\t\t%s\t\t\t%s\t\t\t%s\t\t\t\t%s" % (class_id2, student_id, course_name, study_count, study_grade)) print("-" * 80) choice = input("输入选项: \n" "1 修改学员成绩\n" "2 返回\n" "Enter your choice: ") if choice == "2": return if choice == "1": print("修改学员成绩--") student = input("输入学员ID: ") grade = input("输入学员成绩: ") if student in student_list: if grade.isdigit(): spc_data[student][course_id][ "grade"] = grade spc_table.save(spc_data) print(spc_data) print("修改成功!") time.sleep(1) else: print("成绩格式错误!") else: print("学员不存在!") else: print("班级ID不存在!") if choice == "2": print("选择班级上课--") class_id3 = input("请输入班级ID: ") if class_id3 in classes: print("开始上课----") time.sleep(1) else: print("班级ID不存在!")
class StudentsAccountCls(object): def __init__(self, cookie): super(StudentsAccountCls, self).__init__() self.cookie = cookie self.user_table = DatabasesCls("students") @staticmethod def login_required(func): # 用户登录装饰器 def wrapper(*args, **kwargs): cookie = user_data if cookie['is_authenticated']: return func(*args, **kwargs) else: print("帐号未登录验证!") return wrapper def set_session(self, user, account_data): try: self.cookie['user_id'] = user self.cookie['is_authenticated'] = True self.cookie['user_data'] = account_data return True except: pass def clear_session(self, user_data): try: user_data['user_id'] = None user_data['is_authenticated'] = False user_data['user_data'] = None return True except: pass def logout(self): StudentsAccountCls.clear_session(self, self.cookie) def login(self): print(self.cookie) accounts_dict = self.user_table.open() retry_count = 0 while retry_count < 3: username = input("Username: "******"Password: "******"" and password != "": if username in accounts_dict: user_pass = accounts_dict[username]["password"] if password == user_pass: print("验证成功!") account_data = accounts_dict[username] StudentsAccountCls.set_session(self, username, account_data) return self.cookie else: print("帐号或密码错误!") retry_count += 1 else: print("该用户名未注册!") else: print("帐号或密码不能为空!") retry_count += 1 def forgot(self): accounts_dict = self.user_table.open() print("----重置密码----") retry_count = 0 while retry_count < 3: username = input("请输入你的用户名: ") if username in accounts_dict: password = input("请输入新密码(3-18位字符): ") if password != "" and len(password) >= 3 and len( password) <= 18: password2 = input("再次输入新密码: ") if password2 == password: accounts_dict[username]["password"] = password2 self.user_table.save(accounts_dict) print("修改成功.") break else: print("两次输入密码不匹配.") retry_count += 1 else: print("密码不符合要求!") retry_count += 1 else: print("该用户不存在!") def registered(self): print("----注册帐号----") print("/" * 15 + "注册帐号 (*选项必填)" + "/" * 15) accounts_dict = self.user_table.open() while True: username = input("*输入您要注册的用户名: ").strip() password = input("*输入您要设置的密码(3-12位字符): ").strip() password2 = input("*再次输入新密码: ").strip() real_name = input("*输入您的姓名: ") gender = input("*输入您的性别: ").strip() dob = input("*输入您的出生年月日(0000-00-00): ").strip() phone = input("*请输入11位数字的手机号码(如:13100224466): ").strip() email = input("*输入您的电子邮箱: ").strip() if username.isalnum() and username not in accounts_dict: if password != "": if password == password2: if gender != "" and dob != "" and phone != "" and email != "" and real_name != "": dict = {} student_id = 10000 + len(accounts_dict) + 1 enroll_date = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()) status = 0 dict["student_id"] = student_id dict["name"] = real_name dict["password"] = password dict["gender"] = gender dict["dob"] = dob dict["phone"] = phone dict["email"] = email dict["enroll_date"] = enroll_date dict["status"] = status accounts_dict[username] = dict choice = input("确定注册[Y|n]?") if choice == "Y" or choice == "y": self.user_table.save(accounts_dict) print("注册成功!") time.sleep(1) break else: print("资料未完善!") break else: print("两次输入密码不匹配!") break else: print("密码不能为空!") break else: print("帐号格式不正确或已存在!") break
class Student(object): def __init__(self, name): super(Student, self).__init__() self.name = name self.student_purchase_course = DatabasesCls("student_purchase_course") def course_list(self): data = CourseStaticCls.course_table.open() purchase_dict = self.student_purchase_course.open() user_name = user_data["user_id"] while True: print("_" * 100) print("课程编号\t\t 课程名称\t\t\t课程价格\t\t\t课程周期\t\t课程大纲") print("-" * 100) for k, v in data.items(): id, name, price, period, outline = \ k, v["name"], v["price"], v["period"], v["outline"] name = name.center(10, " ") print("<< %s >>\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s" % \ (id, name, price, period, outline)) print("-" * 100) choice = input("请输入课程编号进行选课购买[B返回]: ").strip() if choice == "B" or choice == "b": break try: choice = int(choice) except: pass if choice in data: enter = input("确认购买?[Y|n]") if enter == "Y" or enter == "y": if user_name not in purchase_dict: dict1 = {} dict2 = {} purchase_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) dict1["purchase_time"] = purchase_time dict1["status"] = 0 dict1["grade"] = 0 dict1["count"] = 0 dict2[choice] = dict1 purchase_dict[user_name] = dict2 self.student_purchase_course.save(purchase_dict) print("购买成功!") time.sleep(1) else: dict1 = {} dict2 = purchase_dict[user_name] if choice not in purchase_dict[user_name]: purchase_time = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()) dict1["purchase_time"] = purchase_time dict1["status"] = 0 dict1["grade"] = 0 dict1["count"] = 0 dict2[choice] = dict1 purchase_dict[user_name] = dict2 self.student_purchase_course.save(purchase_dict) print("购买成功!") time.sleep(1) else: print("您已购买该课程, 快开始学习吧!") else: print("取消购买.") else: print("课程ID不存在!") def my_course(self): print("我的课程--") user_name = user_data["user_id"] sc_table = DatabasesCls("student_classroom") cr_table = DatabasesCls("classroom") courses_table = DatabasesCls("courses") spc_table = DatabasesCls("student_purchase_course") spc_data = spc_table.open() courses_data = courses_table.open() sc_data = sc_table.open() cr_data = cr_table.open() print(sc_data) print(cr_data) print(courses_data) print(spc_data) while True: print("_" * 50) print("课程ID\t\t\t课程名称\t\t\t课程大纲") print("-" * 50) course_dict = spc_data[user_name] for id, value in course_dict.items(): course_id = id course_name = courses_data[course_id]["name"] course_outline = courses_data[course_id]["outline"] print("%s\t\t\t\t%s\t\t\t%s" % (course_id, course_name, course_outline)) print("-" * 50) choice = input("选择课程ID进入学习[B返回]: ") print(course_dict) if choice == "B" or choice == "b": break try: choice = int(choice) except: pass if choice in course_dict: course = courses_data[choice]["name"] try: course_dict[choice]["count"] += 1 except: course_dict[choice]["count"] = 0 finally: spc_data[user_name] = course_dict spc_table.save(spc_data) print("正在学习课程%s, 已学习%s次..." % (course, course_dict[choice]["count"])) time.sleep(1) def my_classroom(self): print("我的班级--") user_name = user_data["user_id"] sc_table = DatabasesCls("student_classroom") cr_table = DatabasesCls("classroom") sc_data = sc_table.open() cr_data = cr_table.open() while True: print("_" * 50) print("班级ID\t\t\t班级名称\t\t\t\t讲师") print("-" * 50) print(sc_data) for class_id, student_list in sc_data.items(): if user_name in student_list: class_name = cr_data[class_id]["class_name"] teacher = cr_data[class_id]["teacher_id"] print("%s\t\t\t%s\t\t\t%s" % (class_id, class_name, teacher)) print("-" * 50) choice = input("[B|b返回]: ") if choice == "B" or choice == "b": break def study_record(self): print("上课记录--") user_name = user_data["user_id"] spc_table = DatabasesCls("student_purchase_course") courses_table = DatabasesCls("courses") spc_data = spc_table.open() course_data = courses_table.open() while True: print("_" * 50) print("课程ID\t\t\t课程名称\t\t\t学习次数") print("-" * 50) course_dict = spc_data[user_name] for course_id, value in course_dict.items(): course_name = course_data[course_id]["name"] try: study_count = value["count"] except: study_count = 0 print("%s\t\t\t\t%s\t\t\t%s" % (course_id, course_name, study_count)) print("-" * 50) choice = input("[B|b返回]: ") if choice == "B" or choice == "b": break def view_grade(self): print("查询成绩--") user_name = user_data["user_id"] spc_table = DatabasesCls("student_purchase_course") courses_table = DatabasesCls("courses") spc_data = spc_table.open() course_data = courses_table.open() while True: print("_" * 50) print("课程ID\t\t\t课程名称\t\t\t学习成绩") print("-" * 50) course_dict = spc_data[user_name] for course_id, value in course_dict.items(): course_name = course_data[course_id]["name"] study_grade = value["grade"] print("%s\t\t\t\t%s\t\t\t%s分" % (course_id, course_name, study_grade)) print("-" * 50) choice = input("[B|b返回]: ") if choice == "B" or choice == "b": break