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>
305 lines
8.6 KiB
C++
305 lines
8.6 KiB
C++
/*
|
|
File: Service.cpp
|
|
Description: Implements the Service class which represents a vehicle service in the Vehicle Service Management System.
|
|
Provides constructors, accessors, and mutators for service details such as ID, name, required inventory items,
|
|
labor cost, and state.
|
|
Author: Trenser
|
|
Date: 19-May-2026
|
|
*/
|
|
|
|
#include <sstream>
|
|
#include "SerializedRecords.h"
|
|
#include "Service.h"
|
|
#include "InventoryItem.h"
|
|
#include "StringHelper.h"
|
|
#include "Factory.h"
|
|
|
|
int Service::m_uid = 0;
|
|
|
|
/*
|
|
Function: Service
|
|
Description: Default constructor that initializes a new service with a unique ID,
|
|
active state, and zero labor cost.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- A new Service object.
|
|
*/
|
|
Service::Service()
|
|
: m_id("SRV" + std::to_string(++m_uid)),
|
|
m_status(util::State::ACTIVE),
|
|
m_laborCost(0.0) {
|
|
}
|
|
|
|
/*
|
|
Function: Service
|
|
Description: Parameterized constructor that initializes a new service with a unique ID and specified details.
|
|
Parameters:
|
|
- name: Name of the service.
|
|
- requiredInventoryItems: Map of inventory items required for the service.
|
|
- laborCost: Labor cost associated with the service.
|
|
Returns:
|
|
- A new Service object.
|
|
*/
|
|
Service::Service(const std::string& name, const util::Map<std::string, InventoryItem*>& requiredInventoryItems, double laborCost)
|
|
: m_id("SRV" + std::to_string(++m_uid)),
|
|
m_name(name),
|
|
m_requiredInventoryItems(requiredInventoryItems),
|
|
m_status(util::State::ACTIVE),
|
|
m_laborCost(laborCost)
|
|
{
|
|
int numberOfInventoryItems = m_requiredInventoryItems.getSize();
|
|
auto inventoryItemPointers = m_requiredInventoryItems.getValues();
|
|
for (int index = 0; index < numberOfInventoryItems; index++)
|
|
{
|
|
m_requiredInventoryItemIDs.push_back(inventoryItemPointers[index]->getId());
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: Service (parameterized constructor with ID)
|
|
Description: Initializes a service with an existing ID, name, inventory item IDs,
|
|
labor cost, and state. Updates UID tracking based on ID.
|
|
Parameters:
|
|
- id: const std::string&, unique service ID
|
|
- name: const std::string&, name of the service
|
|
- requiredInventoryItemIDs: const util::Vector<std::string>&, IDs of required inventory items
|
|
- laborCost: double, labor cost of the service
|
|
- status: util::State, state of the service (ACTIVE/INACTIVE)
|
|
Returns:
|
|
- A new Service object
|
|
*/
|
|
Service::Service(const std::string& id, const std::string& name, const util::Vector<std::string>& requiredInventoryItemIDs, double laborCost, util::State status)
|
|
: m_id(id),
|
|
m_name(name),
|
|
m_requiredInventoryItemIDs(requiredInventoryItemIDs),
|
|
m_status(status),
|
|
m_laborCost(laborCost)
|
|
{
|
|
int idNumber = util::extractNumber(m_id);
|
|
if (idNumber > m_uid)
|
|
{
|
|
m_uid = idNumber;
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: getId
|
|
Description: Retrieves the unique ID of the service.
|
|
Returns:
|
|
- const std::string& representing the service ID.
|
|
*/
|
|
const std::string& Service::getId() const
|
|
{
|
|
return m_id;
|
|
}
|
|
|
|
/*
|
|
Function: getName
|
|
Description: Retrieves the name of the service.
|
|
Returns:
|
|
- const std::string& representing the service name.
|
|
*/
|
|
const std::string& Service::getName() const
|
|
{
|
|
return m_name;
|
|
}
|
|
|
|
/*
|
|
Function: getRequiredInventoryItemIDs
|
|
Description: Retrieves the IDs of required inventory items for the service.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- const util::Vector<std::string>&: Inventory item IDs
|
|
*/
|
|
const util::Vector<std::string>& Service::getRequiredInventoryItemIDs() const
|
|
{
|
|
return m_requiredInventoryItemIDs;
|
|
}
|
|
|
|
/*
|
|
Function: getRequiredInventoryItems
|
|
Description: Retrieves the map of inventory items required for the service.
|
|
Returns:
|
|
- const util::Map<std::string, InventoryItem*>& representing the required inventory items.
|
|
*/
|
|
const util::Map<std::string, InventoryItem*>& Service::getRequiredInventoryItems() const
|
|
{
|
|
return m_requiredInventoryItems;
|
|
}
|
|
|
|
/*
|
|
Function: getLaborCost
|
|
Description: Retrieves the labor cost associated with the service.
|
|
Returns:
|
|
- double representing the labor cost.
|
|
*/
|
|
double Service::getLaborCost() const
|
|
{
|
|
return m_laborCost;
|
|
}
|
|
|
|
/*
|
|
Function: getState
|
|
Description: Retrieves the current state (ACTIVE/INACTIVE) of the service.
|
|
Returns:
|
|
- util::State representing the service state.
|
|
*/
|
|
util::State Service::getState() const
|
|
{
|
|
return m_status;
|
|
}
|
|
|
|
/*
|
|
Function: setId
|
|
Description: Sets the unique ID of the service.
|
|
Parameters:
|
|
- id: New service ID string.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Service::setId(const std::string& id)
|
|
{
|
|
m_id = id;
|
|
}
|
|
|
|
/*
|
|
Function: setName
|
|
Description: Sets the name of the service.
|
|
Parameters:
|
|
- name: New service name string.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Service::setName(const std::string& name)
|
|
{
|
|
m_name = name;
|
|
}
|
|
|
|
/*
|
|
Function: setRequiredInventoryItems
|
|
Description: Sets the inventory items required for the service.
|
|
Parameters:
|
|
- requiredInventoryItems: Map of inventory items.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Service::setRequiredInventoryItems(const util::Map<std::string, InventoryItem*>& requiredInventoryItems)
|
|
{
|
|
m_requiredInventoryItems = requiredInventoryItems;
|
|
m_requiredInventoryItemIDs.clear();
|
|
int numberOfRequiredInventoryItems = m_requiredInventoryItems.getSize();
|
|
auto inventoryItemPointers = m_requiredInventoryItems.getValues();
|
|
for (int index = 0; index < numberOfRequiredInventoryItems; index++)
|
|
{
|
|
m_requiredInventoryItemIDs.push_back(inventoryItemPointers[index]->getId());
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: setLaborCost
|
|
Description: Sets the labor cost for the service.
|
|
Parameters:
|
|
- laborCost: New labor cost value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Service::setLaborCost(double laborCost)
|
|
{
|
|
m_laborCost = laborCost;
|
|
}
|
|
|
|
/*
|
|
Function: setState
|
|
Description: Sets the state (ACTIVE/INACTIVE) of the service.
|
|
Parameters:
|
|
- status: New state value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Service::setState(util::State status)
|
|
{
|
|
m_status = status;
|
|
}
|
|
|
|
/*
|
|
Function: getInventoryItemIDsAsString (static helper)
|
|
Description: Converts a vector of inventory item IDs into a single string separated by '|'.
|
|
Parameters:
|
|
- inventoryItemIds: const util::Vector<std::string>&, vector of inventory item IDs
|
|
Returns:
|
|
- std::string: Concatenated inventory item IDs string
|
|
*/
|
|
static std::string getInventoryItemIDsAsString(const util::Vector<std::string>& inventoryItemIds)
|
|
{
|
|
int numberOfInventoryItems = inventoryItemIds.getSize();
|
|
std::string inventoryItemIDs;
|
|
for (int index = 0; index < numberOfInventoryItems; index++)
|
|
{
|
|
inventoryItemIDs += inventoryItemIds[index];
|
|
if (index < numberOfInventoryItems - 1)
|
|
{
|
|
inventoryItemIDs += '|';
|
|
}
|
|
}
|
|
return inventoryItemIDs;
|
|
}
|
|
|
|
/*
|
|
Function: getInventoryItemIDsAsVector (static helper)
|
|
Description: Converts a string of inventory item IDs separated by '|' into a vector.
|
|
Parameters:
|
|
- inventoryItemIDsString: const std::string&, concatenated inventory item IDs string
|
|
Returns:
|
|
- util::Vector<std::string>: Vector of inventory item IDs
|
|
*/
|
|
static util::Vector<std::string> getInventoryItemIDsAsVector(const std::string& inventoryItemIDsString)
|
|
{
|
|
util::Vector<std::string> inventoryItemIDs;
|
|
std::string inventoryItemID;
|
|
std::istringstream serializedInventoryItemIDs(inventoryItemIDsString);
|
|
while (getline(serializedInventoryItemIDs, inventoryItemID, '|'))
|
|
{
|
|
inventoryItemIDs.push_back(inventoryItemID);
|
|
}
|
|
return inventoryItemIDs;
|
|
}
|
|
|
|
/*
|
|
Function: serialize
|
|
Description: Serializes the Service object into a SerializedService record.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- SerializedService: Serialized representation of the service
|
|
*/
|
|
SerializedService Service::serialize() const
|
|
{
|
|
SerializedService serialized = {};
|
|
strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
|
|
strcpy_s(serialized.name, sizeof(serialized.name), m_name.c_str());
|
|
strcpy_s(serialized.inventoryItemIDs, sizeof(serialized.inventoryItemIDs), getInventoryItemIDsAsString(m_requiredInventoryItemIDs).c_str());
|
|
serialized.laborCost = m_laborCost;
|
|
serialized.status = m_status;
|
|
return serialized;
|
|
}
|
|
|
|
/*
|
|
Function: deserialize
|
|
Description: Deserializes a SerializedService record into a Service object.
|
|
Parameters:
|
|
- serializedService: const SerializedService&, serialized service record
|
|
Returns:
|
|
- Service*: Pointer to the deserialized Service object
|
|
*/
|
|
Service* Service::deserialize(const SerializedService& serializedService)
|
|
{
|
|
util::Vector<std::string> inventoryItemIDs = getInventoryItemIDsAsVector(serializedService.inventoryItemIDs);
|
|
return Factory::getObject<Service>(
|
|
serializedService.id,
|
|
serializedService.name,
|
|
inventoryItemIDs,
|
|
serializedService.laborCost,
|
|
serializedService.status);
|
|
} |