222: Build an employee management system that dynamically manages an employee database.Create a class representing an employee with attributes like name, ID, and salary.Implement a dynamic array to store and manipulate employee records, including adding new employees, removing employees, and displaying the details of all employees.

 

// Build an employee management system that dynamically manages an employee database.Create a class
// representing an employee with attributes like name, ID, and salary.Implement a dynamic array to store and
// manipulate employee records, including adding new employees, removing employees, and displaying the
// details of all employees.

#include <iostream>
using namespace std;
class Employee
{
private:
    string name, ID;
    int salary;

public:
    Employee(string n = "", string id = "", int s = 0) { name = n, ID = id, salary = s; }
    void setName(string n = "") { name = n; }
    void setID(string id = "") { ID = id; }
    void setSalary(int s = 0) { salary = s; }
    string getName() { return name; }
    string getID() { return ID; }
    int getSalary() { return salary; }
    void setData(const string n, const string id, const int s) { name = n, ID = id, salary = s; }
    void getData(string *n, string *id, int *s) { *n = name, *id = ID, *s = salary; }
    void display() { cout << "Name: " << name << endl
                          << "ID: " << ID << endl
                          << "Salary: $" << salary << endl
                          << endl; }
};

// each object of this database is a database itself
class employeeDatabase
{
private:
    // creating an array database, that is pointing to the pointer employee
    Employee *Database;

    // capacity is entire size of database
    // contains filled rows + empty rows
    // could be increased when array is full
    int Capacity;

    int rows; // total rows that are currently full

public:
    // constructor
    employeeDatabase(int C = 10) { Database = new Employee[C], Capacity = C, rows = 0; }
    // new database is constructed with total capacity of 10 and as it has no data currently so it has rows zero.

    // destructor
    ~employeeDatabase()
    {
        delete[] Database;
    }

    // function to add an employee
    void addEmployee(const string n, const string id, const int s)
    {
        if (rows != Capacity)
        {
            // first add an employee at index row, then increase row by 1 ie row++;
            Database[rows++] = Employee(n, id, s);
        }

        // if capacity is full first increase the capacity, then copy previous array to new array with increased size, then empty previous array, then pass ownership of new array to old one, then perform the tasks.
        else
        {
            // increasing capacity
            Capacity *= 2;

            // make a new database with increased capacity
            Employee *newDatabase = new Employee[Capacity];

            // copy previous array to new one
            for (int i = 0; i < rows; i++)
            {
                newDatabase[i] = Database[i];
            }

            // delete everything from previous array
            delete[] Database;

            // transfer the ownership of new array to old array so that we don't have to change in our code to perform functions on new array. We can just make new array transfer ownership to old array and make changes in old array which we do changes in new array.
            Database = newDatabase; // address of newDatabase is now address of Database

            // now do same as we did in if condition
            Database[rows++] = Employee(n, id, s);
        }
    }

    // function to remove employee
    void removeEmployee(string id)
    {
        // first find the index at which the person with specific id is, when found that index, shift all array next to it one index behind. In this way, you will delete person of that id, then after for loop decrease no. of rows by 1. Then return from function.
        for (int i = 0; i < rows; i++)
        {
            if (Database[i].getID() == id)
            {
                // when index is found, shift remaining elements to fill the gap
                for (int j = i; j < rows - 1; j++)
                {
                    Database[j] = Database[j + 1];
                }

                // decreasing 1 from rows
                --rows;

                // return from the function
                return;
            }
        }
    }

    void displayAllEmployees() const
    {
        for (int i = 0; i < rows; i++)
        {
            cout << "Employee # " << i + 1 << endl;
            Database[i].display();
        }
    }
};

int main()
{
    employeeDatabase Database1;

    Database1.addEmployee("Olivia Johnson", "ID23XYZ", 50000);
    Database1.addEmployee("Ethan Rodriguez", "ABC456", 62500);
    Database1.addEmployee("Ava Brown", "789JKL", 48700);
    Database1.addEmployee("Liam Wilson", "MNO123", 75200);
    Database1.addEmployee("Sophia Martinez", "PQR789", 55900);
    Database1.addEmployee("Jackson Smith", "XYZ456", 64300);
    Database1.addEmployee("Mia Davis", "123ABC", 42800);
    Database1.addEmployee("Aiden Taylor", "LMN890", 58600);
    Database1.addEmployee("Emma Anderson", "456DEF", 69150);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);
    Database1.addEmployee("Lucas Thomas", "GHI123", 51400);

    Database1.displayAllEmployees();

    cout << endl
         << endl;

    Database1.removeEmployee("MNO123");
    Database1.removeEmployee("123ABC");
    Database1.removeEmployee("ID23XYZ");
    Database1.removeEmployee("ABC456");

    Database1.displayAllEmployees();

    return 0;
}


Comments

Popular posts from this blog

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_