Not in java format but in both header file and cpp format A local company has as
ID: 3859993 • 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:
File: Employee_C.h
#ifndef EMPLOYEE_C_H_INCLUDED
#define EMPLOYEE_C_H_INCLUDED
#include <iostream>
using namespace std;
//Class definition
class Employee_C
{
//Private member variables
private:
string fullName;
int id;
double salary;
char gender;
public:
//Public methods
static double totalSalaries;
Employee_C();
Employee_C(string tName, int tId, double tSal, char tGender);
string getName();
int getId();
double getSalary();
char getGender();
void setName(string tName);
void setId(int id);
void setSalary(double tSal);
void setGender(char tGender);
};
#endif // EMPLOYEE_C_H_INCLUDED
File: Employee_C.cpp
#include <iostream>
#include "Employee_C.h"
using namespace std;
//Default Constructor
Employee_C::Employee_C()
{
fullName = "";
salary = 0.0;
id = 0;
gender = ' ';
}
double Employee_C::totalSalaries = 0;
//Argument Constructor
Employee_C::Employee_C(string tName, int tId, double tSal, char tGender)
{
fullName = tName;
salary = tSal;
id = tId;
gender = tGender;
totalSalaries += tSal;
}
//Setter for name
void Employee_C::setName(string tName)
{
fullName = tName;
}
//Setter for id
void Employee_C::setId(int tId)
{
id = tId;
}
//Setter for salary
void Employee_C::setSalary(double tSal)
{
salary = tSal;
totalSalaries += salary;
}
//Setter for gender
void Employee_C::setGender(char tGender)
{
gender = tGender;
}
//Getter for name
string Employee_C::getName()
{
return fullName;
}
//Getter for id
int Employee_C::getId()
{
return id;
}
//Getter for salary
double Employee_C::getSalary()
{
return salary;
}
//Getter for gender
char Employee_C::getGender()
{
return gender;
}
File: main.cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "Employee_C.h"
using namespace std;
//Function that fills the vector with given data
void FillVector(vector<Employee_C> &emps)
{
string fullName;
int id;
double salary;
char gender;
char ch;
do
{
//Reading employee data
cout << " Enter Employee data: ";
cout << " Enter Name: ";
getline(cin, fullName);
//Reading employee id
cout << " Enter Id: ";
cin >> id;
//Reading employee id
cout << " Enter Salary: ";
cin >> salary;
//Reading employee gender
cout << " Enter gender: ";
cin >> gender;
//Creating an employee object
Employee_C empObj(fullName, id, salary, gender);
//Storing into vector
emps.push_back(empObj);
cout << " Do you want to add more? (y/n): ";
cin >> ch;
cin.ignore();
}while(ch=='y' || ch=='Y');
}
//Function that extracts ids
void Extract_ID(vector<Employee_C> emps, vector<int> &ids)
{
int i;
//Iterating over emps vector
for(i=0; i<emps.size(); i++)
{
//Storing id's
ids.push_back(emps.at(i).getId());
}
}
//Compare function for searching
bool compare(int i,int j) { return (i<j); }
//Finding an employee
bool Find_Employee(vector<int> ids, int id)
{
//Sorting ids
sort(ids.begin(), ids.end());
//Searching for id
if (binary_search(ids.begin(), ids.end(), id, compare))
{
return true;
}
else
{
return false;
}
}
//Main function
int main()
{
//Vector of employees
vector<Employee_C> emps;
//Vector of ids
vector<int> ids;
int id, i;
//Fills vector with given data
FillVector(emps);
//Extracting ids
Extract_ID(emps, ids);
//Searching
while(1)
{
//Reading id
cout << " Enter employee id to search(-1 to stop): ";
cin >> id;
if(id == -1)
{
return 0;
}
//If found
if(Find_Employee(ids, id))
{
//Searching for info
for(i=0; i<emps.size(); i++)
{
//Comparing ids
if(emps.at(i).getId() == id)
{
//Printing data
cout << " Name: " << emps.at(i).getName() << " Gender: " << emps.at(i).getGender() << " Salary: " << emps.at(i).getSalary() << " ";
}
}
//Printing total salary
cout << " Total Salary: $" << Employee_C::totalSalaries << " ";
}
else
{
cout << " Invalid ID.... ";
}
}
cout << " " << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.