This program is in C++:
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.
Here is the test cases:
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.
Here is the solution to above problem in C++. PLEASE GIVE A THUMBS UP!!!
Read the code comments for more information
C++ CODE
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Employee
{
//data members
private:
int empId;
string name;
double salary;
int age;
int deptId;
public:
Employee(int empId,string
name,double salary,int age,int deptId)
{
this->empId=empId;
this->name=name;
this->salary=salary;
this->age=age;
this->deptId=deptId;
}
//constructor for class
Employee()
{
}
//accessor method for the class
//setter and getter
int getEmpId()
{
return empId;
}
string getName()
{
return name;
}
double getSalary()
{
return salary;
}
int getAge()
{
return age;
}
int getDeptId()
{
return deptId;
}
void setEmpId(int id)
{
empId=id;
}
void setName(string name)
{
this->name=name;
}
void setSalary(double sal)
{
salary=sal;
}
void setAge(int a)
{
age=a;
}
void setDeptId(int dept)
{
deptId = dept;
}
};
class Department
{
private:
int deptId;
string name;
string headName;
public:
//constructor
Department()
{
}
//parameterized constructor
Department(int deptId, string
name,string headName)
{
this->deptId=
deptId;
this->name=name;
this->headName=headName;
}
//accerssor methods for the
class
//setter and getter
int
getDeptId()
{
return deptId;
}
void setDeptId(int dept)
{
deptId = dept;
}
void setName(string name)
{
this->name=name;
}
string getName()
{
return name;
}
void setHeadName(string name)
{
this->headName=name;
}
string getHeadName()
{
return headName;
}
};
int main()
{
//employee array with size 5
Employee E[5];
//department array wiht size 3
Department D[3];
int choice=0;
int indexD=0;
int indexE=0;
//display the menu to the user
while(choice!=6)
{
cout<<"1. Create
Department\n";
cout<<"2. Create
Employee\n";
cout<<"3. Write Out Data
File\n";
cout<<"4. Read In Data
File\n";
cout<<"5. Display Salary
Report\n";
cout<<"6. Quit\n";
cout<<"Please make a
selection: ";
cin>>choice;
switch(choice)
{
case 1:
{
int id;
bool flag=false;
//if array is full
if(indexD==3)
{
cout<<"The array is
full you cannot add more entry\n";
break;
}
string name;
string headName;
cout<<"Enter the Department ID: ";
cin>>id;
//check if the department
already exists
for(int i=0;i<indexD;++i)
{
if(id ==
D[i].getDeptId())
flag=true;
}
//if dept does exists
if(flag==true)
{
cout<<"Value must be
Unique!\n";
break;
}
cout<<"Enter the Department Name: ";
cin>>name;
cout<<"Enter the Department Head Name:
";
cin>>headName;
//if dept does not
exists
if(flag==false)
{
Department
temp(id,name,headName);
D[indexD++]=temp;
}
}
break;
case 2:
{
int id;
string name;
double salary ;
int age;
int deptId;
bool flag=false;
//if array is full
if(indexE==5)
{
cout<<"The array is
full, you cannot add more entry\n";
break;
}
cout<<"Enter the Employee ID: ";
cin>>id;
//check if the employee
already exists
for(int i=0;i<indexE;++i)
{
if(id ==
E[i].getEmpId())
flag=true;
}
//if emp does exists
if(flag==true)
{
cout<<"Value must be
Unique!\n";
break;
}
cout<<"Enter the Employee Name: ";
cin>>name;
cout<<"Enter the Employee age: ";
cin>>age;
cout<<"Enter the Employee Salary:
$";
cin>>salary;
cout<<"Enter the Employee Department Id:
";
cin>>deptId;
//if dept does not
exists
if(flag==false)
{
bool flagForDept=false;
//check if dept exists
for(int
i=0;i<indexD;++i)
{
if(deptId==D[i].getDeptId())
flagForDept=true;
}
if(flagForDept==true)
{
Employee
temp(id,name,salary,age,deptId);
E[indexE++]=temp;
}
else
{
cout<<"Please enter a
valid department Id: ";
cin>>deptId;
Employee
temp(id,name,salary,age,deptId);
E[indexE++]=temp;
}
}
}
break;
case 3:
{
ofstream
foutE("employee.txt");
ofstream
foutD("department.txt");
//wriete employee data
for(int
i=0;i<indexE;++i)
{
foutE<<E[i].getEmpId()<<endl;
foutE<<E[i].getName()<<endl;
foutE<<E[i].getSalary()<<endl;
foutE<<E[i].getAge()<<endl;
foutE<<E[i].getDeptId()<<endl;
}
foutE.close();
//write department data
for(int
i=0;i<indexD;++i)
{
foutD<<D[i].getDeptId()<<endl;
foutD<<D[i].getName()<<endl;
foutD<<D[i].getHeadName()<<endl;
}
foutD.close();
}
break;
case 4:
{
ifstream
fine("employee.txt");
ifstream
find("department.txt");
int ie=0;
int ide=0;
int id;
string name;
double salary ;
string headName;
int age;
int deptId;
while(fine>>id)
{
fine>>name;
fine>>salary;
fine>>age;
fine>>deptId;
Employee
temp(id,name,salary,age,deptId);
E[ie++]=temp;
}
fine.close();
while(find>>id)
{
find>>name;
find>>headName;
Department
temp(id,name,headName);
D[ide++]=temp;
}
find.close();
indexE=ie;
indexD=ide;
}
break;
case 5:
{
cout<<"Salary Report by
Department\n";
for(int i=0;i<indexD;++i)
{
cout<<"Dept:
"<<D[i].getName()<<endl;
double sum=0;
for(int
j=0;j<indexE;++j)
{
if(E[j].getDeptId()==D[i].getDeptId())
sum+=E[j].getSalary();
}
cout<<"Total Salary:
$"<<sum<<endl;
}
}
break;
case 6:
{
cout<<"Thank you, goodbye\n";
}
break;
default:
{
cout<<"Please enter a valid choice(1-6):\n";
}
}
}
return 0;
}
SCREENSHOT OF OUTPUT
Get Answers For Free
Most questions answered within 1 hours.