Practical 8: Create Class EMPLOYEE for Storing Details and Functions for Employee Count
Problem Statement
Create a class EMPLOYEE for storing details (Name, Designation, Gender, Date of Joining, Salary). Define function members to compute:
- Total number of
employees.
- Count of male and female employees.
- Employees with salary greater than 10,000.
- Employees with designation “Asst Manager”.
Solution Code
class Employee:
total_employees = 0
male_count = 0
female_count = 0
high_salary_count = 0
asst_manager_count = 0
def __init__(self, name, designation, gender, dob, salary):
self.name = name
self.designation = designation
self.gender = gender
self.dob = dob
self.salary = salary
Employee.total_employees += 1
if self.gender == "Male":
Employee.male_count += 1
elif self.gender == "Female":
Employee.female_count += 1
if self.salary > 10000:
Employee.high_salary_count += 1
if self.designation == "Asst Manager":
Employee.asst_manager_count += 1
@classmethod
def employee_count(cls):
print(f"Total Employees: {cls.total_employees}")
print(f"Male Employees: {cls.male_count}")
print(f"Female Employees: {cls.female_count}")
print(f"Employees with Salary > 10,000: {cls.high_salary_count}")
print(f"Employees with Designation 'Asst Manager': {cls.asst_manager_count}")
# Creating employee objects
emp1 = Employee("Alice", "Asst Manager", "Female", "2020-05-15", 12000)
emp2 = Employee("Bob", "Developer", "Male", "2019-07-12", 9000)
# Displaying employee count and statistics
Employee.employee_count()
Output
The program should display total employees, male and female counts, employees with salary greater than 10,000, and employees with the designation “Asst Manager”.
Example Output
Total Employees: 2
Male Employees: 1
Female Employees: 1
Employees with Salary > 10,000: 1
Employees with Designation 'Asst Manager': 1
Process finished with exit code 0
Explanation of
- Class Variables: Class variables are used to track the overall employee statistics.
- Constructor (__init__): The constructor initializes employee details and updates the class variables accordingly.
- Class Method: employee_count() prints the summary of employee details.
Key Concepts Learned
- Class Methods
- Class methods allow you to interact with class variables.
- Instance Variables
- Instance variables are specific to each object and can be used to calculate statistics.