The Employee Management System is a console-based Java application developed using Java Collections and Exception Handling.
This project demonstrates how to manage employee records dynamically using the ArrayList collection and handle runtime errors using Java's built-in and custom exception handling mechanisms.
The application allows users to add, display, search, and remove employee records through a menu-driven interface.
It also prevents duplicate employee IDs and handles situations where an employee does not exist.
- Understand how Java Collections work in real-world applications.
- Implement
ArrayListto store employee records dynamically. - Perform CRUD operations (Create, Read, and Delete).
- Understand custom exception creation and handling.
- Implement exception handling using
try,catch,throw, andthrows. - Understand communication between multiple Java classes.
- Apply object-oriented programming concepts.
| Feature | Description |
|---|---|
| Add Employee | Register a new employee with ID, name, and salary. |
| Display Employees | Display all registered employees. |
| Search Employee | Search employee details using employee ID. |
| Remove Employee | Remove an employee using employee ID. |
| Duplicate ID Validation | Prevent duplicate employee IDs. |
| Employee Not Found | Handle searches or removals for unavailable employees. |
| Input Validation | Validate employee ID, name, and salary. |
| Exception Handling | Handle application errors without abruptly terminating the program. |
- Programming Language: Java
- Java Version: Java 8
- IDE: Eclipse
- Collection Framework: ArrayList
- Exception Handling: Built-in and Custom Exceptions
- Input Handling: Scanner
- Concepts: OOP, Encapsulation, Methods, Loops, Conditional Statements
EmployeeManagementSystem
│
└── src
│
└── com.industry.collectionexception
│
├── Employee.java
│
├── EmployeeService.java
│
├── EmployeeNotFoundException.java
│
├── DuplicateEmployeeException.java
│
└── EmployeeManagementApp.java
- Represents employee information.
- Contains employee ID, name, and salary.
- Uses private variables to implement encapsulation.
- Provides getter methods to access employee details.
- Contains a display method to print employee information.
- Manages employee-related operations.
- Uses
ArrayList<Employee>to store employee objects. - Implements add, display, search, and remove operations.
- Checks for duplicate employee IDs.
- Throws custom exceptions when required.
- Custom checked exception.
- Triggered when an employee ID is not available.
- Extends the
Exceptionclass.
- Custom checked exception.
- Triggered when an employee ID already exists.
- Prevents duplicate employee records.
- Main class of the application.
- Starts program execution through the
main()method. - Displays the menu-driven interface.
- Accepts user input using Scanner.
- Calls methods from EmployeeService.
- Handles exceptions using try-catch.
This project uses two custom exceptions and one built-in exception.
1. DuplicateEmployeeException
When a user enters an employee ID that already exists, the application throws a custom exception.
Example:
throw new DuplicateEmployeeException(
"Employee ID already exists!"
);2. EmployeeNotFoundException
When a user searches for or removes an employee whose ID is unavailable, the application throws this exception.
Example:
throw new EmployeeNotFoundException(
"Employee not found with ID: " + id
);IllegalArgumentException
Used when invalid employee details are entered, such as:
- Employee ID less than or equal to zero.
- Empty employee name.
- Salary less than or equal to zero.
try {
service.addEmployee(emp);
}
catch (DuplicateEmployeeException |
EmployeeNotFoundException |
IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}The catch block handles the exceptions and displays meaningful error messages instead of allowing these expected errors to terminate the application.
This project uses ArrayList to store employee objects.
private ArrayList<Employee> employees = new ArrayList<>();- Dynamic storage: Size increases automatically when new employees are added.
- Duplicate objects: Allows multiple objects, while the application separately validates unique employee IDs.
- Easy operations: Provides methods such as
add(),remove(), andisEmpty(). - Iteration: Supports enhanced for-loops to search and display records.
| Method | Purpose |
|---|---|
add() |
Adds an employee object. |
remove() |
Removes an employee object. |
isEmpty() |
Checks whether the collection is empty. |
| Enhanced for-loop | Iterates through employee records. |
===== EMPLOYEE MANAGEMENT SYSTEM =====
1. Add Employee
2. Display Employees
3. Search Employee
4. Remove Employee
5. Exit
Enter your choice: 1
Enter Employee ID: 101
Enter Employee Name: Rahul
Enter Salary: 45000
Employee added successfully!
Enter your choice: 1
Enter Employee ID: 102
Enter Employee Name: Amit
Enter Salary: 50000
Employee added successfully!
Enter your choice: 2
ID | Name | Salary
101 | Rahul | 45000.0
102 | Amit | 50000.0
Enter your choice: 1
Enter Employee ID: 101
Enter Employee Name: Raj
Enter Salary: 35000
Error: Employee ID already exists!
Enter your choice: 3
Enter Employee ID: 105
Error: Employee not found with ID: 105
Enter your choice: 4
Enter Employee ID: 102
Employee removed successfully!
Enter your choice: 5
Application Closed.
- The
main()method starts the application. - The application creates an
EmployeeServiceobject. - The user selects an operation from the menu.
- Employee details are accepted through Scanner.
- The
EmployeeServiceclass performs the requested operation. - Employee objects are stored in the ArrayList.
- Custom exceptions are thrown when duplicate or missing employee IDs are detected.
- The main class catches the exceptions and displays error messages.
- The application continues until the user selects Exit.
| Concept | Implementation |
|---|---|
| Encapsulation | Private employee variables with getter methods. |
| Abstraction | Employee operations are separated into the service class. |
| Classes and Objects | Employee and EmployeeService objects. |
| Constructor | Initializes employee information. |
| Exception Handling | Handles invalid operations and errors. |
| Collections | Stores employee objects dynamically. |
The project can be extended with the following features:
- Update employee details.
- Search employees by name.
- Sort employees by salary.
- Use HashMap for faster employee ID-based lookup.
- Store employee records in a database using JDBC.
- Implement file handling for permanent data storage.
- Add a login and authentication system.
Q1. Why did you use ArrayList in this project?
Answer: I used ArrayList because it provides dynamic storage and allows easy addition, removal, and iteration of employee objects.
Q2. Why did you create custom exceptions?
Answer: Custom exceptions help handle application-specific errors such as duplicate employee IDs and employee records not found.
Q3. What is the difference between throw and throws?
Answer: throw is used to explicitly throw an exception, whereas throws declares possible exceptions in a method signature.
Q4. Why did you separate Employee and EmployeeService classes?
Answer: To maintain separation of responsibilities, making the application easier to understand, maintain, and extend.
Q5. What happens when an employee ID already exists?
Answer: The application throws DuplicateEmployeeException, which is handled by the catch block.
This project demonstrates the practical implementation of Java Collections and Exception Handling in a real-world employee management scenario.
By developing this application, I practiced managing objects using ArrayList, implementing CRUD operations, creating custom exceptions, validating user input, and organizing Java code into multiple classes.
It helped strengthen my understanding of Java 8, OOP principles, Collection Framework, and Exception Handling.