Employee database project
This projects maintains Employee details and has following options:
-
Add employee record
-
Display all employee records
-
Search an employee record by employee no
-
Search an employee record by name
-
Delete an employee record by employee no
-
Delete an employee record by employee name
-
Modify an employee record by employee no
-
Modify an employee record by employee name
-
Random display of employee record
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<graphics.h>
#include<iomanip.h>
void wel()
{
int gd=DETECT,gm;
int i;
initgraph(&gd,&gm,”c:\\turboc3″);
cleardevice();
setcolor(4);
settextstyle(1,0,8);
outtextxy(150,50,”Welcome”);
setcolor(5);
settextstyle(1,0,5);
outtextxy(180,180,”To”);
setcolor(2);
settextstyle(1,0,9);
outtextxy(100,280,”Project On”);
getch();
cleardevice();
setcolor(4);
settextstyle(1,0,8);
outtextxy(150,1,”Employee”);
setcolor(4);
settextstyle(1,0,8);
outtextxy(100,100,”Management”);
setcolor(4);
settextstyle(1,0,8);
outtextxy(120,200,”System”);
getch();
cleardevice();
setcolor(14);
settextstyle(1,0,4);
outtextxy(100,240,”Dev. BY”);
setcolor(14);
settextstyle(1,0,6);
outtextxy(100,270,”Abc”);
settextstyle(1,0,4);
setcolor(9);
outtextxy(100,330,”and”);
settextstyle(1,0,8);
setcolor(14);
outtextxy(100,360,”Xyz”);
getch();
closegraph();
}
void thanks()
{
int gd=DETECT,gm;
int i;
initgraph(&gd,&gm,”c:\\turboc3″);
cleardevice();
setcolor(4);
settextstyle(1,0,9);
outtextxy(10,10,”Thank You”);
getch();
closegraph();
}
class emp
{
private:
int empno;
char name[20];
float sal;
public:
void read();
void show();
void add();
void disp_all();
void menu();
void search_empno();
void search_empname();
void del_empno();
void del_empname();
void modi_empno();
void rand_disp();
void heading();
void line1();
int check_empno(int e);
int check_sal(float s);
};
int emp::check_empno(int e)
{
if(e<=0)
return(0);
else
return(1);
}
int emp::check_sal(float s)
{
if(s<=0)
return(0);
else
return(1);
}
void emp::read()
{
int z=0;
while(1)
{
cout<<“Enter empno “;
cin>>empno;
z=check_empno(empno);
if(z==0)
cout<<“invalid empno, enter again”<<endl;
else
break;
}
cout<<“Enter name “;
cin>>name;
while(1)
{
cout<<“Enter sal “;
cin>>sal;
z=check_sal(sal);
if(z==0)
cout<<“Invalid salary , enter again”<<endl;
else
break;
}
}
void emp::show()
{
cout<<setw(10)<<empno<<setw(20)<<name<<setw(20)<<sal<<endl;
}
void emp::add()
{
fstream abc;
emp e;
abc.open(“emp1”,ios::in);
if(abc.fail())
{
cout<<“file does not exist”<<endl;
abc.open(“emp1”,ios::out);
}
else
{
cout<<“File is present”<<endl;
abc.close();
abc.open(“emp1”,ios::app);
}
e.read();
abc.write((char*)&e,sizeof(e));
abc.close();
}
//line1
void emp::line1()
{
int i;
for(i=1;i<=60;i++)
{
cout<<“-“;
}
cout<<endl;
}
//heading
void emp::heading()
{
line1();
cout<<” E m p l o y e e M a n a g e m e n t S y s t e m”<<endl;
cout<<” =================================================”<<endl;
line1();
cout<<setw(10)<<“EMP NO”<<setw(20)<<“NAME”<<setw(20)<<“SALARY”<<endl;
line1();
}
void emp::disp_all()
{
fstream abc;
emp e;
abc.open(“emp1”,ios::in);
if(abc.fail())
{
cout<<“unable to open the file”<<endl;
return;
}
heading();
abc.read((char*)&e,sizeof(e));
while(!abc.eof())
{
e.show();
abc.read((char*)&e,sizeof(e));
}
line1();
abc.close();
}
void emp::search_empno()
{
fstream abc;
emp e;
int te,z=0;
cout<<“Enter empno to search “;
cin>>te;
abc.open(“emp1”,ios::in);
if(abc.fail())
{
cout<<“unable to open the file”<<endl;
return;
}
abc.read((char*)&e,sizeof(e));
heading();
while(!abc.eof())
{
if(te==e.empno)
{
z=1;
e.show();
//break;
}
abc.read((char*)&e,sizeof(e));
}
line1();
abc.close();
if(z==0)
cout<<“rec is not present”<<endl;
}
void emp::search_empname()
{
fstream abc;
emp e;
int z=0;
char tn[20];
cout<<“Enter empname to search “;
cin>>tn;
abc.open(“emp1”,ios::in);
if(abc.fail())
{
cout<<“unable to open the file”<<endl;
return;
}
abc.read((char*)&e,sizeof(e));
heading();
while(!abc.eof())
{
if(strcmpi(tn,e.name)==0)
{
z=1;
e.show();
}
abc.read((char*)&e,sizeof(e));
}
line1();
abc.close();
if(z==0)
cout<<“rec is not present”<<endl;
}
void emp::del_empno()
{
fstream abc,pqr;
emp e;
int te,z=0;
cout<<“Enter empno to delete “;
cin>>te;
abc.open(“emp1”,ios::in);
pqr.open(“temp”,ios::out);
if(abc.fail())
{
cout<<“unable to open the file”<<endl;
return;
}
abc.read((char*)&e,sizeof(e));
heading();
while(!abc.eof())
{
if(te==e.empno)
{
z=1;
e.show();
}
else
pqr.write((char*)&e,sizeof(e));
abc.read((char*)&e,sizeof(e));
}
line1();
abc.close();
if(z==0)
cout<<“rec is not present”<<endl;
else
{
remove(“emp1”);
rename(“temp”,”emp1″);
}
}
void emp::del_empname()
{
fstream abc,pqr;
emp e;
int z=0;
char tn[20];
cout<<“Enter empname to delete “;
cin>>tn;
abc.open(“emp1”,ios::in);
pqr.open(“temp”,ios::out);
if(abc.fail())
{
cout<<“unable to open the file”<<endl;
return;
}
abc.read((char*)&e,sizeof(e));
heading();
while(!abc.eof())
{
if(strcmpi(tn,e.name)==0)
{
z=1;
e.show();
}
else
pqr.write((char*)&e,sizeof(e));
abc.read((char*)&e,sizeof(e));
}
line1();
abc.close();
if(z==0)
cout<<“rec is not present”<<endl;
else
{
remove(“emp1”);
rename(“temp”,”emp1″);
}
}
void emp::modi_empno()
{
fstream abc,pqr;
emp e;
int te,z=0;
cout<<“Enter empno to modify “;
cin>>te;
abc.open(“emp1”,ios::in);
pqr.open(“temp”,ios::out);
if(abc.fail())
{
cout<<“unable to open the file”<<endl;
return;
}
abc.read((char*)&e,sizeof(e));
heading();
while(!abc.eof())
{
if(te==e.empno)
{
z=1;
cout<<“Old details”<<endl;
e.show();
cout<<“Enter new details “<<endl;
e.read();
}
pqr.write((char*)&e,sizeof(e));
abc.read((char*)&e,sizeof(e));
}
line1();
abc.close();
if(z==0)
cout<<“rec is not present”<<endl;
else
{
remove(“emp1”);
rename(“temp”,”emp1″);
}
}
//******
void emp::menu()
{
cout<<“Main Menu”<<endl;
cout<<“1. Add record”<<endl;
cout<<“2. Display all records “<<endl;
cout<<“3. Search by empno”<<endl;
cout<<“4. Search by name”<<endl;
cout<<“5. Del by empno”<<endl;
cout<<“6. Del by name “<<endl;
cout<<“7. Modify by empno”<<endl;
cout<<“8. Random display of record”<<endl;
cout<<“0. Exit”<<endl;
cout<<“Enter your choice “;
}
void emp::rand_disp()
{
fstream abc;
emp e;
long reclen=0,totrec=0,filelen=0;
int n;
abc.open(“emp1”,ios::in);
abc.seekg(0,ios::end);
reclen=sizeof(e);
filelen=abc.tellp();
totrec=filelen/reclen;
cout<<“size is “<<sizeof(e)<<endl;
cout<<“total file length is “<<filelen<<endl;
cout<<“total records are “<<totrec<<endl;
cout<<“Enter the rec number to display “;
cin>>n;
if (n<=0 || n>totrec)
{
cout<<“Invalid record number “;
getch();
}
else
{
abc.seekg(0,ios::beg);
abc.seekg((n-1)*sizeof(e),ios::beg);
abc.read((char*)&e,sizeof(e));
cout<<“empo “<<e.empno<<endl;
cout<<“name ” <<e.name<<endl;
cout<<“sal “<<e.sal<<endl;
}
}
void main()
{
int op;
emp e1;
wel();
while(op!=0)
{
clrscr();
e1.menu();
cin>>op;
clrscr();
switch(op)
{
case 1:
e1.add();
break;
case 2:
e1.disp_all();
break;
case 3:
e1.search_empno();
break;
case 4:
e1.search_empname();
break;
case 5:
e1.del_empno();
break;
case 6:
e1.del_empname();
break;
case 7:
e1.modi_empno();
break;
case 8:
e1.rand_disp();
break;
case 0:
//thanks();
//
getch();
exit(0);
default:
cout<<“Invalid choice”<<endl;
break;
}//switch
getch();
}//while
}//main