Create an Employee Management System in C++ with Free Codes

Intro – Employee Management System

An Employee Management System (EMS) is a software application designed to manage employee records and related data. This system allows user to perform various operations such as adding new employees, deleting existing records, updating employee information, searching specific employees, and displaying all stored records.

Key Features

  1. Employee Record:
    • Each employee record typically includes attributes such as employee ID, name, age, gender, position, and salary.
  2. Functionalities:
    • Add Employee: Allows adding a new employee record to the system.
    • Delete Employee: Deletes an existing employee record based on employee ID.
    • Update Employee: Modifies information (e.g., position, salary) of an existing employee.
    • Search Employee: Searches for an employee record based on employee ID or name.
    • Display All Employees: Displays all employee records stored in the system.
  3. Data Management:
    • Utilizes data structures like arrays, linked lists, or dynamic arrays (vectors) to store and manage employee records.
    • Implements file handling for data persistence (optional).

Project Codes

Here is the Complete Project Codes of Employee Management System,

#include <iostream>
#include <vector>
#include <algorithm> // for std::find_if

class Employee {
private:
    int employeeID;
    std::string name;
    int age;
    char gender;
    std::string position;
    float salary;

public:
    Employee(int id, const std::string& n, int a, char g, const std::string& p, float s)
        : employeeID(id), name(n), age(a), gender(g), position(p), salary(s) {}

    int getEmployeeID() const { return employeeID; }
    std::string getName() const { return name; }
    int getAge() const { return age; }
    char getGender() const { return gender; }
    std::string getPosition() const { return position; }
    float getSalary() const { return salary; }

    void displayDetails() const {
        std::cout << "Employee ID: " << employeeID << "\n";
        std::cout << "Name: " << name << "\n";
        std::cout << "Age: " << age << "\n";
        std::cout << "Gender: " << gender << "\n";
        std::cout << "Position: " << position << "\n";
        std::cout << "Salary: " << salary << "\n";
        std::cout << "------------------\n";
    }

    void updatePosition(const std::string& newPosition) {
        position = newPosition;
    }

    void updateSalary(float newSalary) {
        salary = newSalary;
    }
};

class EmployeeManagementSystem {
private:
    std::vector<Employee> employees;
    int nextEmployeeID = 1; // Start employee IDs from 1 and increment

public:
    void addEmployee(const std::string& name, int age, char gender, const std::string& position, float salary) {
        employees.emplace_back(nextEmployeeID++, name, age, gender, position, salary);
    }

    void deleteEmployee(int employeeID) {
        auto it = std::find_if(employees.begin(), employees.end(),
            [employeeID](const Employee& e) { return e.getEmployeeID() == employeeID; });

        if (it != employees.end()) {
            employees.erase(it);
            std::cout << "Employee with ID " << employeeID << " deleted.\n";
        } else {
            std::cout << "Employee with ID " << employeeID << " not found.\n";
        }
    }

    void updateEmployee(int employeeID, const std::string& newPosition, float newSalary) {
        auto it = std::find_if(employees.begin(), employees.end(),
            [employeeID](const Employee& e) { return e.getEmployeeID() == employeeID; });

        if (it != employees.end()) {
            it->updatePosition(newPosition);
            it->updateSalary(newSalary);
            std::cout << "Employee with ID " << employeeID << " updated.\n";
        } else {
            std::cout << "Employee with ID " << employeeID << " not found.\n";
        }
    }

    void searchEmployee(int employeeID) const {
        auto it = std::find_if(employees.begin(), employees.end(),
            [employeeID](const Employee& e) { return e.getEmployeeID() == employeeID; });

        if (it != employees.end()) {
            it->displayDetails();
        } else {
            std::cout << "Employee with ID " << employeeID << " not found.\n";
        }
    }

    void displayAllEmployees() const {
        for (const auto& employee : employees) {
            employee.displayDetails();
        }
    }
};

int main() {
    EmployeeManagementSystem ems;

    // Adding some initial employees
    ems.addEmployee("Alice", 30, 'F', "Manager", 75000);
    ems.addEmployee("Bob", 25, 'M', "Developer", 55000);
    ems.addEmployee("Charlie", 28, 'M', "Designer", 60000);

    int choice;
    int employeeID;
    std::string name;
    int age;
    char gender;
    std::string position;
    float salary;

    do {
        std::cout << "\nEmployee Management System Menu\n";
        std::cout << "1. Add Employee\n";
        std::cout << "2. Delete Employee\n";
        std::cout << "3. Update Employee\n";
        std::cout << "4. Search Employee\n";
        std::cout << "5. Display All Employees\n";
        std::cout << "6. Exit\n";
        std::cout << "Enter your choice: ";
        std::cin >> choice;

        switch (choice) {
            case 1:
                std::cout << "Enter name, age, gender (M/F), position, salary: ";
                std::cin >> name >> age >> gender >> position >> salary;
                ems.addEmployee(name, age, gender, position, salary);
                std::cout << "Employee added successfully.\n";
                break;
            case 2:
                std::cout << "Enter employee ID to delete: ";
                std::cin >> employeeID;
                ems.deleteEmployee(employeeID);
                break;
            case 3:
                std::cout << "Enter employee ID, new position, and new salary: ";
                std::cin >> employeeID >> position >> salary;
                ems.updateEmployee(employeeID, position, salary);
                break;
            case 4:
                std::cout << "Enter employee ID to search: ";
                std::cin >> employeeID;
                ems.searchEmployee(employeeID);
                break;
            case 5:
                std::cout << "Displaying all employees:\n";
                ems.displayAllEmployees();
                break;
            case 6:
                std::cout << "Exiting...\n";
                break;
            default:
                std::cout << "Invalid choice. Please try again.\n";
        }
    } while (choice != 6);

    return 0;
}

Employee Management System in C++ Language

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top