1. 程式人生 > >[python]選課系統

[python]選課系統

選課系統

1.學校檢視

建立班級;建立課程;建立老師;顯示班級;顯示課程;顯示老師

2.教師檢視

顯示班級;顯示班級學生

3.學生檢視

註冊

程式包含五個類:學校,課程,班級,老師,學生

班級中關聯課程和學生,

老師中關聯所教的班級

一個班級一個課程一個老師,

一個課程可建多個班級

github:https://github.com/nasnan/CourseSystem

/conf/common.py

def checkInput(list):
    while 1:
        inputItemp = input()
        if inputItemp in list:
            return inputItemp
        else:
            print("輸入錯誤,請重新輸入")


def checkRepeat(item,list,name):
    if item in list:
        print("%s已存在,輸入y修改現有資料,輸入q返回" %name)
        chooseList = ["y", "q"]
        choose = checkInput(chooseList)
        if choose == "y":
            return 1
        elif choose == "q":
            return 0

def wait():
    input("輸入任意鍵返回")


/conf/settings.py

def checkInput(list):
    while 1:
        inputItemp = input()
        if inputItemp in list:
            return inputItemp
        else:
            print("輸入錯誤,請重新輸入")


def checkRepeat(item,list,name):
    if item in list:
        print("%s已存在,輸入y修改現有資料,輸入q返回" %name)
        chooseList = ["y", "q"]
        choose = checkInput(chooseList)
        if choose == "y":
            return 1
        elif choose == "q":
            return 0

def wait():
    input("輸入任意鍵返回")


/core/star.py

from main import *

start = start()
start.welcome()


/core/main.py

import sys
import os

BASE_DIR =os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

from conf.settings import *
from conf.common import *
from database.dbapi import *
from modules.school import School


class start(object):


    def welcome(self):

        choose_dict ={
            "1": schoolManage,
            "2":teacherManage,
            "3":studentManage,
            "q":exit
        }


        while 1:
            print("請選擇:\n1.學校檢視\n2.教師檢視\n3.學生檢視\nq.退出")
            choose = checkInput(choose_dict)
            choose_dict[choose]()







# 學校檢視
class schoolManage(object):
    def __init__(self):
        self.school_db = load_data_from_db(database_dir)    #將本地資料讀到self.school_db
        self.welcome()


    def welcome(self):

        choose_dict = {
            "1":self.createClass,
            "2":self.createCourse,
            "3":self.createTeacher,
            "4":self.showClass,
            "5":self.showCourse,
            "6":self.showTeacher,
            "q":exit
        }


        while 1 :
            print("請選擇:\n1.建立班級\n2.建立課程\n3.建立老師\n4.顯示班級\n5.顯示課程\n6.顯示老師\nq.退出")
            choose = checkInput(choose_dict)
            choose_dict[choose]()
            wait()


    def saveChange(self):
        write_new_to_db(database_dir, self.school_db)


    def createClass(self):
        className = input("請輸入班級號:")
        if checkRepeat(className,self.school_db.schoolClass,"班級") == 0:
            return 0

        courseList = self.school_db.schoolCourse
        self.showCourse()
        print("請輸入該班級關聯的課程:")
        classCourse = checkInput(courseList)
        courseObj = self.school_db.schoolCourse[classCourse]
        self.school_db.createClass(className,courseObj)
        print("建立成功!")
        self.saveChange()




    def createCourse(self):
        courseName = input("請輸入課程名字:")

        if checkRepeat(courseName,self.school_db.schoolCourse,"課程") == 0:
            return 0
        courseTime = input("請輸入課程課時:")
        coursePrice = input("請輸入課程價格:")
        self.school_db.createCourse(courseName,courseTime,coursePrice)
        print("建立成功!")
        self.saveChange()


    # 建立老師
    def createTeacher(self):
        teacherName = input("請輸入老師姓名:")
        if checkRepeat(teacherName,self.school_db.schoolTeacher,"老師") == 0:
            return 0
        busyClass = set()
        for tea in self.school_db.schoolTeacher:
            for cla in self.school_db.schoolTeacher[tea].teachClass:
                busyClass.add(cla)
        while 1:
            print("請輸入任課班級")
            self.showClass()
            classNumber = checkInput(self.school_db.schoolClass)
            if classNumber not in busyClass:
                break
            else:
                print("此班級已有任課老師!")
        classObj = self.school_db.schoolClass[classNumber]
        self.school_db.createTeacher(teacherName,classObj)
        print("建立成功!")
        self.saveChange()



    def showClass(self):
        self.school_db.showClass()

    def showCourse(self):
        self.school_db.showCourse()


    def showTeacher(self):
        self.school_db.showTeacher()






# 老師檢視
class teacherManage(object):

    def __init__(self):
        school_db = load_data_from_db(database_dir)
        self.teacher_db = school_db.schoolTeacher
        self.welcome()


    def welcome(self):
        print("請輸入老師姓名:")
        teacherName = checkInput(self.teacher_db)
        self.teacherObj = self.teacher_db[teacherName]
        self.class_list = self.teacherObj.teachClass
        print("wlecome!")

        while 1:
            choose_dic = {
                "1":self.showClass,
                "2":self.showStudent,
                "q":exit
            }

            print("請選擇:\n1.檢視所教班級\n2.檢視班級學生\nq.退出")
            choose = checkInput(choose_dic)
            choose_dic[choose]()
            wait()



    def showClass(self):
        print("所教班級有:")
        for cla in self.class_list:
            print(self.class_list[cla].className)


    def showStudent(self):
        print("選擇要檢視的班級:")
        className = checkInput(self.class_list)
        classObj = self.class_list[className]
        print("%s 課程:%s 學生列表:%s" %(classObj.className,classObj.classCourse.courseName,classObj.classStudent))



# 學生檢視
class studentManage(object):

    def __init__(self):
        self.school_db = load_data_from_db(database_dir)
        self.student_list = self.school_db.schoolStudent
        self.welcome()


    def welcome(self):
        while 1:
            print("請選擇:\n1.註冊\n2.登陸\nq.退出")
            choose_dict = {
                "1":self.signUp,
                "q":exit
            }

            choose = checkInput(choose_dict)
            choose_dict[choose]()


    # 註冊
    def signUp(self):

        while 1:
            studentName = input("請輸入名字:")
            if studentName not in self.student_list:
                break
            else:
                print("名字已存在!")

        self.school_db.showClass()
        print("請選擇班級:")
        className = checkInput(self.school_db.schoolClass)
        courseObj = self.school_db.schoolClass[className].classCourse
        print("此班級的課程%s課時為:%s,價格為%s。" %(courseObj.courseName,courseObj.courseTime,courseObj.coursePrice))
        print("輸入y確認,q退出!")
        choose_list = ["q","y"]
        choose = checkInput(choose_list)
        if choose == "y":
            self.school_db.createStudent(studentName,className)
            self.saveChange()
            print("註冊成功!")
        else:
            exit()



    def saveChange(self):
        write_new_to_db(database_dir, self.school_db)



/database/dbapi.py

import pickle


# 載入資料
def load_data_from_db(dbname):
    dbtemp = open(dbname,'rb')
    p =pickle.load(dbtemp)
    dbtemp.close()
    return p


# 寫入資料庫
def write_new_to_db(dbname,data):
    dbtemp = open(dbname,'wb')
    p = pickle.dump(data,dbtemp)
    dbtemp.close()


/database/initDb.py

import sys
import os

BASE_DIR =os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

from modules.school import School
from dbapi import *

school_Db = School("sch1")

write_new_to_db("database.db",school_Db)



/modules/classes.py

class Class(object):
    def __init__(self,className,classCourse):
        self.className = className
        self.classCourse = classCourse
        self.classStudent = {}

    def addStudent(self,studentName,studentObj):
        self.classStudent[studentName] = studentObj


/database/major.py

class Course(object):
    def __init__(self,courseName,courseTime,coursePrice):
        self.courseName = courseName
        self.courseTime = courseTime
        self.coursePrice = coursePrice


/database/school.py

from modules.classes import Class
from modules.major import Course
from modules.student import Student
from modules.teacher import Teacher


class School(object):
    def __init__(self,schoolName):
        self.schoolName = schoolName
        self.schoolCourse = {}
        self.schoolClass = {}
        self.schoolTeacher = {}
        self.schoolStudent = {}


    # 建立課程
    def createCourse(self,courseName,courseTime,coursePrice):
        courseObj = Course(courseName,courseTime,coursePrice)
        self.schoolCourse[courseName] = courseObj


    # 建立班級
    def createClass(self,className,courseObj):
        classObj = Class(className,courseObj)
        self.schoolClass[className] = classObj

    # 建立老師
    def createTeacher(self,teacherName,teachClass):
        teacherObj = Teacher(teacherName)
        teacherObj.teacherAddClass(teachClass.className,teachClass)   #給老師分班級
        self.schoolTeacher[teacherName] = teacherObj



    # 學生註冊
    def createStudent(self,studentName,className):
        studentObj = Student(studentName)
        classObj = self.schoolClass[className]
        self.schoolStudent[studentName] = studentObj    # 學校學生字典里加新學生
        classObj.addStudent(studentName,studentObj)     # 班級類中加學生
        self.schoolClass[className] = classObj      # 更新學校的班級字典


    # 顯示課程
    def showCourse(self):
        for course in self.schoolCourse:
            course = self.schoolCourse[course]
            print("課名:%s 課時:%s 價格:%s" %(course.courseName,course.courseTime,course.coursePrice))

    # 顯示班級
    def showClass(self):
        for oneClass in self.schoolClass:
            oneClass = self.schoolClass[oneClass]
            stuList = []
            for stu in oneClass.classStudent:
                stuList.append(oneClass.classStudent[stu].studentName)
            print("班級:",oneClass.className," 課程:",oneClass.classCourse.courseName," 學生:",stuList)

    # 顯示老師
    def showTeacher(self):
        for teacher in self.schoolTeacher:
            teacher = self.schoolTeacher[teacher]
            cla = []
            for classes in teacher.teachClass:
                cla.append(teacher.teachClass[classes].className)
            print("姓名:%s 所教班級:%s" %(teacher.teacherName,cla))


/database/student.py

class Student(object):
    def __init__(self,studentName):
        self.studentName = studentName


/database/teacher.py

class Teacher(object):
    def __init__(self,teacherName):
        self.teacherName = teacherName
        self.teachClass = {}

    def teacherAddClass(self,classname,classObj):

        self.teachClass[classname] = classObj