python作业之选课系统

题目要求:

python作业之选课系统_第1张图片

一、需求分析

利用规范化目录完成一个学生选课系统
角色:学生/管理员/讲师
对象:课程/校区
功能分析:
用户登陆之后就可以直接判断用户身份(是学生还是管理员)
学生登陆之后有以下几个功能:
1.察看所有课程
2.选择课程
3.查看所选课程
管理员登陆之后有以下几个功能:
1.创建课程
2.创建学生账号
3.察看所有课程
4.察看所有学生
5.察看所有学生的选课情况
讲师登陆之后有以下几个功能:
1.查看课程
2.选择课程
3.查看学生
4.修改学生分数
必须包含的属性如下
课程属性:价格、周期
校区属性:滨海、河西、小黑屋
管理员属性:ID、用户名、密码、课程列表、校区、分数
讲师属性:ID、用户名、密码、课程列表
学员: ID、用户名、密码

二、代码

课程信息(class_information)

import json          
import pickle

class Role:        
    def __init__(self, usr_name, number, password=None):
        self.usr_name = usr_name
        self.number = number
        self.password = password


class Administrator(Role):       
    def __init__(self, usr_name, number, password):
        Role.__init__(self, usr_name, number, password)
        self.school_lst = []
    pass


class Student(Role):
    def __init__(self, usr_name, number, password, school):
        Role.__init__(self, usr_name, number, password)
        self.school = school
        self.subject_lst = []  # subject_lst: list[obj]
        self.score_dic = {
     }  # score_dic: dict{subject_name: score}

    def choose_subject(self, subject_name: str):
        self.subject_lst.append(obj_information_lst[4][subject_name].name)
        obj_information_lst[4][subject_name].student_lst.append(self.usr_name)
        self.score_dic[subject_name] = 60

    def check_score(self):
        for key, value in self.score_dic.items():
            print(key, value)


class Teacher(Role):
    def __init__(self, usr_name, number, password):
        Role.__init__(self, usr_name, number, password)
        self.subject_lst = []  # subject_lst: list[obj]

    def choose_subject(self, subject_name: str):
        self.subject_lst.append(obj_information_lst[4][subject_name].name)
        obj_information_lst[4][subject_name].teacher = self.usr_name

    def check_my_subject(self):
        print(self.subject_lst)


class School:       

    def __init__(self, name):
        self.name = name
        self.subject_lst = []  # subject_lst: list[str]


class Subject:

    def __init__(self, name, price, cycle,
                 school: object):
        self.name = name
        self.teacher = None  # teacher: str
        self.information = [name, price, cycle, school.name, self.teacher]
        self.school = school
        self.school.subject_lst.append(self.name)
        self.student_lst = []  # student_lst: list[str]


def create_a_school():           
    name_str = input('请输入学区名称:')
    name = School(name_str)
    obj_information_lst[3][name_str] = name
    save_obj_information()
    print('学区创建完毕')


def create_a_teacher():         
    usr_name_str = input('请输入教师姓名:')  # usr_name: str
    number = input('请输入教师id:')
    password = input('请输入密码:')
    usr_name = Teacher(usr_name_str, number, password)  # usr_name: obj
    obj_information_lst[1][usr_name_str] = usr_name
    usr_password_data_lst[1][number] = password
    save_obj_information()
    save_usr_password()
    print('教师创建完毕')


def create_a_subject():     
    name_str = input('请输入课程名称:')  # name: str
    price = input('请输入课程价格:')
    cycle = input('请输入课程周期:')
    school = choose_school()    
    name = Subject(name_str, price, cycle, school)
    obj_information_lst[4][name_str] = name
    save_obj_information()
    print('课程创建完毕')



def check_all_subject(): 
    for school_name, school_obj in obj_information_lst[3].items():
        print(school_name, school_obj.subject_lst)


def further_check(usr_name):      
    choice = input('1.further check\n2.choose subject\n3.end\n')
    if choice == '1':
        subject_name = input('subject name')
        print(obj_information_lst[4][subject_name].information)
        further_check(usr_name)
    elif choice == '2':
        subject_name = input('subject name')
        usr_name.choose_subject(subject_name)
        print('选课成功')
        further_check(usr_name)
    elif choice == '3':
        pass
    else:
        further_check(usr_name)


def choose_school():          
    print(obj_information_lst[3].keys())
    school_str = input('请输入校区')
    school = obj_information_lst[3][school_str]
    return school
 
 
admin_usr_password_dic = {
     }          
teacher_usr_password_dic = {
     }
student_usr_password_dic = {
     }
admin_dic = {
     }
teacher_dic = {
     }
student_dic = {
     }
school_dic = {
     }
subject_dic = {
     }

usr_password_data_lst = [admin_usr_password_dic, teacher_usr_password_dic,
                         student_usr_password_dic]
obj_information_lst = [admin_dic, teacher_dic, student_dic, school_dic, subject_dic]


def save_usr_password():       
    with open('UserData.json', 'w', encoding='utf-8') as fp:
        json.dump(usr_password_data_lst, fp)
    fp.close()


def save_obj_information():
    with open('UserData.pkl', 'wb') as fp:
        pickle.dump(obj_information_lst, fp)
    fp.close()

学生

import class_information     


def stu_register():      
    usr_name_str = input('请输入用户名:')
    number = input('请输入id:')
    password = input('请输入密码:')
    school = class_information.choose_school()
    usr_name = class_information.Student(usr_name=usr_name_str, number=number,
                                         password=password, school=school)
    class_information.obj_information_lst[2][usr_name_str] = usr_name
    class_information.usr_password_data_lst[2][number] = password
    print('over')


def student_inter(usr_name):     
    print('1.choose_subject\n2.check_score\n3.log out')
    tem = input('make a choice:')
    if tem == '1':
        print(usr_name.school.subject_lst)
        class_information.further_check(usr_name)
        student_inter(usr_name)
    elif tem == '2':
        usr_name.check_score()
        student_inter(usr_name)
    elif tem == '3':
        print('log out successfully')
        pass
    else:
        print('请在目录里面选择')
        student_inter(usr_name)

管理员

import class_information     


def admin_register():      
    usr_name_str = input('请输入用户名:')
    number = input('请输入id:')
    password = input('请输入密码:')
    usr_name = class_information.Administrator(usr_name=usr_name_str,
                                               number=number, password=password)
    class_information.usr_password_data_lst[0][number] = password
    class_information.obj_information_lst[0][usr_name_str] = usr_name
    print('over')


def admin_inter(usr_name: object):      
    print('1.create a school\n2.create a teacher\n3.create a subject\n4.log out')
    tem = input('make a choice')
    if tem == '1':          
        class_information.create_a_school()
        admin_inter(usr_name)
    elif tem == '2':
        class_information.create_a_teacher()
        admin_inter(usr_name)
    elif tem == '3':
        class_information.create_a_subject()
        admin_inter(usr_name)
    elif tem == '4':
        print('log out successfully')
        pass
    else:
        print('请在目录里面选择')
        admin_inter(usr_name)

讲师

import class_information    

def weather_further_check():    
    while True:
        choise = input('whether further_check(1.yes/2.no)\nneed a int')


def teacher_inter(usr_name):      
    print('1.choose_subject\n2.check_my_subject\n'
          '3.check_student or set_score\n4.log out')
    tem = input('make a choise:') 
    if tem == '1':                   
        class_information.check_all_subject()
        class_information.further_check(usr_name)
        class_information.save_obj_information()
        teacher_inter(usr_name)
    elif tem == '2':
        usr_name.check_my_subject()
        teacher_inter(usr_name)
    elif tem == '3':
        subject_name = input('subject_name')
        subject = class_information.obj_information_lst[4][subject_name]
        print(subject.student_lst)
        student_str = input('student_name')
        student = class_information.obj_information_lst[2][student_str]
        print('score = ', student.score_dic[subject_name])
        choise = input('1.set score\n2.end')
        if choise == '1':
            score = input('set a score\n')
            student.score_dic[subject_name] = score
            class_information.save_obj_information()
        elif choise == '2':
            pass
        teacher_inter(usr_name)
    elif tem == '4':
        print('log out successfully')
        pass
    else:
        print('请在目录里面选择')
        teacher_inter(usr_name)

教务系统

import pickle         
import json           

import class_information    
import admin_inter
import student_inter
import teacher_inter

 

def weather_initial():                     
    choice = input()     
    if choice == 'initial':          
        initial()
    else:
        pass
    load_information()          

def initial():
    with open('UserData.pkl', 'wb') as fp:    
        pickle.dump(class_information.obj_information_lst, fp)
    fp.close()                       
    with open('UserData.json', 'w', encoding='utf-8') as fp:   
        json.dump(class_information.usr_password_data_lst, fp)
    fp.close()                   

def load_information():
    with open('UserData.pkl', 'rb') as fp:  
        class_information.obj_information_lst = pickle.load(fp)
    fp.close()
    with open('UserData.json', 'r', encoding='utf-8') as fp:     #
        class_information.usr_password_data_lst = json.load(fp)
    fp.close()
    choose_status()


def choose_status():  
    print('''                     
        ------------>总菜单<------------
        1.管理员登录
        2.讲师登录
        3.学生登录
        4.退出
        --------------------------------------
            ''')
    status = input('请选择身份:\n')
    if status == '1':             
        log_or_register(status)
    elif status == '2':
        log_in(status)
    elif status == '3':
        log_or_register(status)
    elif status == '4':
        pass
    else:
        print('请在目录里面选择')
        choose_status()


def log_or_register(status):     
    choice = input("目录\n1.注册\n2.登录\n请输入你的选择:\n")   
    if choice == '1':
        register(status)
    elif choice == '2':
        log_in(status)
    else:
        print('请在目录里面选择')
        log_or_register(status)


def register(status):     
    if status == '1':
        admin_inter.admin_register()
    else:
        student_inter.stu_register()
    class_information.save_usr_password()
    class_information.save_obj_information()
    choose_status()


def log_in(status):        
    if status == '1':
        tem = 0
    elif status == '2':
        tem = 1
    else:
        tem = 2
    usr_name = input('请输入用户名:')
    number = input('请输入id:')
    password = input('请输入密码:')
    if number in class_information.usr_password_data_lst[tem]:
        if password == class_information.usr_password_data_lst[tem][number]:
            print('log in successfully')
            enter(status, class_information.obj_information_lst[tem][usr_name])
        else:
            print('password is not match id')
            log_in(status)
    else:
        print('no usr')
        log_in(status)


def enter(status, usr: object):    
    if status == '1':
        admin_inter.admin_inter(usr)
    elif status == '2':
        teacher_inter.teacher_inter(usr)
    elif status == '3':
        student_inter.student_inter(usr)
    class_information.save_usr_password()
    class_information.save_obj_information()
    choose_status()
    pass


def main():
    weather_initial()


if __name__ == '__main__':
    main()

总结

好好学习 又是快乐的一年。

你可能感兴趣的:(python,面向对象编程)