Question

In: Computer Science

Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined...

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.

Solutions

Expert Solution

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();

}

}


Related Solutions

1.  What is an abstract data type? In an ADT, what is known and what is hidden?
1.  What is an abstract data type? In an ADT, what is known and what is hidden?
Implement a program as an object using a class (abstract data type) in C++ that does...
Implement a program as an object using a class (abstract data type) in C++ that does the following: 1) reads the firstName, lastName and 3 test scores of at least five students. 2) calculate student test score totals, average, letter grade for each student. 3) Display the results in a table format showing firstName, lastName, test1, test2, test3, total, average, letterGrade, of all the students. 3 files .h, .cpp, main.cpp create an object that can hold records. must get records...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.
What do you understand by abstract data type or ADT? Provide examples and discuss implementation mechanisms...
What do you understand by abstract data type or ADT? Provide examples and discuss implementation mechanisms in C++ and in object-oriented programming languages in general.
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What...
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What is the running time for each operation?
In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement each member function in the class below. Some of the functions we may have already done in the lecture, that's fine, try to do those first without looking at your notes. You may add whatever private data members or private member functions you want to this class. #include #include typedef std::string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private:...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type for payroll. It has data members for an employee’s hourly pay rate, number of hours worked, and total pay for the week. Your class must include the following member functions: a constructor to set the hours and pay rate as arguments, a default constructor to set data members to 0, member functions to set each of the member variables to values given as an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT