Write a program to allow the user to: 1. Create two classes. Employee and Depart
ID: 3916181 • Letter: W
Question
Write a 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.
Modify :
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.
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 midterm. 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 Midterm where you allowed only limited number of departments and employees.
You may use the following snippet of code which creates a new data file for employtee and Department if one does not already exist. Please note that the names of your data files must be departments.dat and employees,dat
fstream departments("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 deparment file....";
return 0;
}
}
fstream employees("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....";
return 0;
}
}
Example Output:
Explanation / Answer
class Employee
{
int employeeID;
char emploeename[20];
double employeesalary;
int employeeage;
int employeeDepartmentID;
public Employee()
{
}
public Employee(int employeeID,char emploeename[20],double employeesalary,int employeeage int employeeDepartmentID;)
{
this.employeeID=employeeID;
this.emploeename=emploeename;
this.employeesalary=employeesalary;
this.employeeage=employeeage;
this.employeeDepartmentID=employeeDepartmentID;
}
int getemployeeID()
{
return this.employeeID;
}
char [] getemployeename()
{
return this.emploeename;
}
double getemployeesalary()
{
return this.employeesalary;
}
int getemployeeage()
{
return this.employeeage;
}
int getemployeeDepartmentID()
{
return this.employeeDepartmentID
}
void displayemp(int emppno,Department emp[])
{
int i;
for(i=0;i<5;i++)
{
if(emp[i].employeeID==empno)
{
count<< " employeeID:"<<emp[i].employeeID;
count<< " employeeanme:"<<emp[i].employeeanme;
count<< " employeeagedept:"<<emp[i].employeeage;
count<< " employeesalary:"<<emp[i].employeesalary;
count<< " employeeDepartmentID:"<<emp[i].employeeDepartmentID;
}
}
void editemp(int emppno)
{
int i;
cout<<" Enter the Employee Data:"
for(i=0;i<5;i++)
{
if(emp[i].employeeID==empno)
{
cout<<" Employee Name:";
cin>>emp[i].employeeanme;
cout<<" Employee Salary: ";
cin>>emp[i].employeesalary;
cout<<" Employee Age:";
cin>>emp[i].employeeage;
cout<<" Department ID:";
cin>>emp[i].employeeDepartmentID;
}
}
}
void deptSalary(Employee emp[])
{
int i,j;
for( i=0;i<5;i++)
{
double salary =0.0;
for( j=0;j<5;j+)
{
emp[i].employeeDepartmentID=emp[j].employeeDepartmentID;
salary=salary+emp[i].employeesalary;
}
cout<<salary;
}
};
class Department
{
int DepartmentID;
char Departmentname[30],
char DepartmentHeadName[30];
public Department()
{
}
public Department(int DepartmentID,char Departmentname[30],char DepartmentHeadName[30])
{
this.DepartmentID=DepartmentID;
this.Departmentname=Departmentname;
this.DepartmentHeadName=DepartmentHeadName;
}
int getDepartmentID()
{
return this.employeeID;
}
char [] getDepartmentname()
{
return this.emploeename;
}
double getDepartmentHeadName()
{
return this.employeesalary;
}
void displaydept(int depno,Department dept[])
{
int i;
for(i=0;i<3;i++)
{
if(dept[i].DepartmentID==depno)
{
count<< " DepartmentID:"<<dept[i].DepartmentID;
count<< " Departmentname:"<<dept[i].Departmentname;
count<< " DepartmentHeaname:"<<dept[i].DepartmentHeadName;
}
}
}
void editdept(int depno,Department dept[])
{
int i;
for(i=0;i<3;i++)
{
if(dept[i].DepartmentID==depno)
{
cout<<"Dept Name:"
cin>>dept[i].Departmentname;
cout<<"Head of Dept Name: ";
cin>>dept[i].DepartmentHeadName;
}
}
}
};
int main()
{
int ch,deptcount=-1,empcount;
Employee emp[5];
Department dept[3];
do{
cout<<"Human Resources Menu 1. Create Department 2. Create Employee 3. Edit Department 4. Edit Employee 5. Display Salary Report 6. -- Quit -- ";
cout <<" Please make a selection";
cin>>ch;
switch(ch)
{
case 1:
deptcount++;
cout<<" Enter the NEW Department Data:";
cout<<" Dept ID:"
cin>>dept[deptcount].DepartmentID;
cout<<"Dept Name:"
cin>>dept[deptcount].Departmentname;
cout<<"Head of Dept Name: ";
cin>>dept[deptcount].DepartmentHeadName;
break;
case 2:
cout<<" Enter the NEW Employee Data:"
cout<<" Employee ID:";
cin>>emp[empcount].employeeID;
cout<<" Employee Name:";
cin>>emp[empcount].employeeanme;
cout<<" Employee Salary: ";
cin>>emp[empcount].employeesalary;
cout<<" Employee Age:";
cin>>emp[empcount].employeeage;
cout<<" Department ID:";
cin>>emp[empcount].employeeDepartmentID;
break;
case 3:
int depno;
cout<<" Which record to EDIT:"
cout<<" Please choose one of the following... 1 to 2 :" ;
cin>>depno;
displaydept(deptno,dept);
editdept(deptno, dept);
// service.CustomerDetails();
break;
case 4:
int empno;
cout<<" Which record to EDIT:"
cout<<" Please choose one of the following... 1 to 3:" ;
cin>>emppno;
displayemp(empno,emp);
editdemp(empno,emp);
break;
case 5:
deptSalary();
break;
case 6:
exit(0);
break;
}
}while(ch!=0);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.