0% found this document useful (0 votes)
13 views5 pages

Employee Management Assignment

The document is a C++ implementation of an Employee Management System using linked lists. It allows users to add, update, search, and delete employee records based on ID or name. The program includes a main menu for user interaction and handles employee data through a structured format.

Uploaded by

saifmohamed01017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Employee Management Assignment

The document is a C++ implementation of an Employee Management System using linked lists. It allows users to add, update, search, and delete employee records based on ID or name. The program includes a main menu for user interaction and handles employee data through a structured format.

Uploaded by

saifmohamed01017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <string>
using namespace std;

struct Employee {
int id;
string name;
string departmentCode;
float basicSalary;
string phone;
string address;
string job;
};

struct Node {
Employee data;
Node* next;
};

Node* head = nullptr;

void addEmployee(Employee emp) {


Node* newNode = new Node();
newNode->data = emp;
newNode->next = nullptr;

if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode;
}
cout << "Employee added successfully!\n";
}

void updateEmployee(int id) {


Node* temp = head;
while (temp != nullptr) {
if (temp->data.id == id) {
cout << "Enter new Name: ";
cin.ignore();
getline(cin, temp->data.name);

cout << "Enter new Department Code: ";


getline(cin, temp->data.departmentCode);

cout << "Enter new Basic Salary: ";


cin >> temp->data.basicSalary;
cin.ignore();

cout << "Enter new Phone: ";


getline(cin, temp->data.phone);
cout << "Enter new Address: ";
getline(cin, temp->data.address);

cout << "Enter new Job: ";


getline(cin, temp->data.job);

cout << "Employee data updated successfully!\n";


return;
}
temp = temp->next;
}
cout << "Employee not found.\n";
}

void searchByID(int id) {


Node* temp = head;
while (temp != nullptr) {
if (temp->data.id == id) {
cout << "Employee Found:\n";
cout << "ID: " << temp->data.id << "\nName: " << temp->data.name
<< "\nDepartment: " << temp->data.departmentCode << "\nSalary: " <<
temp->data.basicSalary
<< "\nPhone: " << temp->data.phone << "\nAddress: " << temp->data.address
<< "\nJob: " << temp->data.job << endl;
return;
}
temp = temp->next;
}
cout << "Employee not found.\n";
}

void searchByName(string name) {


Node* temp = head;
bool found = false;
while (temp != nullptr) {
if (temp->data.name == name) {
cout << "Employee Found:\n";
cout << "ID: " << temp->data.id << "\nName: " << temp->data.name
<< "\nDepartment: " << temp->data.departmentCode << "\nSalary: " <<
temp->data.basicSalary
<< "\nPhone: " << temp->data.phone << "\nAddress: " << temp->data.address
<< "\nJob: " << temp->data.job << endl;
found = true;
}
temp = temp->next;
}
if (!found) cout << "No employee found with this name.\n";
}

void deleteEmployee(int id) {


Node* temp = head;
Node* prev = nullptr;

while (temp != nullptr && temp->data.id != id) {


prev = temp;
temp = temp->next;
}

if (temp == nullptr) {
cout << "Employee not found.\n";
return;
}

if (prev == nullptr) {
head = head->next;
} else {
prev->next = temp->next;
}

delete temp;
cout << "Employee deleted successfully.\n";
}

int main() {
int choice;
while (true) {
cout << "\n----- Employee Management System -----\n";
cout << "1. Add Employee\n";
cout << "2. Update Employee by ID\n";
cout << "3. Search Employee by ID\n";
cout << "4. Search Employee by Name\n";
cout << "5. Delete Employee by ID\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1) {
Employee emp;
cout << "Enter ID: ";
cin >> emp.id;
cin.ignore();

cout << "Enter Name: ";


getline(cin, emp.name);

cout << "Enter Department Code: ";


getline(cin, emp.departmentCode);

cout << "Enter Basic Salary: ";


cin >> emp.basicSalary;
cin.ignore();

cout << "Enter Phone: ";


getline(cin, emp.phone);

cout << "Enter Address: ";


getline(cin, emp.address);

cout << "Enter Job: ";


getline(cin, emp.job);
addEmployee(emp);
} else if (choice == 2) {
int id;
cout << "Enter ID to update: ";
cin >> id;
updateEmployee(id);
} else if (choice == 3) {
int id;
cout << "Enter ID to search: ";
cin >> id;
searchByID(id);
} else if (choice == 4) {
string name;
cout << "Enter Name to search: ";
cin.ignore();
getline(cin, name);
searchByName(name);
} else if (choice == 5) {
int id;
cout << "Enter ID to delete: ";
cin >> id;
deleteEmployee(id);
} else if (choice == 6) {
cout << "Exiting...\n";
break;
} else {
cout << "Invalid choice. Try again.\n";
}
}
return 0;
}

You might also like