In: Computer Science
Submission Question 3: Polymorphism
Problem
You are writing software for a company’s human resources department. As part of the requirements, it would like to have a function that calculates the salary of an individual based on the position he or she holds, years of service, and hours worked.
This program will demonstrate the following:
Solving the Problem
Step 1
The first thing that you must determine is what attributes are common to all employees and what methods they can share. Can salary be easily calculated by the same method without some additional input from the user? By using polymorphism, you can make one method that calculates salaries for different groups. First, determine the base class and what method needs to be implemented by the child classes. By making the calcSalary() method abstract, it will be a required method of the child classes.
Step 2
You can then define the child classes that inherit the shared attributes from the base Employee class but also inherit the requirement that they implement from the calcSalary() method. Each employee type will have a different set of attributes and a different method of calculating the salary, but the same method call will be used to calculate it.
Step 3
You can now create a list to hold all employee types and populate it.
Step 4
Because you used polymorphism in the classes, you can now use one loop to calculate and output the salaries of the employees.
Documentation Guidelines:
Use Python Programming. Use good programming style (e.g., indentation for readability) and document each of your program parts with the following items (the items shown between the '<' and '>' angle brackets are only placeholders. You should replace the placeholders and the comments between them with your specific information). Your cover sheet should have some of the same information, but what follows should be at the top of each program's sheet of source code. Some lines of code should have an explanation of what is to be accomplished, this will allow someone supporting your code years later to comprehend your purpose. Be brief and to the point. Start your design by writing comment lines of pseudocode. Once that is complete, begin adding executable lines. Finally run and test your program.
Deliverable(s):
Your deliverable should be a Word document with screenshots showing the source code and running results, and discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them for all of the programs listed above as well as the inputs and outputs from running them. Submit a cover sheet with the hardcopy of your work.
Note: While copy pasting you may lose some indentation so please check that befire executing otherwise you will get indentation error.
You can copy paste in one file or create different module for each class here i have created all the class in one file.
from abc import ABC, abstractmethod
#Base class with abstract method
class Employees(ABC):
def __init__(self ,empId,name,hoursWorked):
self.empId=empId
self.name=name
self.hoursWorked =hoursWorked
@abstractmethod
def calcSalary(self):
pass
#Child classes inheriting from base class
class Manager(Employees):
def calcSalary(self):
salary = 10000+ (500* self.hoursWorked)
print('Name: '+self.name+' '+'Salary: '+ str(salary)+'
EmployeeType: Manager')
class Analyst(Employees):
def calcSalary(self):
salary = 10000+ (300* self.hoursWorked)
print('Name: '+self.name+' Salary: '+ str(salary)+' EmployeeType:
Analyst')
class HrExecutive(Employees):
def calcSalary(self):
salary = 10000+ (200* self.hoursWorked)
print('Name: '+self.name+' '+'Salary: '+ str(salary)+'
EmployeeType: HrExecutive')
list =['manager','analyst','hrExecutive']
for employee in list:
if employee.upper() =='MANAGER':
m = Manager(2,'CapAmerica',4)
m.calcSalary()
if employee.upper() =='ANALYST':
a=Analyst(4,'Hulk',5)
a.calcSalary()
if employee.upper() =='HREXECUTIVE':
h=HrExecutive(5,'Thor',6)
h.calcSalary()