Lab 5
Lab 5
#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;
}
// 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;
}