380 lines
8.6 KiB
C++
380 lines
8.6 KiB
C++
/*
|
||
File: User.cpp
|
||
Description: Implements the User class which represents system users in the Vehicle Service Management System.
|
||
Provides constructors, destructor, accessors, and mutators for user details such as ID, username,
|
||
password, name, phone, email, role, state, and notifications.
|
||
Author: Trenser
|
||
Date: 19-May-2026
|
||
*/
|
||
|
||
#include <sstream>
|
||
#include "SerializedRecords.h"
|
||
#include "User.h"
|
||
#include "Notification.h"
|
||
#include "Enums.h"
|
||
#include "Factory.h"
|
||
#include "StringHelper.h"
|
||
|
||
int User::m_uid = 0;
|
||
|
||
/*
|
||
Function: User
|
||
Description: Default constructor that initializes a new user with a unique ID,
|
||
default role as CUSTOMER, and active state.
|
||
Parameters:
|
||
- None
|
||
Returns:
|
||
- A new User object.
|
||
*/
|
||
User::User()
|
||
: m_id("USR" + std::to_string(++m_uid)),
|
||
m_type(util::UserType::CUSTOMER),
|
||
m_status(util::State::ACTIVE) {}
|
||
|
||
/*
|
||
Function: User
|
||
Description: Parameterized constructor that initializes a new user with a unique ID and specified details.
|
||
Parameters:
|
||
- userName: Username for login.
|
||
- password: Password for authentication.
|
||
- name: Full name of the user.
|
||
- phone: Phone number of the user.
|
||
- email: Email address of the user.
|
||
- role: Role of the user (CUSTOMER, ADMIN, TECHNICIAN, etc.).
|
||
Returns:
|
||
- A new User object.
|
||
*/
|
||
User::User(const std::string& userName, const std::string& password, const std::string& name, const std::string& phone, const std::string& email, util::UserType role)
|
||
: m_id("USR" + std::to_string(++m_uid)),
|
||
m_userName(userName),
|
||
m_password(password),
|
||
m_name(name),
|
||
m_phone(phone),
|
||
m_email(email),
|
||
m_type(role),
|
||
m_status(util::State::ACTIVE) {}
|
||
|
||
/*
|
||
Function: User (parameterized constructor with ID)
|
||
Description: Initializes a user with an existing ID, credentials, personal details,
|
||
role, and state. Updates UID tracking based on ID.
|
||
Parameters:
|
||
- userId: const std::string&, unique user ID
|
||
- userName: const std::string&, username
|
||
- password: const std::string&, password
|
||
- name: const std::string&, full name
|
||
- phone: const std::string&, phone number
|
||
- email: const std::string&, email address
|
||
- role: util::UserType, role of the user
|
||
- status: util::State, state of the user (ACTIVE/INACTIVE)
|
||
Returns:
|
||
- A new User object
|
||
*/
|
||
User::User(const std::string& userId, const std::string& userName, const std::string& password, const std::string& name, const std::string& phone, const std::string& email, util::UserType role, util::State status)
|
||
: m_id(userId),
|
||
m_userName(userName),
|
||
m_password(password),
|
||
m_name(name),
|
||
m_phone(phone),
|
||
m_email(email),
|
||
m_type(role),
|
||
m_status(status)
|
||
{
|
||
int idNumber = util::extractNumber(m_id);
|
||
if (idNumber > m_uid)
|
||
{
|
||
m_uid = idNumber;
|
||
}
|
||
}
|
||
|
||
/*
|
||
Function: ~User
|
||
Description: Destructor that cleans up dynamically allocated notifications associated with the user.
|
||
Parameters:
|
||
- None
|
||
Returns:
|
||
- void
|
||
*/
|
||
User::~User()
|
||
{
|
||
auto values = m_notifications.getValues();
|
||
for (int index = 0; index < values.getSize(); index++)
|
||
{
|
||
delete values[index];
|
||
}
|
||
}
|
||
|
||
/*
|
||
Function: getId
|
||
Description: Retrieves the unique ID of the user.
|
||
Returns:
|
||
- const std::string& representing the user ID.
|
||
*/
|
||
const std::string& User::getId() const
|
||
{
|
||
return m_id;
|
||
}
|
||
|
||
/*
|
||
Function: getUserName
|
||
Description: Retrieves the username of the user.
|
||
Returns:
|
||
- const std::string& representing the username.
|
||
*/
|
||
const std::string& User::getUserName() const
|
||
{
|
||
return m_userName;
|
||
}
|
||
|
||
/*
|
||
Function: getPassword
|
||
Description: Retrieves the password of the user.
|
||
Returns:
|
||
- const std::string& representing the password.
|
||
*/
|
||
const std::string& User::getPassword() const
|
||
{
|
||
return m_password;
|
||
}
|
||
|
||
/*
|
||
Function: getName
|
||
Description: Retrieves the full name of the user.
|
||
Returns:
|
||
- const std::string& representing the name.
|
||
*/
|
||
const std::string& User::getName() const
|
||
{
|
||
return m_name;
|
||
}
|
||
|
||
/*
|
||
Function: getPhone
|
||
Description: Retrieves the phone number of the user.
|
||
Returns:
|
||
- const std::string& representing the phone number.
|
||
*/
|
||
const std::string& User::getPhone() const
|
||
{
|
||
return m_phone;
|
||
}
|
||
|
||
/*
|
||
Function: getEmail
|
||
Description: Retrieves the email address of the user.
|
||
Returns:
|
||
- const std::string& representing the email.
|
||
*/
|
||
const std::string& User::getEmail() const
|
||
{
|
||
return m_email;
|
||
}
|
||
|
||
/*
|
||
Function: getNotifications
|
||
Description: Retrieves the map of notifications associated with the user.
|
||
Returns:
|
||
- util::Map<std::string, Notification*>& representing the notifications.
|
||
*/
|
||
util::Map<std::string, Notification*>& User::getNotifications()
|
||
{
|
||
return m_notifications;
|
||
}
|
||
|
||
/*
|
||
Function: getUserType
|
||
Description: Retrieves the role of the user.
|
||
Returns:
|
||
- util::UserType representing the user role.
|
||
*/
|
||
util::UserType User::getUserType() const
|
||
{
|
||
return m_type;
|
||
}
|
||
|
||
/*
|
||
Function: getState
|
||
Description: Retrieves the current state (ACTIVE/INACTIVE) of the user.
|
||
Returns:
|
||
- util::State representing the user state.
|
||
*/
|
||
util::State User::getState() const
|
||
{
|
||
return m_status;
|
||
}
|
||
|
||
/*
|
||
Function: setId
|
||
Description: Sets the unique ID of the user.
|
||
Parameters:
|
||
- id: New user ID string.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::setId(const std::string& id)
|
||
{
|
||
m_id = id;
|
||
}
|
||
|
||
/*
|
||
Function: setUserName
|
||
Description: Sets the username of the user.
|
||
Parameters:
|
||
- userName: New username string.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::setUserName(const std::string& userName)
|
||
{
|
||
m_userName = userName;
|
||
}
|
||
|
||
/*
|
||
Function: setPassword
|
||
Description: Sets the password of the user.
|
||
Parameters:
|
||
- password: New password string.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::setPassword(const std::string& password)
|
||
{
|
||
m_password = password;
|
||
}
|
||
|
||
/*
|
||
Function: setName
|
||
Description: Sets the full name of the user.
|
||
Parameters:
|
||
- name: New name string.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::setName(const std::string& name)
|
||
{
|
||
m_name = name;
|
||
}
|
||
|
||
/*
|
||
Function: setPhone
|
||
Description: Sets the phone number of the user.
|
||
Parameters:
|
||
- phone: New phone number string.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::setPhone(const std::string& phone)
|
||
{
|
||
m_phone = phone;
|
||
}
|
||
|
||
/*
|
||
Function: setEmail
|
||
Description: Sets the email address of the user.
|
||
Parameters:
|
||
- email: New email string.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::setEmail(const std::string& email)
|
||
{
|
||
m_email = email;
|
||
}
|
||
|
||
/*
|
||
Function: addNotification
|
||
Description: Adds a new notification to the user’s notification map.
|
||
Parameters:
|
||
- notification: Pointer to the Notification object.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::addNotification(Notification* notification)
|
||
{
|
||
if (notification)
|
||
{
|
||
m_notifications.insert(notification->getId(), notification);
|
||
}
|
||
}
|
||
|
||
/*
|
||
Function: setRole
|
||
Description: Sets the role of the user.
|
||
Parameters:
|
||
- role: New user role value.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::setRole(util::UserType role)
|
||
{
|
||
m_type = role;
|
||
}
|
||
|
||
/*
|
||
Function: setState
|
||
Description: Sets the state (ACTIVE/INACTIVE) of the user.
|
||
Parameters:
|
||
- status: New state value.
|
||
Returns:
|
||
- void
|
||
*/
|
||
void User::setState(util::State status)
|
||
{
|
||
m_status = status;
|
||
}
|
||
|
||
/*
|
||
Function: serialize
|
||
Description: Serializes the User object into a SerializedUser record.
|
||
Parameters:
|
||
- None
|
||
Returns:
|
||
- SerializedUser: Serialized representation of the user
|
||
*/
|
||
SerializedUser User::serialize() const
|
||
{
|
||
SerializedUser serialized = {};
|
||
strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
|
||
strcpy_s(serialized.username, sizeof(serialized.username), m_userName.c_str());
|
||
strcpy_s(serialized.password, sizeof(serialized.password), m_password.c_str());
|
||
strcpy_s(serialized.name, sizeof(serialized.name), m_name.c_str());
|
||
strcpy_s(serialized.phone, sizeof(serialized.phone), m_phone.c_str());
|
||
strcpy_s(serialized.email, sizeof(serialized.email), m_email.c_str());
|
||
serialized.userType = m_type;
|
||
serialized.status = m_status;
|
||
return serialized;
|
||
}
|
||
|
||
/*
|
||
Function: deserialize
|
||
Description: Deserializes a SerializedUser record into a User object.
|
||
Parameters:
|
||
- serializedUser: const SerializedUser&, serialized user record
|
||
Returns:
|
||
- User*: Pointer to the deserialized User object
|
||
*/
|
||
User* User::deserialize(const SerializedUser& serializedUser)
|
||
{
|
||
return Factory::getObject<User>(
|
||
serializedUser.id,
|
||
serializedUser.username,
|
||
serializedUser.password,
|
||
serializedUser.name,
|
||
serializedUser.phone,
|
||
serializedUser.email,
|
||
serializedUser.userType,
|
||
serializedUser.status);
|
||
}
|
||
|
||
/*
|
||
Function: getHeaders
|
||
Description: Retrieves the CSV headers for user serialization.
|
||
Parameters:
|
||
- None
|
||
Returns:
|
||
- std::string: Header string ("ID,Username,Password,Name,Phone,Email,UserType,UserStatus")
|
||
*/
|
||
std::string User::getHeaders()
|
||
{
|
||
return "ID,Username,Password,Name,Phone,Email,UserType,UserStatus";
|
||
} |