COSC 2436 Python Programming Assignment Payroll Classes ✓ Solved
```html
COSC 2436 Python Programming Assignment Payroll Classes – Inh
COSC 2436 Python Programming Assignment Payroll Classes – Inheritance and Polymorphism Specification: ABC Corp. has a staff of employees and needs to calculate pay for all of its employees. Below is a class hierarchy of the employees.
Class Name: Employee – super class
- Members in the Employee class: All data fields are private members
- Data fields
- name: string - Employee’s name
- department: string - Employee’s department
- Constructor Purpose: __init__ - Parameter list: (name='', department='') - Assign parameter value to data fields
- Method Name
- get_name: string - Return data field name
- set_name: None - Parameter list: (self, name)
- get_department: string - Return data field department
- set_department: None - Parameter list: (self, department)
- pay: float - Return 0.0
- __str__: string - Return the description of the Employee object
Class Name: CommissionPaid – subclass of Employee
- Members in the Executive class: All data fields are private members
- Data fields
- base_rate: float - Base salary for the week
- sales: float - Weekly sales
- Constructor Purpose: __init__ - Call super class’s __init__ - Parameter list: (self, base_rate, sales) - Assign parameter value to data fields
- Method Name
- get_base_rate: float - Return data field base_rate
- set_base_rate: None - Parameter list: (self, base_rate)
- get_sales: float - Return data field sales
- set_sales: None - Parameter list: (self, sales)
- pay: float - Return base pay + commission for that week
- Commission calculation:
- If sales amount is greater than 30,000 commission is 3% of the sales amount
- Else if sales amount is between 5,000 to 30,000 commission is 1% of the sales amount
- Else (the sales amount is less than 5,000) there is no commission
- __str__: string - Return the description of the CommissionPaid object
Class Name: HourlyPaid – subclass of Employee
- Members in the HourlyPaid class:
- Data fields
- hourly_rate: float - Hourly rate
- hours: float - Hours worked for the week
- Constructor Purpose: __init__ - Call super class’s __init__ - Parameter list: (self, hourly_rate, hours) - Assign parameter value to data fields
- Method Name
- get_hourly_rate: float - Return data field hourly_rate
- set_hourly_rate: None - Parameter list: (self, hourly_rate)
- get_hours: float - Return data field hours
- set_hours: None - Parameter list: (self, hours)
- pay: float - Calculate weekly pay including overtime pay
- Overtime pay calculation: If hours worked are over 40, the overtime pay will be calculated using hourly rate overtime hours 1.5
- __str__: string - Return description of the HourlyPaid object
Class Name: SalaryPaid– subclass of Employee
- Members in the SalaryPaid class:
- Data fields
- salary: float - Weekly salary
- Constructor Purpose: __init__ - Call super class’s __init__ - Parameter list: (self, salary) - Assign parameter value to data field
- Method Name
- get_salary: float - Return data field salary
- set_salary: None - Parameter list: (self, salary)
- pay: float - return weekly salary
- __str__: string - Return description of the SalaryPaid object
Functions in the main program:
- Function Name: total_pay - Parameter: a list of employee objects - Returns total weekly pay for all employees
- Function Name: print_employee_list - Parameter: a list of employee objects - Displays the employee information.
Assignment Instructions:
- Create a python module to store all classes and name it employees.py
- Create a python file which will contain the main function to test your classes and name it assignment1_yourFirstName_yourLastName.py
- To test your code, create a list of objects containing 3 instances. Summarize your data to display each employee information, and total payroll for the week.
- Here is the test data.
- CommissionPaid object: Justin White, Marketing department, base pay: $500, sales:$50,000
- HourlyPaid object: John Smith, Accounting department, hourly rate: $20.5, hours: 55.0
- SalaryPaid object: Mary Smith, Finance department, salary: $2,000.00
- You can create extra function to enhance your program
- Name your variables clearly
- Add comments to document your project and code
- Add doc string to document your module/functions
- Submit two python files and a screen shot of your output in Canvas for grading
Program Output:
Employee Type Employee Name Department Weekly Pay
------------- ------------- ---------- ----------
Hourly Paid John Smith Accounting $ 1281.25
Salary Paid Mary Smith Finance $ 2500.00
Commission Paid Justin White Marketing $ 1400.00
Total Weekly Pay: $ 5181.00
Paper For Above Instructions
To implement the payroll class system for ABC Corp., we will create three subclasses derived from a base class called Employee. This design utilizes inheritance and polymorphism, allowing each subclass to define its own methods peculiar to its pay structure while maintaining standard attributes from the Employee base class. Below, I will outline the implementation of each of these classes and how they work together to calculate employee pay.
Class Definitions
The Employee class serves as the superclass containing common attributes such as the employee's name and department. This class provides the structure for the data members and basic getter and setter methods for these attributes:
class Employee:
def __init__(self, name='', department=''):
self.__name = name
self.__department = department
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def get_department(self):
return self.__department
def set_department(self, department):
self.__department = department
def pay(self):
return 0.0
def __str__(self):
return f"{self.__class__.__name__} - Name: {self.get_name()}, Department: {self.get_department()}"
Subsequently, we define the CommissionPaid class, which calculates employee pay based on base pay and sales. The commission structure varies based on the total sales:
class CommissionPaid(Employee):
def __init__(self, base_rate, sales, name='', department=''):
super().__init__(name, department)
self.__base_rate = base_rate
self.__sales = sales
def get_base_rate(self):
return self.__base_rate
def set_base_rate(self, base_rate):
self.__base_rate = base_rate
def get_sales(self):
return self.__sales
def set_sales(self, sales):
self.__sales = sales
def pay(self):
commission = 0.0
if self.__sales > 30000:
commission = self.__sales * 0.03
elif 5000
commission = self.__sales * 0.01
return self.__base_rate + commission
def __str__(self):
return super().__str__() + f", Base Pay: {self.get_base_rate()}, Sales: {self.get_sales()}"
Next, the HourlyPaid class computes the pay based on hourly rates and any overtime worked. If an employee completed more than 40 hours in a week, they receive overtime pay:
class HourlyPaid(Employee):
def __init__(self, hourly_rate, hours, name='', department=''):
super().__init__(name, department)
self.__hourly_rate = hourly_rate
self.__hours = hours
def get_hourly_rate(self):
return self.__hourly_rate
def set_hourly_rate(self, hourly_rate):
self.__hourly_rate = hourly_rate
def get_hours(self):
return self.__hours
def set_hours(self, hours):
self.__hours = hours
def pay(self):
if self.__hours > 40:
return (40 self.__hourly_rate) + ((self.__hours - 40) self.__hourly_rate * 1.5)
return self.__hours * self.__hourly_rate
def __str__(self):
return super().__str__() + f", Hourly Rate: {self.get_hourly_rate()}, Hours: {self.get_hours()}"
Lastly, the SalaryPaid class is straightforward, as it simply returns a predetermined weekly salary:
class SalaryPaid(Employee):
def __init__(self, salary, name='', department=''):
super().__init__(name, department)
self.__salary = salary
def get_salary(self):
return self.__salary
def set_salary(self, salary):
self.__salary = salary
def pay(self):
return self.__salary
def __str__(self):
return super().__str__() + f", Salary: {self.get_salary()}"
Main Program
In the main function, we will create instances of each employee type and store them in a list. We will also define functions to calculate total weekly pay and print employee details:
def total_pay(employee_list):
total = 0.0
for employee in employee_list:
total += employee.pay()
return total
def print_employee_list(employee_list):
for employee in employee_list:
print(employee)
print(f"Total Weekly Pay: ${total_pay(employee_list):.2f}")
Main Function
if __name__ == "__main__":
employees = [
CommissionPaid(500, 50000, "Justin White", "Marketing"),
HourlyPaid(20.5, 55, "John Smith", "Accounting"),
SalaryPaid(2500, "Mary Smith", "Finance"),
]
print_employee_list(employees)
Conclusion
The payroll system designed for ABC Corp. demonstrates how to utilize class inheritance and polymorphism in Python programming effectively. Each subclass allows for specific payroll calculations while reusing the structural logic defined in the base class. With this design, the company can accurately compute employee wages based on their employment type, ensuring clear documentation and maintainability.
References
- Python Software Foundation. (2023). Python 3 Documentation. Retrieved from https://docs.python.org/3/
- Guido van Rossum. (2023). Python.org. Retrieved from https://www.python.org/
- Downey, A. (2015). Think Python. 2nd Edition. Green Tea Press.
- McKinney, W. (2018). Python for Data Analysis. O'Reilly Media.
- Press, O. (2020). Learning Python. O'Reilly Media.
- Nested Objects in Python. (2023). Real Python. Retrieved from https://realpython.com/
- O'Reilly Media - Get Started With Python. (2023). Retrieved from https://www.oreilly.com/
- Dive Into Python 3. (2023). Mark Pilgrim. Retrieved from http://www.diveintopython3.net/
- Python Programming: An Introduction to Computer Science. (2020). John M. Zelle.
- Fluent Python: Clear, Concise Programming. (2015). Luciano Ramalho. O'Reilly Media.
```