Files
Training-VehicleService-May26/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/InventoryItem.cpp
T
Avinash Rajesh b98062d45c 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>
2026-06-12 15:30:34 +05:30

245 lines
5.8 KiB
C++

/*
File: InventoryItem.cpp
Description: Implements the InventoryItem class which represents an inventory item in the Vehicle Service Management System.
Provides constructors, accessors, and mutators for item details such as ID, part name, quantity, price, and state.
Author: Trenser
Date: 19-May-2026
*/
#include <sstream>
#include <stdexcept>
#include "SerializedRecords.h"
#include "Factory.h"
#include "StringHelper.h"
#include "InventoryItem.h"
int InventoryItem::m_uid = 0;
/*
Function: InventoryItem
Description: Default constructor that initializes a new inventory item with a unique ID,
active state, zero quantity, and zero price.
Parameters:
- None
Returns:
- A new InventoryItem object.
*/
InventoryItem::InventoryItem()
: m_id("IIM" + std::to_string(++m_uid)),
m_quantity(0),
m_status(util::State::ACTIVE),
m_price(0.0) {
}
/*
Function: InventoryItem
Description: Parameterized constructor that initializes a new inventory item with a unique ID,
specified part name, quantity, price, and active state.
Parameters:
- partName: Name of the inventory item.
- quantity: Initial quantity of the item.
- price: Price of the item.
Returns:
- A new InventoryItem object.
*/
InventoryItem::InventoryItem(const std::string& partName, int quantity, double price)
: m_id("IIM" + std::to_string(++m_uid)),
m_partName(partName),
m_quantity(quantity),
m_status(util::State::ACTIVE),
m_price(price) {
}
/*
Function: InventoryItem (parameterized constructor with ID)
Description: Initializes an inventory item with an existing ID, part name, quantity,
price, and state. Updates UID tracking based on ID.
Parameters:
- id: const std::string&, unique ID of the item
- partName: const std::string&, name of the part
- quantity: int, quantity of the part
- price: double, price of the part
- status: util::State, state of the item (ACTIVE/INACTIVE)
Returns:
- A new InventoryItem object
*/
InventoryItem::InventoryItem(const std::string& id, const std::string& partName, int quantity, double price, util::State status)
: m_id(id),
m_partName(partName),
m_quantity(quantity),
m_status(status),
m_price(price)
{
int idNumber = util::extractNumber(m_id);
if (idNumber > m_uid)
{
m_uid = idNumber;
}
}
/*
Function: getId
Description: Retrieves the unique ID of the inventory item.
Parameters:
- None
Returns:
- const std::string& representing the item ID.
*/
const std::string& InventoryItem::getId() const
{
return m_id;
}
/*
Function: getPartName
Description: Retrieves the part name of the inventory item.
Parameters:
- None
Returns:
- const std::string& representing the part name.
*/
const std::string& InventoryItem::getPartName() const
{
return m_partName;
}
/*
Function: getQuantity
Description: Retrieves the current quantity of the inventory item.
Parameters:
- None
Returns:
- int representing the quantity.
*/
int InventoryItem::getQuantity() const
{
return m_quantity;
}
/*
Function: getPrice
Description: Retrieves the price of the inventory item.
Parameters:
- None
Returns:
- double representing the price.
*/
double InventoryItem::getPrice() const
{
return m_price;
}
/*
Function: getState
Description: Retrieves the current state (ACTIVE/INACTIVE) of the inventory item.
Parameters:
- None
Returns:
- util::State representing the item state.
*/
util::State InventoryItem::getState() const
{
return m_status;
}
/*
Function: setId
Description: Sets the unique ID of the inventory item.
Parameters:
- id: New ID string.
Returns:
- void
*/
void InventoryItem::setId(const std::string& id)
{
m_id = id;
}
/*
Function: setPartName
Description: Sets the part name of the inventory item.
Parameters:
- partName: New part name string.
Returns:
- void
*/
void InventoryItem::setPartName(const std::string& partName)
{
m_partName = partName;
}
/*
Function: setQuantity
Description: Sets the quantity of the inventory item.
Parameters:
- quantity: New quantity value.
Returns:
- void
*/
void InventoryItem::setQuantity(int quantity)
{
m_quantity = quantity;
}
/*
Function: setPrice
Description: Sets the price of the inventory item.
Parameters:
- price: New price value.
Returns:
- void
*/
void InventoryItem::setPrice(double price)
{
m_price = price;
}
/*
Function: setState
Description: Sets the state (ACTIVE/INACTIVE) of the inventory item.
Parameters:
- status: New state value.
Returns:
- void
*/
void InventoryItem::setState(util::State status)
{
m_status = status;
}
/*
Function: serialize
Description: Serializes the InventoryItem object into a SerializedInventoryItem record.
Parameters:
- None
Returns:
- SerializedInventoryItem: Serialized representation of the inventory item
*/
SerializedInventoryItem InventoryItem::serialize() const
{
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 SerializedInventoryItem record into an InventoryItem object.
Parameters:
- serializedInventoryItem: const SerializedInventoryItem&, serialized inventory item record
Returns:
- InventoryItem*: Pointer to the deserialized InventoryItem object
*/
InventoryItem* InventoryItem::deserialize(const SerializedInventoryItem& serializedInventoryItem)
{
return Factory::getObject<InventoryItem>(
serializedInventoryItem.id,
serializedInventoryItem.partName,
serializedInventoryItem.quantity,
serializedInventoryItem.price,
serializedInventoryItem.status);
}