1. 程式人生 > >類相關的簡單作業

類相關的簡單作業

bject eno nts cycle 查看 eache 註冊 角色 to do

角色:學校、學員、課程、講師
要求:
1. 創建北京、上海 2 所學校
2. 創建linux , python , go 3個課程 , linux\py 在北京開, go 在上海開
3. 課程包含,周期,價格,通過學校創建課程
4. 通過學校創建班級, 班級關聯課程、講師
5. 創建學員時,選擇學校,關聯班級
6. 創建講師角色時要關聯學校,
7. 提供兩個角色接口
7.1 學員視圖, 可以註冊, 交學費, 選擇班級,
7.2 講師視圖, 講師可管理自己的班級, 上課時選擇班級, 查看班級學員列表 , 修改所管理的學員的成績
7.3 管理視圖,創建講師, 創建班級,創建課程

8. 上面的操作產生的數據都通過pickle序列化保存到文件裏

#啟動類
# Author:zylong
"""
schoole 啟動類
"""
import os,sys

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

sys.path.append(BASE_DIR)

import core


core.main.run()

入口類:main

# Author:zylong

from core import homework
from core import admin
# from core import teacher
#
from core import student choose1 = { "admin":1, "teacher":2, "student":3 } def run(): # print("run") for i in choose1: print(i,choose1[i]) youchoose = int(input("選擇用戶")) if youchoose == 1: admin.run() # elif youchoose == 2: # teacher.run() #
elif youchoose == 3: # student.run()

管理員類:

#創建班級,創建課程
# python = Course("python",30,13000)
# linux = Course("linux",40,14000)
# go = Course("go",50,15000)
# curses = {
#     "python":python,
#     "linux":linux,
#     "go":go
# }
from core import homework
from core import common

adminchoose = {
    "創建班級":1,#這裏簡化,創建班級的時候,必須得有課程,要不不讓創建
    "創建課程":2,#課程不能單獨存在,必須依存班級
    "創建講師":3,
}
# courselist = list()
def run():
    while True:
        schoole = common.getSchoole()
        for i in adminchoose:
            print(i,adminchoose[i])
        yourchoose = int(input("what you want to do"))
        if yourchoose == 1:
            #創建班級
            garde = homework.Grade("Python班")
            schoole.createGrade(garde)
            print("you have create ",schoole.grades)
        elif yourchoose == 2:
            course = homework.Course("python",30,13000)
            # courselist.append(course)
            schoole.courses.append(course)
            schoole.makeCourse(schoole.grades[0], course)
        elif yourchoose == 3:
            teacher = homework.Teacher("zhang",20,"北京","python")
            #添加老師
            schoole.addTeacher(teacher)
            #為班級分配老師
            schoole.distributionTeacher(schoole.grades[0],teacher)
        elif yourchoose == 4:
            #退出程序,並保存信息,這裏保存的就是學校的信息,老師,課程,班級等信息
            common.saveInfo(schoole)
            break

所有的類(這裏不分開了)

"""

角色:學校、學員、課程、講師
要求:
1. 創建北京、上海 2 所學校
2. 創建linux , python , go 3個課程 , linux\py 在北京開, go 在上海開
3. 課程包含,周期,價格,通過學校創建課程
4. 通過學校創建班級, 班級關聯課程、講師
5. 創建學員時,選擇學校,關聯班級
6. 創建講師角色時要關聯學校,
7. 提供兩個角色接口
7.1 學員視圖, 可以註冊, 交學費, 選擇班級,
7.2 講師視圖, 講師可管理自己的班級, 上課時選擇班級, 查看班級學員列表 , 修改所管理的學員的成績
7.3 管理視圖,創建講師, 創建班級,創建課程

8. 上面的操作產生的數據都通過pickle序列化保存到文件裏


"""
#1
class Schoole:
    #通過地址創建學校,初始化學校的時候,默認開一個課程,後期可以再添加課程
    def __init__(self,addr):
        self.addr = addr
        #2.課程必須是實例裏的所以定義在實例裏面
        self.courses = []
        #學校中有班級
        self.grades = []
        #學校中有學生
        self.students = {}
        #學校中有老師
        self.teachers = {}
    #添加課程
    def addCourse(self,course):
        self.courses.append(course)
    #創建班級
    def createGrade(self,grade):
        self.grades.append(grade)
    #為班級制定課程
    def makeCourse(self,grade,course):
        grade.course = course
    #為班級制定老師
    def distributionTeacher(self,grade,teacher):
        #判定這個老師是否符合這個班級
        if teacher.know == grade.course.name:
            grade.teacher = teacher
        else:
            print("this teacher %s Not suitable %  "%(teacher.name,grade.course.name))

    #學生註冊
    def addStudent(self,student):
        self.students[student.name] = student
    #老師註冊
    def addTeacher(self,teacher):
        #判斷老師是否能夠加入到學校
        for course in self.courses:
            if course.name == teacher.know:
                self.teachers[teacher.name] = teacher

    #學生通過學校交那個課程的學費
    def payTuition(self,student,money,courseName):
        #這裏判斷交的錢是否足夠,如果足夠,將這個學生添加到那個班級裏
        for i in self.courses:
            if courseName == self.courses[i].name:
                course = self.courses[i]
        if course :
            if money >= course.price :
                print("%s is pay %s and join %s"%(student.name,money,course.name))
                #將學生添加到班級中
                # #根據課程找到老師,根據老師找到班級這裏簡化一下,班級裏面有課程,不跟隨老師變化
                # for teacher in self.teachers:
                #     if teacher.know == course.name:
                #         #將學生加入到老師所在的班級中
                #         self.grades
                for grade in self.grades:
                    if grade.course.name == course.name:
                        grade.students = list().append(student)

            else:
                print("%s money is not enough for %s"%(student.name,course.name))
        else:
            print("dont have this %s"%courseName)



#3(linux , python , go)
class Course:
    #課程包含 名稱,周期,價格
    def __init__(self,name,cycle,price):
        self.name = name
        self.cycle = cycle
        self.price = price
#4 班級,班級裏面包含,學生和講師,但是班級不能脫離學校直接存在
#實例化班級的時候,其實只需要課程(這裏也不需要,統一在學校中處理)
class Grade:
    def __init__(self,name):
        self.name = name


class Person(object):
    def __init__(self,name,age,addr):
        self.name = name
        self.age = age
        self.addr = addr

class Student(Person):
    #學生有特有的屬性,學生需要學習
    def study(self):
        print("%s is study"%self.name)

#老師會什麽知識,才能教什麽課(這裏簡單一個老師只能教一個課程)
class Teacher(Person):
    def __init__(self,name,age,addr,know):
        super(Teacher,self).__init__(name,age,addr)
        self.know = know
    def teachind(self):
        print("%s is teaching"%self.name)


#這裏實例化兩個學校

beijingschoole = Schoole("北京")
shanghaischoole = Schoole("上海")

common類:

# import os,sys
#
# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from core import homework
import pickle
def getSchoole():
    schooleList = [homework.beijingschoole, homework.shanghaischoole]
    for s in schooleList:
        print(s.addr)
    schooleaddr = input("please input your schoole")
    if schooleaddr == homework.beijingschoole.addr:
        schoole = homework.beijingschoole
    else:
        schoole = homework.shanghaischoole
    return schoole

def saveInfo(schoole):
    with open("../conf/schoole","wb") as f:
        pickle.dump(schoole,f)

def readInfo():
    with open("../conf/schoole","rb") as f:
        schoole = pickle.load(f)
        print(schoole.addr,schoole.courses)
#測試,可以發現學校的信息已經保存到文件中了
readInfo();

類相關的簡單作業