Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Not in java format but in both header file and cpp format A local company has as

ID: 3860055 • Letter: N

Question

Not in java format but in both header file and cpp format

A local company has asked you to write a program which creates an Employee class, a vector of Employee class objects, and fills the objects with employee data, creating a "database" of employee information. The program allows the user to repeatedly search for an employee in the vector by entering the employee's ID number and if found, display the employee's data.

The Employee_C class should have the following data:

-employee full name

-employee ID number (int of 6 digits)

-employee salary

-employee gender (char)

-static variable to hold the total of all employees' salaries

Also create the appropriate constructors and getter and setter member functions for the class variables (hint: an employee's salary should be set, not accumulated; and before a salary is set and added to the static variable, the prior salary should be removed from the static variable).

In main, declare two vectors: one for Employee_C class objects, and one for ID numbers (int).

The program will have three functions:

1) Fill_Vector: have the user repeatedly enter employees' data into Employee objects (unknown number of employees), and place the objects into the Employee vector (hint: fill an object, then place it into the vector).

2) Extract_ID: retrieve the employees' IDs from the Employee vector and place them into the ID vector, i. e., create a vector of employee IDs.

3) Find_Employee: in main the user will repeatedly enter employee IDs and call this function, which will search the ID vector with the entered ID (must use STL algorithm "binary_search") and return either true or false via the function's return type.

Then in main: if the entered ID was found display the employee's data (from the employee vector) plus the total of all employee salaries (using the static variable), or if not found display an error message along with the erroneous ID number.

Enter the following data (full name, ID, salary, gender):

-Joe Jones, 123456, $60,000, M

-Jill James, 987654, $65,000, F

-Leonardo DeApe, 567890, $20,000, M

Then have the user enter the following IDs to search for an employee:

-987654

-123456

-135790 (should produce an error)

Explanation / Answer

C++ code:

#include <bits/stdc++.h>

#include <iostream>   

using namespace std;

int total_salary;

class employee_c

{

public:

string name;

int ID;

int salary;

string gender;

};

int bin_search(vector<int> arr, int l, int r, int x)

{

if (r >= l)

{

int mid = l + (r - l)/2;

if (arr[mid] == x) return mid;

if (arr[mid] > x) return bin_search(arr, l, mid-1, x);

return bin_search(arr, mid+1, r, x);

}

return -1;

}

vector< employee_c > fill_vector()

{

vector< employee_c > all_data;

// std::vector<int> all_IDs;

string line;

cout << "Enter the number of employees! ";

int n;

// cin >> n;

getline(cin , line);

n = atoi(line.c_str());

for (int i = 0; i < n; ++i)

{

string name;

int ID;

int salary;

string gender;

cout << "Enter Info of " << i+1 << " th employee! ";

cout << "Name ";

getline(cin , line);

name = line;

cout << "ID ";

getline(cin , line);

ID = atoi(line.c_str());

// cin >> ID;

cout << "salary ";

getline(cin , line);

salary = atoi(line.c_str());

// cin >> salary;

cout << "Gender ";

// cin >> gender;

getline(cin , line);

gender = line;

employee_c tmp;

tmp.name = name; tmp.ID = ID; tmp.salary = salary; tmp.gender = gender;

all_data.push_back(tmp);

total_salary = total_salary + salary;

}

return all_data;

}

vector<int> ExctractIds(std::vector<employee_c> all_data)

{

vector<int> all_IDs;

for (int i = 0; i < all_data.size(); ++i)

{

all_IDs.push_back(all_data[i].ID);

}

return all_IDs;

}

void find_employee( vector<employee_c> all_data, vector<int> all_IDs)

{

while(true)

{

int tmp_id;

cout << "Enter employee Id to be searched ";

cin >> tmp_id;

if(bin_search(all_IDs,0,all_IDs.size()-1,tmp_id) == -1)

{

cout << "No employee found with ID = " << tmp_id << endl;

}

else

{

int index = bin_search(all_IDs,0,all_IDs.size()-1,tmp_id);

string name = all_data[index].name;

int ID = all_data[index].ID ;

int salary = all_data[index].salary ;

string gender = all_data[index].gender ;

cout << "Details of employee with ID = " << tmp_id << endl;

cout << "Name = " << name << endl;

cout << "ID = " << ID << endl;

cout << "Salary = " << salary << endl;

cout << "Gender = " << gender << endl;

cout << "Salary of all the employees = " << total_salary << endl;

}

}

}

int main()

{

total_salary = 0;

vector< employee_c > all_data;

all_data = fill_vector();

vector<int> all_IDs = ExctractIds(all_data);

find_employee(all_data, all_IDs);

return 0;

}

Sample Output:

Enter the number of employees!
2
Enter Info of 1 th employee!
Name
Joe Jones
ID
123456
salary
60000
Gender
M
Enter Info of 2 th employee!
Name
Jill James
ID
987654
salary
65000
Gender
F
Enter employee Id to be searched
987654
Details of employee with ID = 987654
Name = Jill James
ID = 987654
Salary = 65000
Gender = F
Salary of all the employees = 125000
Enter employee Id to be searched
123456
Details of employee with ID = 123456
Name = Joe Jones
ID = 123456
Salary = 60000
Gender = M
Salary of all the employees = 125000
Enter employee Id to be searched
135790
No employee found with ID = 135790
Enter employee Id to be searched

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote