老男孩python全栈s21day25作业(学生选课系统作业)

根据以下需求,完成学生选课系统

https://www.cnblogs.com/Eva-J/articles/9235899.html

目录结构:
在这里插入图片描述

#run.py

import account

#管理员密码用户名和密码,默认为ljb/ljb,写死在代码account.py中
if __name__ == '__main__':
    print("欢迎来到学生选课系统!")
    name = input("请输入学生或管理员用户名:")
    pwd = input("请输入学生或管理员密码:")
    account.login(name,pwd)

#account.py
# import hashlib
import sys
import pickle
import os

class Courese(object):
    def __init__(self,name,price,period,teacher):
        self.course_name = name
        self.course_price = price
        self.course_period = period
        self.course_teacher = teacher

class Admin(object):
    def __init__(self,name,pwd):
        self.admin_name = name
        self.admin_pwd = pwd
    def create_course(self):
        course_name = input("%s请输入需要创建的课程名:"%(self.admin_name))
        course_price =input("%s请输入需要创建的课程价格:"%(self.admin_name))
        course_period =input("%s请输入需要创建的课程周期:"%(self.admin_name))
        course_teacher =input("%s请输入需要创建的课程授课老师:"%(self.admin_name))
        course = Courese(course_name,course_price,course_period,course_teacher)
        print("管理员%s创建\n\t课程名:%s\t课程价格:%s\t 课程周期:%s\t 课程授课老师:%s \n成功!!!"
              %(self.admin_name,course.course_name,course.course_price,course_period,course_teacher))
        #以pickle方式存储对象到文件中去
        with open('../db/%s_course_object.pickle'%(self.admin_name),mode='ab') as f :
            f.write(pickle.dumps(course))
            print("课程对象已写入到%s_course_object.pickle文件中!!"%(self.admin_name))
        # print(val)
        # data = pickle.loads(val)
        # print(data, type(data))
    def show_course(self):
        if os.path.exists('../db/%s_course_object.pickle'%(self.admin_name)):
            with open('../db/%s_course_object.pickle'%(self.admin_name),mode='rb') as f :
                try:
                    print('目前已创建如下课程\n')
                    while True:
                        data = pickle.load(f)
                        print("\t课程名:%s\t课程价格:%s\t 课程周期:%s\t 课程授课老师:%s \n"
                              %(data.course_name,data.course_price,data.course_period,data.course_teacher))
                except Exception as e:
                    print("课程从%s_course_object.pickle文件加载完毕!!"%(self.admin_name))
        else:
            print('还未创建课程,请创建课程后,再进入此菜单!!')

    def create_student(self):
        student_name = input("%s请输入需要创建的学生姓名:"%(self.admin_name))
        student_pwd = input("%s请输入需要分配的学生密码:"%(self.admin_name))
        student = Student(student_name,student_pwd)
        print("管理员%s完成创建学生\n\t学生姓名:%s\t默认密码:%s\n "
              %(self.admin_name,student.student_name,student.student_pwd))
        # 以pickle方式存储对象到文件中去
        with open('../db/%s_student_object.pickle' % (self.admin_name), mode='ab') as f:
            f.write(pickle.dumps(student))
            print("学生对象已写入到%s_student_object.pickle文件中!!" % (self.admin_name))

    def show_student(self):
        if os.path.exists('../db/%s_student_object.pickle' % (self.admin_name)):
            with open('../db/%s_student_object.pickle' % (self.admin_name), mode='rb') as f:
                try:
                    print('目前已有如下学生\n')
                    while True:
                        data = pickle.load(f)
                        print('\t学生姓名:%s\t默认密码:%s\n'%(data.student_name,data.student_pwd))
                except Exception as e :
                    print("所有学生信息,从%s_student_object.pickle文件加载完毕!!" % (self.admin_name))
        else:
            print("还未创建学生!!请先创建学生,再进入此菜单!")
    def show_student_course(self):
        student_list = []
        if os.path.exists('../db/%s_student_object.pickle' % (self.admin_name)):
            with open('../db/%s_student_object.pickle' % (self.admin_name), mode='rb') as f:
                try:
                    print('所有学生选课信息如下\n')
                    while True:
                        data = pickle.load(f)
                        # print('\t学生姓名:%s\t默认密码:%s\n' % (data.student_name, data.student_pwd))
                        student_list.append(data.student_name)
                except EOFError:
                    #获取有学生信息到列表成功
                    # print(student_list)
                    for item in student_list:
                        if os.path.exists('../db/%s_student_course.txt'%(item)):
                            print('学生%s的选课信息如下:'%(item))
                            with open('../db/%s_student_course.txt'%(item),mode='r',encoding='utf-8') as f :
                                print(f.read())
                        else:
                            print('学生%s还未选课!!'%(item))
        else:
            print("还未创建学生!!请先创建学生,再进入此菜单!")
    def quit(self):
        print("管理员%s退出学生选课系统成功!"%(self.admin_name))
        sys.exit()
# def get_md5(data):
#     obj = hashlib.md5("frantic20190921".encode('utf-8'))
#     obj.update(data.encode('utf-8'))
#     return obj.hexdigest()



# 创建初始化管理员
admin = Admin('ljb', 'ljb')
class Student(object):
    def __init__(self, name, pwd, courese=None):
        self.student_name = name
        self.student_course = courese
        self.student_pwd = pwd

    def avaliable_course(self):  # 查看所有课程
        if os.path.exists('../db/%s_course_object.pickle'%(admin.admin_name)):
            with open('../db/%s_course_object.pickle'%(admin.admin_name),mode='rb') as f:
                try:
                    print('目前如下课程可选\n')
                    while True:
                        data = pickle.load(f)
                        print("\t课程名:%s\t课程价格:%s\t 课程周期:%s\t 课程授课老师:%s \n"
                              % (data.course_name, data.course_price, data.course_period, data.course_teacher))
                except EOFError:
                    pass
        else:
            print("管理员还未创建课程,或者课程信息已被删除,请联系管理员%s!!"%(admin.admin_name))
    def select_course(self):  # 选择课程
        #从pickle文件中获取课程信息,插入到课程字典中用于和输入做匹配
        course_dic = {'course_name':[],'course_price':[],'course_period':[],'course_teacher':[]}
        if os.path.exists('../db/%s_course_object.pickle' % (admin.admin_name)):
            with open('../db/%s_course_object.pickle'%(admin.admin_name),mode='rb') as f:
                try:
                    while True:
                        data = pickle.load(f)
                        course_dic['course_name'].append(data.course_name)
                        course_dic['course_price'].append(data.course_price)
                        course_dic['course_period'].append(data.course_period)
                        course_dic['course_teacher'].append(data.course_teacher)
                except EOFError:
                    pass
            select_course_namme = input("请输入课程名:")
            if select_course_namme in course_dic['course_name']:
                index = course_dic['course_name'].index(select_course_namme)
                print("如下课程被选中")
                print("\t课程名:%s\t课程价格:%s\t 课程周期:%s\t 课程授课老师:%s \n"
                      %(course_dic['course_name'][index],
                        course_dic['course_price'][index],
                        course_dic['course_period'][index],
                        course_dic['course_teacher'][index]))
                with open('../db/%s_student_course.txt'%(self.student_name),mode='a',encoding='utf-8') as f:
                    content = '课程名:%s\t课程价格:%s\t课程周期:%s\t课程授课老师:%s\n'\
                              %(course_dic['course_name'][index],
                                course_dic['course_price'][index],
                                course_dic['course_period'][index],
                                course_dic['course_teacher'][index])
                    f.write(content)
                    print('课程已写入%s_student_course.txt文件中'%(self.student_name))

            else:
                print("输入错误!!")
        else:
            print("管理员还未创建课程,或者课程信息已被删除,请联系管理员%s!!"%(admin.admin_name))

    def own_course(self):  # 查看所选课程
        if os.path.exists('../db/%s_student_course.txt'%(self.student_name)):
            print('学生%s已选如下课程'%(self.student_name))
            with open('../db/%s_student_course.txt'%(self.student_name),mode='r',encoding='utf-8') as f:
                print(f.read())
        else:
            print('学生%s还未选择课程,请选择课程后再进入此菜单'%(self.student_name))


    def quit(self):  # 退出程序
        print("学生%s退出学生选课系统成功!" % (self.student_name))
        sys.exit()


def login(name,pwd):
    # regist(admin.admin_name, admin.admin_pwd)
    #进入管理员系统
    while name ==admin.admin_name and pwd ==admin.admin_pwd:
        print('欢迎管理员%s登录系统'%(admin.admin_name))
        func_dict = {'1': admin.create_course, '2': admin.create_student, '3': admin.show_course,
                     '4': admin.show_student, '5': admin.show_student_course, '6': admin.quit}
        choice = input(">>>>>>管理员%s请选择需要做的操作:\n"
                       "\t1.创建课程\n"
                       "\t2.创建学生学生账号\n"
                       "\t3.查看所有课程\n"
                       "\t4.查看所有学生\n"
                       "\t5.查看所有学生的选课情况\n"
                       "\t6.退出程序:\n"
                       % (name))
        if choice in func_dict.keys():
            func_dict[choice]()
        else:
            print("输入错误!!")
    #进入学生系统
    if os.path.exists('../db/%s_student_object.pickle'%(admin.admin_name)):
        with open('../db/%s_student_object.pickle' % (admin.admin_name), mode='rb') as f:
            try:
                while True:
                    data = pickle.load(f)
                    while name==data.student_name and pwd == data.student_pwd:
                        print("欢迎学生%s登录学生选课系统!!"%(data.student_name))
                        new_student =Student(data.student_name,data.student_pwd)
                        func_dict = {'1':new_student.avaliable_course,'2':new_student.select_course,'3':new_student.own_course,'4':new_student.quit}
                        # func_dict = {'1':data.avaliable_course,'2':data.select_course,'3':data.own_course,'4':data.quit}
                        choice = input(">>>>>>学生%s请选择需要做的操作:\n"
                                       "\t1.查看所有课程\n"
                                       "\t2.选择课程\n"
                                       "\t3.查看所选课程\n"
                                       "\t4.退出程序\n"
                                       % (name))
                        if choice in func_dict.keys():
                            func_dict[choice]()
                        else:
                            print("输入错误!!")
            except EOFError:
                pass
    else:
        print("学生用户信息未创建,或学生用户信息文件%s_student_object.pickle被删除,请注意!!!"%(admin.admin_name))
    print('用户名或者密码输入错误,自动退出系统!!')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值