python programming
Question Description
Requirements: – Use the code written in previous lab for Employee, HourlyEmployee, and SalariedEmployee and make the following changes:
Update the appropriate class(es) to do the following:
- Add a __repr__ method, so that the reference for each object is printed.
- Add a get_id method, that will return the unique id for the object.Must also add other logic / variables to generate a unique id as we have in past examples.
- Update the printing logic in __str__ will also print the id instance variable.
- For all these requirements, these must be applied so that they work for all classes:Employee, HourlyEmployee and SalariedEmployee.Given the requirement, to which class(es) should we make these updates?We must utilize inheritance so that we make the minimum number of updates.
Update main to do the following:
Create a list and append each HourlyEmployee and SalariedEmployee to the list.Print the list.This will test your implementation for the __repr__ method.Test to ensure that the new variable, id, is printed
code:
class Employee:
def __init__(self, fname, lname, ssn):
self.__first_name = fname
self.__last_name = lname
self.__social_security = ssn
def __str__(self):
return 'Name: {:s}'.format(
self.get_full_name())
def set_first_name(self, fname):
self.__first_name = fname
def set_last_name(self, lname):
self.__last_name = lname
def set_social_security(self, ssn):
self.__social_security = ssn
def get_full_name(self):
return '{:s} {:s}'.format(
self.__first_name, self.__last_name)
class HourlyEmployee(Employee):
def __init__(self, fname, lname, ssn, hrate):
super().__init__(fname, lname, ssn)
self.__hourly_rate = hrate
def set_hourly_rate(self, hrate):
self.__hourly_rate = hrate
def get_hourly_rate(self):
return self.__hourly_rate
def __str__(self):
s = super().__str__()
s = s + ' Hourly Rate: ' + str(self.__hourly_rate)
return s
class SalariedEmployee(Employee):
def __init__(self, fname, lname, ssn, salary):
super().__init__(fname, lname, ssn)
self.__salary = salary
def set_salary(self, salary):
self.__salary = salary
def get_salary(self):
return self.__salary
def __str__(self):
s = super().__str__()
s = s + ' Salary: ' + str(self.__salary)
return s
def main():
hre1 = HourlyEmployee('Sam', 'Peter', 33322, 15.00)
hre2 = HourlyEmployee('harry', 'Johnson', 23422, 20.00)
ems1 = SalariedEmployee('Tia', 'Cruiz', 32344, 5000.00)
ems2 = SalariedEmployee('Tim', 'Clinton', 98934, 34000.00)
print('Hourly Employee 1 is', hre1)
print('Hourly Employee 2 is', hre2)
print('nSalary Employee 1 is', ems1)
print('Salary Employee 2 is', ems2)
main()
Get your college paper done by experts
Do my question How much will it cost?Place an order in 3 easy steps. Takes less than 5 mins.
Leave a Reply
Want to join the discussion?Feel free to contribute!