7d51eeccdf
- implement FileDatabase<T> for loading and saving objects - add serialize / deserialize methods to all model classes - add foreign key relationships among classes alongside object ownership - replace object references and usernames with persistent IDs - replace users map from username key to userId key - load full state on startup and persist state on shutdown - re-link users, restaurants, menu items, orders, items, and assignments after load - fix: add order to controller m_orders - refactor: store shared pointer to MenuItem in Item, not a reference - refactor: store shared pointer to Order in DeliveryAssignment - refactor: remove customer username from Order - add initial snapshot files for users, restaurants, and menu items - add empty snapshot files for remaining persisted entities
120 lines
2.4 KiB
C++
120 lines
2.4 KiB
C++
/*
|
|
Author: Joel Mathew Thomas
|
|
Date: 18-12-2026
|
|
*/
|
|
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include "Order.h"
|
|
|
|
int Order::m_uid = 0;
|
|
|
|
|
|
Order::Order(int id, OrderStatus status, int restaurantId,int customerId):
|
|
m_id(id),
|
|
m_status(status),
|
|
m_customerId(customerId),
|
|
m_restaurantId(restaurantId)
|
|
{
|
|
if (id > m_uid)
|
|
{
|
|
m_uid = id;
|
|
}
|
|
}
|
|
|
|
int Order::getId() const
|
|
{
|
|
return m_id;
|
|
}
|
|
|
|
void Order::addItem(const Item& item)
|
|
{
|
|
m_items.push_back(std::make_shared<Item>(item));
|
|
}
|
|
|
|
items& Order::getItems()
|
|
{
|
|
return m_items;
|
|
}
|
|
|
|
double Order::getTotal() const
|
|
{
|
|
double total = 0;
|
|
for (auto& itemPointer : m_items)
|
|
{
|
|
total += itemPointer->getMenuItem()->getPrice() * itemPointer->getQuantity();
|
|
}
|
|
return total;
|
|
}
|
|
|
|
OrderStatus Order::getStatus() const
|
|
{
|
|
return m_status;
|
|
}
|
|
|
|
void Order::setStatus(const OrderStatus& orderStatus)
|
|
{
|
|
m_status = orderStatus;
|
|
}
|
|
|
|
int Order::getCustomerId() const
|
|
{
|
|
return m_customerId;
|
|
}
|
|
|
|
int Order::getRestaurantId() const
|
|
{
|
|
return m_restaurantId;
|
|
}
|
|
|
|
std::string Order::serialize() const
|
|
{
|
|
std::ostringstream serializedOrder;
|
|
serializedOrder << m_id << '|'
|
|
<< static_cast<int>(m_status) << '|'
|
|
<< m_customerId << '|'
|
|
<< m_restaurantId;
|
|
return serializedOrder.str();
|
|
}
|
|
|
|
std::shared_ptr<Order> Order::deserialize(const std::string& record)
|
|
{
|
|
int id, customerId, restaurantId;
|
|
OrderStatus status;
|
|
std::string token;
|
|
std::istringstream serializedOrder(record);
|
|
getline(serializedOrder, token, '|');
|
|
try {
|
|
id = std::stoi(token);
|
|
}
|
|
catch (...)
|
|
{
|
|
throw std::runtime_error("Invalid Order ID in snapshot");
|
|
}
|
|
getline(serializedOrder, token, '|');
|
|
int statusInt = std::stoi(token);
|
|
if (statusInt < static_cast<int>(OrderStatus::CREATED) ||
|
|
statusInt > static_cast<int>(OrderStatus::CANCELLED))
|
|
{
|
|
throw std::runtime_error("Invalid Order Status for Order in snapshot");
|
|
}
|
|
status = static_cast<OrderStatus>(statusInt);
|
|
getline(serializedOrder, token, '|');
|
|
try {
|
|
customerId = std::stoi(token);
|
|
}
|
|
catch (...)
|
|
{
|
|
throw std::runtime_error("Invalid Customer ID for Order in snapshot");
|
|
}
|
|
getline(serializedOrder, token, '|');
|
|
try {
|
|
restaurantId = std::stoi(token);
|
|
}
|
|
catch (...)
|
|
{
|
|
throw std::runtime_error("Invalid Restaurant ID for Order in snapshot");
|
|
}
|
|
return std::make_shared<Order>(id, status, restaurantId, customerId);
|
|
}
|