4b76cae358
<UserStory> 1955: Model Refactoring</UserStory> UserStory #1955 <Changes> 1. Replaced CSV-based serialization and deserialization in ComboPackage, JobCard, Service, and ServiceBooking models with fixed-size SerializedRecord structures for shared memory storage. 2. Implemented serialize() methods to convert objects into SerializedComboPackage, SerializedJobCard, SerializedService, and SerializedServiceBooking records. 3. Implemented deserialize() methods to reconstruct objects directly from SerializedRecord types instead of parsing CSV strings. 4. Updated model class interfaces to use SerializedRecord types, removing legacy CSV serialization APIs and header generation functions. 5. Added SerializedRecords.h dependencies and forward declarations for Serialized structures across affected models. </Changes> <Test> N/A </Test> <Review> Sreeja Reghukumar, please review </Review>
309 lines
8.6 KiB
C++
309 lines
8.6 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 "SerializedRecords.h"
|
|
#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 ComboPackage object into a SerializedComboPackage record.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- SerializedComboPackage: Serialized representation of the combo package
|
|
*/
|
|
SerializedComboPackage ComboPackage::serialize() const
|
|
{
|
|
SerializedComboPackage serialized = {};
|
|
strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
|
|
strcpy_s(serialized.packageName, sizeof(serialized.packageName), m_packageName.c_str());
|
|
strcpy_s(serialized.serviceIDs, sizeof(serialized.serviceIDs), getServiceIDsAsString(m_serviceIDs).c_str());
|
|
serialized.discountPercentage = m_discountPercentage;
|
|
serialized.status = m_status;
|
|
return serialized;
|
|
}
|
|
|
|
/*
|
|
Function: deserialize
|
|
Description: Deserializes a SerializedComboPackage record into a ComboPackage object.
|
|
Parameters:
|
|
- serializedComboPackage: const SerializedComboPackage&, serialized combo package record
|
|
Returns:
|
|
- ComboPackage*: Pointer to the deserialized ComboPackage object
|
|
*/
|
|
ComboPackage* ComboPackage::deserialize(const SerializedComboPackage& serializedComboPackage)
|
|
{
|
|
util::Vector<std::string> serviceIDs = getServiceIDsAsVector(serializedComboPackage.serviceIDs);
|
|
return Factory::getObject<ComboPackage>(
|
|
serializedComboPackage.id,
|
|
serializedComboPackage.packageName,
|
|
serializedComboPackage.discountPercentage,
|
|
serviceIDs,
|
|
serializedComboPackage.status);
|
|
} |