Refactored validation logic and centralized employee validators

<SRS> SRS02 : Employee Management </SRS>

<Changes>
 - Moved employee validation logic (active type check, email and phone duplication) to Validator utility
 - Updated service to use util::hasActiveEmployeeOfType, util::isEmailDuplicate, and util::isPhoneDuplicate
 - Integrated validator usage in MenuHelper for input validation
 - Cleaned up formatting and minor inconsistencies
</Changes>

<Review>
Smitha Mohan
</Review>
This commit is contained in:
2026-04-10 20:29:55 +05:30
parent c50700e70c
commit 1d94f1680c
4 changed files with 111 additions and 75 deletions
@@ -14,45 +14,6 @@
#include "FileManager.h"
#include "ApplicationConfig.h"
static bool hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const employeeMap& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeeType() == employeeType && employee->getEmployeeAccountStatus() == Enums::AccountStatus::ACTIVE)
{
return true;
}
}
return false;
}
static bool isEmailDuplicate(const std::string& email, const employeeMap& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeeEmail() == email)
{
return true;
}
}
return false;
}
static bool isPhoneDuplicate(const std::string& phone, const employeeMap& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeePhone() == phone)
{
return true;
}
}
return false;
}
void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType, Enums::EmployeeDesignation employeeDesignation, const std::string& email, const std::string& name, const std::string& phone)
{
auto& employees = m_dataStore.getEmployees();
@@ -64,7 +25,7 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
Enums::EmployeeType authenticatedEmployeeType = authenticatedEmployee->getEmployeeType();
std::shared_ptr<Employee> employee;
std::shared_ptr<Payroll> payroll;
if (employeeType != Enums::EmployeeType::GENERAL && hasActiveEmployeeOfType(employeeType, employees))
if (employeeType != Enums::EmployeeType::GENERAL && util::hasActiveEmployeeOfType(employeeType, employees))
{
throw std::runtime_error("Cannot create more than one employee of type " + Enums::getEmployeeTypeString(employeeType));
}
@@ -76,11 +37,11 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
{
throw std::runtime_error("Invalid Phone");
}
if (isEmailDuplicate(email, employees))
if (util::isEmailDuplicate(email, employees))
{
throw std::runtime_error("Duplicate Email");
}
if (isPhoneDuplicate(phone, employees))
if (util::isPhoneDuplicate(phone, employees))
{
throw std::runtime_error("Duplicate Phone Number!");
}
@@ -1,6 +1,6 @@
#include <string>
#include <cctype>
#include "Validator.h"
#include "Employee.h"
#include "ApplicationConfig.h"
bool util::isPhoneNumberValid(const std::string& phoneNumber) {
@@ -69,3 +69,54 @@ bool util::isPasswordValid(const std::string& password)
}
return hasUpper && hasLower && hasDigit && hasSpecial;
}
bool util::hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const std::map<std::string, std::shared_ptr<Employee>> & employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeeType() == employeeType && employee->getEmployeeAccountStatus() == Enums::AccountStatus::ACTIVE)
{
return true;
}
}
return false;
}
bool util::isEmailDuplicate(const std::string& email, const std::map<std::string, std::shared_ptr<Employee>>& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeeEmail() == email)
{
return true;
}
}
return false;
}
bool util::isPhoneDuplicate(const std::string& phone, const std::map<std::string, std::shared_ptr<Employee>>& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeePhone() == phone)
{
return true;
}
}
return false;
}
bool util::isPhoneDuplicate(const std::string& phone, const std::vector<std::shared_ptr<const Employee>>& employees)
{
for (const auto& employee : employees)
{
if (employee->getEmployeePhone() == phone)
{
return true;
}
}
return false;
}
@@ -1,11 +1,21 @@
#pragma once
#include<string>
#include<map>
#include<memory>
#include<vector>
#include<algorithm>
#include<cctype>
#include "Enums.h"
class Employee;
namespace util
{
bool isPhoneNumberValid(const std::string&);
bool isEmailValid(const std::string&);
bool isPasswordValid(const std::string&);
bool hasActiveEmployeeOfType(Enums::EmployeeType, const std::map<std::string, std::shared_ptr<Employee>>&);
bool isEmailDuplicate(const std::string&, const std::map<std::string, std::shared_ptr<Employee>>&);
bool isPhoneDuplicate(const std::string&, const std::map<std::string, std::shared_ptr<Employee>>&);
bool isPhoneDuplicate(const std::string&, const std::vector<std::shared_ptr<const Employee>>&);
}
+16 -2
View File
@@ -9,6 +9,7 @@
#include "MenuHelper.h"
#include "InputHelper.h"
#include "OutputHelper.h"
#include "Validator.h"
#include "Enums.h"
void createEmployee(std::shared_ptr<ZenvyController> controller);
@@ -31,16 +32,29 @@ inline void updateProfile(std::shared_ptr<ZenvyController> m_zenvyController)
switch (choice)
{
case 1:
std::cout << "Enter your updated Name :";
std::cout << "Enter your updated Name: ";
util::read(name);
m_zenvyController->updateProfile(name, phone);
std::cout << "Profile Updated Successfully\n";
util::pressEnter();
break;
case 2:
std::cout << "Enter your updated phone Number :";
std::cout << "Enter your updated phone Number: ";
util::read(phone);
if (!util::isPhoneNumberValid(phone))
{
std::cout << "Error: Invalid Phone Number";
util::pressEnter();
}
if (util::isPhoneDuplicate(phone, m_zenvyController->getEmployees()))
{
std::cout << "Error: Duplicate Phone Number!";
util::pressEnter();
return;
}
m_zenvyController->updateProfile(name, phone);
std::cout << "Profile Updated Successfully\n";
util::pressEnter();
break;
case 3:
return;