Files
Training-Team2-Zenvy-Jan26/Trenser.Zenvy/Trenser.Zenvy/models/Employee.cpp
T
2026-04-17 10:26:33 +05:30

327 lines
7.6 KiB
C++

/*
* File: Employee.cpp
* Description: The Employee class manages employee information and associated work records such as payroll, attendance, leaves, and payslips.
* Author: Trenser
* Created: 31-Mar-2026
*/
#include <sstream>
#include "Employee.h"
#include "Factory.h"
#include "StringHelper.h"
#include "Admin.h"
#include "HRManager.h"
#include "ITExecutive.h"
#include "TalentExecutive.h"
#include "TeamExecutive.h"
#include "FinanceExecutive.h"
#include "GeneralEmployee.h"
int Employee::m_uid = 0;
Employee::Employee(const std::string& id,
const std::string& name,
const std::string& phone,
const std::string& password,
const std::string& email,
const std::string& teamId,
Enums::TeamStatus teamStatus,
Enums::EmployeeType employeeType,
Enums::AccountStatus accountStatus)
: m_id(id),
m_password(password),
m_name(name),
m_phone(phone),
m_email(email),
m_accountStatus(accountStatus),
m_teamStatus(teamStatus),
m_teamId(teamId),
m_employeeType(employeeType),
m_payroll()
{
int idNumber = util::extractNumber(m_id);
if (idNumber > m_uid)
{
m_uid = idNumber;
}
}
const std::string& Employee::getId() const
{
return m_id;
}
const std::string& Employee::getEmployeePassword() const
{
return m_password;
}
const std::string& Employee::getEmployeeName() const
{
return m_name;
}
const std::string& Employee::getEmployeePhone() const
{
return m_phone;
}
const std::string& Employee::getEmployeeEmail() const
{
return m_email;
}
Enums::AccountStatus Employee::getEmployeeAccountStatus() const
{
return m_accountStatus;
}
Enums::TeamStatus Employee::getEmployeeTeamStatus() const
{
return m_teamStatus;
}
const std::string& Employee::getEmployeeTeamId() const
{
return m_teamId;
}
Payroll* Employee::getPayroll() const
{
return m_payroll;
}
const payslipMap& Employee::getEmployeePayslips() const
{
return m_payslips;
}
const attendanceMap& Employee::getEmployeeAttendances() const
{
return m_attendances;
}
const leaveMap& Employee::getEmployeeLeaves() const
{
return m_leaves;
}
Enums::EmployeeType Employee::getEmployeeType() const
{
return m_employeeType;
}
std::string Employee::getHeaders()
{
return "EmployeeId,Email,Name,Phone,Password,TeamID,TeamStatus,AccountStatus,EmployeeType";
}
void Employee::setEmployeeId(const std::string& id)
{
m_id = id;
}
void Employee::setEmployeePassword(const std::string& password)
{
m_password = password;
}
void Employee::setEmployeeName(const std::string& name)
{
m_name = name;
}
void Employee::setEmployeePhone(const std::string& phone)
{
m_phone = phone;
}
void Employee::setEmployeeAccountStatus(Enums::AccountStatus status)
{
m_accountStatus = status;
}
void Employee::setEmployeeTeamStatus(Enums::TeamStatus status)
{
m_teamStatus = status;
}
void Employee::setEmployeeTeamId(const std::string& teamId)
{
m_teamId = teamId;
}
void Employee::setEmployeePayroll(Payroll* payroll)
{
m_payroll = payroll;
}
/*
* Function: addPayslip
* Description : Adds a payslip to the employee's payslip records
* Parameters :
payslip - Pointer to the Payslip object to be added
* Returns :
void
*/
void Employee::addPayslip(Payslip* payslip)
{
if (payslip)
{
m_payslips[payslip->getId()] = payslip;
}
}
/*
* Function: addAttendance
* Description: Adds an attendance record to the employee's attendance history
* Parameters:
* attendance - Pointer to the Attendance object containing login information
* Returns:
* void
*/
void Employee::addAttendance(Attendance* attendance)
{
if (attendance)
{
m_attendances[attendance->getLoginTime().getDateAsInt()][attendance->getAttendanceId()] = attendance;
}
}
/*
* Function: addLeave
* Description: Adds a leave record to the employee's leave history
* Parameters:
* leave - Pointer to the Leave object to be added
* Returns:
* void
*/
void Employee::addLeave(Leave* leave)
{
if (leave)
{
m_leaves[leave->getLeaveId()] = leave;
}
}
/*
* Function: serialize
* Description: Serializes the employee object's core details into a comma-separated string
* Parameters:
None
* Returns:
A string containing serialized employee data in CSV format
*/
std::string Employee::serialize() const
{
std::ostringstream serializedEmployee;
serializedEmployee << m_id << ','
<< m_email << ','
<< m_name << ','
<< m_phone << ','
<< m_password << ','
<< m_teamId << ','
<< Enums::getTeamStatusString(m_teamStatus) << ','
<< Enums::getAccountStatusString(m_accountStatus) << ','
<< Enums::getEmployeeTypeString(m_employeeType);
return serializedEmployee.str();
}
/*
* Function: deserialize
* Description: Creates and returns an Employee object from a serialized comma-separated record string
* Parameters:
record - A string containing serialized employee data in CSV format
* Returns:
Pointer to a newly created Employee object based on the employee type
*/
Employee* Employee::deserialize(const std::string& record)
{
std::string id, name, phone, password, email;
std::string teamId, teamStatusString, accountStatusString, employeeTypeString;
std::istringstream serializedEmployee(record);
getline(serializedEmployee, id, ',');
getline(serializedEmployee, email, ',');
getline(serializedEmployee, name, ',');
getline(serializedEmployee, phone, ',');
getline(serializedEmployee, password, ',');
getline(serializedEmployee, teamId, ',');
getline(serializedEmployee, teamStatusString, ',');
getline(serializedEmployee, accountStatusString, ',');
getline(serializedEmployee, employeeTypeString, ',');
Enums::TeamStatus teamStatus = Enums::getTeamStatus(teamStatusString);
Enums::AccountStatus accountStatus = Enums::getAccountStatus(accountStatusString);
Enums::EmployeeType employeeType = Enums::getEmployeeType(employeeTypeString);
switch (employeeType)
{
case Enums::EmployeeType::IT:
return Factory::getObject<ITExecutive>(
id,
name,
phone,
password,
email,
teamId,
teamStatus,
accountStatus
);
case Enums::EmployeeType::FINANCE:
return Factory::getObject<FinanceExecutive>(
id,
name,
phone,
password,
email,
teamId,
teamStatus,
accountStatus
);
case Enums::EmployeeType::HR:
return Factory::getObject<HRManager>(
id,
name,
phone,
password,
email,
teamId,
teamStatus,
accountStatus
);
case Enums::EmployeeType::TEAM:
return Factory::getObject<TeamExecutive>(
id,
name,
phone,
password,
email,
teamId,
teamStatus,
accountStatus
);
case Enums::EmployeeType::TALENT_ACQUISITION:
return Factory::getObject<TalentExecutive>(
id,
name,
phone,
password,
email,
teamId,
teamStatus,
accountStatus
);
case Enums::EmployeeType::ADMIN:
return Factory::getObject<Admin>(
id,
name,
phone,
password,
email,
teamId,
teamStatus,
accountStatus
);
case Enums::EmployeeType::GENERAL:
throw std::runtime_error("Cannot deserialize GeneralEmployee!");
default:
return nullptr;
}
}