From b98062d45c5585a1bbf2c0042a81f1af21645283 Mon Sep 17 00:00:00 2001 From: Avinash Rajesh Date: Fri, 12 Jun 2026 15:30:34 +0530 Subject: [PATCH] Implement Model Refactoring 1957: Model Refactoring UserStory #1957 1. Added SerializedRecords.h dependency and forward declaration for SerializedInventoryItem to support fixed-size record storage. 2. Replaced CSV-based serialization in InventoryItem with serialize() method returning SerializedInventoryItem structure. 3. Replaced CSV-based deserialization logic with deserialize() method that reconstructs InventoryItem directly from SerializedInventoryItem record. 4. Removed legacy CSV parsing, header generation, and exception handling tied to string-based serialization. 5. Updated InventoryItem class interface in InventoryItem.h to use SerializedInventoryItem types instead of std::string serialization APIs. N/A Sreeja Reghukumar --- .../models/InventoryItem.cpp | 79 ++++++------------- .../models/InventoryItem.h | 8 +- 2 files changed, 27 insertions(+), 60 deletions(-) diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.cpp index f34b8a5..67fb7ba 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.cpp @@ -8,6 +8,7 @@ Date: 19-May-2026 #include #include +#include "SerializedRecords.h" #include "Factory.h" #include "StringHelper.h" #include "InventoryItem.h" @@ -27,7 +28,8 @@ InventoryItem::InventoryItem() : m_id("IIM" + std::to_string(++m_uid)), m_quantity(0), m_status(util::State::ACTIVE), - m_price(0.0) {} + m_price(0.0) { +} /* Function: InventoryItem @@ -45,7 +47,8 @@ InventoryItem::InventoryItem(const std::string& partName, int quantity, double p m_partName(partName), m_quantity(quantity), m_status(util::State::ACTIVE), - m_price(price) {} + m_price(price) { +} /* Function: InventoryItem (parameterized constructor with ID) @@ -206,73 +209,37 @@ void InventoryItem::setState(util::State status) /* Function: serialize -Description: Serializes the inventory item into a CSV-formatted string. +Description: Serializes the InventoryItem object into a SerializedInventoryItem record. Parameters: - None Returns: - - std::string: Serialized inventory item record + - SerializedInventoryItem: Serialized representation of the inventory item */ -std::string InventoryItem::serialize() const +SerializedInventoryItem InventoryItem::serialize() const { - std::ostringstream serializedInventoryItem; - serializedInventoryItem << m_id << ',' - << m_partName << ',' - << m_quantity << ',' - << m_price << ',' - << util::getStateString(m_status); - return serializedInventoryItem.str(); + SerializedInventoryItem serialized = {}; + strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str()); + strcpy_s(serialized.partName, sizeof(serialized.partName), m_partName.c_str()); + serialized.quantity = m_quantity; + serialized.price = m_price; + serialized.status = m_status; + return serialized; } /* Function: deserialize -Description: Deserializes a CSV-formatted string into an InventoryItem object. +Description: Deserializes a SerializedInventoryItem record into an InventoryItem object. Parameters: - - record: const std::string&, serialized inventory item record + - serializedInventoryItem: const SerializedInventoryItem&, serialized inventory item record Returns: - InventoryItem*: Pointer to the deserialized InventoryItem object -Throws: - - std::runtime_error if data is invalid */ -InventoryItem* InventoryItem::deserialize(const std::string& record) +InventoryItem* InventoryItem::deserialize(const SerializedInventoryItem& serializedInventoryItem) { - std::string id, partName; - std::string quantityString, priceString, statusString; - int quantity; - double price; - std::istringstream serializedInventoryItem(record); - getline(serializedInventoryItem, id, ','); - getline(serializedInventoryItem, partName, ','); - getline(serializedInventoryItem, quantityString, ','); - getline(serializedInventoryItem, priceString, ','); - getline(serializedInventoryItem, statusString, ','); - try - { - quantity = std::stoi(quantityString); - price = std::stod(priceString); - } - catch (...) - { - throw std::runtime_error("Invalid inventory item data"); - } - util::State status = util::getState(statusString); return Factory::getObject( - id, - partName, - quantity, - price, - status - ); -} - -/* -Function: getHeaders -Description: Retrieves the CSV headers for inventory item serialization. -Parameters: - - None -Returns: - - std::string: Header string ("ID,PartName,Quantity,Price,Status") -*/ -std::string InventoryItem::getHeaders() -{ - return "ID,PartName,Quantity,Price,Status"; + serializedInventoryItem.id, + serializedInventoryItem.partName, + serializedInventoryItem.quantity, + serializedInventoryItem.price, + serializedInventoryItem.status); } \ No newline at end of file diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.h b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.h index 5808e8f..90b716a 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.h +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.h @@ -6,11 +6,12 @@ Author: Trenser Date: 19-May-2026 */ - #pragma once #include #include "Enums.h" +struct SerializedInventoryItem; + class InventoryItem { private: @@ -34,7 +35,6 @@ public: void setQuantity(int quantity); void setPrice(double price); void setState(util::State status); - std::string serialize() const; - static InventoryItem* deserialize(const std::string&); - static std::string getHeaders(); + SerializedInventoryItem serialize() const; + static InventoryItem* deserialize(const SerializedInventoryItem&); }; \ No newline at end of file