C++ This program needs to be modified to the following: 1. Replace Employee and
ID: 3859487 • Letter: C
Question
C++
This program needs to be modified to the following:
1. Replace Employee and Department classes with Employee and Department Structures.
2. Inside each structure, replace all string variables with array of characters.
3. Make Employee and Department editable. That means, the user should be able to edit a given Employee and Department.
4. Do not allow the user to edit the Employee ID and Department ID.
5. Use Random Access Files to store the data in Binary Form. This means, you should not use an Arrays to
store the data in the memory. Instead, when the user wants to create a new Employee/Department,
you write it to the file right away. Also when the user says he/she wants to edit
an Employee/Department, ask the user to enter EmployeeID/DepartmentID.
Read the data from the file and display it to the user.
Allow the user to enter new data and write it back to the file in the same position inside the file.
------------------------------------------------
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
class Departments{
string departmentID,departmentName,departmentHeadName;
public:
Departments(){}
Departments(string ID,string name){
departmentID="";
departmentName="";
departmentHeadName="";
}
Departments(string ID,string name,string head){
departmentID=ID;
departmentName=name;
departmentHeadName=head;
}
void setDepartmentID(string ID){
departmentID=ID;
}
void setDepartmentName(string name){
departmentName=name;
}
void setDepartmentHeadName(string head){
departmentHeadName=head;
}
string getDepartmentID(){
return departmentID;
}
string getDepartmentName(){
return departmentName;
}
string getDepartmentHeadName(){
return departmentHeadName;
}
};
class Employee{
string employeeID, employeeName, employeeDepartmentID;
int employeeAge;
double employeeSalary;
public:
Employee(){}
Employee(string ID, string name){
employeeID=ID;
employeeName=name;
}
Employee(string ID, string name,int age){
employeeID=ID;
employeeName=name;
employeeAge=age;
}
Employee(string ID, string name,int age, double salary){
employeeID=ID;
employeeName=name;
employeeAge=age;
employeeSalary=salary;
}
Employee(string ID, string name,int age, double salary,string department){
employeeID=ID;
employeeName=name;
employeeAge=age;
employeeSalary=salary;
employeeDepartmentID=department;
}
void setEmployeeID(string ID){
employeeID=ID;
}
void setEmployeeName(string name){
employeeName=name;
}
void setEmployeeAge(int age){
employeeAge=age;
}
void setEmployeeSalary(double salary){
employeeSalary=salary;
}
void setEmployeeDepartment(string dept){
employeeDepartmentID=dept;
}
string getEmployeeID(){
return employeeID;
}
string getEmployeeName(){
return employeeName;
}
int getEmployeeAge(){
return employeeAge;
}
double getEmployeeSalary(){
return employeeSalary;
}
string getEmployeeDepartment(){
return employeeDepartmentID;
}
};
int main(){
Employee e[7];
Departments d[3];
int ch=0, employeeAge, i=0, j=0;
string departmentID,departmentName,departmentHeadName;
string employeeID, employeeName, employeeDepartmentID;
double employeeSalary;
do{
cout<<" 1. Create Department 2. Create Employee";
cout<<" 3. Write the data to the file 4. Retrieve data from file";
cout<<" 5. Display Report 6.Exit Enter your choice : ";
cin>>ch;
switch(ch){
case 1: if(i<3){
cout<<" Enter the Department Id : ";
cin.ignore();
getline(cin,departmentID);
d[i].setDepartmentID(departmentID);
cout<<" Enter the Department Name : ";
cin.ignore();
getline(cin,departmentName);
d[i].setDepartmentName(departmentName);
cout<<" Enter the Department HeadName : ";
cin.ignore();
getline(cin,departmentHeadName);
d[i].setDepartmentHeadName(departmentHeadName);
i++;
}
else
cout<<" The array is full, you can not add any more Departments";
break;
case 2: if(j<7){
cout<<" Enter the Employee Id : ";
cin.ignore();
getline(cin,employeeID);
e[j].setEmployeeID(employeeID);
cout<<" Enter the Employee Name : ";
cin.ignore();
getline(cin,employeeName);
e[j].setEmployeeName(employeeName);
cout<<" Enter the Employee Department ID : ";
cin.ignore();
getline(cin,employeeDepartmentID);
e[j].setEmployeeDepartment(employeeDepartmentID);
cout<<" Enter the Employee Age : ";
cin>>employeeAge;
e[j].setEmployeeAge(employeeAge);
cout<<" Enter the Employee Salary : ";
cin>>employeeSalary;
e[j].setEmployeeSalary(employeeSalary);
j++;
}
else
cout<<" The array is full, you can not add any more Employees";
break;
case 3: {
ofstream myfile;
myfile.open ("example.txt");
for(int k=0;k<3;k++){
myfile<<" "<<d[k].getDepartmentHeadName();
myfile<<" "<<d[k].getDepartmentID();
myfile<<" "<<d[k].getDepartmentName()<<" ";
}
myfile.close();
break;
}
case 5: cout<<" Report : ";
for(int k=0;k<3;k++){
cout<<" "<<d[k].getDepartmentHeadName()<<" ";
cout<<" "<<d[k].getDepartmentID()<<" ";
cout<<" "<<d[k].getDepartmentName()<<" ";
}
break;
case 6: exit(1);
break;
default : cout<<" Invalid option";
break;
}
}while(ch != 6);
//3. Write the data to the 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.)
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
//1st Class declaration
class Departmentstructure
{
private:
string DepartmentstructureID;
string DepartmentstructureName;
string DepartmentstructureHeadName;
public:
Departmentstructure()
{}
Departmentstructure(string ID, string name)
{
DepartmentstructureID=" ";
DepartmentstructureName=" ";
DepartmentstructureHeadName=" ";
}
Departmentstructure(string ID, string name, string head)
{
DepartmentstructureID=ID;
DepartmentstructureName=name;
DepartmentstructureHeadName=head;
}
void setDepartmentstructureID(string ID)
{
DepartmentstructureID=ID;
}
void setDepartmentstructureName(string name)
{
DepartmentstructureName=name;
}
void setDepartmentstructureHeadName(string head)
{
DepartmentstructureHeadName=head;
}
string getDepartmentstructureID()
{
return DepartmentstructureID;
}
string getDepartmentstructureName()
{
return DepartmentstructureName;
}
string getDepartmentstructureHeadName()
{
return DepartmentstructureHeadName;
}
};
//2nd Class declaration
class Employee
{
private:
string employeeID[];
string employeeName[];
double employeeSalary[];
int employeeAge[];
string employeeDepartmentstructureID[];
public:
Employee()
{}
Employee(string ID, string name)
{
employeeID=ID;
employeeName=name;
}
Employee(string ID, string name, int age)
{
employeeID=ID;
employeeName=name;
employeeAge=age;
}
Employee(string ID, string name, int age, double salary)
{
employeeID=ID;
employeeName=name;
employeeAge=age;
employeeSalary=salary;
}
Employee (string ID, string name, int age, double salary, string Departmentstructure)
{
employeeID=ID;
employeeName=name;
employeeAge=age;
employeeSalary=salary;
employeeDepartmentstructureID=Departmentstructure;
}
void setEmployeeID(string ID)
{
employeeID=ID;
}
void setEmployeeName(string name)
{
employeeName=name;
}
void setEmployeeAge(int age)
{
employeeAge=age;
}
void setEmployeeSalary(double salary)
{
employeeSalary=salary;
}
void setEmployeeDepartmentstructure(string Departmentstructure)
{
employeeDepartmentstructureID=Departmentstructure;
}
string getEmployeeID()
{
return employeeID;
}
string getEmployeeName()
{
return employeeName;
}
int getEmployeeAge()
{
return employeeAge;
}
double getEmployeeSalary()
{
return employeeSalary;
}
string getEmployeeDepartmentstructure()
{
return employeeDepartmentstructureID;
}
};
//Main function
int main()
{
//Variables
Employee e[7]; //Employee array with size 7
Departmentstructure d[5]; //Departmentstructure array with size 5
int choice=0;
int employeeAge=0;
int i=0;
int j=0;
string DepartmentstructureID;
string DepartmentstructureName;
string DepartmentstructureHeadName;
string employeeID;
string employeeName;
string employeeDepartmentstructureID;
double employeeSalary=0.0;
ifstream inputFile;
ofstream outputFile;
string dhn;
string Id;
string dn;
string emPd;
double es;
int ea;
string en;
string ed;
do
{
cout<<" 1. Create Departmentstructure 2. Create Employee";
cout<<" 3. Write Departmentstructure data to file 4. Retrieve Departmentstructure data from file";
cout<<" 5. Write EMPLOYEE data to file 6. Retrieve EMPLOYEE data from file";
cout<<" 7. Display Report 8. Exit Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: if(i<3)
{
cout<<" Enter the Departmentstructure ID: ";
cin.ignore(); //Skip the newline character
getline(cin,DepartmentstructureID);
d[i].setDepartmentstructureID(DepartmentstructureID);
cout<<" Enter the Departmentstructure name: ";
getline(cin,DepartmentstructureName);
d[i].setDepartmentstructureName(DepartmentstructureName);
cout<<" Enter the Departmentstructure head name: ";
getline(cin,DepartmentstructureHeadName);
d[i].setDepartmentstructureHeadName(DepartmentstructureHeadName);
i++;
}
else
cout<<" The array is full, you can not add any more Departmentstructures ";
break;
case 2: if(j<7)
{
cout<<" Enter the employee ID: ";
cin.ignore();
getline(cin,employeeID);
e[j].setEmployeeID(employeeID);
cout<<" Enter the employee name: ";
getline(cin,employeeName);
e[j].setEmployeeName(employeeName);
cout<<" Enter Departmentstructure ID: ";
getline(cin,DepartmentstructureID);
e[j].setEmployeeDepartmentstructure(DepartmentstructureID);
cout<<" Enter the employee age: ";
cin>>employeeAge;
e[j].setEmployeeAge(employeeAge);
cout<<" Enter the employee salary: $";
cin>>employeeSalary;
e[j].setEmployeeSalary(employeeSalary);
j++;
}
else
cout<<" The array is full, you can not add any more Employees ";
break;
case 3: cout<<" Writing Departmentstructure data to file ";
{
ofstream outputFile;
outputFile.open("output.txt");
for(int k=0;k<3;k++)
{
outputFile<<" "<<d[k].getDepartmentstructureHeadName();
outputFile<<" "<<d[k].getDepartmentstructureID();
outputFile<<" "<<d[k].getDepartmentstructureName()<<' ';
}
outputFile.close();
break;
}
case 4: cout<<" Reading Departmentstructure data from file ";
{
ifstream inputFile;
inputFile.open("output.txt");
for(int k=0;k<3;k++)
{
cout<<' ';
inputFile>>dn;
cout<<"Departmentstructure Head Name: "<<d[k].getDepartmentstructureHeadName()<<' ';
inputFile>>Id;
cout<<"Departmentstructure ID: "<<d[k].getDepartmentstructureID()<<' ';
inputFile>>dhn;
cout<<"Departmentstructure Name: "<<d[k].getDepartmentstructureName()<<' ';
}
inputFile.close();
break;
}
case 5: cout<<" Writing EMPLOYEE data to file ";
{
ofstream outputFile;
outputFile.open("output.txt");
for(int l=0;l<7;l++)
{
outputFile<<" "<<e[l].getEmployeeID();
outputFile<<" "<<e[l].getEmployeeName();
outputFile<<" "<<e[l].getEmployeeAge();
outputFile<<" "<<e[l].getEmployeeSalary();
outputFile<<" "<<e[l].getEmployeeDepartmentstructure()<<' ';
}
outputFile.close();
break;
}
case 6: cout<<" Reading EMPLOYEE data from file ";
{
ifstream inputFile;
inputFile.open("output.txt");
for(int l=0;l<7;l++)
{
cout<<' ';
inputFile>>emPd;
cout<<"Employee Departmentstructure ID: "<<e[l].getEmployeeDepartmentstructure()<<' ';
inputFile>>es;
cout<<"Employee Salary: $"<<e[l].getEmployeeSalary()<<' ';
inputFile>>ea;
cout<<"Employee Age: "<<e[l].getEmployeeAge()<<' ';
inputFile>>en;
cout<<"Employee Name: "<<e[l].getEmployeeName()<<' ';
inputFile>>ed;
cout<<"Employee ID: "<<e[l].getEmployeeID()<<' ';
}
inputFile.close();
break;
}
case 7: cout<<" Report: ";
{
double totSalary=0;
for(j=0;j<7;j++)
{
totSalary=totSalary+e[j].getEmployeeSalary();
}
cout << " The total salary of all the Departmentstructures employees is: $"<<totSalary<<' ';
break;
}
case 8: cout<<" Exiting: ";
{
char exitChoice;
cout<<"ALERT. Data has not been saved and will be lost.";
cout<<' ';
cout<<" Do you wish to continue (Y/N)? ";
cin>>exitChoice;
if(exitChoice=='Y' || exitChoice=='y')
{
cout<<"Thank You. Good Bye";
cout<<' '<<' ';
exit(0);
break;
}
else if(exitChoice=='N' || exitChoice=='n' )
{}
}
default : cout<<" Select Option: "<<' ';
break;
}
}while(choice!=9);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.