选课系统作业

Posted Optimus丶G

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了选课系统作业相关的知识,希望对你有一定的参考价值。

一、作业需求

程序名称: 选课系统

角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程 
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
6. 创建讲师角色时要关联学校, 
7. 提供两个角色接口
8. 学员视图, 可以注册, 交学费, 选择班级,
9. 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩 
10. 管理视图,创建讲师, 创建班级,创建课程

11. 上面的操作产生的数据都通过pickle序列化保存到文件里
二、文件目录

三、源代码

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Sam"
# Date: 2017/7/4

import sys,os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from src import core

if __name__ == \'__main__\':
    core.func()
start.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Sam"
# Date: 2017/7/4

STUDENTDB_PATH=r\'F:\\python文件\\选课系统\\db\\student\'
TEACHERDB_PATH=r\'F:\\python文件\\选课系统\\db\\teacher\'
COURSE_PATH=r\'F:\\python文件\\选课系统\\db\\course\'
SCHOOL_PATH=r\'F:\\python文件\\选课系统\\db\\school\'
CLASS_PATH=r\'F:\\python文件\\选课系统\\db\\class\'
settings.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Sam"
# Date: 2017/7/4



from src import models
from conf import settings
import pickle
import os

s1 = models.School(\'北京校区\', \'中国北京\')
s2 = models.School(\'上海校区\', \'中国上海\')
python = s1.create_course(\'Python\', 19800, \'6mons\')
linux = s1.create_course(\'Linux\', 17800, \'4mons\')
go = s2.create_course(\'Go\', 9000, \'4mons\')
school_obj=None

def func():
    while True:
        print(\'欢迎进入老男孩选课系统\'.center(30,\'*\'),
                  \'\\n1.北京校区\\n\'
                  \'2.上海校区\\n\'
                  \'3.退出程序\')
        res=input(\'请选择学校: \').strip()
        global school_obj
        if res == \'1\':
            school_obj=s1
        elif res == \'2\':
            school_obj=s2
        elif res == \'3\':
            break
        else:
            print(\'输入错误\')
            continue

        while True:
            print(\'请选择视图\'.center(30,\'*\'))
            cmd=input(\'1.学生视图\\n\'
                      \'2.老师视图\\n\'
                      \'3.管理员视图\\n\'
                      \'4.返回上一级\\n\'
                      \'5.退出选课系统: \').strip()
            if cmd == \'1\':
                student_view()
            elif cmd == \'2\':
                teacher_view()
            elif cmd == \'3\':
                admin_viwe()
            elif cmd == \'4\':
                break
            elif cmd == \'5\':
                exit()
            else:
                print(\'输入错误,请重新输入!\')
                continue
    return school_obj

def student_view():
    \'\'\'学生视图\'\'\'
    while True:
        tag = True
        print(\'欢迎进入学生视图,请选择功能\'.center(30,\'*\'))
        cmd=input(\'1.注册信息\\n\'
                  \'2.登录\\n\'                          #循环选择功能
                  \'3.返回上级\\n\'
                  \'4.退出程序: \').strip()
        if cmd == \'1\':
            while True:
                print(\'欢迎注册学生信息\'.center(30, \'-\'))
                name=input(\'name: \').strip()
                name_id=input(\'name_id: \').strip()      #注册信息
                age=input(\'age: \').strip()
                sex=input(\'sex: \').strip()
                if not name:
                    print(\'姓名必填\')
                    continue
                break
            student_obj=models.Student(name,name_id,age,sex)
            student_obj.save()
        elif cmd == \'2\':
            while tag:
                print(\'请登录\'.center(30,\'-\'))
                name = input(\'请输入用户名: \')
                name_id = input(\'请输入用户id: \')                #登录,如果数据库中有这个人的文件,继续,否则打印用户不存在
                res = os.listdir(settings.STUDENTDB_PATH)
                for item in res:
                    file_path = r\'%s\\%s\' % (settings.STUDENTDB_PATH, item)
                    with open(file_path, \'rb\') as f:
                        student_obj = pickle.load(f)
                        if name == student_obj.name and name_id == student_obj.user_id:
                            while tag:
                                cmd=input(\'登录成功\\n\'
                                      \'1.交学费\\n\'
                                      \'2.选择班级\\n\'
                                      \'3.返回上级\\n\'
                                      \'4.退出程序\\n\'
                                      \'请选择功能: \').strip()
                                if cmd == \'1\':          #交学费
                                    money = int(input(\'请输入缴纳金额: \'))
                                    student_obj.tuition+=money
                                    print(\'缴纳成功,学费余额为%s\' %student_obj.tuition)
                                    student_obj.save()
                                elif cmd == \'2\':        #选择班级
                                    res = os.listdir(settings.CLASS_PATH)
                                    for item in res:
                                        file_path = r\'%s\\%s\' % (settings.CLASS_PATH, item)
                                        with open(file_path, \'rb\') as f:
                                            class_obj = pickle.load(f)
                                            print(\'班级名称:<%s> 班级所学课程<%s>\' % (class_obj.name, class_obj.course))
                                    course_cmd = input(\'请选择班级:\')
                                    res = os.listdir(settings.CLASS_PATH)
                                    for item in res:
                                        file_path = r\'%s\\%s\' % (settings.CLASS_PATH, item)
                                        with open(file_path, \'rb\') as f:
                                            class_obj = pickle.load(f)
                                        if course_cmd == class_obj.name:
                                            for i in class_obj.student:
                                                if student_obj.id == i.id:
                                                    print(\'班级已有此人\')         #如果班级里已有此人,则结束村换
                                                    tag=False
                                                else:
                                                    class_obj.student.append(student_obj)
                                                    student_obj.student_class.append(class_obj)
                                                    student_obj.save()
                                                    class_obj.save()
                                                    print(\'班级添加成功\')
                                                    student_obj.tell_info()
                                elif cmd == \'3\':
                                    tag=False
                                elif cmd == \'4\':
                                    exit()
                                else:
                                    continue
                        else:
                            print(\'用户不存在\')
                            tag=False
        elif cmd == \'3\':
            return
        elif cmd == \'4\':
            exit()
        else:continue

def teacher_view():
    \'\'\'讲师视图\'\'\'
    tag = True  #添加一个标志位
    while tag:
        print(\'请登录\'.center(30,\'-\'))
        name=input(\'请输入用户名: \').strip()
        name_id=input(\'请输入用户id: \').strip()
        res = os.listdir(settings.TEACHERDB_PATH)
        for item in res:
            file_path = r\'%s\\%s\' % (settings.TEACHERDB_PATH, item)
            with open(file_path, \'rb\') as f:
                teacher_obj = pickle.load(f)
                if name == teacher_obj.name and name_id == teacher_obj.user_id:
                    while tag:
                        print(\'欢迎进入讲师视图\'.center(30,\'*\'))
                        cmd=input(\'1.管理班级\\n\'
                                  \'2.选择上课班级\\n\'
                                  \'3.查看班级学员列表\\n\'
                                  \'4.修改学员成绩\\n\'
                                  \'5.返回上一级\\n\'
                                  \'6.退出选课系统\\n\'
                                  \'请选择功能: \').strip()
                        if cmd == \'1\':
                            for i in teacher_obj.teacher_class:
                                i.tell_info()
                        elif cmd == \'2\':
                            res = os.listdir(settings.CLASS_PATH)
                            for item in res:
                                file_path = r\'%s\\%s\' % (settings.CLASS_PATH, item)
                                with open(file_path, \'rb\') as f:
                                    class_obj = pickle.load(f)
                                    print(\'班级名称:<%s> 班级所学课程<%s>\' % (class_obj.name, class_obj.course))
                            class_cmd = input(\'请选择班级:\')
                            res = os.listdir(settings.CLASS_PATH)
                            for item in res:
                                file_path = r\'%s\\%s\' % (settings.CLASS_PATH, item)
                                with open(file_path, \'rb\') as f:
                                    class_obj = pickle.load(f)
                                if class_obj.name == class_cmd:
                                    class_obj.teacher.append(teacher_obj)
                                    teacher_obj.teacher_class.append(class_obj)
                                    teacher_obj.save()
                                    class_obj.save()
                                    teacher_obj.tell_info()
                        elif cmd == \'3\':
                            for i in teacher_obj.teacher_class:
                                for j in i.student:
                                    j.tell_info()
                        elif cmd == \'4\':
                            name=input(\'请输入想要修改的学生姓名:\').strip()
                            name_id=input(\'请输入学生id: \').strip()
                            num=int(input(\'请输入要修改的成绩: \').strip())
                            res = os.listdir(settings.STUDENTDB_PATH)
                            for item in res:
                                file_path = r\'%s\\%s\' % (settings.STUDENTDB_PATH, item)
                                with open(file_path, \'rb\') as f:
                                    student_obj = pickle.load(f)
                                    if name == student_obj.name and name_id == student_obj.user_id:
                                        student_obj.grade+=num
                                        print(\'%s 的成绩已修改为 %s\' %(student_obj.name,num))
                                        student_obj.save()
                        elif cmd == \'5\':
                            tag=False
                        elif cmd == \'6\':
                            exit()
                        else:
                            continue

def admin_viwe():
    \'\'\'管理员视图\'\'\'
    print(\'请用管理员的身份登录,用户名:sam,密码:666\'.center(30,\'*\'))  #管理员有固定的用户名密码
    name=input(\'请输入用户名: \')
    password=input(\'请输入密码: \')
    if name == \'sam\' and password == \'666\':
        while True:
            print(\'欢迎进入管理员视图\'.center(30,\'-\'))
            cmd=input(\'1.创建讲师\\n\'
                      \'2.创建班级\\n\'
                      \'3.创建课程\\n\'
                      \'4.返回\\n\'
                      \'5.退出选课系统\\n\'
                      \'请选择功能: \')
            if cmd == \'1\':
                name=input(\'请输入讲师姓名: \').strip()
                user_id=input(\'请输入讲师id: \').strip()
                age=input(\'请输入讲师年纪: \').strip()
                sex=input(\'请输入讲师性别: \').strip()
                salary=input(\'请输入讲师薪资: \').strip()
                if not name:
                    print(\'名字不能为空\')
                    continue
                teacher = school_obj.create_teacher(name,user_id,age,sex,salary)
                teacher.save()
                school_obj.save()
                print(\'%s 老师的信息创建成功\' %teacher.name)
                teacher.tell_info()
            elif cmd == \'2\':
                name = input(\'请输入班级名: \').strip()
                course = input(\'请输入班级课程: \').strip()
                if not name or not course:
                    print(\'输入错误\')
                    continue
                class_obj=school_obj.create_class(name,course)
                class_obj.save()
                school_obj.save()
                print(\'%s 班级创建成功\' %class_obj.name)
                class_obj.tell_info()
            elif cmd == \'3\':
                course = input(\'请输入课程名字: \').strip()
                price = input(\'请输入课程价钱: \').strip()
                period = input(\'请输入课程周期: \').strip()
                if not course or not price or not period:
                    print(\'输入错误\')
                    continue
                course_obj = school_obj.create_course(course,price,period)
                course_obj.save()
                school_obj.save()
                print(\'%s 课程创建成功\' %course_obj.course)
                course_obj.tell_course()
            elif cmd == \'4\':
                return
            elif cmd == \'5\':
                exit()
            else:
                continue
    else:
        print(\'用户名密码错误\')

if __name__ == \'__main__\':
    func()
core.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Sam"
# Date: 2017/7/4
from conf import settings
import pickle
import hashlib
import os

class School:
    \'\'\'学校类\'\'\'
    def __init__(self,school_name,school_site):
        self.school_name=school_name
        self.school_site=school_site
        self.school_courses=[]  #为学校所有的课程创建一个空列表
        self.school_terchers=[] #创建一个空列表存放老师
        self.school_class=[]    #创建一个空列表存放班级

    def create_class(self,name,course):
        \'\'\'创建班级\'\'\'
        class_obj=Class(name,course)
        self.school_class.append(class_obj)
        return class_obj

    def create_course(self,course,price,period):
        \'\'\'创建课程\'\'\'
        course_obj=Course(course,price,period)
        self.school_courses.append(course_obj)
        return course_obj

    def create_teacher(self,name,user_id,age,sex,salary):
        \'\'\'创建老师\'\'\'
        teacher_obj=Teacher(name,user_id,age,sex,salary)
        self.school_terchers.append(teacher_obj)
        return teacher_obj

    def create_student(self,name,user_id,age,sex,classroom):
        \'\'\'创建学生\'\'\'
        student_obj=Student(name,user_id,age,sex)
        classroom.student.append(student_obj)
        return student_obj

    def save(self):
        \'\'\'保存信息\'\'\'
        with open(r\'%s\\%s\' %(settings.SCHOOL_PATH,self.school_name),\'wb\') as f:
            pickle.dump(self,f)

class Class:
    \'\'\'班级类\'\'\'
    def __init__(self,name,course):
        self.name=name
        self.teacher=[]
        self.course=course
        self.student=[]
    def tell_info(self):
        \'\'\'查看班级信息\'\'\'
        print(\'\'\'
        -------%s info---------
        name:%s
        teacher:%s
        course:%s
        student:%s
        \'\'\' %(self.name,self.name,self.teacher,self.course,self.student))
    def save(self):
        \'\'\'保存班级信息\'\'\'
        with open(r\'%s\\%s\' %(settings.CLASS_PATH,self.name),\'wb\') as f:
            pickle.dump(self,f)

class Course:
    \'\'\'课程类\'\'\'
    def __init__(self,course,price,period):
        self.course=course
        self.price=price
        self.period=period
    def tell_course(self):
        \'\'\'查看课程信息\'\'\'
        print(\'\'\'
        --------%s info---------
        COURSE:%s
        PRICE:%s
        PERIOD:%s
        \'\'\' %(self.course,self.course,self.price,self.period))
    def save(self):
        \'\'\'以上是关于选课系统作业的主要内容,如果未能解决你的问题,请参考以下文章

Python作业-选课系统

选课系统作业

作业 选课系统

Day7作业:选课系统

第六周作业——选课系统

作业:选课系统