519 lines
14 KiB
C++
519 lines
14 KiB
C++
/*
|
|
File: ServiceBooking.cpp
|
|
Description: Implementation file containing the method definitions of the
|
|
ServiceBooking class, including constructors, getters, and setters
|
|
for booking attributes.
|
|
Author: Trenser
|
|
Date:19-May-2026
|
|
*/
|
|
#include <stdexcept>
|
|
#include <sstream>
|
|
#include "ServiceBooking.h"
|
|
#include "Service.h"
|
|
#include "Enums.h"
|
|
#include "Factory.h"
|
|
#include "StringHelper.h"
|
|
|
|
int ServiceBooking::m_uid = 0;
|
|
|
|
/*
|
|
Function: ServiceBooking
|
|
Description: Default constructor that initializes a new service booking with a unique ID,
|
|
null customer, and zero discount percentage.
|
|
Parameters: None
|
|
Returns: A new ServiceBooking object.
|
|
*/
|
|
ServiceBooking::ServiceBooking()
|
|
: m_id("SRV" + std::to_string(++m_uid)),
|
|
m_customer(nullptr),
|
|
m_assignedTechnician(nullptr),
|
|
m_discountPercentage(0.0) {}
|
|
|
|
/*
|
|
Function: ServiceBooking
|
|
Description: Parameterized constructor that initializes a new service booking with a unique ID and specified details.
|
|
Parameters:
|
|
- id: Booking ID string.
|
|
- status: Current status of the booking (e.g., PENDING, COMPLETED).
|
|
- services: Map of services included in the booking.
|
|
- customerId: ID of the customer.
|
|
- customer: Pointer to the User object representing the customer.
|
|
- vehicleNumber: Vehicle registration number.
|
|
- vehicleBrand: Brand of the vehicle.
|
|
- vehicleModel: Model of the vehicle.
|
|
- discountPercentage: Discount applied to the booking.
|
|
Returns:
|
|
- A new ServiceBooking object.
|
|
*/
|
|
ServiceBooking::ServiceBooking(
|
|
util::ServiceJobStatus status,
|
|
const util::Map<std::string,
|
|
Service*>& services,
|
|
const std::string& customerId,
|
|
User* customer,
|
|
const std::string& vehicleNumber,
|
|
const std::string& vehicleBrand,
|
|
const std::string& vehicleModel,
|
|
double discountPercentage
|
|
)
|
|
: m_id("SRV" + std::to_string(++m_uid)),
|
|
m_status(status),
|
|
m_services(services),
|
|
m_customerId(customerId),
|
|
m_customer(customer),
|
|
m_vehicleNumber(vehicleNumber),
|
|
m_vehicleBrand(vehicleBrand),
|
|
m_vehicleModel(vehicleModel),
|
|
m_assignedTechnicianId(""),
|
|
m_assignedTechnician(nullptr),
|
|
m_discountPercentage(discountPercentage)
|
|
{
|
|
int numberOfServices = m_services.getSize();
|
|
auto servicePointers = m_services.getValues();
|
|
for (int index = 0; index < numberOfServices; index++)
|
|
{
|
|
m_serviceIDs.push_back(servicePointers[index]->getId());
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: ServiceBooking (parameterized constructor with ID)
|
|
Description: Initializes a service booking with an existing ID, status, service IDs,
|
|
customer details, vehicle details, technician ID, and discount percentage.
|
|
Updates UID tracking based on ID.
|
|
Parameters:
|
|
- id: const std::string&, unique booking ID
|
|
- status: util::ServiceJobStatus, job status of the booking
|
|
- serviceIDs: const util::Vector<std::string>&, IDs of booked services
|
|
- customerId: const std::string&, ID of the customer
|
|
- vehicleNumber: const std::string&, vehicle number
|
|
- vehicleBrand: const std::string&, vehicle brand
|
|
- vehicleModel: const std::string&, vehicle model
|
|
- assignedTechnicianId: const std::string&, ID of the assigned technician
|
|
- discountPercentage: double, discount applied
|
|
Returns:
|
|
- A new ServiceBooking object
|
|
*/
|
|
ServiceBooking::ServiceBooking(
|
|
const std::string& id,
|
|
util::ServiceJobStatus status,
|
|
const util::Vector<std::string>& serviceIDs,
|
|
const std::string& customerId,
|
|
const std::string& vehicleNumber,
|
|
const std::string& vehicleBrand,
|
|
const std::string& vehicleModel,
|
|
const std::string& assignedTechnicianId,
|
|
double discountPercentage
|
|
)
|
|
: m_id(id),
|
|
m_status(status),
|
|
m_serviceIDs(serviceIDs),
|
|
m_customerId(customerId),
|
|
m_customer(nullptr),
|
|
m_vehicleNumber(vehicleNumber),
|
|
m_vehicleBrand(vehicleBrand),
|
|
m_vehicleModel(vehicleModel),
|
|
m_assignedTechnicianId(assignedTechnicianId),
|
|
m_assignedTechnician(nullptr),
|
|
m_discountPercentage(discountPercentage)
|
|
{
|
|
int idNumber = util::extractNumber(m_id);
|
|
if (idNumber > m_uid)
|
|
{
|
|
m_uid = idNumber;
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: getId
|
|
Description: Retrieves the unique identifier of the service booking.
|
|
Parameter: None
|
|
Return type: const std::string&
|
|
*/
|
|
const std::string& ServiceBooking::getId() const
|
|
{
|
|
return m_id;
|
|
}
|
|
|
|
/*
|
|
Function: getStatus
|
|
Description: Retrieves the current status of the service booking.
|
|
Parameter: None
|
|
Return type: util::ServiceJobStatus
|
|
*/
|
|
util::ServiceJobStatus ServiceBooking::getStatus() const
|
|
{
|
|
return m_status;
|
|
}
|
|
|
|
/*
|
|
Function: getServiceIDs
|
|
Description: Retrieves the IDs of services booked.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- const util::Vector<std::string>&: Service IDs
|
|
*/
|
|
const util::Vector<std::string>& ServiceBooking::getServiceIDs() const
|
|
{
|
|
return m_serviceIDs;
|
|
}
|
|
|
|
/*
|
|
Function: getServices
|
|
Description: Retrieves the services associated with the booking.
|
|
Parameter: None
|
|
Return type: const util::Map<std::string, Service*>&
|
|
*/
|
|
/*
|
|
Function: getServices
|
|
Description: Retrieves the services associated with the booking.
|
|
Parameter: None
|
|
Return type: const util::Map<std::string, Service*>&
|
|
*/
|
|
const util::Map<std::string, Service*>& ServiceBooking::getServices() const
|
|
{
|
|
return m_services;
|
|
}
|
|
|
|
/*
|
|
Function: getCustomerId
|
|
Description: Retrieves the customer ID associated with the booking.
|
|
Parameter: None
|
|
Return type: const std::string&
|
|
*/
|
|
const std::string& ServiceBooking::getCustomerId() const
|
|
{
|
|
return m_customerId;
|
|
}
|
|
|
|
/*
|
|
Function: getCustomer
|
|
Description: Retrieves the customer object associated with the booking.
|
|
Parameter: None
|
|
Return type: User*
|
|
*/
|
|
User* ServiceBooking::getCustomer() const
|
|
{
|
|
return m_customer;
|
|
}
|
|
|
|
/*
|
|
Function: getVehicleNumber
|
|
Description: Retrieves the vehicle registration number for the booking.
|
|
Parameter: None
|
|
Return type: const std::string&
|
|
*/
|
|
const std::string& ServiceBooking::getVehicleNumber() const
|
|
{
|
|
return m_vehicleNumber;
|
|
}
|
|
|
|
/*
|
|
Function: getVehicleBrand
|
|
Description: Retrieves the brand of the vehicle for the booking.
|
|
Parameter: None
|
|
Return type: const std::string&
|
|
*/
|
|
const std::string& ServiceBooking::getVehicleBrand() const
|
|
{
|
|
return m_vehicleBrand;
|
|
}
|
|
|
|
/*
|
|
Function: getVehicleModel
|
|
Description: Retrieves the model of the vehicle for the booking.
|
|
Parameter: None
|
|
Return type: const std::string&
|
|
*/
|
|
const std::string& ServiceBooking::getVehicleModel() const
|
|
{
|
|
return m_vehicleModel;
|
|
}
|
|
|
|
/*
|
|
Function: getAssignedTechnicianId
|
|
Description: Retrieves the ID of the technician assigned to the booking.
|
|
Parameter: None
|
|
Return type: const std::string&
|
|
*/
|
|
const std::string& ServiceBooking::getAssignedTechnicianId() const
|
|
{
|
|
return m_assignedTechnicianId;
|
|
}
|
|
|
|
/*
|
|
Function: getAssignedTechnician
|
|
Description: Retrieves the technician object assigned to the booking.
|
|
Parameter: None
|
|
Return type: User*
|
|
*/
|
|
User* ServiceBooking::getAssignedTechnician() const
|
|
{
|
|
return m_assignedTechnician;
|
|
}
|
|
/*
|
|
Function: getDiscountPercentage
|
|
Description: Retrieves the discount percentage applied to the booking.
|
|
Parameter: None
|
|
Return type: double
|
|
*/
|
|
double ServiceBooking::getDiscountPercentage() const
|
|
{
|
|
return m_discountPercentage;
|
|
}
|
|
|
|
/*
|
|
Function: setId
|
|
Description: Sets the unique identifier of the service booking.
|
|
Parameter: const std::string& id - new booking ID
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setId(const std::string& id)
|
|
{
|
|
m_id = id;
|
|
}
|
|
|
|
/*
|
|
Function: setStatus
|
|
Description: Sets the current status of the service booking.
|
|
Parameter: const util::ServiceJobStatus& status - new booking status
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setStatus(const util::ServiceJobStatus& status)
|
|
{
|
|
m_status = status;
|
|
}
|
|
|
|
/*
|
|
Function: setServices
|
|
Description: Sets the services associated with the booking.
|
|
Parameter: const util::Map<std::string, Service*>& services - new services map
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setServices(const util::Map<std::string, Service*>& services)
|
|
{
|
|
m_services = services;
|
|
m_serviceIDs.clear();
|
|
int numberOfServices = m_services.getSize();
|
|
auto servicePointers = m_services.getValues();
|
|
for (int index = 0; index < numberOfServices; index++)
|
|
{
|
|
m_serviceIDs.push_back(servicePointers[index]->getId());
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: setCustomerId
|
|
Description: Sets the customer ID for the booking.
|
|
Parameter: const std::string& customerId - new customer ID
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setCustomerId(const std::string& customerId)
|
|
{
|
|
m_customerId = customerId;
|
|
}
|
|
|
|
/*
|
|
Function: setCustomer
|
|
Description: Sets the customer object for the booking.
|
|
Parameter: User* customer - pointer to the customer object
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setCustomer(User* customer)
|
|
{
|
|
m_customer = customer;
|
|
}
|
|
|
|
/*
|
|
Function: setVehicleNumber
|
|
Description: Sets the vehicle registration number for the booking.
|
|
Parameter: const std::string& vehicleNumber - new vehicle number
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setVehicleNumber(const std::string& vehicleNumber)
|
|
{
|
|
m_vehicleNumber = vehicleNumber;
|
|
}
|
|
|
|
/*
|
|
Function: setVehicleBrand
|
|
Description: Sets the brand of the vehicle for the booking.
|
|
Parameter: const std::string& vehicleBrand - new vehicle brand
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setVehicleBrand(const std::string& vehicleBrand)
|
|
{
|
|
m_vehicleBrand = vehicleBrand;
|
|
}
|
|
|
|
/*
|
|
Function: setVehicleModel
|
|
Description: Sets the model of the vehicle for the booking.
|
|
Parameter: const std::string& vehicleModel - new vehicle model
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setVehicleModel(const std::string& vehicleModel)
|
|
{
|
|
m_vehicleModel = vehicleModel;
|
|
}
|
|
|
|
/*
|
|
Function: setAssignedTechnicianId
|
|
Description: Sets the ID of the technician assigned to the booking.
|
|
Parameter: const std::string& assignedTechnicianId - new technician ID
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setAssignedTechnicianId(const std::string& assignedTechnicianId)
|
|
{
|
|
m_assignedTechnicianId = assignedTechnicianId;
|
|
}
|
|
|
|
/*
|
|
Function: setAssignedTechnician
|
|
Description: Sets the technician object assigned to the booking.
|
|
Parameter: User* assignedTechnician - pointer to the technician object
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setAssignedTechnician(User* assignedTechnician)
|
|
{
|
|
m_assignedTechnician = assignedTechnician;
|
|
}
|
|
|
|
/*
|
|
Function: setDiscountPercentage
|
|
Description: Sets the discount percentage for the booking.
|
|
Parameter: double discountPercentage - new discount percentage
|
|
Return type: void
|
|
*/
|
|
void ServiceBooking::setDiscountPercentage(double discountPercentage)
|
|
{
|
|
m_discountPercentage = discountPercentage;
|
|
}
|
|
|
|
/*
|
|
Function: getServiceIDsAsString (static helper)
|
|
Description: Converts a vector of service IDs into a single string separated by '|'.
|
|
Parameters:
|
|
- serviceIDs: const util::Vector<std::string>&, vector of service IDs
|
|
Returns:
|
|
- std::string: Concatenated service IDs string
|
|
*/
|
|
static std::string getServiceIDsAsString(const util::Vector<std::string>& serviceIDs)
|
|
{
|
|
int numberOfServices = serviceIDs.getSize();
|
|
std::string serviceIDsString;
|
|
for (int index = 0; index < numberOfServices; index++)
|
|
{
|
|
serviceIDsString += serviceIDs[index];
|
|
if (index < numberOfServices - 1)
|
|
{
|
|
serviceIDsString += '|';
|
|
}
|
|
}
|
|
return serviceIDsString;
|
|
}
|
|
|
|
/*
|
|
Function: getServiceIDsAsVector (static helper)
|
|
Description: Converts a string of service IDs separated by '|' into a vector.
|
|
Parameters:
|
|
- serviceIDsString: const std::string&, concatenated service IDs string
|
|
Returns:
|
|
- util::Vector<std::string>: Vector of service IDs
|
|
*/
|
|
static util::Vector<std::string> getServiceIDsAsVector(const std::string& serviceIDsString)
|
|
{
|
|
util::Vector<std::string> serviceIDs;
|
|
std::string serviceID;
|
|
std::istringstream serializedServiceIDs(serviceIDsString);
|
|
while (getline(serializedServiceIDs, serviceID, '|'))
|
|
{
|
|
serviceIDs.push_back(serviceID);
|
|
}
|
|
return serviceIDs;
|
|
}
|
|
|
|
/*
|
|
Function: serialize
|
|
Description: Serializes the service booking into a CSV-formatted string.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- std::string: Serialized booking record
|
|
*/
|
|
std::string ServiceBooking::serialize() const
|
|
{
|
|
std::ostringstream serializedBooking;
|
|
serializedBooking << m_id << ','
|
|
<< util::getServiceJobStatusString(m_status) << ','
|
|
<< getServiceIDsAsString(m_serviceIDs) << ','
|
|
<< m_customerId << ','
|
|
<< m_vehicleNumber << ','
|
|
<< m_vehicleBrand << ','
|
|
<< m_vehicleModel << ','
|
|
<< m_assignedTechnicianId << ','
|
|
<< m_discountPercentage << ',';
|
|
return serializedBooking.str();
|
|
}
|
|
|
|
/*
|
|
Function: deserialize
|
|
Description: Deserializes a CSV-formatted string into a ServiceBooking object.
|
|
Parameters:
|
|
- record: const std::string&, serialized booking record
|
|
Returns:
|
|
- ServiceBooking*: Pointer to the deserialized ServiceBooking object
|
|
Throws:
|
|
- std::runtime_error if discount percentage parsing fails
|
|
*/
|
|
ServiceBooking* ServiceBooking::deserialize(const std::string& record)
|
|
{
|
|
std::string id, customerId, vehicleNumber, vehicleBrand, vehicleModel, assignedTechnicianId;
|
|
std::string serviceJobStatusString, serviceIDsString, discountPercentageString;
|
|
double discountPercentage;
|
|
std::istringstream serializedBooking(record);
|
|
getline(serializedBooking, id, ',');
|
|
getline(serializedBooking, serviceJobStatusString, ',');
|
|
getline(serializedBooking, serviceIDsString, ',');
|
|
getline(serializedBooking, customerId, ',');
|
|
getline(serializedBooking, vehicleNumber, ',');
|
|
getline(serializedBooking, vehicleBrand, ',');
|
|
getline(serializedBooking, vehicleModel, ',');
|
|
getline(serializedBooking, assignedTechnicianId, ',');
|
|
getline(serializedBooking, discountPercentageString, ',');
|
|
util::Vector<std::string> serviceIDs = getServiceIDsAsVector(serviceIDsString);
|
|
try
|
|
{
|
|
discountPercentage = std::stod(discountPercentageString);
|
|
}
|
|
catch (...)
|
|
{
|
|
throw std::runtime_error("Invalid discount percentage");
|
|
}
|
|
util::ServiceJobStatus status = util::getServiceJobStatus(serviceJobStatusString);
|
|
return Factory::getObject<ServiceBooking>(
|
|
id,
|
|
status,
|
|
serviceIDs,
|
|
customerId,
|
|
vehicleNumber,
|
|
vehicleBrand,
|
|
vehicleModel,
|
|
assignedTechnicianId,
|
|
discountPercentage
|
|
);
|
|
}
|
|
|
|
/*
|
|
Function: getHeaders
|
|
Description: Retrieves the CSV headers for service booking serialization.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- std::string: Header string ("ID,Status,ServiceIDs,CustomerID,VehicleNumber,VehicleBrand,VehicleModel,AssignedTechnicianID,DiscountPercentage")
|
|
*/
|
|
std::string ServiceBooking::getHeaders()
|
|
{
|
|
return "ID,Status,ServiceIDs,CustomerID,VehicleNumber,VehicleBrand,VehicleModel,AssignedTechnicianID,DiscountPercentage";
|
|
} |