Question

In: Computer Science

Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments....

Write a C++ program to allow the user to:

1. Create two classes. Employee and Departments.
The Department class will have: DepartmentID, Departmentname, DepartmentHeadName.
The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID.

Both of the above classes should have appropriate constructors, accessor methods.

2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3.
Your program should display a menu for the user to do the following:

1. Create Department. Collect all information about a department. Make sure the department ID does not already exist in the array containing Department objects. If it does not, then insert the Department object into the array. When the array is full, display the error message to the user "The array is full, you can not add any more departments"

2. Create Employee. Collect all information about an Employee. Make sure the Employee ID does not already exist in the array containing Employee objects. If it does not, then insert the Employee object into the array. Also make sure that the DepartmentID that the employee belongs also exists. If it does not, then display error message.
When the array is full, display the error message to the user "The array is full, you can not add any more Employees"

3. Write the data to the file. When the user selects this option, dump the information in each array into a separate file.

4. Retrieve data from file. When user selects this option, open each file, load the data from the file into the appropriate array.

5. Display Report: When user selects this option, go through arrays and display the total salary paid for each department. The report should display the department name and the total salary of all employees in that department.

Test Case

TEST CASE1:

1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1
Please Enter Department Details:
Department ID : 1
Department Name : Sales
Head of Department : Anna
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1
Please Enter Department Details:
Department ID : 1
Value must be unique!
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1
Please Enter Department Details:
Department ID : 2
Department Name : Marketing
Head of Department : Mark
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1
Please Enter Department Details:
Department ID : 3
Department Name : GlobalSales           
Head of Department : Patil
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1

The array is full, you can not add any more Departments.
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 1
Employee Name :John
Salary: $45000
Age : 25
Department ID : 1
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 1
Value must be unique!
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1

The array is full, you can not add any more Departments.
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 2
Employee Name :Susan
Salary: $45000
Age : 26
Department ID : 1
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 3
Employee Name :Adam
Salary: $50000
Age : 24
Department ID : 2
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 4
Employee Name :Manny
Salary: $60000
Age : 26
Department ID : 2
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 5
Employee Name :Maria
Salary: $60000
Age : 23
Department ID : 9
Please enter a valid department ID: 3
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
The array is full, you can not add any more Employees.
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 5

Salary Report By Department

Dept         : Sales
Total Salary : $90000

Dept         : Marketing
Total Salary : $110000

Dept         : GlobalSales
Total Salary : $60000
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 9
Please enter a valid choice (1 - 6): 3
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 6
Thank you, goodbye.


TEST CASE 2:

1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1
Please Enter Department Details:
Department ID : 1
Department Name : Sales
Head of Department : Markus
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1
Please Enter Department Details:
Department ID : 1
Value must be unique!
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 1
Please Enter Department Details:
Department ID : 2
Department Name : Marketing
Head of Department : Anna
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 1
Employee Name :John
Salary: $45000
Age : 23
Department ID : 1
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 2    
Employee Name :Susan
Salary: $40000
Age : 45
Department ID : 1
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 2
Please Enter Employee Details:
Employee ID : 3
Employee Name :many
Salary: $70000
Age : 34
Department ID : 2
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 5

Salary Report By Department

Dept         : Sales
Total Salary : $85000

Dept         : Marketing
Total Salary : $70000
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 3
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 
4
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 5

Salary Report By Department

Dept         : Sales
Total Salary : $85000

Dept         : Marketing
Total Salary : $70000
1. Create Department
2. Create Employee
3. Write Out Data File
4. Read In Data File
5. Display Salary Report
6. -- Quit -- 
Please make a selection : 6
Thank you, goodbye.

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

Source Code for main.cpp:

//This program is for creating departments and employees to report the total salary paid for each department.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


class Departments //designing the class named Departments
{
private:
int departmentID;
string departmentName;
string departmentHeadName;

public:
  
Departments() //Constructor
{ departmentID = 0;
departmentName = "NotDefined";
departmentHeadName = "Anonymous";
}
  
void setDepartmentID(int depID)
{ departmentID = depID;
}
  
void setDepartmentName_HeadDepartment()
{
cout << "Department Name : ";
cin >> departmentName;

cout << "Head of Department : ";
cin >> departmentHeadName;
}
  
int getDepartmentID()
{ return departmentID;
}
  
string getDepartmentName()
{ return departmentName;
}
  
string getDepartmentHeadName()
{ return departmentHeadName;
}
};

class Employee //designing the class named Employee
{
private:
int employeeID;
string employeeName;
int employeeSalary;
int employeeAge;
int employeeDepartmentID;
  
  
public:

Employee() //Constructor
{   
employeeID = 0;
employeeName = "Anonymous";
employeeSalary = 0;
employeeAge = 0;
employeeDepartmentID = 0;
}
  
void setEmployeeID(int empID)
{ employeeID = empID;
}

void setEmployeeDepData(int empDepData)
{ employeeDepartmentID = empDepData;
}

void setEmployeeOtherData()
{
cout << "Employee Name :";
cin >> employeeName;
  
cout << "Salary: $";
cin >> employeeSalary;
  
cout << "Age : ";
cin >> employeeAge;
}
  
int getEmployeeID()
{ return employeeID;
}
  
string getEmployeeName()
{ return employeeName;
}
  
int getEmployeeSalary()
{ return employeeSalary;
}
  
int getEmployeeAge()
{ return employeeAge;
}
  
int getEmployeeDepartmentID()
{ return employeeDepartmentID;
}
};

void displayMenu() //the function of display menu
{
cout << "1. Create Department" << endl;
cout << "2. Create Employee" << endl;
cout << "3. Write Out Data File" << endl;
cout << "4. Read In Data File" << endl;
cout << "5. Display Salary Report" << endl;
cout << "6. -- Quit -- " << endl;
}


int main()
{
int numEmployee = 5;
int numDepartments = 3;
int tempDepartmentID, tempEmployeeID, tempEmpDepID, i = 0, e = 0, depIndex, empIndex = 0, k=0, j;
  
int fromFileDepID[numDepartments], fromFileEmpID[numEmployee], fromFileEmpSalary[numEmployee], fromFileEmpAge[numEmployee], fromFileEmpDeppartID[numEmployee];
string fromFileDepName[numDepartments], fromFileDepHeadName[numDepartments], fromFileEmpName[numEmployee];
  
int totalSalary[numDepartments];
  
char charSelection;
  
  
Employee employee[numEmployee]; //declaring an employee object of class Employee
Departments departments[numDepartments]; //declaring a departments object of class Departments
ofstream outputFile;
ifstream inputFile;
  
do
{
displayMenu(); //calling the displayMenu function
  
cout << "Please make a selection : ";
cin >> charSelection;
if (charSelection <= '0' || charSelection > '7') //validating the variable of charSelection
{ cout << "Please enter a valid choice (1 - 6): ";
cin >> charSelection;
}
  
switch(charSelection)
{
case '1': while (i < numDepartments) //finding if this array is full using while loop
{ if (departments[i].getDepartmentID() != 0)   
i++;
else break;   
}   
  
if (i == numDepartments) //in case the array is full
cout << endl << "The array is full, you can not add any more Departments." << endl;   
  
else //in case the array is not full
{
cout << "Please Enter Department Details:" << endl;
cout << "Department ID : ";
cin >> tempDepartmentID;
  
for (depIndex=0; depIndex < numDepartments; depIndex++) //finding if the departmentID is unique
{ if (tempDepartmentID == departments[depIndex].getDepartmentID() ) //in case the departmentID is not unique
{ cout << "Value must be unique!" << endl;
break;
}
}   

if (depIndex == numDepartments) //in case the departmentID is unique
{ for (int j=0; j < numDepartments; j++)   
{ if (departments[j].getDepartmentID() == 0 )
{ departments[j].setDepartmentID(tempDepartmentID);
departments[j].setDepartmentName_HeadDepartment();
break;
}
}
}
else
break; // escaping switch statement because the departmentID is not unique
}

break;
  
case '2': while (e < numEmployee) //finding if this array is full using while loop
{ if (employee[e].getEmployeeID() != 0)
e++;
else break;   
}
  
if (e == numEmployee) //in case the array is full
cout << "The array is full, you can not add any more Employees." << endl;
  
else //in case the array is not full
{
cout << "Please Enter Employee Details:" << endl;
cout << "Employee ID : ";
cin >> tempEmployeeID;
  
for (empIndex=0; empIndex < numEmployee; empIndex++) //finding if the employeeID is unique
{ if (tempEmployeeID == employee[empIndex].getEmployeeID() ) //in case the employeeID is not unique
{ cout << "Value must be unique!" << endl;
break;
}
}   

if (empIndex == numEmployee) //in case the employeeID is unique
{   
for (j=0; j < numEmployee; j++)   
{ if (employee[j].getEmployeeID() == 0 )
{ employee[j].setEmployeeID(tempEmployeeID);
employee[j].setEmployeeOtherData();
break;
}
}
}
else
break; //escaping switch statement because the employeeID is not unique

cout << "Department ID : ";
cin >> tempEmpDepID;

do //validating employee's Department ID using while loop
{
for (k=0; k < numDepartments; k++)   
{ if (tempEmpDepID == departments[k].getDepartmentID() ) //in case the department ID of an employee is valid
{ employee[j].setEmployeeDepData(tempEmpDepID);
break;
}
}

if (tempEmpDepID == departments[k].getDepartmentID()) //escaping do..while loop because the department ID of an employee is valid
break;

else //if (k == numDepartments) //in case the department ID of an employee is not valid
{ cout << "Please enter a valid department ID: ";
cin >> tempEmpDepID;
}
}while (k == numDepartments);
  
}

break;
  
case '3': outputFile.open("department.dat"); //Opening the output file named "department.dat"
for (int outFileIndex = 0; outFileIndex < numDepartments; outFileIndex++) //writing the data to the file using for loop
{
outputFile << departments[outFileIndex].getDepartmentID() << endl;
outputFile << departments[outFileIndex].getDepartmentName() << endl;
outputFile << departments[outFileIndex].getDepartmentHeadName() << endl;
}
outputFile.close(); //Closing the file

outputFile.open("employee.dat"); //Opening the output file named "employee.dat"
for (int outFileIndex = 0; outFileIndex < numEmployee; outFileIndex++) //writing the data to the file using for loop
{
outputFile << employee[outFileIndex].getEmployeeID() << endl;
outputFile << employee[outFileIndex].getEmployeeName() << endl;
outputFile << employee[outFileIndex].getEmployeeSalary() << endl;
outputFile << employee[outFileIndex].getEmployeeAge() << endl;
outputFile << employee[outFileIndex].getEmployeeDepartmentID() << endl;
}
outputFile.close(); //Closing the file

break;

case '4': inputFile.open("department.dat"); //Opening the input file named "department.dat"
for (int inFileIndex = 0; inFileIndex < numDepartments; inFileIndex++) //Retrieving data from the file using array and for loop
{
inputFile >> fromFileDepID[inFileIndex];
inputFile >> fromFileDepName[inFileIndex];
inputFile >> fromFileDepHeadName[inFileIndex];
}
inputFile.close(); //Closing the file

inputFile.open("employee.dat"); //Opening the input file named "employee.dat"
for (int inFileIndex = 0; inFileIndex < numEmployee; inFileIndex++) //Retrieving data from the file using array and for loop
{
inputFile >> fromFileEmpID[inFileIndex];
inputFile >> fromFileEmpName[inFileIndex];
inputFile >> fromFileEmpSalary[inFileIndex];   
inputFile >> fromFileEmpAge[inFileIndex];
inputFile >> fromFileEmpDeppartID[inFileIndex];
}   
inputFile.close(); //Closing the file

break;

case '5': for (int p=0; p < numDepartments; p++) //Calculating the total salary for each department
{
totalSalary[p] = 0;
for (int m=0; m < numEmployee; m++)
{
if (departments[p].getDepartmentID() == employee[m].getEmployeeDepartmentID())
totalSalary[p] += employee[m].getEmployeeSalary();
}
}

//Start to display report

cout << endl << "Salary Report By Department" << endl;
for (int n=0; n < numDepartments; n++)
{
cout << endl;
cout << left << setw(13) << "Dept" << ": " << departments[n].getDepartmentName() << endl;
cout << left << setw(13) << "Total Salary" << ": $" << totalSalary[n] << endl;
}

break;
  
}
  

}while (charSelection != '6');
  
cout << "Thank you, goodbye.";

return 0;
}


Related Solutions

Write a C++ program that : 1. Allow the user to enter the size of the...
Write a C++ program that : 1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11. 2. Create an vector of size N x N. 3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array. 4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix...
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
Write a python program that will allow a user to draw by inputting commands. The program...
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing. Must include the following: change attributes: color [red | green | blue] width [value] heading [value] position [xval] [yval] drawing: draw_axes draw_tri [x1] [y1] [x2] [y2] [x3] [y3 draw_rect [x] [y] [b] [h] draw_poly [x] [y] [n] [s] draw_path [path] random random [color | width...
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS...
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS COVERING THE FOLLOWING POINTS: 1) COUNTING NUMBER OF OBJECTS CREATED ( NO OBJECT ARRAY TO BE USED) USING ROLE OF STATIC MEMBER 2) SHOWING THE VALID INVALID STATEMENTS IN CASE OF STATIC MEMBER WITH NON STATIC MEMBER FUNCTION, NON STATIC MEMBERS OF CLASS WITH STATIC MEMBER FUNCTION, BOTH STATIC. SHOW THE ERRORS WHERE STATEMENTS ARE INVALID. 3) CALL OF STATIC MEMBER FUNCTION, DECLARATION OF...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Create design document for a program that will allow the user: Convert a number to binary....
Create design document for a program that will allow the user: Convert a number to binary. Convert from binary to number. Display the hexadecimal representation of a binary number. Given an hexadecimal display its binary representation. Convert a number to IEEE single precision and vice versa. More to come. PLEASE ADD PSEUDOCODE AND USE C PROGRAMMING USE FUNCTIONS IF POSSIBLE
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT