# employee.py # Walker M. White (wmw2) # October 19, 2012 """Module with two classes to show off inheritancs""" class Employee(object): """Instance is an employee with a salary.""" _name = '' # Employee's name; a string _start = -1 # Year hired (an int); -1 if undefined _salary = 0.0 # Salary; int or float >= 0 @property def name(self): """The employee's name. Invariant: A string. Cannot be empty.""" return self._name @name.setter def name(self,value): assert type(value) == str and value != '', `value`+' is an invalid name' self._name = value @property def start(self): """The year hired. Invariant: An int > 1970, or -1 if undefined.""" return self._start @start.setter def start(self,value): assert type(value) == int, `value`+' is not an int' assert value > 1970 or value == -1, `value`+' is an invalid start date' self._start = value @property def salary(self): """Annual salary Invariant: A number (int or float) >= 0.""" return self._salary @salary.setter def salary(self,value): assert type(value) == int or type(value) == float, `value`+' is not a number' assert value >= 0, `value`+' is negative' self._salary = value @property def compensation(self): """Annual compensation. Read only property.""" return self.salary def __init__(self, n, d=-1, s=50000.0): """Constructor: Creates an Employee with name n, year hired d, salary s Preconditions: n is a nonempty string d is an int > 1970 or -1 if undefined (default) s is an int or float >= 0 (50000.0 default)""" assert type(n) == str and n != '', `n`+' is an invalid name' assert type(d) == int, `d`+' is not an int' assert d > 1970 or d == -1, `d`+' is an invalid start date' assert type(s) == int or type(s) == float, `s`+' is not a number' assert s >= 0, `value`+' is negative' self._name = n self._start = d self._salary = s def __str__(self): """Returns: 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 the same attributes as self. False otherwise.""" # Check if this is an Employee if type(other) != Employee: return False return (self.name == other.name and self.start == other.state and self.salary == other.salary) class Executive(Employee): """An instance is an Employee with a bonus.""" _bonus = 0.0 # Yearly bonus; an float >= 0 @property def bonus(self): """Bonus salary. Invariant: A number (int or float) >= 0.""" return self._bonus @bonus.setter def bonus(self,value): assert type(value) == int or type(value) == float, `value`+' is not a number' assert value >= 0, `value`+' is negative' self._bonus = value # Override compensation. @property def compensation(self): """Annual compensation. Read only property.""" return self.salary+self.bonus # Override salary to be read-only. @property def salary(self): return self._salary # NO SETTER. MAKES SALARY READ ONLY def __init__(self, n, d, b=0.0): """Constructor: Creates an Executive with name n, year hired d, salary 50,000, and bonus b""" assert type(b) == int or type(b) == float, `b`+' is not a number' assert b >= 0, `b`+' is negative' # Asserts precondition for n and d super(Executive,self).__init__(n,d) self.bonus = b def __str__(self): """Returns: a string representation of this Executive""" # Add on to the string representation of the base class. return super(Executive,self).__str__() + ', bonus ' + str(self.bonus) def __eq__(self,other): """Returns: True if e an Executive, with the same attributes as self. False otherwise.""" # Check if this is an Executive if type(other) != Executive: return False return (self.name == other.name and self.start == other.state and self.salary == other.salary and self.bonus == other.bonus)