天天看点

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¬ω¬))