In: Computer Science
This program is in C++
Please kindly make sure that the output exactly matches the test case so I can vote a thumbs up. If it doesn't the program is wrong.
Test Cases and the given code are given after the prompt
Prompt:
Modify the given code to:
1.. Store the data in Binary file and access it in Random Access mode.
2.Replace Class with Structure for Employee and Department.
3. Inside each structure, replace all string variables with array of characters. please read the chapter 12(More about characters and strings). Though we do not have homework on this, the knowledge from this chapter will help you do the final exam project.
4. Make Employee and Department editable. That means, the user should be able to edit a given Employee and Department. Youc an allow the user to edit Employee name, age etc and assign him/her to different department. Similarly department name and department head can be changed. However, do not allow the uesr to Employee ID in Employee file and Department ID in department file.
5. Please note that the data will no longer be stored in the array as it was in the given code. Instead, it should be written to the file as soon as you collect the data from the user. If you are editing a record, read it from the file,collect new data from the user, store the record back to the file in the same place it was found inside the file. That means, the menu will not have options to save data to file or read data from file. Also, this should provide the ability for user to create unlimited number of employees and departments unlike in given code where you allowed only limited number of departments and employees.
Test Cases:
////////////////////////////////////
TEST CASE1: Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 1 Enter the NEW Department Data: Dept ID: 1 Dept Name: Sales Head of Dept Name: Anna Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 2 Enter the NEW Employee Data: Employee ID: 1 Employee Name: John Employee Salary: $45000 Employee Age: 23 Department ID: 1 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 5 Salary Report By Department Dept : Sales Total Salary : $45000 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 2 Enter the NEW Employee Data: Employee ID: 2 Employee Name: Susan Employee Salary: $50000 Employee Age: 30 Department ID: 1 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 5 Salary Report By Department Dept : Sales Total Salary : $95000 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 1 Enter the NEW Department Data: Dept ID: 2 Dept Name: Marketing Head of Dept Name: Marcus Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 2 Enter the NEW Employee Data: Employee ID: 3 Employee Name: Adam Employee Salary: $60000 Employee Age: 30 Department ID: 2 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 5 Salary Report By Department Dept : Sales Total Salary : $95000 Dept : Marketing Total Salary : $60000 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 3 Which record to EDIT: Please choose one of the following... 1 to 2 : 2 Display Department Details: Dept ID : 2 Dept Name : Marketing Dept Head : Marcus EDIT the Department Data: Dept Name: Global Marketing Head of Dept Name: Butler Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 5 Salary Report By Department Dept : Sales Total Salary : $95000 Dept : Global Marketing Total Salary : $60000 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 4 Which record to EDIT: Please choose one of the following... 1 to 3 : 3 Display Employee Details: ID : 3 Name : Adam Salary : $60000 Age : 30 Dept : 2 Edit the Employee Data: Employee Name: Adam Employee Salary: $75000 Employee Age: 31 Department ID: 1 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 5 Salary Report By Department Dept : Sales Total Salary : $170000 Dept : Global Marketing Total Salary : $0 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 9 Please enter a valid choice (1 - 6): 6 Thank you, goodbye. TEST CASE2: Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 1 Enter the NEW Department Data: Dept ID: 1 Dept Name: HumanResources Head of Dept Name: Maria Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 1 Enter the NEW Department Data: Dept ID: 1 Please enter a unique Deptartment ID: 2 Dept Name: Marketing Head of Dept Name: Samy Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 2 Enter the NEW Employee Data: Employee ID: 1 Employee Name: John Employee Salary: $45000 Employee Age: 35 Department ID: 9 Please enter an existing Deptartment ID: 1 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 2 Enter the NEW Employee Data: Employee ID: 1 Please enter a unique Employee ID: 2 Employee Name: Susan Employee Salary: $70000 Employee Age: 25 Department ID: 2 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 5 Salary Report By Department Dept : HumanResources Total Salary : $45000 Dept : Marketing Total Salary : $70000 Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- Please make a selection : 9 Please enter a valid choice (1 - 6): 6 Thank you, goodbye.
/////////////////////////////////////
The Given Code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
class Depts
{ private:
int DeptID;
string DeptName;
string DeptHName;
public:
Depts() {}
Depts(int inId, string name, string headName)
{ DeptID = inId;
DeptHName = headName;
DeptName = name;
}
string getDeptHname() const
{ return DeptHName;
}
int getDeptHID() const
{ return DeptID;
}
string getDeptName() const
{ return DeptName;
}
void setDeptHName(string DeptHName)
{ this->DeptHName = DeptHName;
}
void setDeptID(int DeptID)
{ this->DeptID = DeptID;
}
void setDeptname(string DeptName)
{ this->DeptName = DeptName;
}
};
class Employee
{ private:
int empID;
string empName;
double empSalary;
double empAge;
int empDeptID;
public:
Employee() {}
Employee(int id, string name, double salary, double age, int dId)
{ empID = id;
empName = name;
empSalary = salary;
empAge = age;
empDeptID = dId;
}
string getEmploeename() const
{ return empName;
}
int getEmpDeptID() const
{ return empDeptID;
}
int getEmpID() const
{ return empID;
}
double getEmpAge() const
{ return empAge;
}
double getEmpSalary() const
{ return empSalary;
}
void setEmpName(string empName)
{ this->empName = empName;
}
void setEmpDeptID(int empDeptID)
{ this->empDeptID = empDeptID;
}
void setEmpID(int empID)
{ this->empID = empID;
}
void setEmpAge(double empAge)
{ this->empAge = empAge;
}
void setEmpSalary(double empSalary)
{ this->empSalary = empSalary;
}
};
int main()
{ Employee employees[5];
Depts departments[3];
int noOfDepartment = 0;
int noOfEmployees = 0;
int choice;
while (choice != 6)
{ cout << "1. Create Department"
"\n2. Create Employee"
"\n3. Write Out Data File"
"\n4. Read In Data File"
"\n5. Display Salary Report"
"\n6. -- Quit -- \n"
"Please make a selection : ";
cin >> choice;
if (choice == 1)
{ if (noOfDepartment == 3)
{ cout << "The array is full, you can not add any more Depts." << endl;
continue;
}
int dId;
string dName;
string dHeadName;
cout << "Please Enter Department Details:" << endl;
cout << "Department ID : ";
cin >> dId;
bool validId = true;
for (int i = 0; i < noOfDepartment; i++)
{ if (departments[i].getDeptHID() == dId)
{ cout << "Value must be unique!" << endl;
validId = false;
}
}
if (!validId)
{ continue;
}
cout << "Department Name : ";
cin >> dName;
cout << "Head of Department : ";
cin >> dHeadName;
Depts d(dId, dName, dHeadName);
departments[noOfDepartment] = d;
noOfDepartment++;
}
else if (choice == 2)
{ if (noOfEmployees == 5)
{ cout << "The array is full, you can not add any more Employees." << endl;
continue;
}
int eId;
string eName;
double eSalary;
double eAge;
int eDepartmentid;
cout << "Please Enter Employee Details:" << endl;
cout << "Employee ID : ";
cin >> eId;
bool validId = true;
for (int i = 0; i < noOfEmployees; i++)
{ if (employees[i].getEmpID() == eId)
{ cout << "Value must be unique!" << endl;
validId = false;
}
}
if (!validId)
{ continue;
}
cout << "Employee Name :";
cin >> eName;
cout << "Salary: $";
cin >> eSalary;
cout << "Age : ";
cin >> eAge;
cout << "Department ID : ";
cin >> eDepartmentid;
bool foundId = false;
while (!foundId)
{ for (int i = 0; i < noOfDepartment; i++)
{ if (departments[i].getDeptHID() == eDepartmentid)
{ foundId = true;
break;
}
}
if (!foundId)
{ cout << "Please enter a valid department ID: ";
cin >> eDepartmentid;
}
}
Employee e(eId, eName, eSalary, eAge, eDepartmentid);
employees[noOfEmployees] = e;
noOfEmployees++;
}
else if (choice == 3)
{ ofstream myfile1("departments.txt");
ofstream myfile2("employees.txt");
for (int i = 0; i < noOfDepartment; i++)
{ myfile1 << departments[i].getDeptHID() << " " << departments[i].getDeptName() << " " << departments[i].getDeptHname() << endl;
}
for (int i = 0; i < noOfEmployees; i++)
{ myfile2 << employees[i].getEmpID() << " " << employees[i].getEmploeename() << " " << employees[i].getEmpSalary() << " " << employees[i].getEmpAge() << " " << employees[i].getEmpDeptID() << endl;
}
myfile1.close();
myfile2.close();
}
else if (choice == 4)
{ ifstream infile;
string line;
int dId;
string dName;
string dHeadName;
noOfDepartment = 0;
infile.open("departments.txt");
ifstream infile2;
int eId;
string eName;
double eSalary;
double eAge;
int eDepartmentid;
noOfEmployees = 0;
infile2.open("employees.txt");
}
else if (choice == 5)
{ string name = "";
double salary = 0;
cout<<"\n";
cout<<"Salary Report By Department\n";
for (int i = 0; i < noOfDepartment; i++)
{ salary = 0;
name = departments[i].getDeptName();
for (int j = 0; j < noOfEmployees; j++)
{ if (departments[i].getDeptHID() == employees[j].getEmpDeptID())
{ salary += employees[j].getEmpSalary();
}
}
cout << "\nDept : " << name << endl;
cout << "Total Salary : $" << salary <<endl;
}
}
else {
cout<<"Thank you, goodbye.";
return 0;
}
}
}
Thank you so much, I will leave a positive rate
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
// alary paid for each department and storing the data in Binary
Files and
// accessing it in Random Access mode.
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
using namespace std;
const int sizeDepName = 20, sizeName = 15;
fstream departments, employees;
struct Department //designing the structure named
Department
{
int departmentID;
char departmentName[sizeDepName];
char departmentHeadName[sizeName];
};
struct Employee //designing the structure named Employee
{
int employeeID;
char employeeName[sizeName];
int employeeSalary;
int employeeAge;
int employeeDepartmentID;
};
Department dep; //declaring a structure variable
Employee emp; //declaring a structure variable
void depFileOpen(); // Function Prototypes
void empFileOpen(); // Function Prototypes
void displayMenu(); // Function Prototypes
void validation(char &num); // Function Prototypes
int vdDepartmentID(int depID); // Function Prototypes
int vdEmployeeID(int empID); // Function Prototypes
int vdEmployeeDepID(int empDepID); // Function Prototypes
int numRecordDep(); // Function Prototypes
int numRecordEmp(); // Function Prototypes
int main()
{
char charSelection; //for user choices
string input; //use to read strings
int choiceEdit; //store the choice to edit department and
employee
int count, countRecordDep, countRecordEmp;
do
{
displayMenu(); //calling the displayMenu function
cout << "Please make a selection : ";
cin >> charSelection;
validation(charSelection); //validating the variable of
charSelection
if (charSelection == '1') //in case of creating departments
{
cout << "Enter the NEW Department Data:" << endl;
cout << "Dept ID: ";
cin >> dep.departmentID;
dep.departmentID = vdDepartmentID(dep.departmentID);
//validating
cin.ignore();
cout << "Dept Name: ";
getline(cin, input);
strcpy(dep.departmentName, input.c_str());
cout << "Head of Dept Name: ";
getline(cin, input);
strcpy(dep.departmentHeadName, input.c_str());
depFileOpen();
departments.seekp(0L, ios::end);
departments.write(reinterpret_cast<char *>(&dep),
sizeof(dep));
departments.close();
}
else if (charSelection == '2') //in case of creating
employees
{
cout << "Enter the NEW Employee Data:" << endl;
cout << "Employee ID: ";
cin >> emp.employeeID;
emp.employeeID = vdEmployeeID(emp.employeeID); //validating
cin.ignore();
cout << "Employee Name: ";
cin.getline(emp.employeeName, sizeName);
cout << "Employee Salary: $";
cin >> emp.employeeSalary;
cout << "Employee Age: ";
cin >> emp.employeeAge;
cout << "Department ID: ";
cin >> emp.employeeDepartmentID;
//validating employee's department should be one of existed
departments
emp.employeeDepartmentID =
vdEmployeeDepID(emp.employeeDepartmentID);
empFileOpen();
employees.seekp(0L, ios::end); //random access
employees.write(reinterpret_cast<char *>(&emp),
sizeof(emp));
employees.close();
}
else if (charSelection == '3') //editing the existed
departments
{
cout << "Which record to Edit:" << endl;
cout << "Please choose one of the following... 1 to "
<< numRecordDep() << " : ";
cin >> choiceEdit;
//validating whether the choice is one of existed departments
while (choiceEdit < 1 || choiceEdit > numRecordDep())
{
cout << "Please enter a valid choice (1 to " <<
numRecordDep() << "): ";
cin >> choiceEdit;
}
//display the department data to edit
depFileOpen();
departments.seekg(sizeof(dep)*(choiceEdit-1), ios::beg);
departments.read(reinterpret_cast<char *>(&dep),
sizeof(dep));
cout << endl << "Display Department Details:" <<
endl;
cout << left << setw(13) << "Dept ID : " <<
choiceEdit << endl;
cout << left << setw(13) << "Dept Name : "
<< dep.departmentName << endl;
cout << left << setw(13) << "Dept Head : "
<< dep.departmentHeadName << endl << endl;
//editing the department data except the department ID
cout << "Edit the Department Data:" << endl;
dep.departmentID = choiceEdit;
cin.ignore();
cout << "Dept Name: ";
getline(cin, input);
strcpy(dep.departmentName, input.c_str());
cout << "Head of Dept Name: ";
getline(cin, input);
strcpy(dep.departmentHeadName, input.c_str());
departments.seekp(sizeof(dep)*(choiceEdit-1), ios::beg);
departments.write(reinterpret_cast<char *>(&dep),
sizeof(dep));
departments.close();
}
else if (charSelection == '4') //editing the existed
employees
{
cout << "Which record to Edit:" << endl;
cout << "Please choose one of the following... 1 to "
<< numRecordEmp() << " : ";
cin >> choiceEdit;
//validating whether the choice is one of existed employees
while (choiceEdit < 1 || choiceEdit > numRecordEmp())
{
cout << "Please enter a valid choice (1 to " <<
numRecordEmp() << "): ";
cin >> choiceEdit;
}
//display the employee data to edit
empFileOpen();
employees.seekg(sizeof(emp)*(choiceEdit-1), ios::beg);
employees.read(reinterpret_cast<char *>(&emp),
sizeof(emp));
cout << endl << "Display Employee Details:" <<
endl;
cout << left << setw(9) << "ID : " <<
choiceEdit << endl;
cout << left << setw(9) << "Name : " <<
emp.employeeName << endl;
cout << left << setw(9) << "Salary : $" <<
emp.employeeSalary << endl;
cout << left << setw(9) << "Age : " <<
emp.employeeAge << endl;
cout << left << setw(9) << "Dept : " <<
emp.employeeDepartmentID << endl << endl;
//editing the employee data except the employee ID
cout << "Edit the Employee Data:" << endl;
emp.employeeID = choiceEdit;
cin.ignore();
cout << "Employee Name: ";
getline(cin, input);
strcpy(emp.employeeName, input.c_str());
cout << "Employee Salary: $";
cin >> emp.employeeSalary;
cout << "Employee Age: ";
cin >> emp.employeeAge;
cout << "Department ID: ";
cin >> emp.employeeDepartmentID;
emp.employeeDepartmentID =
vdEmployeeDepID(emp.employeeDepartmentID);
employees.seekp(sizeof(emp)*(choiceEdit-1), ios::beg);
employees.write(reinterpret_cast<char *>(&emp),
sizeof(emp));
employees.close();
}
else if (charSelection == '5') //in case of displaying salary
report by department
{
cout << endl << "Salary Report By Department" <<
endl;
countRecordDep = numRecordDep();
int totalSalary[countRecordDep];
depFileOpen();
for (int p=0; p < countRecordDep ; p++) //Calculating the total
salary for each department
{
departments.seekg(p*sizeof(dep), ios::beg);
departments.read(reinterpret_cast<char *>(&dep),
sizeof(dep));
totalSalary[p] = 0;
countRecordEmp = numRecordEmp();
empFileOpen();
for (int m=0; m < countRecordEmp ; m++)
{
employees.seekg(m*sizeof(emp), ios::beg);
employees.read(reinterpret_cast<char *>(&emp),
sizeof(emp) );
if (dep.departmentID == emp.employeeDepartmentID)
totalSalary[p] += emp.employeeSalary;
}
employees.close();
cout << endl;
cout << left << setw(13) << "Dept" << ": "
<< dep.departmentName << endl;
cout << left << setw(13) << "Total Salary"
<< ": $" << totalSalary[p] << endl;
}
departments.close();
}
}while (charSelection != '6');
cout << "Thank you, goodbye.";
return 0;
}
void depFileOpen() // Function for opening a department file as
binary (both input and output)
{
departments.open("departments.dat", ios::in | ios::out |
ios::binary);
if (departments.fail())
{
departments.open("departments.dat", ios::out | ios::in |
ios::binary | ios::trunc);
if (departments.fail())
{
cout << "Error opening department file....";
}
}
}
void empFileOpen() // Function for opening a employee file as
binary (both input and output)
{
employees.open("employees.dat", ios::in | ios::out |
ios::binary);
if (employees.fail())
{
employees.open("employees.dat", ios::out | ios::in | ios::binary |
ios::trunc);
if (employees.fail())
{
cout << "Error opening employee file....";
}
}
}
void displayMenu() // function for displaying menu
{
cout << endl;
cout << "Human Resources Menu" << endl;
cout << "1. Create Department" << endl;
cout << "2. Create Employee" << endl;
cout << "3. Edit Department" << endl;
cout << "4. Edit Employee" << endl;
cout << "5. Display Salary Report" << endl;
cout << "6. -- Quit -- " << endl;
}
void validation(char &sel) //Funtion for validating the
choice at the beginning menu
{
while (sel <= '0' || sel >= '7') //validating the variable of
Selection
{
cout << "Please enter a valid choice (1 - 6): ";
cin >> sel;
}
}
int vdDepartmentID(int depID) //Validating the department ID
{
while (depID <= 0)
{
cout << "Please enter the positive number: ";
cin >> depID;
}
depFileOpen();
departments.seekg(0L, ios::beg);
departments.read(reinterpret_cast<char *>(&dep),
sizeof(dep));
while (!departments.eof())
{
while (depID == dep.departmentID)
{
cout << "Please enter a unique Deptartment ID: ";
cin >> depID;
}
departments.read(reinterpret_cast<char *>(&dep),
sizeof(dep));
}
departments.close();
return depID;
}
int vdEmployeeID(int empID) //Validating the employee ID
{
while (empID <= 0)
{
cout << "Please enter the positive number: ";
cin >> empID;
}
empFileOpen();
employees.seekg(0L, ios::beg);
employees.read(reinterpret_cast<char *>(&emp),
sizeof(emp));
while (!employees.eof() )
{
while (empID == emp.employeeID)
{
cout << "Please enter a unique Employee ID: ";
cin >> empID;
}
employees.read(reinterpret_cast<char *>(&emp),
sizeof(emp));
}
employees.close();
return empID;
}
int vdEmployeeDepID(int empDepID) //Validating the employee's
department ID
{
bool flag;
flag = false;
while (!flag)
{
depFileOpen();
departments.seekg(0L, ios::beg);
departments.read(reinterpret_cast<char *>(&dep),
sizeof(dep));
while (!departments.eof() && !flag)
{
if (dep.departmentID == empDepID)
flag = true;
departments.read(reinterpret_cast<char *>(&dep),
sizeof(dep));
}
departments.close();
if (!flag)
{
cout << "Please enter an existing Deptartment ID: ";
cin >> empDepID;
}
}
return empDepID;
}
int numRecordDep() //Return the number of records in departments
file
{
int count = 0;
depFileOpen();
departments.read(reinterpret_cast<char *>(&dep),
sizeof(dep));
while (!departments.eof() )
{
count++;
departments.read(reinterpret_cast<char *>(&dep),
sizeof(dep));
}
departments.close();
return count;
}
int numRecordEmp() //Return the number of records in employees
file
{
int count = 0;
empFileOpen();
employees.read(reinterpret_cast<char *>(&emp),
sizeof(emp));
while (!employees.eof() )
{
count++;
employees.read(reinterpret_cast<char *>(&emp),
sizeof(emp));
}
employees.close();
return count;
}
Sample Output Screenshots:
Hope it helps, if you like the answer give it a thumbs up. Thank you.