Skip to content

About

The Employee Management System is a console-based Java application developed using Java Collections and Exception Handling.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Employee Management System

Java Collections + Exception Handling

1. Project Overview

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.

2. Project Objectives

  • Understand how Java Collections work in real-world applications.
  • Implement ArrayList to store employee records dynamically.
  • Perform CRUD operations (Create, Read, and Delete).
  • Understand custom exception creation and handling.
  • Implement exception handling using try, catch, throw, and throws.
  • Understand communication between multiple Java classes.
  • Apply object-oriented programming concepts.

3. Features

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.

4. Technologies Used

  • 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

5. Project Structure

EmployeeManagementSystem
│
└── src
    │
    └── com.industry.collectionexception
        │
        ├── Employee.java
        │
        ├── EmployeeService.java
        │
        ├── EmployeeNotFoundException.java
        │
        ├── DuplicateEmployeeException.java
        │
        └── EmployeeManagementApp.java

6. Class Responsibilities

1. Employee.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.

2. EmployeeService.java

  • 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.

3. EmployeeNotFoundException.java

  • Custom checked exception.
  • Triggered when an employee ID is not available.
  • Extends the Exception class.

4. DuplicateEmployeeException.java

  • Custom checked exception.
  • Triggered when an employee ID already exists.
  • Prevents duplicate employee records.

5. EmployeeManagementApp.java

  • 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.

7. Exception Handling Flow

This project uses two custom exceptions and one built-in exception.

Custom Exceptions

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
);

Built-in Exception

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.

Exception Handling Mechanism

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.

8. Collection Framework Implementation

This project uses ArrayList to store employee objects.

private ArrayList<Employee> employees = new ArrayList<>();

Why 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(), and isEmpty().
  • Iteration: Supports enhanced for-loops to search and display records.

Collection Operations Used

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.

9. Sample Application Output

===== 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.

10. Execution Flow

  1. The main() method starts the application.
  2. The application creates an EmployeeService object.
  3. The user selects an operation from the menu.
  4. Employee details are accepted through Scanner.
  5. The EmployeeService class performs the requested operation.
  6. Employee objects are stored in the ArrayList.
  7. Custom exceptions are thrown when duplicate or missing employee IDs are detected.
  8. The main class catches the exceptions and displays error messages.
  9. The application continues until the user selects Exit.

11. OOP Concepts Used

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.

12. Future Enhancements

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.

13. Interview Questions

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.

14. Conclusion

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.

About

The Employee Management System is a console-based Java application developed using Java Collections and Exception Handling.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages