w3resource

Python Class - Employee Management System

Python Class Real-life Problem: Exercise-1 with Solution

Write a Python class Employee with attributes like emp_id, emp_name, emp_salary, and emp_department and methods like calculate_emp_salary, emp_assign_department, and print_employee_details.

Sample Employee Data:
"ADAMS", "E7876", 50000, "ACCOUNTING"
"JONES", "E7499", 45000, "RESEARCH"
"MARTIN", "E7900", 50000, "SALES"
"SMITH", "E7698", 55000, "OPERATIONS"

  • Use 'assign_department' method to change the department of an employee.
  • Use 'print_employee_details' method to print the details of an employee.
  • Use 'calculate_emp_salary' method takes two arguments: salary and hours_worked, which is the number of hours worked by the employee. If the number of hours worked is more than 50, the method computes overtime and adds it to the salary. Overtime is calculated as following formula:
overtime = hours_worked – 50
Overtime amount = (overtime * (salary / 50))

Sample Solution:

Python Code:

class Employee:
    def __init__(self, name, emp_id, salary, department):
        self.name = name
        self.id = emp_id
        self.salary = salary
        self.department = department

    def calculate_salary(self, salary, hours_worked):
        overtime = 0
        if hours_worked > 50:
            overtime = hours_worked - 50
        self.salary = self.salary + (overtime * (self.salary / 50))

    def assign_department(self, emp_department):
        self.department = emp_department

    def print_employee_details(self):
        print("\nName: ", self.name)
        print("ID: ", self.id)
        print("Salary: ", self.salary)
        print("Department: ", self.department)
        print("----------------------")


employee1 = Employee("ADAMS", "E7876", 50000, "ACCOUNTING")
employee2 = Employee("JONES", "E7499", 45000, "RESEARCH")
employee3 = Employee("MARTIN", "E7900", 50000, "SALES")
employee4 = Employee("SMITH", "E7698", 55000, "OPERATIONS")

print("Original Employee Details:")
employee1.print_employee_details()
employee2.print_employee_details()
employee3.print_employee_details()
employee4.print_employee_details()

# Change the departments of employee1 and employee4
employee1.assign_department("OPERATIONS")
employee4.assign_department("SALES")

# Now calculate the overtime of the employees who are eligible:
employee2.calculate_salary(45000, 52)
employee4.calculate_salary(45000, 60)

print("Updated Employee Details:")
employee1.print_employee_details()
employee2.print_employee_details()
employee3.print_employee_details()
employee4.print_employee_details()

Sample Output:

Original Employee Details:

Name:  ADAMS
ID:  E7876
Salary:  50000
Department:  ACCOUNTING
----------------------

Name:  JONES
ID:  E7499
Salary:  45000
Department:  RESEARCH
----------------------

Name:  MARTIN
ID:  E7900
Salary:  50000
Department:  SALES
----------------------

Name:  SMITH
ID:  E7698
Salary:  55000
Department:  OPERATIONS
----------------------
Updated Employee Details:

Name:  ADAMS
ID:  E7876
Salary:  50000
Department:  OPERATIONS
----------------------

Name:  JONES
ID:  E7499
Salary:  46800.0
Department:  RESEARCH
----------------------

Name:  MARTIN
ID:  E7900
Salary:  50000
Department:  SALES
----------------------

Name:  SMITH
ID:  E7698
Salary:  66000.0
Department:  SALES
----------------------

Flowchart:

Flowchart: Employee Management System
Flowchart: Employee Management System

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Python Class Exercises Home.
Next: Data Structures and Algorithms Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.