In C++. This Program does not use Files.. Write a structure that maps the inform
ID: 3720987 • Letter: I
Question
In C++. This Program does not use Files.. Write a structure that maps the information of an employee. This information is
SSN, firstName, lastName, Salary, deparment_no
Where deparment_no ia the department number the employee works at.
Write a structure that maps the information of a department. This information is
dept_no, dept_name,
The size of the dept is based on the number of employees it contains.
The program should behave the following
- First time: Program displays a menu
1- Create Department
2- Exit
- Afterwards:
1- Add employee
2- Create Department
3- Display department information
4- Display all Employee information
§ In a specific dept
§ In all dept
5- Search for employee by SSN
6- Delete employee
7- Delete dept
You should have employee vector and a dept vector
Explanation / Answer
Source code:-
--------------------------
#include<iostream>
#include<iomanip>
#include<string.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
struct Emp
{
int SSN;
char FirstName[100];
char LastName[100];
double Salary;
int Department_no;
struct Emp *next;
};
struct Emp *first=NULL,*last=NULL,*swapping,*temp=NULL;
struct Dept
{
int Dept_no;
char DeptName[100];
struct Dept *next;
};
struct Dept *first1=NULL,*last1=NULL,*swapping2,*temp1=NULL;
class Employee_Info
{
public:
void display_AllEmployee()
{
struct Emp *head;
head=temp;
cout<<"------------------------------------------------------------------------------------"<<endl;
cout<<" SSN.No"<<setw(15)<<"FirstName"<<setw(15)<<"LastName"<<setw(15)<<"Salary"<<setw(15)<<endl;
cout<<" ---------------------------------------------------------------------------------"<<endl;
while(head!=NULL)
{
cout<<head->SSN<<setw(15)<<head->FirstName<<setw(15)<<head->LastName<<setw(15)<<head->Salary<<setw(15)<<head->Department_no<<endl;
head=head->next;
}
}
public:
void display_DeptEmployee(int key)
{
struct Dept *head;
head=temp1;
while(head!=NULL)
{
if(key==head->Dept_no)
{
cout<<"--------------------------------------"<<endl;
cout<<" Dept.No"<<setw(15)<<"Department_Name"<<endl;
cout<<" ------------------------------------"<<endl;
cout<<head->Dept_no<<setw(15)<<head->DeptName<<endl;
}
head=head->next;
}
}
void display_DeptInfo()
{
struct Dept *head;
head=temp1;
cout<<"--------------------------------------"<<endl;
cout<<" Dept.No"<<setw(15)<<"Department_Name"<<endl;
cout<<" ------------------------------------"<<endl;
while(head!=NULL)
{
cout<<head->Dept_no<<setw(15)<<head->DeptName<<endl;
head=head->next;
}
}
public:
int Employee_search(int key)
{
int index=0;
struct Emp* head=temp;
while(head!=NULL)
{
if(key==head->SSN)
{
cout<<" The Details are:"<<endl;
cout<<head->SSN<<setw(15)<<head->FirstName<<setw(15)<<head->LastName<<setw(15)<<head->Salary<<setw(15)<<head->Department_no<<endl;
return index;
}
index++;
head=head->next;
}
return -1;
}
public:
void addnewEmployee(struct Emp** head,int ssn,char firstName[],char LastName[],double salary,int deptno)
{
struct Emp* new_Emp=(struct Emp*)malloc(sizeof(struct Emp));
last=*head;
new_Emp->SSN=ssn;
strcpy(new_Emp->FirstName,firstName);
strcpy(new_Emp->LastName,LastName);
new_Emp->Salary=salary;
new_Emp->Department_no=deptno;
new_Emp->next=NULL;
while (last->next != NULL)
last = last->next;
last->next = new_Emp;
}
void addnewDepartment(struct Dept** head,int deptno,char DepartmentName[])
{
struct Dept* new_Dept=(struct Dept*)malloc(sizeof(struct Dept));
last1=*head;
new_Dept->Dept_no=deptno;
strcpy(new_Dept->DeptName,DepartmentName);
new_Dept->next=NULL;
while(last1->next != NULL)
last1=last1->next;
last1->next=new_Dept;
}
public:
void DeleteEmp(struct Emp **head, int position)
{
if (*head == NULL)
return;
struct Emp* temp = *head;
if (position == 0)
{
*head = temp->next;
free(temp);
return;
}
for(int i=0; temp!=NULL && i<position-1; i++)
temp = temp->next;
if(temp == NULL || temp->next == NULL)
return;
last = temp->next->next;
free(temp->next);
temp->next = last;
}
public:
void DeleteDept(struct Dept **head, int position)
{
if (*head == NULL)
return;
struct Dept* temp = *head;
if (position == 0)
{
*head = temp->next;
free(temp);
return;
}
for(int i=0; temp!=NULL && i<position-1; i++)
temp = temp->next;
if(temp == NULL || temp->next == NULL)
return;
last1= temp->next->next;
free(temp->next);
temp->next = last1;
}
public:
int Dept_search(int key)
{
int index=0;
struct Dept *head;
head=temp1;
while(head!=NULL)
{
if(key==head->Dept_no)
{
return index;
}
head=head->next;
index++;
}
return -1;
}
public:
static void reverse_emp(struct Emp** head)
{
struct Emp* prev = NULL;
struct Emp* current = *head;
struct Emp* next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
public:
static void reverse_dept(struct Dept** head)
{
struct Dept* prev = NULL;
struct Dept* current = *head;
struct Dept* next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
};
int main()
{
Employee_Info obj;
int ssn_no;
char firstname[50];
char lastName[50];
char tempstr[100];
char Departmentname[100];
double emp_sal;
int deptno;
int option,retval;
ifstream emp_file,dept_file;
emp_file.open("Emp_records.txt");
if(emp_file.is_open())
{
for(int i=0;i<6;i++)
{
temp = (struct Emp*)malloc(sizeof(struct Emp));
memset(firstname,0,sizeof(firstname));
memset(lastName,0,sizeof(lastName));
emp_file>>ssn_no;
emp_file>>firstname;
emp_file>>lastName;
emp_file>>emp_sal;
emp_file>>deptno;
temp->SSN=ssn_no;
strcpy(temp->FirstName,firstname);
strcpy(temp->LastName,lastName);
temp->Salary=emp_sal;
temp->Department_no=deptno;
temp->next=swapping;
swapping=temp;
}
obj.reverse_emp(&temp);
emp_file.close();
dept_file.open("Dept_Details.txt");
for(int i=0;i<6;i++)
{
temp1= (struct Dept*)malloc(sizeof(struct Dept));
memset(Departmentname,0,sizeof(Departmentname));
dept_file>>deptno;
dept_file>>Departmentname;
temp1->Dept_no=deptno;
strcpy(temp1->DeptName,Departmentname);
temp1->next=swapping2;
swapping2=temp1;
}
obj.reverse_dept(&temp1);
dept_file.close();
}
else
{
cout<<" ! Couldn't Able to Open The File"<<endl;
exit(0);
}
while(true)
{
cout<<" ****** MENU *****"<<endl;
cout<<" 1- Add employee"<<endl;
cout<<" 2- Create Department"<<endl;
cout<<" 3- Display department information"<<endl;
cout<<" 4- Display all Employee information"<<endl;
cout<<" 5- Search for employee by SSN"<<endl;
cout<<" 6- Delete employee"<<endl;
cout<<" 7- Delete dept"<<endl;
cout<<" 8.Exit"<<endl;
cout<<" Please Enter Any Option"<<endl;
cin>>option;
switch(option)
{
case 1:
cout<<" Please Enter the SSN of an EMployee "<<endl;
cin>>ssn_no;
cout<<" Please Enter FirstName"<<endl;
cin>>firstname;
cout<<" Please Enter LastName"<<endl;
cin>>lastName;
cout<<" Please Enter Salary"<<endl;
cin>>emp_sal;
cout<<" Please Enter Department No "<<endl;
cin>>deptno;
obj.addnewEmployee(&temp,ssn_no,firstname,lastName,emp_sal,deptno);
cout<<" New EMployee Details are Added Successfully"<<endl;
break;
case 2:
cout<<" Please Enter Department No "<<endl;
cin>>deptno;
cout<<" Please Enter the Department Name"<<endl;
cin>>Departmentname;
obj.addnewDepartment(&temp1,deptno,Departmentname);
cout<<" New Department Added Successfully"<<endl;
break;
case 3:
obj.display_DeptInfo();
break;
case 4:
int choice;
cout<<" (i).Display by Specific Department"<<endl;
cout<<" (ii)Display All Employee Information"<<endl;
cin>>choice;
if(choice==1)
{
cout<<" Please Enter the Department no:"<<endl;
cin>>deptno;
obj.display_DeptEmployee(deptno);
}
else
obj.display_AllEmployee();
break;
case 5:
cout<<" Please Enter a SSN of Employee: "<<endl;
cin>>ssn_no;
retval=obj.Employee_search(ssn_no);
if(retval==-1)
cout<<" the Employee Deatails are not Found"<<endl;
break;
case 6:
cout<<" Please Enter the SSN to remove from the Employee Record"<<endl;
cin>>ssn_no;
retval=obj.Employee_search(ssn_no);
if(retval==-1)
{
cout<<" The id is Not Exist Please try again"<<endl;
}
else
{
obj.DeleteEmp(&temp,retval);
cout<<" The Node is Deleted Successfully"<<endl;
cout<<" The New Records Are:"<<endl;
obj.display_AllEmployee();
}
break;
case 7:
cout<<" Please Enter the Department number to remove from the Department Record"<<endl;
cin>>deptno;
retval=obj.Dept_search(deptno);
if(retval==-1)
{
cout<<" The Dept no is Not Exist Please try again"<<endl;
}
else
{
obj.DeleteDept(&temp1,retval);
cout<<" The Department is Deleted Successfully"<<endl;
}
break;
case 8:
exit(0);
default:
cout<<"Invalid option"<<endl;
}
}
}
Emp_records.txt:-
--------------------------
1111 venkanna koothada 25000 123
2222 Prudhvi Maddi 30000 345
3333 Madhu Tirumla 40000 567
4444 Narayan Chekka 31000 789
5555 Subhani Khan 50000 777
6666 Sindhu Pallavi 30000 123
Dept_Details.txt:-
---------------------
123 Cprograming
345 PHP_Programming
567 Angular
789 UI_Developement
777 Product_Engineer
123 Java_Programming