Consider the following simple employee class: (codeboard.io):
Here’s the updated Employee class “Employee.cpp“:
class Employee:
'Common base class for all employees'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print("Total Employees", Employee.empCount)
def displayEmployee(self):
print("Name : ", self.name, ", Salary: ", self.salary)
Here is code that uses it:
from Employee import Employee
emp1=Employee("Bill Yeakus",50000)
emp2=Employee("Sally Smith",75000)
emp3=Employee("Joan Jones",66000)
emp1.displayEmployee()
Part 1: Extending the Employee Class
First, let’s enhance the Employee class by adding several more fields: position, department, and ID. We’ll also modify the class to assign a unique ID to each employee upon creation.
Step 1: Modify Employee.py
- New Fields: We add
positionanddepartmentattributes. - Unique ID Generation: We’ll use a class-level variable to track and assign each employee a unique ID.
Here’s the updated Employee class:
# Employee.py",
class Employee:
'Common base class for all employees'
empCount = 0
next_id = 1 # Tracks the next unique ID
def __init__(self, name, salary, position, department):
self.name = name
self.salary = salary
self.position = position
self.department = department
self.id = Employee.next_id # Assign a unique ID
Employee.next_id += 1 # Increment for the next employee
Employee.empCount += 1 # Increment total employee count
def displayCount(self):
print("Total Employees:", Employee.empCount)
def displayEmployee(self):
print("ID:self.id," Name:",self.name," Position:",self.position," Department: ",self.department," Salary:$",self.salary)
Explanation
- Unique ID: The
next_idclass variable starts at1and increments each time a newEmployeeis created, ensuring each employee has a uniqueid. - New Fields:
positionanddepartmentare now part of the constructor.
Step 2: Create Employees in main.py
Now, we can create instances of Employee in main.py with the new fields:
# main.py
from Employee import Employee
emp1 = Employee("Bill Yeakus", 50000, "Engineer", "Development")
emp2 = Employee("Sally Smith", 75000, "Manager", "Sales")
emp3 = Employee("Joan Jones", 66000, "Analyst", "Finance")
emp1.displayEmployee()
emp2.displayEmployee()
emp3.displayEmployee()
Expected Output
ID: 1, Name: Bill Yeakus, Position: Engineer, Department: Development, Salary: $ 50000
ID: 2, Name: Sally Smith, Position: Manager, Department: Sales, Salary: $ 75000
ID: 3, Name: Joan Jones, Position: Analyst, Department: Finance, Salary: $ 66000
Part 2: Creating the Employees Management Class
To manage multiple employees, we’ll create a new Employees class that stores a list of Employee objects and provides methods to add, remove, search, update, and list employees.
Step 1: Define the Employees Class
In a new file, Employees.py, we’ll define the Employees class. This class manages a collection of Employee objects and allows operations on this collection.
# Employees.py
from Employee import Employee
class Employees:
def __init__(self):
self.employee_list = []
def add_employee(self, employee):
self.employee_list.append(employee)
print("Added employee:",employee.name")
def remove_employee(self, employee_id):
for emp in self.employee_list:
if emp.id == employee_id:
self.employee_list.remove(emp)
print("Removed employee:",emp.name")
return
print("Employee with ID",employee_id,"not found.")
def search_employee(self, employee_id):
for emp in self.employee_list:
if emp.id == employee_id:
return emp
return None
def update_employee(self, employee_id, name=None, salary=None, position=None, department=None):
emp = self.search_employee(employee_id)
if emp:
if name:
emp.name = name
if salary:
emp.salary = salary
if position:
emp.position = position
if department:
emp.department = department
print("Updated employee:",emp.name)
else:
print("Employee with ID",employee_id,"not found.")
def list_employees(self):
print("Listing all employees:")
for emp in self.employee_list:
emp.displayEmployee()
Explanation
- add_employee: Adds an
Employeeobject to the list. - remove_employee: Removes an
Employeeby ID. - search_employee: Searches for an
Employeeby ID. - update_employee: Updates an employee’s details based on provided arguments.
- list_employees: Displays all employees.
Step 2: Using the Employees Class in main.py
Now, let’s manage our employees using the Employees class in main.py.
# main.py
from Employee import Employee
from Employees import Employees
# Initialize the Employees management system
employee_manager = Employees()
# Create Employee instances
emp1 = Employee("Bill Yeakus", 50000, "Engineer", "Development")
emp2 = Employee("Sally Smith", 75000, "Manager", "Sales")
emp3 = Employee("Joan Jones", 66000, "Analyst", "Finance")
# Add employees to the manager
employee_manager.add_employee(emp1)
employee_manager.add_employee(emp2)
employee_manager.add_employee(emp3)
# List all employees
employee_manager.list_employees()
# Search for an employee by ID
emp = employee_manager.search_employee(2)
if emp:
print("Employee found:")
emp.displayEmployee()
else:
print("Employee not found.")
# Update an employee's information
employee_manager.update_employee(3, name="Joan Johnson", salary=68000)
# Remove an employee
employee_manager.remove_employee(1)
# List employees after updates
employee_manager.list_employees()
Expected Output
Added employee: Bill Yeakus
Added employee: Sally Smith
Added employee: Joan Jones
Listing all employees:
ID: 1, Name: Bill Yeakus, Position: Engineer, Department: Development, Salary: $50000
ID: 2, Name: Sally Smith, Position: Manager, Department: Sales, Salary: $75000
ID: 3, Name: Joan Jones, Position: Analyst, Department: Finance, Salary: $66000
Employee found:
ID: 2, Name: Sally Smith, Position: Manager, Department: Sales, Salary: $75000
Updated employee: Joan Johnson
Removed employee: Bill Yeakus
Listing all employees:
ID: 2, Name: Sally Smith, Position: Manager, Department: Sales, Salary: $75000
ID: 3, Name: Joan Johnson, Position: Analyst, Department: Finance, Salary: $68000
Summary
In this activity, we:
- Enhanced the
Employeeclass with additional fields (position,department, andID) and added automatic ID assignment. - Created the
Employeesclass to manage a collection of employees, with methods for adding, removing, searching, updating, and listing employees.
This setup provides a basic but powerful structure for managing employees, which can be further expanded to include more complex functionality, such as file-based persistence or advanced search criteria.
