E.3. Question 3: Class and objects Employee Database Create a class called \"Emp
ID: 3849672 • Letter: E
Question
E.3. Question 3: Class and objects Employee Database Create a class called "Employee with four member variables (First name, last name, employee ID, and salary) to store information about employees, and a member function to display those information. Now carry out the following program requirements Page 7 of 12 Program Requirements: No Global variables can be used for this program. You are required to use the function prototypes given below. In the main function, ask the user to enter the size (number of employees. By using this size, create an Employee type amay dynamically From the main0 function, a function should be called to populate the employee array Define this function with the function prototype: void dataEntry(Employee edb [],int size) where in the parameter list the first parameter edb will take care of the employee amay and the second parameter size will take care of the size of the array. Inside this function, validate that the salary is a positive number and validate the employee ID is a 5 digit number The program will then display a menu showing the following options: a. Sort the contents of the amay in the order from the greatest to least with respect to the salary of the employee. This function should not contain any cout The function prototype: void sort Array salary(Employee myArray int sz) b. Sort the contents of the array in alphabetical order with respect to the last name of the employee. This function should not contain any cout statement. The function prototype: void sortArrayLName(Employee nyArray[1, int sz) c. Find the average salary. The function prototype: double find AverageSalary(Enployee myArray[], int sz) d. Search the array with the given ID number and display the employee info if found, otherwise display "Not found The function prototype: void searchArrayID(Employee myArray[], int sz, int ID) In the above prototype, the ID variable should take care ofthe employee tobe e. Search the amay with the given first name and display the employees info if found otherwise display "Not found' The function prototype void searc hArray FName (Employee nyArray[1, int sza string fane) In the above prototype, the string variable should take care of the employee to be f Search the amay with two given salaries and display all the employees whose salaries are between them otherwise display "Not f The function prototype: hArray (Employee smyArray, int sz, double salary.low, double salary highExplanation / Answer
The answer is as follows:
#include<iostream.h>
#include<string>
class Employee
{
public:
string fname;
string lname;
int id;
double salary;
void disp()
{
cout << fname << " " << lname << " " << id << " " << salary << endl;
}
};
void dataEntry(Employee edb[], int size)
{
for (int i = 0; i<size; i++)
{
cout << "Enter first name of employee " << i ;
cin >> edb[i].fname;
cout << "Enter last name of employee " << i ;
cin >> edb[i].lname;
do {
cout << "Enter id (should be 5 digits) of employee " << i ;
cin >> edb[i].id;
} while (edb[i].id < 10000 && edb[i] > 99999)
do {
cout << "Enter salary (should be +ve) of employee " << i ;
cin >> edb[i].salary
} while (edb[i].id >= 0)
}
}
void sortArraySalary(Employee my_array[], int size)
{
Employee temp;
for (int i = 0; i<size; ++i)
{
for (int j = 0; j <size-i-1; ++j)
{
if (my_array[j].salary > my_array[j+1].salary)
{
temp = my_array[j];
my_array[j] = my_array[j+1];
my_array[j+1]= temp;
}
}
}
cout << "The array has been sorted" << endl;
}
void sortArrayName(Employee my_array[], int size)
{
Employee temp;
for (int i = 0; i<size; ++i)
{
for (int j = 0; j <size-i-1; ++j)
{
if (my_array[j].lname > my_array[j+1].lname)
{
temp = my_array[j];
my_array[j] = my_array[j+1];
my_array[j+1]= temp;
}
}
}
cout << "The array has been sorted" << endl;
}
double findAverageSalary(Employee my_array[], int size)
{
double sum;
sum = 0;
for (int i = 0; i<size; i++)
{
sum = sum + my_array[i].salary;
}
return sum/size;
}
void searchArrayID(Employee my_array[], int size, int id)
{
int found = 0;
for (int i = 0; i<size; i++)
{
if (my_array[i].id == id)
{
if (found == 0)
{
cout << "Found!" << endl;
cout << "Database Listing" << endl;
cout << "First Name" << " " << "Last Name" << " " << "ID" << "Salary" << endl;
cout << "=====================================================================";
found = 1;
}
my_array[i].disp();
found = 1;
}
}
if (found == 1)
cout << "=====================================================================";
if (found == 0)
cout << "Not Found" << endl;
}
void searchArrayFname(Employee my_array[], int size, string name)
{
int found = 0;
for (int i = 0; i<size; i++)
{
if (my_array[i].fname == name)
{
if (found == 0)
{
cout << "Found!" << endl;
cout << "Database Listing" << endl;
cout << "First Name" << " " << "Last Name" << " " << "ID" << "Salary" << endl;
cout << "=====================================================================";
found = 1;
}
my_array[i].disp();
}
}
if (found == 1)
cout << "=====================================================================";
if (found == 0)
cout << "Not Found" << endl;
}
void searchArray(Employee my_array[], int size, double sal_high, double sal_low)
{
int found = 0;
for (int i = 0; i<size; i++)
{
if (my_array[i].salary >= sal_low && my_array[i].salary <= sal_high)
{
if (found == 0)
{
cout << "Results" << endl;
cout << "Database Listing" << endl;
cout << "First Name" << " " << "Last Name" << " " << "ID" << "Salary" << endl;
cout << "=====================================================================";
found = 1;
}
my_array[i].disp();
}
}
if (found == 1)
cout << "=====================================================================";
if (found == 0)
cout << "Not Found" << endl;
}
void printArray(Employee my_array[], int size)
{
int found = 0;
cout << "First Name" << " " << "Last Name" << " " << "ID" << "Salary" << endl;
cout << "=====================================================================";
for (int i = 0; i<size; i++)
{
my_array[i].disp();
}
cout << "=====================================================================";
}
int main()
{
int n;
string name;
int id;
char choice;
double sal_low, sal_high;
cout << "Enter number of employees: " ;
cin >> n;
Employee *list = new Employee[n];
dataEntry(list, n);
do
{
cout << "======================================";
cout << "a.Sort by salary" << endl;
cout << "b.Sort by last name" << endl;
cout << "c.Find the average salary" << endl;
cout << "d.Search by ID" << endl;
cout << "e.Search by first name" << endl;
cout << "f.Search by salary range" << endl;
cout << "g.Print database" << endl;
cout << "h.Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 'a' :
sortArraySalary(list, n);
case 'b' :
sortArrayName(list, n);
case 'c' :
cout << "Average Salary is:"<< findAverageSalary(list, n);
case 'd' :
cout << "Enter the ID you are searching:";
cin >> id;
searchArrayID(list, n, id);
case 'e' :
cout << "Enter the first name you are searching:";
cin >> name;
searchArrayFname(list, n, name);
case 'f' :
cout << "Enter low salary:";
cin >> sal_low;
cout << "Enter high salary:";
cin >> sal_high;
searchArray(list, n, sal_low, sal_high);
case 'g' :
printArray(list, n);
}
} while (choice != 'h')
delete[] list;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.