0% found this document useful (0 votes)
18 views

Lab 5

The document describes an Employee class with attributes like ID, name and department. It also includes methods to create, update and print employee records stored in a vector. The main function contains a menu driven program to perform create, update, print operations on employee records.

Uploaded by

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

Lab 5

The document describes an Employee class with attributes like ID, name and department. It also includes methods to create, update and print employee records stored in a vector. The main function contains a menu driven program to perform create, update, print operations on employee records.

Uploaded by

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

#include <iostream>

#include <vector>
using namespace std;

// Employee class
class Employee {
private:
int id;
string name;
string department;

public:
// Constructor with default values
Employee(int empID = 0, string empName = "#", string empDept = "#") {
id = empID;
name = empName;
department = empDept;
}

// Accessors
int getId() {
return id;
}

string getName() {
return name;
}

string getDepartment() {
return department;
}

// Mutators
void setId(int empID) {
id = empID;
}

void setName(string empName) {


name = empName;
}

void setDepartment(string empDept) {


department = empDept;
}

// toString method
string toString() {
return "Employee ID: " + to_string(id) + "\nEmployee Name: " + name + "\
nEmployee Department: " + department;
}
};

int main() {
vector<Employee> employees; // Vector to store employee records

while (true) {
cout << "\nMenu:\n";
cout << "a. Create new Employee record\n";
cout << "b. Update name based on ID\n";
cout << "c. Print All Employees\n";
cout << "d. Print Department Specific employees\n";
cout << "e. Exit\n";
cout << "Enter your choice (a/b/c/d/e): ";

char choice;
cin >> choice;

switch (choice) {
case 'a': {
int empID;
string empName, empDept;
cout << "Enter Employee ID: ";
cin >> empID;
cout << "Enter Employee Name: ";
cin.ignore();
getline(cin, empName);
cout << "Enter Employee Department: ";
getline(cin, empDept);
employees.push_back(Employee(empID, empName, empDept));
cout << "Employee record created successfully.\n";
break;
}
case 'b': {
int empID;
string empName;
cout << "Enter Employee ID: ";
cin >> empID;
cout << "Enter updated Employee Name: ";
cin.ignore();
getline(cin, empName);
bool updated = false;
for (Employee &emp : employees) {
if (emp.getId() == empID) {
emp.setName(empName);
cout << "Employee name updated successfully.\n";
updated = true;
break;
}
}
if (!updated) {
cout << "Employee ID not found.\n";
}
break;
}
case 'c':
cout << "\nAll Employees:\n";
for (const Employee &emp : employees) {
cout << emp.toString() << endl;
}
break;
case 'd': {
string empDept;
cout << "Enter Department name: ";
cin.ignore();
getline(cin, empDept);
cout << "\nEmployees in Department " << empDept << ":\n";
for (const Employee &emp : employees) {
if (emp.getDepartment() == empDept) {
cout << emp.toString() << endl;
}
}
break;
}
case 'e':
cout << "Exiting the program.\n";
return 0;
default:
cout << "Invalid choice. Please select a valid option.\n";
}
}

return 0;
}

You might also like