Write a Class for an Employee

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2019-05-28

Problem Statement

Design and implement an Employee class that models a real-world employee in an organization.


The class should capture key attributes, define meaningful behaviors, and demonstrate principles of object-oriented design (Encapsulation, Abstraction, etc.).


📋 Problem Description


You are required to design a class that represents an Employee in a company.


Each employee has identifiable details such as name, ID, role, department, and salary.


The class should support operations such as updating employee details, calculating bonuses, and displaying information.


This problem tests your understanding of:


•Class design fundamentals
•Encapsulation and access modifiers
•Method overloading/overriding
•Object instantiation and validation


🧱 Core Requirements


1. Attributes


Each Employee object should contain at least the following attributes:


•employeeId — unique identifier (e.g., UUID or integer)
•name — full name of the employee
•designation — job title (e.g., “Software Engineer”)
•department — department name
•salary — current salary
•dateOfJoining — when the employee joined


2. Behaviors / Methods


Implement methods to:


•Get/Set employee details (with validation)
•e.g., prevent setting negative salary or empty names.
•Display employee details (formatted output).
•Calculate annual bonus based on role or experience.
•Increment salary by a given percentage.
•Compute years of service from date of joining.


⚙️ Functional Requirements


1.Create multiple Employee instances.
2.Ensure all attributes are private, accessed via getters and setters.
3.Include a constructor for initialization.
4.Implement toString() / __str__() method to print formatted info.
5.Add a bonus calculation method that returns bonus based on role (e.g., Managers get 20%, Engineers 10%).


🧩 Example Scenario

1 2Employee emp1 = new Employee(101, "Alice", "Software Engineer", "IT", 80000, LocalDate.of(2020, 5, 10)); 3 4emp1.displayEmployeeDetails(); 5emp1.incrementSalary(10); // Increases salary by 10% 6System.out.println("Annual Bonus: " + emp1.calculateBonus()); 7System.out.println("Years of Service: " + emp1.getYearsOfService());


Sample Output:

1 2Employee ID: 101 3Name: Alice 4Designation: Software Engineer 5Department: IT 6Salary: 88000.0 7Years of Service: 5 8Annual Bonus: 8800.0


⚙️ Non-Functional Requirements


AspectRequirement


Reusability

Class should be easily extendable (e.g., FullTimeEmployee, ContractEmployee).


Encapsulation

All fields must be private and accessed via getters/setters.


Validation

Ensure valid salary, ID, and name.


Maintainability

Clean, readable, and modular code.


🧠 Design Discussion Points


•Should bonus calculation vary by employee type (use inheritance)?
•How would you represent different contract types or grades?
•Could you persist employees in a list, map, or database?
•How can you override equality (equals / hashCode) for Employee objects?


✅ Deliverables


•UML class diagram for Employee
•Implementation of the Employee class in Java/Python
•Example main/test class demonstrating usage


🚀 Stretch Goals


•Create subclasses like FullTimeEmployee and PartTimeEmployee that override bonus logic.
•Integrate a simple EmployeeManager class to add/remove/search employees.
•Support serialization to store employee data in a file or DB.


Solution

Comments