In: Computer Science
Write the code in C++.
Write a program to implement Employee Directory. The main
purpose of the class is to get the data, store it
into a file, perform some operations and display data.
For the purpose mentioned above, you should write a template class
Directory for storing the data
empID(template),
empFirstName(string),
empLastName(string),
empContactNumber(string)
and
empAddress(string) of each Employee in a file
EmployeeDirectory.txt.
1. Write a function Add to write the above mentioned contact
details of the employee into
EmployeeDirectory.txt. The record of each employee should be
written in one line having tab '\t' character
among the attributes. Consider the file below as example for
EmployeeDirectory.txt
2. Write a template function SearchByID - SearchByID receives
empID of type template and 4 references
(firstname, lastname, contactno, address) of type string. The
return type of the function should be bool,
which should return either true or false. If the record is found,
return true and copy the details of the
record into the passed references for the caller to use.
3. Also write a void function printDetails which must read the file
completely and print the record stored
in EmployeeDirectory.txt.
#include <bits/stdc++.h>
using namespace std;
template<class empID, class empFirstName, class empLastName,
class empContactNumber, class empAddress>
class Add
{
empID id;
empFirstName fname;
empLastName lname;
empContactNumber contact;
empAddress eaddress;
string empdata;
public:
Add(empID x, empFirstName y, empLastName z,empContactNumber
a,empAddress b)
{
id=x;
fname=y;
lname=z;
contact=a;
eaddress=b;
empdata= x+ '\t'+ y+'\t'+z+'\t'+a+'\t'+b;
}
void AddData()
{
ofstream fileOUT("EmployeeDirectory.txt", ios::app);
fileOUT<<empdata<<endl;
fileOUT.close();
}
void printDetails()
{
char s[1000000];
ifstream ifile;
ifile.open("EmployeeDirectory.txt");
if(!ifile)
{
cout<<"Error in opening
file..!!";
exit(0);
}
while(ifile.eof()==0)
{
ifile>>s;
cout<<s<<" ";
}
cout<<endl;
ifile.close();
}
};
int main()
{
string name="EmployeeDirectory.txt";
ifstream file(name);
if(!file)
{
fstream file;
file.open("EmployeeDirectory.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
cout<<"File created successfully.";
file.close();
}
string empid, empfname,emplname,empadd,empcon;
cout << "Enter a employee ID : ";
getline (cin, empid);
cout << "Enter a employee First Name : ";
getline (cin, empfname);
cout << "Enter a employee Last Name : ";
getline (cin, emplname);
cout << "Enter a employee address : ";
getline (cin, empadd);
cout << "Enter a employee Contact Number : ";
getline (cin, empcon);
Add <string, string, string,string,string> EmployeeDirectory1
(empid, empfname,emplname,empcon,empadd);
EmployeeDirectory1.AddData();
EmployeeDirectory1.printDetails();
return 0;
}