天天看點

OOP-學生選課系統的python實作

日期:2019-11-13

作者:老李

目标:

OOP-學生選課系統的python實作

過程:

1.調包:

import numpy as np
           

2.學生類:

class Student:
    def __init__(self, name, idnum, number, course):
        self.name = name
        self.idnum = idnum
        self.number = number
        self.course = course
    def addCourse(self, newcourse):
        self.course.append(newcourse)
    def viewInfo(self):
        print("=" * 30)
        print("the information of this student: ")
        print("=" * 30)
        print("Name: ", self.name)
        print("ID: ", self.idnum)
        print("Telephone Number: ", self.number)
        print("Course: ", self.course)
           

3.課程類;

class Course:
 def __init__(self, index, courseName, teacherName):
  self.index = index
  self.courseName = courseName
  self.teacherName = teacherName
 def arrTeacher(self, newteacher):
  self.teacherName = newteacher
 def viewInfo(self):
  print("=" * 30)
  print("the information of this course: ")
  print("=" * 30)
  print("the index of this course is ", self.index)
  print("the name of this course is ", self.courseName)
  print("the teacher of this course is ", self.teacherName)
           

4.教師類

class Teacher:
 def __init__(self, index, name, tele, course):
  self.index = index
  self.name = name
  self.tele = tele
  self.course = course
 def viewInfo(self):
  print("=" * 30)
  print("the information of this teacher: ")
  print("=" * 30)
  print("the index of this teacher is: ", self.index)
  print("the name of this teacher is: ", self.name)
  print("the telephone number of this teacher is: ", self.tele)
  print("this teacher teaches following course(s): ", self.course)
           

5.輸入資料并存儲在清單中

#data
#create the student's information
student = []
for i in range(20):
 k = Student("student"+str(i+1), i+1, 135*100000000+(i+4)*100+(i+3)*10+i, [])
 student.append(k)
#create the course infomation
course = []
course1 = Course(1, "numbericalAnalysis", " ")
course.append(course1)
course2 = Course(2, "dataStruct", " ")
course.append(course2)
course3 = Course(3, "advancedProgram", " ")
course.append(course3)
course4 = Course(4, "CT", " ")
course.append(course4)
course5 = Course(5, "TOEFL", " ")
course.append(course5)
course6 = Course(6, "GRE", " ")
course.append(course6)
#create the teacher's information
teacher = []
teacher1 = Teacher(1, "chen", 13245454896, ["advancedProgram","dataStruct","numbericalAnalysis"])
teacher.append(teacher1)
teacher2 = Teacher(2, "yu", 13445678210, ["TOEFL", "GRE"])
teacher.append(teacher2)
teacher3 = Teacher(3, "zhao", 13564852157, ["CT","dataStruct","TOEFL"])
teacher.append(teacher3)
           

6.建立兩個函數

分别為:

(1)給課程安排老師

(2)給學生安排課程

#function
#random arrange ome teacher for each course
def ranArrTea(teacher, course):
 for i in range(6):
  courseTeacher = []
  for j in range(3):
   for k in range(len(teacher[j].course)): 
    if course[i].courseName == teacher[j].course[k]:
     courseTeacher.append(teacher[j])
  l = len(courseTeacher)
  ran = np.random.randint(0,l)
  course[i].arrTeacher(courseTeacher[ran])
#random arrange three course for each student
def ranCourse(student, course):
 for i in range(20):
  c = np.random.choice(course, 3)
  for j in range(3):
   student[i].addCourse(c[j].courseName)
           

7.安排上!

#do
#start arrange
ranArrTea(teacher, course)
ranCourse(student, course)
           

8.将學生選課情況列印出來

#show
for i in range(20):
 student[i].viewInfo()
           

結果:

OOP-學生選課系統的python實作
OOP-學生選課系統的python實作
OOP-學生選課系統的python實作
OOP-學生選課系統的python實作
OOP-學生選課系統的python實作

反思:

其實我這樣的封裝還并不完美,在更好的情況下,我應該追求資料和類以及函數分開存放,建立一些接口将函數封裝地更完美一些,并最終可以追求直接寫入檔案中。

這些以後再補充,一次不要所求太多,要先尋求些滿足感((lll¬ω¬))