170 likes | 288 Views
This lecture focuses on object-oriented programming concepts using Employee and Course management classes in Python. It covers the Employee class, its data members (name, hourly rate, and department), and methods such as getName, getHourlyRate, and calc_pay. Additionally, it introduces the Course class, detailing its attributes and methods for managing student enrollments and course listings. The lecture also demonstrates how to process course requests and add students, providing a comprehensive understanding of class interactions and methods.
E N D
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) • get hourlyRate(self) • getDept(self) • setRate(self) • setDept(self) • calc_pay(self,nHrs)
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
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.
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):
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()
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
The constructor • def __init__(self, filename): • #initialize the course list by reading all the details from the file name filename
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)
The only “get” • def getList(self): • return self.courseList
find_course() • def find_course(self, dept, nbr): • #return the Course object whose department and number match the parameters
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
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
add_student() • def add_student(self, dept, nbr, name): • c = self.find_course(dept,nbr) • c.add_student(name)
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.
process_request() • def process_request(self, cList, sName): • for (x,y) in cList: • self.add_student(x, y, sName)