Files
Training-Team2-Zenvy-Jan26/Trenser.Zenvy/Trenser.Zenvy/views/UserInterface.cpp
T
2026-04-16 18:02:07 +05:30

222 lines
5.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* File: UserInterface.cpp
* Description: Implements the UserInterface class functions including menu loop, login handling, and routing to appropriate role-based menus.
* Author: Trenser
* Created: 02-Apr-2026
*/
#include <iostream>
#include <utility>
#include <stdexcept>
#include "UserInterface.h"
#include "AdminMenu.h"
#include "EmployeeMenu.h"
#include "FinanceExecutiveMenu.h"
#include "HRManagerMenu.h"
#include "ITExecutiveMenu.h"
#include "TalentExecutiveMenu.h"
#include "TeamExecutiveMenu.h"
#include "TeamLeadMenu.h"
#include "ZenvyController.h"
#include "InputHelper.h"
#include "OutputHelper.h"
#include "Validator.h"
/*
* Function: UserInterface::run
* Description: Starts the user interface loop and displays login/exit options until exit is selected
* Parameters:
* None
* Returns:
* None
*/
void UserInterface::run()
{
bool isMenuActive = true;
try
{
m_controller->loadStates();
}
catch (const std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
return;
}
while (isMenuActive)
{
try
{
int choice;
util::clear();
std::cout << "Zenvy - HR Management System\n1. Login\n2. Exit\nEnter your Choice: ";
util::read(choice);
if (!handleOperation(choice))
{
isMenuActive = false;
}
}
catch (const std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
util::pressEnter();
}
}
try
{
m_controller->persistStates();
}
catch (const std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
return;
}
}
/*
* Function: UserInterface::handleOperation
* Description: Handles the users menu choice and executes the corresponding action
* Parameters:
* choice - integer representing the selected menu option
* Returns:
* true - if the interface should remain active
* false - if the user chooses to exit
*/
bool UserInterface::handleOperation(int choice)
{
switch (choice)
{
case 1:
login();
break;
case 2:
std::cout << "Exiting..." << std::endl;
return false;
default:
std::cout << "Enter a valid choice!" << std::endl;
util::pressEnter();
}
return true;
}
/*
* Function: UserInterface::login
* Description: Authenticates the user by email and password, validates login status, and routes to the appropriate menu
* Parameters:
* None
* Returns:
* None
*/
void UserInterface::login()
{
std::string email, password;
util::clear();
std::cout << "Enter email: ";
util::read(email);
std::cout << "Enter password: ";
util::read(password);
AuthenticationDTO authenticationContext = m_controller->login(email, password);
Enums::LoginStatus loginStatus = std::get<0>(authenticationContext);
Enums::EmployeeType employeeType = std::get<1>(authenticationContext);
Enums::EmployeeDesignation employeeDesignation = std::get<2>(authenticationContext);
if (loginStatus == Enums::LoginStatus::USER_NOT_FOUND)
{
std::cout << "Error: User Not Found! Try Again\n";
util::pressEnter();
return;
}
if (loginStatus == Enums::LoginStatus::INVALID_PASSWORD)
{
std::cout << "Error: Invalid Password! Try Again\n";
util::pressEnter();
return;
}
if (loginStatus == Enums::LoginStatus::FIRST_LOGIN)
{
util::clear();
std::cout << "Warning: You're using the default password!\n"
<< "Please change it.\n";
std::cout << "Enter new password: \n";
util::read(password);
if (util::isPasswordValid(password))
{
m_controller->changePassword(password);
}
else
{
std::cout << "Error: Invalid Password\n";
util::pressEnter();
return;
}
}
util::clear();
switch (employeeType)
{
case Enums::EmployeeType::ADMIN:
{
AdminMenu menu;
menu.run();
break;
}
case Enums::EmployeeType::HR:
{
HRManagerMenu menu;
menu.run();
break;
}
case Enums::EmployeeType::FINANCE:
{
FinanceExecutiveMenu menu;
menu.run();
break;
}
case Enums::EmployeeType::IT:
{
ITExecutiveMenu menu;
menu.run();
break;
}
case Enums::EmployeeType::TEAM:
{
TeamExecutiveMenu menu;
menu.run();
break;
}
case Enums::EmployeeType::TALENT_ACQUISITION:
{
TalentExecutiveMenu menu;
menu.run();
break;
}
case Enums::EmployeeType::GENERAL:
{
if (employeeDesignation == Enums::EmployeeDesignation::TEAM_LEAD)
{
TeamLeadMenu menu;
menu.run();
}
else
{
EmployeeMenu menu;
menu.run();
}
break;
}
default:
throw std::runtime_error("Error: Unsupported employee type!\n");
util::pressEnter();
break;
}
m_controller->logout();
}
UserInterface::~UserInterface()
{
delete m_controller;
delete m_employeeMenu;
delete m_adminMenu;
delete m_financeExecutiveMenu;
delete m_hrManagerMenu;
delete m_itExecutiveMenu;
delete m_talentExecutiveMenu;
delete m_teamExecutiveMenu;
delete m_teamLeadMenu;
}