Implement Model Refactoring
<UserStory> 1957: Model Refactoring </UserStory> UserStory #1957 <Changes> 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. </Changes> <Test> N/A </Test> <Review> Sreeja Reghukumar </Review>
This commit is contained in:
+23
-56
@@ -8,6 +8,7 @@ Date: 19-May-2026
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include "SerializedRecords.h"
|
||||||
#include "Factory.h"
|
#include "Factory.h"
|
||||||
#include "StringHelper.h"
|
#include "StringHelper.h"
|
||||||
#include "InventoryItem.h"
|
#include "InventoryItem.h"
|
||||||
@@ -27,7 +28,8 @@ InventoryItem::InventoryItem()
|
|||||||
: m_id("IIM" + std::to_string(++m_uid)),
|
: m_id("IIM" + std::to_string(++m_uid)),
|
||||||
m_quantity(0),
|
m_quantity(0),
|
||||||
m_status(util::State::ACTIVE),
|
m_status(util::State::ACTIVE),
|
||||||
m_price(0.0) {}
|
m_price(0.0) {
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: InventoryItem
|
Function: InventoryItem
|
||||||
@@ -45,7 +47,8 @@ InventoryItem::InventoryItem(const std::string& partName, int quantity, double p
|
|||||||
m_partName(partName),
|
m_partName(partName),
|
||||||
m_quantity(quantity),
|
m_quantity(quantity),
|
||||||
m_status(util::State::ACTIVE),
|
m_status(util::State::ACTIVE),
|
||||||
m_price(price) {}
|
m_price(price) {
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: InventoryItem (parameterized constructor with ID)
|
Function: InventoryItem (parameterized constructor with ID)
|
||||||
@@ -206,73 +209,37 @@ void InventoryItem::setState(util::State status)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Function: serialize
|
Function: serialize
|
||||||
Description: Serializes the inventory item into a CSV-formatted string.
|
Description: Serializes the InventoryItem object into a SerializedInventoryItem record.
|
||||||
Parameters:
|
Parameters:
|
||||||
- None
|
- None
|
||||||
Returns:
|
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 serialized = {};
|
||||||
serializedInventoryItem << m_id << ','
|
strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
|
||||||
<< m_partName << ','
|
strcpy_s(serialized.partName, sizeof(serialized.partName), m_partName.c_str());
|
||||||
<< m_quantity << ','
|
serialized.quantity = m_quantity;
|
||||||
<< m_price << ','
|
serialized.price = m_price;
|
||||||
<< util::getStateString(m_status);
|
serialized.status = m_status;
|
||||||
return serializedInventoryItem.str();
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: deserialize
|
Function: deserialize
|
||||||
Description: Deserializes a CSV-formatted string into an InventoryItem object.
|
Description: Deserializes a SerializedInventoryItem record into an InventoryItem object.
|
||||||
Parameters:
|
Parameters:
|
||||||
- record: const std::string&, serialized inventory item record
|
- serializedInventoryItem: const SerializedInventoryItem&, serialized inventory item record
|
||||||
Returns:
|
Returns:
|
||||||
- InventoryItem*: Pointer to the deserialized InventoryItem object
|
- 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<InventoryItem>(
|
return Factory::getObject<InventoryItem>(
|
||||||
id,
|
serializedInventoryItem.id,
|
||||||
partName,
|
serializedInventoryItem.partName,
|
||||||
quantity,
|
serializedInventoryItem.quantity,
|
||||||
price,
|
serializedInventoryItem.price,
|
||||||
status
|
serializedInventoryItem.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";
|
|
||||||
}
|
}
|
||||||
@@ -6,11 +6,12 @@ Author: Trenser
|
|||||||
Date: 19-May-2026
|
Date: 19-May-2026
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "Enums.h"
|
#include "Enums.h"
|
||||||
|
|
||||||
|
struct SerializedInventoryItem;
|
||||||
|
|
||||||
class InventoryItem
|
class InventoryItem
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@@ -34,7 +35,6 @@ public:
|
|||||||
void setQuantity(int quantity);
|
void setQuantity(int quantity);
|
||||||
void setPrice(double price);
|
void setPrice(double price);
|
||||||
void setState(util::State status);
|
void setState(util::State status);
|
||||||
std::string serialize() const;
|
SerializedInventoryItem serialize() const;
|
||||||
static InventoryItem* deserialize(const std::string&);
|
static InventoryItem* deserialize(const SerializedInventoryItem&);
|
||||||
static std::string getHeaders();
|
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user