Files
Training-VehicleService-May26/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/ComboPackage.cpp
T

342 lines
9.3 KiB
C++

/*
File: ComboPackage.cpp
Description: Implements the ComboPackage class which represents a bundled set of services in the Vehicle Service Management System.
Provides constructors, accessors, and mutators for package details such as ID, name, discount percentage, state,
and associated services.
Author: Trenser
Date: 19-May-2026
*/
#include <sstream>
#include <stdexcept>
#include "ComboPackage.h"
#include "Service.h"
#include "Factory.h"
#include "StringHelper.h"
int ComboPackage::m_uid = 0;
/*
Function: ComboPackage
Description: Default constructor that initializes a new combo package with a unique ID,
active state, and zero discount percentage.
Parameters:
- None
Returns:
- A new ComboPackage object.
*/
ComboPackage::ComboPackage()
: m_id("CMP" + std::to_string(++m_uid)),
m_status(util::State::ACTIVE),
m_discountPercentage(0.0) {}
/*
Function: ComboPackage
Description: Parameterized constructor that initializes a new combo package with a unique ID,
specified package name, discount percentage, active state, and associated services.
Parameters:
- packageName: Name of the combo package.
- discountPercentage: Discount percentage applied to the package.
- services: Map of services included in the package.
Returns:
- A new ComboPackage object.
*/
ComboPackage::ComboPackage(const std::string& packageName, double discountPercentage, const util::Map<std::string, Service*>& services)
: m_id("CMP" + std::to_string(++m_uid)),
m_packageName(packageName),
m_discountPercentage(discountPercentage),
m_status(util::State::ACTIVE),
m_services(services)
{
int numberOfServices = m_services.getSize();
auto servicePointers = m_services.getValues();
for (int index = 0; index < numberOfServices; index++)
{
m_serviceIDs.push_back(servicePointers[index]->getId());
}
}
/*
Function: ComboPackage (parameterized constructor with ID)
Description: Initializes a combo package with an existing ID, name, discount percentage,
service IDs, and state. Updates UID tracking based on ID.
Parameters:
- id: const std::string&, unique ID of the package
- packageName: const std::string&, name of the package
- discountPercentage: double, discount percentage applied
- serviceIDs: const util::Vector<std::string>&, IDs of services included
- status: util::State, state of the package (ACTIVE/INACTIVE)
Returns:
- A new ComboPackage object
*/
ComboPackage::ComboPackage(const std::string& id, const std::string& packageName, double discountPercentage, const util::Vector<std::string>& serviceIDs, util::State status)
: m_id(id),
m_packageName(packageName),
m_discountPercentage(discountPercentage),
m_serviceIDs(serviceIDs),
m_status(status)
{
int idNumber = util::extractNumber(m_id);
if (idNumber > m_uid)
{
m_uid = idNumber;
}
}
/*
Function: getId
Description: Retrieves the unique ID of the combo package.
Parameters:
- None
Returns:
- const std::string& representing the package ID.
*/
const std::string& ComboPackage::getId() const
{
return m_id;
}
/*
Function: getPackageName
Description: Retrieves the name of the combo package.
Parameters:
- None
Returns:
- const std::string& representing the package name.
*/
const std::string& ComboPackage::getPackageName() const
{
return m_packageName;
}
/*
Function: getDiscountPercentage
Description: Retrieves the discount percentage applied to the combo package.
Parameters:
- None
Returns:
- double representing the discount percentage.
*/
double ComboPackage::getDiscountPercentage() const
{
return m_discountPercentage;
}
/*
Function: getState
Description: Retrieves the current state (ACTIVE/INACTIVE) of the combo package.
Parameters:
- None
Returns:
- util::State representing the package state.
*/
util::State ComboPackage::getState() const
{
return m_status;
}
const util::Vector<std::string>& ComboPackage::getServiceIDs() const
{
return m_serviceIDs;
}
/*
Function: getServices
Description: Retrieves the map of services included in the combo package.
Parameters:
- None
Returns:
- const util::Map<std::string, Service*>& representing the services.
*/
const util::Map<std::string, Service*>& ComboPackage::getServices() const
{
return m_services;
}
/*
Function: setId
Description: Sets the unique ID of the combo package.
Parameters:
- id: New ID string.
Returns:
- void
*/
void ComboPackage::setId(const std::string& id)
{
m_id = id;
}
/*
Function: setPackageName
Description: Sets the name of the combo package.
Parameters:
- packageName: New package name string.
Returns:
- void
*/
void ComboPackage::setPackageName(const std::string& packageName)
{
m_packageName = packageName;
}
/*
Function: setDiscountPercentage
Description: Sets the discount percentage for the combo package.
Parameters:
- discountPercentage: New discount percentage value.
Returns:
- void
*/
void ComboPackage::setDiscountPercentage(double discountPercentage)
{
m_discountPercentage = discountPercentage;
}
/*
Function: setServices
Description: Sets the services included in the combo package.
Parameters:
- services: Map of services to be associated with the package.
Returns:
- void
*/
void ComboPackage::setServices(const util::Map<std::string, Service*>& services)
{
m_services = services;
m_serviceIDs.clear();
int numberOfServices = m_services.getSize();
auto servicePointers = m_services.getValues();
for (int index = 0; index < numberOfServices; index++)
{
m_serviceIDs.push_back(servicePointers[index]->getId());
}
}
/*
Function: setState
Description: Sets the state (ACTIVE/INACTIVE) of the combo package.
Parameters:
- status: New state value.
Returns:
- void
*/
void ComboPackage::setState(util::State status)
{
m_status = status;
}
/*
Function: getServiceIDsAsString (static helper)
Description: Converts a vector of service IDs into a single string separated by '|'.
Parameters:
- serviceIDs: const util::Vector<std::string>&, vector of service IDs
Returns:
- std::string: Concatenated service IDs string
*/
static std::string getServiceIDsAsString(const util::Vector<std::string>& serviceIDs)
{
int numberOfServices = serviceIDs.getSize();
std::string serviceIDsString;
for (int index = 0; index < numberOfServices; index++)
{
serviceIDsString += serviceIDs[index];
if (index < numberOfServices - 1)
{
serviceIDsString += '|';
}
}
return serviceIDsString;
}
/*
Function: getServiceIDsAsVector (static helper)
Description: Converts a string of service IDs separated by '|' into a vector.
Parameters:
- serviceIDsString: const std::string&, concatenated service IDs string
Returns:
- util::Vector<std::string>: Vector of service IDs
*/
static util::Vector<std::string> getServiceIDsAsVector(const std::string& serviceIDsString)
{
util::Vector<std::string> serviceIDs;
std::string serviceID;
std::istringstream serializedServiceIDs(serviceIDsString);
while (getline(serializedServiceIDs, serviceID, '|'))
{
serviceIDs.push_back(serviceID);
}
return serviceIDs;
}
/*
Function: serialize
Description: Serializes the combo package into a CSV-formatted string.
Parameters:
- None
Returns:
- std::string: Serialized combo package record
*/
std::string ComboPackage::serialize() const
{
std::ostringstream serializedComboPackage;
serializedComboPackage << m_id << ','
<< m_packageName << ','
<< m_discountPercentage << ','
<< getServiceIDsAsString(m_serviceIDs) << ','
<< util::getStateString(m_status);
return serializedComboPackage.str();
}
/*
Function: deserialize
Description: Deserializes a CSV-formatted string into a ComboPackage object.
Parameters:
- record: const std::string&, serialized combo package record
Returns:
- ComboPackage*: Pointer to the deserialized ComboPackage object
Throws:
- std::runtime_error if data is invalid
*/
ComboPackage* ComboPackage::deserialize(const std::string& record)
{
std::string id, packageName;
std::string discountPercentageString, serviceIDsString, statusString;
double discountPercentage;
std::istringstream serializedComboPackage(record);
getline(serializedComboPackage, id, ',');
getline(serializedComboPackage, packageName, ',');
getline(serializedComboPackage, discountPercentageString, ',');
getline(serializedComboPackage, serviceIDsString, ',');
getline(serializedComboPackage, statusString, ',');
try
{
discountPercentage = std::stod(discountPercentageString);
}
catch (...)
{
throw std::runtime_error("Invalid combo package data");
}
util::Vector<std::string> serviceIDs = getServiceIDsAsVector(serviceIDsString);
util::State status = util::getState(statusString);
return Factory::getObject<ComboPackage>(
id,
packageName,
discountPercentage,
serviceIDs,
status
);
}
/*
Function: getHeaders
Description: Retrieves the CSV headers for combo package serialization.
Parameters:
- None
Returns:
- std::string: Header string ("ID,PackageName,DiscountPercentage,ServiceIDs,Status")
*/
std::string ComboPackage::getHeaders()
{
return "ID,PackageName,DiscountPercentage,ServiceIDs,Status";
}