Академический Документы
Профессиональный Документы
Культура Документы
Learning Objectives
The designing of the Employee Attendance project enable the students to: Create a simple C++ application using the concept of Object-oriented programming Use pointers extensively Explore the use of date and time functions Perform validation to execute the program effectively Create, store, update and retrieve data in files
attend_mark (): Receives the absentee employee number, validates the employee number and passes the values to the WriteAttendance () function. It also identifies the employees, who are present in the organization for a particular day. WriteAttendance (): Writes the data received from attend_mark() function into the Attendance.dat file.
The employee class receives the employee information from the user. It also takes care of validating the user entries such as name and address validation (accepting white space) and phone number validation. Functions get_InputName() getEmpInfo() Description Receives the name from the user by accepting the white space. Receives complete information of the employees by making necessary validations.
"
emp->empno = no; emp->getEmpInfo(); storeemp.putemp(emp); } int main() { int opt; char ch; do { system("cls"); cout<<"\n\n\n\n\n\n"; cout<<" cout<<"
EMPLOYEE INFORMATION ------------------- 1\n\n\n"; EMPLOYEE LEAVE FORM ------------------- 2\n\n\n"; EMPLOYEE ATTENDANCE ------------------- 3\n\n\n"; CLOSE MONTHLY ATTENDANCE ------------------- 4\n\n\n"; EMPLOYEE LEAVE STATUS ------------------- 5\n\n\n"; EXIT ------------------- 6\n\n\n"; ENTER THE OPTION (1-6): ";
switch(opt) { case 1: GetUserData(); break; case 2: { Leave *leave; leave = new Leave(); leave->getleave_Info(); // Updating the leave information in a file EmployeeLeaveUpdation *up1; up1 = new EmployeeLeaveUpdation(); up1->UpdateEmployeeData(leave); ShowLeaveData *show; show = new ShowLeaveData(); show->StoreUpdatedData(up1); break; } case 3: { Attendance *attend; attend = new Attendance(); attend->attend_mark(); break; } case 4:{ close_Attend *ca; ca = new close_Attend(); ca->display_Attend(); break; } case 5: { ShowLeaveData *show; show = new ShowLeaveData(); show->ShowData(); break; } case 6: goto a; default:{ cout<<"\n\t\t\tINVALID ENTRY (ENTER BETWEEN 1 AND 6)"; break; } }
cout<<"\n\n\n"; cout<<" RETURN TO MAIN MENU? (Y/N) "; cin>> ch; }while (ch=='Y' || ch == 'y'); a: if (ch!='y' || ch!='Y') { cout<<"\n\n\n"; cout<<" } getch(); return 0; } #endif
THANK YOU!!!";
EmpAttendance.h
#include<iostream> #include<istream> #include<string> #include<conio.h> #include<time.h> #include <cctype> #include <algorithm> #include <time.h> #include<stdlib.h> using namespace std; vector <int> v_emp;
date_Validation(char *from, char *to) { strcpy(strdate1,from); strcpy(strdate2,to); } int dateInput() { int flag=0; date1=DateExtract(strdate1); month1=MonthExtract(strdate1); year1=YearExtract(strdate1); date2=DateExtract(strdate2); month2=MonthExtract(strdate2); year2=YearExtract(strdate2); if(month1 > 12 || month2 >12 || date1 >31 || date2 > 31) { flag=1; }
if (year1 == year2) { if (month1 == month2) { days=date2-date1+1; } else { temp1=month1; temp2=month2; if(month1<=month2) { if(month1 == 02) { if (year1%4 == 0) datecount1 = 29 - date1 +1; else datecount1 = 28 -date1 +1; } else { count1 = calculatedays(temp1); count2= calculatedays(temp2); datecount1=count1-date1+1; } datecount2 = date2; days = datecount1 + datecount2+1; days<<endl; } else//month1<month2 { cout<<"Enter valid date\n\n"; flag=1; } } } else { cout<<"\n\nEnter the date of the same calender year" <<"\n\n"; flag=1; } if (flag==1) return -1;
int DateExtract(string stdate) { int l=0; int iDate; string strDate=stdate; strDate=strDate.substr(3,2); char aDate[2]; while(strDate[l]!='\0') { aDate[l] = strDate[l]; l++; } iDate = atoi(aDate); return iDate; } int MonthExtract(string stdate) { int l=0; int iMonth; string strMonth=stdate; strMonth=strMonth.substr(0,2); char aMonth[2]; while(strMonth[l]!='\0') { aMonth[l] = strMonth[l]; l++; } iMonth = atoi(aMonth); return iMonth; } int YearExtract(string stdate) {
int l=0; int iYear; string strYear=stdate; strYear=strYear.substr(6,4); char aYear[2]; while(strYear[l]!='\0') { aYear[l] = strYear[l]; l++; } iYear = atoi(aYear); return iYear; } int calculatedays(int months) { int count; switch(months) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { count = 31; cout<<endl<<"I am in function. Days in month is " << count; break; } case 4: case 6: case 9: case 11: { count = 30; break; } }//switch return count; }// calculate days };
// Retrieves the name from the user with white spaces void get_InputName() { char *emp_name; emp_name=new char[100]; cin.ignore(); cin.get(emp_name,100,'\n'); } //Prompts the user to enter the employee information void getEmpInfo() { CL=10; ML=20; EL=20; cout<<"\n\n"; cout<<"\t EMPLOYEE NAME : "; get_InputName(); cout<<"\n\n"; cout<<" \t AGE (21 to 58) : "; bb: cin>>age; int age_len = strlen(age); //Checking for valid age entry. //Age should be between 21 and 58. //Age should not involve and alphabets. if(age_len<=2) { int k=0; for(k=0;k<2;k++) { if((age[k] >='a' && age[k]<='z') ||(age[k] >='A' && age[k]<='Z')) { cout<<"ENTER VALID AGE: "; goto bb; } } int age_int = atoi(age); if(age_int <21 || age_int >58) { cout<<"\nENTER VALID AGE (21 to 58): "; goto bb; } } else {
cout <<"\nENTER VALID AGE (21 to 58)"; goto bb; } cout<<"\n\n"; cout<<" cc: cin>>sex; transform (sex.begin(), sex.end(), sex.begin(), toupper); if((sex.compare("FEMALE")!=0)&& (sex.compare("MALE")!=0)) { ch.check_All("GENDER: "); goto cc; } cout<<"\n\n"; cout<<" ADDRESS : "; cin.ignore(); cin.get(addr,100); cout<<"\n\n"; cout<<" PHONE NUMBER :\n "; cout<<" CODE : "; cin>>phno1; int i=0,flag=0; //Checking for valid phone number entry while(phno1[i]!='\0') { if (!(phno1[i] >='0') || !(phno1[i] <='9')) { flag=2; } i++; } if(flag==2) { cout<<"\nENTER VALID CODE: "; goto dd; } cout<<"\n"; cout<<" NUMBER : "; cin>>phno2; i=0; flag=0; while(phno2[i]!='\0') { if (!(phno2[i] >='0') || !(phno2[i] <='9')) { flag=3; } i++; } if(flag==3) GENDER (MALE/FEMALE) : ";
dd:
kk:
{ cout<<"ENTER VALID PHONE NO: "; goto kk; } cout<<"\n\n"; cout<<" cin>>salary; if(salary<=0) { cout<<" goto mm; } cout<<"\n\n"; cout<<" cout<<" cout<<" cout<<" } }; Employee *emp1 = new Employee; SALARY : Rs.";
mm:
LEAVE ELIGIBLE :\n\n"; CASUAL LEAVE (CL) : "<<CL<<"\n\n"; MEDICAL LEAVE (ML) : "<<ML<<"\n\n"; EARNED LEAVE (EL) : "<<EL<<"\n\n";
if (len==0) { eno+=1; } // If not, then retrieve the last employee number and increment it by 1 and insert it for the new //employee. else { int c; in.seekg(0,ios::beg); while (in) { in.read((char*) emp1, sizeof(Employee)); c =emp1->empno; } eno=c + 1; in.close(); } return eno; } // Storing the employee data void putemp(Employee *emp) { emp1=emp; ofstream out; out.open("EmployeeInformation.dat",ios::app); out.write ((char*) emp1, sizeof(Employee)); out.close(); } };
int getEno() { return eno; } string getLmode() { return lmode; } //Receiving leave information void getleave_Info() { system("cls"); Employee *emp; emp= new Employee; cout<<"\n\n\n"; cout<<" EMPLOYEE LEAVE INFORMATION\n"; cout<<" ***************************\n\n\n"; _strdate(cdate); cout<<" Date: "<<cdate; cout<<"\n\n"; cout<<"EMPLOYEE NUMBER: "; cin>>eno; int ret = CheckEmployeeNumber(eno); if (ret==1) goto a2; else cout<<"\nENTER VALID EMPLOYEE NUMBER : "; goto a1; cout<<"\n\n\n"; cout<<" LEAVE PARTICULARS :"; cout<<"\n\n"; cout<<" FROM DATE (MM/DD/YYYY) : "; cin>> from_date; int fdate=atoi(from_date); cout<<"\n\n"; cout<<" TO DATE (MM/DD/YYYY) : "; cin>> to_date; date_Validation dat(from_date, to_date); days=dat.dateInput(); if (days==-1 ) goto entry; cout<<"\n\n"; cout<<" REASON : "; cin.ignore(); cin.get(reason,100); cout<<"\n\n"; cout<<" cout<<"\n\n\n"; NUMBER OF DAYS : "<<days;
a1:
a2:
entry:
leave_validate(); } // This method is used to check whether mode of leave is CL or ML or EL. void leave_validate() { cout<<" aa: cin>>lmode; MODE OF LEAVE(CL/ML/EL) : ";
transform (lmode.begin(), lmode.end(), lmode.begin(), toupper); int a=lmode.length(); if((a>2) || ((lmode.compare("CL")!=0)&&(lmode.compare("ML")! =0)&&(lmode.compare("EL")!=0))) { check_All(" goto aa; } } void check_All(string p) { system("cls"); cout<<"\n\n\n\n"; cout<<" ENTER VALID DATA FOR "<<p; } MODE OF LEAVE(CL/ML/EL) : ");
};
vector <int> vemp,vcl,vml,vel; string mode = lv->getLmode(); int days = lv->days; int eno = lv->getEno(); NoOfDays = lv->days; Employee *emp1 = new Employee(); ifstream in; in.open("EmployeeInformation.dat",ios::in); int flag =0; in.seekg(0,ios::beg); while (in) { in.read((char*) emp1, sizeof(Employee)); vemp.push_back(emp1->empno); vcl.push_back(emp1->getCL()); vml.push_back(emp1->getML()); vel.push_back(emp1->getEL()); } for(int i=0;i<vemp.size();i++) { if(eno == vemp[i]) { EmpNo = eno; CL = vcl[i]; ML = vml[i]; EL = vel[i]; if(mode == "CL") { flag=1; cl=vcl[i]-days; CL=cl; } if(mode=="ML") { flag=2; ml=vml[i]-days; ML=ml; } if(mode=="EL") { flag=3; el=vel[i]-days; EL=el; } TotalLeave = CL+ML+EL;
} } in.clear(); } };
aa1:
cin>>eno; cout<<"\n\n"; if(eno!= 0) { int ret = CheckEmployeeNumber(eno); if (ret==1) goto aa2; else { cout<<"\nENTER VALID EMPLOYEE NUMBER : "; goto aa1; } n++; v1.push_back(eno); PA='A'; //Inserting absentee data WriteAttendance(eno,PA); } else goto ll; }
aa2:
ll:
in.close(); Employee *emp1; emp1 = new Employee; //Inserting P for other employees in.open("EmployeeInformation.dat",ios::in); in.seekg(0,ios::beg); while(in) { in.read((char *) emp1, sizeof(Employee)); v2.push_back(emp1->empno); } cout<<"\n\n"; v3.clear(); // Identifying the employees for whom present needs to be marked for(j=0; j<v1.size() ;j++) { for(int s=0;s<v2.size()-1;s++) { if(v2[s] != v1[j]) { // The vector v3 contains the employee (with duplicate values)
v3.push_back(v2[s]); } } } //Removing duplication from vector v3 int half = v3.size()/2; for(int s=half;s<v3.size();s++) { v4.push_back(v3[s]); } if(v1.size()==1) { for( j=0;j<v2.size()-1;j++) { if( v1[0] != v2[j]) WriteAttendance(v2[j],'P'); } } else { for(j=0; j<half ;j++) { for(s=0;s<v4.size();s++) { char P_A; if(v3[j] == v4[s]) { P_A='P'; WriteAttendance(v3[j],P_A); } }//for s loop }//for j loop } in.close(); } void WriteAttendance(int eno,char cc) { Attendance *att; att = new Attendance(); att->empno = eno; att->PA = cc; _strdate(att->date); ofstream out; out.open ("Attendance.dat",ios::app); out.write((char *) att, sizeof(Attendance)); out.close(); delete att; } };
for(int i=1;i<=31;i++) // Displaying the no of the days in a month { cout<<i<<" "; } cout<<"\n\n"; int l=3; int p=1; string str1,str; vector <int> vempno ; //Retrieving the Employee numbers in a vector vempno
ifstream in1; in1.open("EmployeeInformation.dat", ios::in); Employee *e = new Employee; while(in1) { in1.read((char*) e, sizeof(Employee)); vempno.push_back(e->empno); } vector <int> vatt; vector <char> vpa; vector <string> vdate; int eno; char PA; string strdt; for(int u=0;u<vempno.size()-1;u++) { in.seekg(0,ios::beg); while(in) { in.read((char*) att, sizeof(Attendance)); eno = att->empno; PA=att->PA; strdt=att->date; if(vempno[u] == eno) { vatt.push_back(eno); vpa.push_back(PA); vdate.push_back(strdt); } } in.clear(); } cout<<endl; for(u=0;u<vempno.size()-1;u++) { cout<<vempno[u]; int first=0; for (int h=0;h<vdate.size();h++) { if(vempno[u] == vatt[h]) { int retDate = DateExtract(vdate[h]); if(retDate<=9) { for(i=1;i<retDate;i++) { cout<<" "; } cout<<vpa[h]<<" "; }
else { first+=1; if(first==1) { retDate = retDate-7.5; retDate = (retDate*2)+2.5; for(i=1;i<=retDate;i++) { cout<<" "; } cout<<vpa[h]<<" "; } else { cout<<vpa[h]<<""; } } } } cout<<"\n\n"; } getch(); } int DateExtract(string strdate) { int iDate; int l=0; string strDate= strdate; strDate = strDate.substr(3,2); char aDate[2]; while (strDate[l]!='\0') { aDate[l] = strDate[l]; l++; } iDate = atoi(aDate); return iDate; } };
{ public: void StoreUpdatedData(EmployeeLeaveUpdation *up) { ofstream out; out.open("LeaveUpdation.dat",ios::app); out.write((char*) up,sizeof(EmployeeLeaveUpdation)); } void ShowData() { system("cls"); char ch='y'; int retrieve; do { system("cls"); cout<<"\n\n\n\n"; cout<<" EMPLOYEE LEAVE STATUS\n"; cout<<" **********************\n\n\n"; cout<<" 1. RETRIEVE DATA FOR A PARTICULAR EMPLOYEE \n\n\n"; cout<<" 2. RETRIEVE DATA FOR ALL EMPLOYEES \n\n\n"<<endl; cout<<" ENTER 1 OR 2 : "; cin>>retrieve; switch(retrieve) { case 1: SingleEmployee();break; case 2: AllEmployees();break; default : cout<<"\n\nENTER EITHER 1 OR 2 "; break; } cout<<"\n\n\n"; cout<<" ENTER 'Y' TO ACCESS EMPLOYEE LEAVE STATUS AND 'N' TO QUIT: "; cin>>ch; }while (ch=='Y' ||ch=='y'); } //ShowData(); void SingleEmployee() { int no; cout<<"\n\n\n\n"; cout<<" ENTER THE EMPLOYEE NUMBER : "; cin>>no; int ret = CheckEmployeeNumber(no); if (ret==1) goto a12; else { cout<<"\nENTER VALID EMPLOYEE NUMBER : "; goto a11;
a11:
} a12: cout<<endl<<endl; EmployeeLeaveUpdation *up; up = new EmployeeLeaveUpdation; ifstream in; in.open("LeaveUpdation.dat",ios::in); in.seekg(0,ios::beg); int cnt =0; while(in) { in.read((char*) up, sizeof(EmployeeLeaveUpdation)); if(no == up->EmpNo) { cnt+=1; break; } else { goto ab; } }//while // Displaying the latest leave record of the employee cout<<"\n\n\n\n\n"; cout<<"EMPLOYEE NUMBER : " << up->EmpNo <<"\n\n\n"; cout<<"TOTAL LEAVE ELIGIBLE : 50"<<"\n\n\n"; cout<<"TOTAL LEAVE AVAILABLE : "<<up->TotalLeave<<"\n\n\n"; cout<<"CASUAL LEAVE : "<<up->CL<<"\n\n\n"; cout<<"MEDICAL LEAVE : "<<up->ML<<"\n\n\n"; cout<<"EARN LEAVE : "<<up->EL<<"\n\n\n"; cout<<"\n\n\n\n"; goto bb; in.clear(); cout<<"\n EMPLOYEE HAS NOT TAKEN LEAVE SO FAR!\n\n\n"; cout<<"Press Enter to continue....\n"; getch(); } void AllEmployees() { system("cls"); EmployeeLeaveUpdation *up; up = new EmployeeLeaveUpdation; ifstream in; in.open("LeaveUpdation.dat",ios::in); in.seekg(0,ios::beg); while(in) {
ab: bb:
in.read((char*) up, sizeof(EmployeeLeaveUpdation)); cout<<"\n\n\n\n\n"; cout<<"EMPLOYEE NUMBER : " << up->EmpNo <<"\n\n\n"; cout<<"TOTAL LEAVE ELIGIBLE : 50"<<"\n\n\n"; cout<<"TOTAL LEAVE AVAILABLE : "<<up->TotalLeave<<"\n\n\n"; cout<<"CASUAL LEAVE : "<<up->CL<<"\n\n\n"; cout<<"MEDICAL LEAVE : "<<up->ML<<"\n\n\n"; cout<<"EARN LEAVE : "<<up->EL<<"\n\n\n"; cout<<"\n\n\n\n"; getch(); cout<<"Press Enter to continue....."; } in.clear(); getch(); }//AllEmployees(); }; // Validating the employee number entered by the user. int CheckEmployeeNumber(int eno) { Employee *emp; emp=new Employee(); int cnt=0; ifstream in; in.open("EmployeeInformation.dat", ios::in); while (in) { in.read((char*) emp, sizeof(Employee)); if(eno == emp->empno) { cnt=1; break; } else { cnt=2; continue; } } if(cnt==1) return 1; else return 0; }
Exercises:
1. Write the C++ code to display the close yearly attendance of the Employee Attendance system. 2. Write the C++ code to view the employee leave status based on date / month.