Refactored employee selection and cleaned up menu logic

<SRS> SRS02 : Employee Management </SRS>

<Changes>
 - Removed getSelectedUserId() from FinanceExecutiveMenu
 - Added reusable selectEmployeeId() in MenuHelper
 - Simplified deactivateEmployee() using common selection logic
 - Fixed missing break in HRManagerMenu
</Changes>

<Review>
Smitha Mohan
</Review>
This commit is contained in:
2026-04-11 14:59:41 +05:30
parent 66c80fd055
commit aa21853a65
5 changed files with 38 additions and 82 deletions
@@ -28,53 +28,11 @@ void FinanceExecutiveMenu::run()
}
}
std::string FinanceExecutiveMenu::getSelectedUserId()
{
int choice;
std::map<int, std::shared_ptr<const Employee>> employeeList;
int index = 0;
auto allEmployees = m_zenvyController->getEmployees();
util::clear();
std::cout << "Select the Employee\n";
for (auto& currentEmployee : allEmployees)
{
if (currentEmployee->getEmployeeType() == Enums::EmployeeType::ADMIN)
{
continue;
}
employeeList[++index] = currentEmployee;
}
std::cout << std::left
<< std::setw(10) << "Index"
<< std::setw(15) << "Employee ID"
<< std::setw(20) << "Name"
<< std::setw(20) << "Employee Type" << std::endl;
for (const auto& employee : employeeList)
{
std::cout << std::left << std::setw(10) << employee.first
<< std::setw(15) << employee.second->getId()
<< std::setw(20) << employee.second->getEmployeeName()
<< std::setw(20) << Enums::getEmployeeTypeString(employee.second->getEmployeeType())
<< std::endl;
}
std::cout << "Enter the Index: ";
util::read(choice);
auto employeeIterator = employeeList.find(choice);
if (employeeIterator != employeeList.end())
{
return (employeeIterator->second->getId());
}
else
{
throw std::runtime_error("Invalid Index");
}
}
void FinanceExecutiveMenu::updatePayroll()
{
std::string employeeId;
double basicSalary, houseRentAllowance, foodAllowance, employeePFContribution, employerPFContribution;
employeeId = getSelectedUserId();
employeeId = selectEmployeeId(m_zenvyController->getEmployees());
util::clear();
if (employeeId != "") {
std::cout << "Enter the New Basic Salary: ";
@@ -13,7 +13,6 @@ public:
FinanceExecutiveMenu() : m_zenvyController(std::make_shared<ZenvyController>()) {};
void run();
bool handleOperation(int);
std::string getSelectedUserId();
void updatePayroll();
};
@@ -70,6 +70,7 @@ bool HRManagerMenu::handleOperation(int choice)
break;
case 13:
deactivateEmployee(m_zenvyController);
break;
case 14:
return false;
default:
@@ -100,4 +100,5 @@ void createEmployee(std::shared_ptr<ZenvyController> controller)
util::read(phone);
controller->createEmployee(employeeType, employeeDesignation, email, name, phone);
std::cout << "\nCreated Employee Successfully.";
util::pressEnter();
}
+35 -38
View File
@@ -65,60 +65,57 @@ inline void updateProfile(std::shared_ptr<ZenvyController> m_zenvyController)
}
}
inline std::map<int, std::shared_ptr<const Employee>> listEmployees(const std::shared_ptr<ZenvyController>& controller)
inline std::string selectEmployeeId(std::vector<std::shared_ptr<const Employee>> allEmployees)
{
auto employees = controller->getEmployees();
int choice;
std::map<int, std::shared_ptr<const Employee>> employeeList;
int index = 0;
util::clear();
std::cout << "Select the Employee\n";
for (auto& currentEmployee : allEmployees)
{
if (currentEmployee->getEmployeeType() == Enums::EmployeeType::ADMIN)
{
continue;
}
employeeList[++index] = currentEmployee;
}
std::cout << std::left
<< std::setw(5) << "Index"
<< std::setw(15) << "ID"
<< std::setw(25) << "Name"
<< "\n";
int index = 1;
for (auto& activeEmployees : employees)
<< std::setw(10) << "Index"
<< std::setw(15) << "Employee ID"
<< std::setw(20) << "Name"
<< std::setw(20) << "Employee Type" << std::endl;
for (const auto& employee : employeeList)
{
std::cout << std::left
<< std::setw(5) << index
<< std::setw(15) << activeEmployees->getId()
<< std::setw(25) << activeEmployees->getEmployeeName()
<< "\n";
employeeList[index] = activeEmployees;
++index;
std::cout << std::left << std::setw(10) << employee.first
<< std::setw(15) << employee.second->getId()
<< std::setw(20) << employee.second->getEmployeeName()
<< std::setw(20) << Enums::getEmployeeTypeString(employee.second->getEmployeeType())
<< std::endl;
}
if (employeeList.empty())
std::cout << "Enter the Index: ";
util::read(choice);
auto employeeIterator = employeeList.find(choice);
if (employeeIterator != employeeList.end())
{
std::cout << "No active employees available.\n";
return (employeeIterator->second->getId());
}
else
{
throw std::runtime_error("Invalid Index");
}
return employeeList;
}
inline void deactivateEmployee(const std::shared_ptr<ZenvyController>& controller)
{
auto employeeList = listEmployees(controller);
if (employeeList.empty())
{
return;
}
int choice;
util::clear();
std::cout << "\nEnter the index of the employee to deactivate: ";
util::read(choice);
auto iterator = employeeList.find(choice);
if (iterator != employeeList.end())
{
std::string id = iterator->second->getId();
bool success = controller->deactivateEmployee(id);
if (success)
if(controller->deactivateEmployee(selectEmployeeId(controller->getEmployees())))
{
std::cout << "Employee deactivated successfully\n";
util::pressEnter();
}
else
{
std::cout << "Employee not found\n";
util::pressEnter();
}
}
else
{
std::cout << "Invalid index.\n";
}
}