In: Computer Science
Program Task (C++)
The program should contain an abstract data type (ADT) for an Employee, defined as follows:
struct Employee
int id; // unique employee identifier
string name; // employee’s full name
double rate; // employee’s hourly rate
double hours; // how many hours they worked since last pay
double taxable; // the current year’s taxable income };
This definition should appear above the main() function. The main() function should include:
1. Declare a single array to hold at most 50 employees a. Declare them locally - should not be in the global scope!
2. Call a function menu() to display the program’s menu and get a valid choice. The function takes no arguments and returns a valid integer. The function should use data validation to ensure the user’s choice is within the valid range. If not, it should display an error message, re-display the menu, and ask for another choice. The function only returns once a valid choice is entered.
Dining Center
----------------------------------
1. Enter new employee information
2. Add hours for an employee
3. Print paychecks
4. Exit the program
3. Create a loop that runs until the call to menu() returns the value 4 to exit the program. Then the loop will terminate, and the program will also stop. Use a switch statement to handle the user’s choices:
a. For choice 1 – call the addEmp() function to add a new employee at the next free element in the array. Before calling the function, ensure there is enough room in the array to store a new employee. If not, display an error message and do not call addEmp()!
b. For choice 2 – call the addHours() function to add hours for an employee
c. For choice 3 – call the printPaychecks() function to print all paychecks d. For choice 4 – exit the loop, terminate the program
4. Create an addEmp() function. This function has 2 arguments: the array of employees and the total number of employees currently in the array. This function should ask the user to enter in the employee details (only ask for id, name, and rate – the other 2 fields can be 0 initialized), create a new Employee and then add it to the next available location in the array. The code should validate the data – id should be unique (and positive) and rate should be greater than 0. Keep asking for data until a valid value is given
5. Create an addHours() function. This function has 2 arguments: the array of employees and the total number of employees currently in the array. The function will ask the user for an employee id, then call findEmp() to get the employee’s subscript. If the employee isn’t found it displays an error. If found, it will then ask for how many hours and update the record by adding those hours to the employee’s existing hours.
a. Print single table row like: “2674 Joe DeAngelo 45 $6100.00” which is the employee’s id, name, rate, and (rate * hours)
b. Update their taxable field, adding (rate * hours) to it
c. Set their hours back to 0
7. Create a findEmp() function. This function has 3 arguments: the target employee id, the array of employees, and the total number of employees currently in the array. The function returns -1 if an employee is not found. Otherwise it returns the subscript in the array that has an employee matching the search target id. The function should not display any output or errors.
ANSWER;
#include <iostream>
using namespace std;
//structure employee
struct Employee
{
int id; //unique employee identifier
string name; //employee's full name
double rate; //employee's hourly rate
double hours; //how many hours they worked since last pay
double taxable; //the current year's taxable income
};
//this function displays menu and returns user choise
int menu()
{
cout << "\n\tBGSU Dining Center\n";
cout.width(35);
cout.fill('-');
cout << " ";
cout << "\n1. Enter new employee information";
cout << "\n2. Add hours for an employee";
cout << "\n3. Print paychecks";
cout << "\n4. Exit the program\n";
cout << "Enter your choise: ";
int choise;
cin >> choise;
//if user has entered invalid choise
if (choise < 1 || choise > 4)
{
//print error message
cout << "Error! invalid choise\n";
//redisplays menu
return menu();
}
return choise;
}
//this function adds new employee to existing employees array
void addEmp(Employee employees[], int n)
{
//loop for employee id if it is -ve of existing
while (true)
{
//read employee id
cout << "Enter employee id: ";
cin >> employees[n].id;
bool is_duplicate = false;
//if entered id is negative then continue the loop
if (employees[n].id <= 0)
{
cout << "Invalid id!\n";
continue;
}
for (int i = 0; i < n; i++)
{
if (employees[n].id == employees[i].id)
{
cout << "Invalid id!\n";
is_duplicate = true;
break;
}
}
//if entered id in duplicate the continue the loop
if (is_duplicate)
continue;
//if id is not duplicate and id positive the break the loop
break;
}
//read employee name
cout << "Enter employee's name: ";
cin >> employees[n].name;
//loop for hourly rate if negative
while (true)
{
//read employee hourly rate
cout << "Enter employee's hourly rate: ";
cin >> employees[n].rate;
//if entered rate is negative then continue the loop
if (employees[n].rate <= 0)
{
cout << "Invalid hourly rate!\n";
continue;
}
break;
}
//set hours and taxable to 0
employees[n].hours = 0;
employees[n].taxable = 0;
}
//this function finds employee by id and returns its index in array of employees
int findEmp(int id, Employee employees[], int n)
{
for (int i = 0; i < n; i++)
{
if (employees[i].id == id)
{
return i;
}
}
//if id not found the return -1
return -1;
}
//this function adds hours to fiven employee id
void addHours(Employee employees[], int n)
{
//read employee id
cout << "Enter employee's id: ";
int id;
cin >> id;
//search id using findEmp function
int found = findEmp(id, employees, n);
//if not found then print invalid id and again call addhours
if (found == -1)
{
cout << "Invalid id!\n";
addHours(employees, n);
}
//if id found then add hours to it
else
{
int hours;
cout << "Enter hours to add: ";
cin >> hours;
employees[found].hours += hours;
}
}
//this function prints paychecks of all employees whose hours worked is greater than 0
void printPaychecks(Employee employees[], int n)
{
cout << "\nId\tName\tRate\tIncome\n";
//for all employees
for (int i = 0; i < n; i++)
{
//print id name rate and income
if (employees[i].hours > 0)
{
cout << employees[i].id << "\t";
cout << employees[i].name << "\t";
cout << employees[i].rate << "\t";
cout << employees[i].rate * employees[i].hours << "\n";
}
}
}
int main()
{
Employee employees[50];
int no_of_employees = 0;
int choise = menu();
while (choise != 4)
{
switch (choise)
{
case 1:
{
if (no_of_employees < 50)
{
addEmp(employees, no_of_employees);
no_of_employees++;
}
else
{
cout << "Error! n space for new employee\n";
}
break;
}
case 2:
{
addHours(employees, no_of_employees);
break;
}
case 3:
{
cout << no_of_employees << "\n";
printPaychecks(employees, no_of_employees);
break;
}
}
choise = menu();
}
}