# employee.py # Walker M. White (wmw2) # November 1, 2013 """Module with two classes to show off inheritance This version of employee does not use properties""" class Employee(object): """Instance is an employee with a salary. INSTANCE ATTRIBUTES: _name: Employee's name [string, not empty] _start: Year hired [int > 1970; -1 if undefined] _salary: Salary [int or float >= 0] """ # GETTERS/SETTERS def getName(self): """Returns: the employee's name.""" return self._name def setName(self,value): """Changes the employee's name to value Parameter value: the new name Precondition: value is a nonempty string""" assert type(value) == str and value != '', `value`+' is an invalid name' self._name = value def getStart(self): """Returns: the year hired.""" return self._start def setStart(self,value): """Changes year hired to value. Parameter value: the new year Precondition: valus is an int > 1970, or -1 if undefined.""" assert type(value) == int, `value`+' is not an int' assert value > 1970 or value == -1, `value`+' is an invalid start date' self._start = value def getSalary(self): """Returns: annual salary""" return self._salary def setSalary(self,value): """Changes annual salary to value. Parameter value: the new salary Precondition: value is a number (int or float) >= 0.""" assert type(value) == int or type(value) == float, `value`+' is not a number' assert value >= 0, `value`+' is negative' self._salary = value def getCompensation(self): """Returns: annual compensation (will be overridden).""" return self._salary # INITIALIZER def __init__(self, n, d=-1, s=50000.0): """Initializer: Makes an Employee with name n, year hired d, salary s Parameter n: the employee name Precondition: n is a nonempty string Parameter d: the employee start date (optional) Precondition: d is an int > 1970 or -1 if undefined (default) Parameter s: the employee salary (optional) Precondition: s is an int or float >= 0 (50000.0 default) """ # LET THE SETTERS ENFORCE THE PRECONDITIONS self.setName(n) self.setStart(d) self.setSalary(s) # OPERATIONS def __str__(self): """Returns: The string representation of this Employee""" return self._name + ', year ' + str(self._start) + ', salary ' + str(self._salary) def __eq__(self,other): """Returns: True if e an Employee, with same attributes as self. The method returns False otherwise. Parameter other: the value to compare Precondition: NONE (can be anything) """ # Check if this is an Employee if not isinstance(other,Employee): # WILL DISCUSS NEXT LECTURE return False return (self._name == other._name and self._start == other._start and self._salary == other._salary) def __ne__(self,other): """Returns: False if e an Employee, with the same attributes as self. The method returns True otherwise. Parameter other: the value to compare Precondition: NONE (can be anything) """ return not self == other # SUBCLASS class Executive(Employee): """An instance is an Employee with a bonus. INSTANCE ATTRIBUTES: _bonus: annual bonus [float >= 0] """ # GETTERS/SETTERS def getBonus(self): """Returns: bonus salary.""" return self._bonus def setBonus(self,value): """Changes bonus salary to value Parameter value: the new bonus Precondition: value is a number (int or float) >= 0.""" assert type(value) == int or type(value) == float, `value`+' is not a number' assert value >= 0, `value`+' is negative' self._bonus = value def getCompensation(self): """Returns: annual compensation (includes bonus).""" return self._salary+self._bonus # INITIALIZER def __init__(self, n, d, b=0.0): """Initializer: Makes an Executive w/ name n, year hired d, and bonus b The default salary of an executive is 50k Parameter n: the executive name Precondition: n is a nonempty string Parameter d: the executive start date (optional) Precondition: d is an int > 1970 or -1 if undefined (default) Parameter b: the executive bonus (optional) Precondition: b is an int or float >= 0 (0.0 default) """ # Asserts precondition for n and d Employee.__init__(self,n,d) self.setBonus(b) # OPERATIONS def __str__(self): """Returns: a string representation of this Executive""" # Add on to the string representation of the base class. return Employee.__str__(self) + ', bonus ' + str(self._bonus) def __eq__(self,other): """Returns: True if e an Executive, with the same attributes as self. The method returns False otherwise. Parameter other: the value to compare Precondition: NONE (can be anything) """ # Check if this is an Executive if not isinstance(other,Executive): # WILL DISCUSS NEXT LECTURE return False # Use previous __eq__ as helper return Employee.__eq__(self,other) and self._bonus == other._bonus