1 / 17

Lecture 28

Lecture 28. More classes that use classes. Review of the Employee Class. Class Employee The data members were Name # a string hourlyRate #a floating point number Dept #a character like “H” The methods getName (self) g et hourlyRate (self) getDept (self)

swain
Download Presentation

Lecture 28

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 28 More classes that use classes

  2. Review of the Employee Class • Class Employee • The data members were • Name # a string • hourlyRate #a floating point number • Dept #a character like “H” • The methods • getName(self) • get hourlyRate(self) • getDept(self) • setRate(self) • setDept(self) • calc_pay(self,nHrs)

  3. Write calc_paychecks() • This function is not a member of the Employee class • It has two parameters: • A list of members of the Employee class • A list of exactly the same size which contains the hours worked by the corresponding employee

  4. getEmployees_by_rate() • This function takes two parameters • A list of Employee objects • A specified pay rate • This function returns a list of Employee objects – each is a copy of one of the Employee objects in the original list who has a pay rate greater than or equal to the specified one.

  5. Back to the Course Class

  6. class Course: def __init__(self, dept, code, max,hrs, aTitle): def __str__(self): s = self.dept + self.nbr + " " + self.title return s def get_dept(self): def get_nbr(self): def get_max(self): def get_hrs(self): def get_count(self): def get_students(self): def add_student(self, name):

  7. A program to create a list of Courses import course import string def main(): courseList = [] infile = open("courseData.txt", "r") for line in infile: data = line.split() title = string.join(data[4:]) c = course.Course(data[0], data[1],data[3], data[2],title) courseList.append(c) print item main()

  8. The Registrar Class • What data does the registrar need? • A list of available courses • What operations should the registrar have on this list • Process a student request for courses • Find a course in this list by dept and number • Add a student to a course • Delete a student from a course

  9. The constructor • def __init__(self, filename): • #initialize the course list by reading all the details from the file name filename

  10. The constructor • def __init__(self, filename): • self.courseList = [] • infile = open(filename, "r") • for line in infile: • data = line.split() • title = string.join(data[4:]) • c =Course(data[0], data[1],data[3], data[2],title) • self.courseList.append(c)

  11. The only “get” • def getList(self): • return self.courseList

  12. find_course() • def find_course(self, dept, nbr): • #return the Course object whose department and number match the parameters

  13. find_course() • def find_course(self, dept, nbr): • for item in self.courseList: • if item.get_dept()==dept and item.get_nbr()== nbr: • return item

  14. add_student() • def add_student(self, dept, nbr, name): • #add the student with the name “name” to the • #course whose department and number match the parameters

  15. add_student() • def add_student(self, dept, nbr, name): • c = self.find_course(dept,nbr) • c.add_student(name)

  16. process_request() • def process_request(self, cList, sName): # this has two parameters, cList which looks like [(“CSC”, “117”), (“ECO”, “210”), (“BIO”, “110”)] and the name of the student. The function puts the student into each course in the list.

  17. process_request() • def process_request(self, cList, sName): • for (x,y) in cList: • self.add_student(x, y, sName)

More Related