HW2 Solution Notes
class Course:
• Constructor needs 3 instance attributes to initialize course id, name and number of
credits
def __eq__(self, other):
1. Check if other is an instance of Course, if it is, check if self.cid == other.cid and
return accordingly
2. If other is not a Course object, return False
class Catalog:
• Constructor needs 1 instance attributes to initialize courseOfferings
def addCourse(self, cid, cname, credits):
1. Check if cid in self.courseOfferings, if it is then return ‘Course already added’
2. Otherwise, create a Course object using cid, cname and credit and add it as a
value of the key cid in the courseOffering dictionary
To remove, repeat step 1 from add, and if the key is in the dictionary, use pop() or del to
remove the item from the dictionary
class Semester:
• Constructor needs 2 instance attributes to initialize sem_num and courses
def __str__(self):
if self.courses=={}:
return ‘No courses’
Otherwise, use ‘,’.join(list of keys) to build the output string
def addCourse(self, course):
# Note that course is a Course object, so it gives you access to cid, name and # of credits
1. Check if course.cid in self.courses, if it is not, add the course to the dictionary,
where the key is the cid and the value the course object
@property
def totalCredits(self):
1. Set total to 0
2. For each course in self.courses increase total by
self.courses[course].credits
class Loan:
• Constructor needs 2 instance attributes to initialize amount and loan_id. Note that
loan_id calls __loanID in the constructor to initialize that value
@property
def __loanID(self):
return random.randint(10000, 99999)
class Person:
• Constructor needs 2 instance attributes to initialize name and __ssn
def __eq__(self, other):
1. Check if other is an instance of Person, Student or Staff, if it is, check if
self.get_ssn() == other.get_ssn() and return accordingly
2. If other is not a Course object, return False
class Staff(Person):
• Constructor needs super().__init__(name, ssn) to initialize attributes from the parent
class and another instance attribute to initialize supervisor
@property
def id(self):
name_list = self.name.split()
initials = name_list[0][0]+ name_list[1][0]
return ‘905’ + initials.lower() + self.get_ssn()[-4:]
def applyHold(self, student):
# Note: Student class has an attribute to handle holds and active status
student.hold=True
return ‘Completed!’
class Student(Person):
• Constructor needs super().__init__(name, ssn) to initialize attributes from the parent
class and attributes to initialize year, semesters dictionary, hold status, active status and
student’s account. Note that a StudentAccount object is created during instantiation, so
account calls __createStudentAccount
def registerSemester(self):
1. Check that both active is True and hold is False
2. Create a Semester object and add it into the dictionary. The key and semester
number can be computed as the length of the dictionary+1
3. Update year attribute
def enrollCourse(self, cid, catalog, semester):
#catalog is a Catalog object, and using cid, we can access the Course objected to add
them into the courses list in the Semester object
1. Check that both active is True and hold is False
2. Get the course object from the catalog, catalog.courseOfferings[cid]
3. Add that course in to the Semester object. The semester object already has a method
to do that, use it. self.semesters[semester].addCourse(course object from 2)
4. Charge the student’s account. Note that account is an instance of Student account, so
you can use the chargeAccount method. You can get the number of credit from the
object you retrieved from 2)
def dropCourse(self, cid):
1. Check that both active is True and hold is False
2. Set sem_num = number of elements in self.semesters
3. Get the semester object from the dictionary using the key from (2)
4. Using the object from (3), access the courses attribute,
self.semesters[semester].courses, to find the Course with the same cid.
5. Use self.semesters[semester].dropCourse(course object from 4) to remove the course
from the Semester object
def getLoan(self, amount):
1. Check that active is True
2. Loan applies to the most recent semester, which you can retrieve using
len(self.semesters) as a key
3. Similar to using addCourse and dropCourse, you can use the Semester object to use
the isFullTime method.
4. If isFullTime returns True, create the Loan object using amount and add it to the loans
dictionary attribute of the student’s account, using the id as key.
class StudentAccount:
• Price per credit can change at any time for all students, so it must be a class attribute
• Constructor needs to initialize attributes for student, a balance=0 and a loans dictionary
def makePayment(self, amount):
Subtract amount from self.balance