278 lines
6.5 KiB
C++
278 lines
6.5 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 "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 inventory item into a CSV-formatted string.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- std::string: Serialized inventory item record
|
|
*/
|
|
std::string InventoryItem::serialize() const
|
|
{
|
|
std::ostringstream serializedInventoryItem;
|
|
serializedInventoryItem << m_id << ','
|
|
<< m_partName << ','
|
|
<< m_quantity << ','
|
|
<< m_price << ','
|
|
<< util::getStateString(m_status);
|
|
return serializedInventoryItem.str();
|
|
}
|
|
|
|
/*
|
|
Function: deserialize
|
|
Description: Deserializes a CSV-formatted string into an InventoryItem object.
|
|
Parameters:
|
|
- record: const std::string&, 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)
|
|
{
|
|
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>(
|
|
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";
|
|
} |