Implement Controller listMenuItems() and addMenuItem()
- Implement Controller listMenuItems() and addMenuItem() - Add m_uid to MenuItem to track unique menu item id - Implement MenuItem getDescription() method - Add addMenuItem() method in Restaurant Class
This commit is contained in:
@@ -304,14 +304,90 @@ void FoodDeliveryController::markOrderReady()
|
||||
|
||||
}
|
||||
|
||||
void FoodDeliveryController::listMenuItems()
|
||||
void FoodDeliveryController::listMenuItems() const
|
||||
{
|
||||
try
|
||||
{
|
||||
util::clear();
|
||||
if (!checkAccess(m_authenticatedUser, "RestaurantOwner"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
RestaurantOwner& restaurantOwner = *(std::dynamic_pointer_cast<RestaurantOwner>(m_authenticatedUser));
|
||||
restaurants::iterator restaurantIterator = pickRestaurantFromRestaurants(restaurantOwner);
|
||||
if (restaurantIterator != restaurantOwner.getRestaurants().end())
|
||||
{
|
||||
util::clear();
|
||||
menuItems& restaurantMenuItems = restaurantIterator->second->getMenuItems();
|
||||
if (restaurantMenuItems.empty())
|
||||
{
|
||||
std::cout << "Restaurant " << restaurantIterator->second->getName() << " has no Menu Items\n";
|
||||
return;
|
||||
}
|
||||
std::cout << restaurantIterator->second->getName() << " Menu\n";
|
||||
std::cout << std::left << std::setw(5) << "ID"
|
||||
<< std::left << std::setw(25) << "Name"
|
||||
<< std::left << std::setw(50) << "Description"
|
||||
<< std::left << std::setw(5) << "Price"
|
||||
<< "\n";
|
||||
for (auto& menuItemPointer : restaurantMenuItems)
|
||||
{
|
||||
std::cout << std::left << std::setw(5) << menuItemPointer.second->getId()
|
||||
<< std::left << std::setw(25) << menuItemPointer.second->getName()
|
||||
<< std::left << std::setw(50) << menuItemPointer.second->getDescription()
|
||||
<< std::left << std::setw(5) << menuItemPointer.second->getPrice()
|
||||
<< "\n";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Invalid Index. Cannot display Menu Items!\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void FoodDeliveryController::addMenuItem()
|
||||
void FoodDeliveryController::addMenuItem() const
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
util::clear();
|
||||
if (!checkAccess(m_authenticatedUser, "RestaurantOwner"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
RestaurantOwner& restaurantOwner = *(std::dynamic_pointer_cast<RestaurantOwner>(m_authenticatedUser));
|
||||
restaurants::iterator restaurantIterator = pickRestaurantFromRestaurants(restaurantOwner);
|
||||
if (restaurantIterator != restaurantOwner.getRestaurants().end())
|
||||
{
|
||||
util::clear();
|
||||
std::string name, description;
|
||||
double price;
|
||||
std::cout << "Enter Item Name: ";
|
||||
util::readString(name);
|
||||
std::cout << "Enter Item Description: ";
|
||||
util::readString(description);
|
||||
std::cout << "Enter Item Price: ";
|
||||
util::readValue<double>(price);
|
||||
MenuItem menuItem(name, description, price);
|
||||
restaurantIterator->second->addMenuItem(menuItem);
|
||||
std::cout << "Added new Menu Item " << menuItem.getName() << " with ID " << menuItem.getId() << " to Restaurant " << restaurantIterator->second->getName() << " successfully\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Invalid Index. Cannot add new Menu Item!\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void FoodDeliveryController::removeMenuItem()
|
||||
|
||||
@@ -10,9 +10,11 @@ Date: 18-02-2026
|
||||
|
||||
class User;
|
||||
class Restaurant;
|
||||
class MenuItem;
|
||||
|
||||
using users = std::map<std::string, std::shared_ptr<User>>;
|
||||
using restaurants = std::map<int, std::shared_ptr<Restaurant>>;
|
||||
using menuItems = std::map<int, std::shared_ptr<MenuItem>>;
|
||||
|
||||
class FoodDeliveryController
|
||||
{
|
||||
@@ -29,8 +31,8 @@ public:
|
||||
void updateRestaurantStatus() const;
|
||||
void listRestaurantOrders();
|
||||
void markOrderReady();
|
||||
void listMenuItems();
|
||||
void addMenuItem();
|
||||
void listMenuItems() const;
|
||||
void addMenuItem() const;
|
||||
void removeMenuItem();
|
||||
void listCustomerOrders();
|
||||
void placeOrder();
|
||||
|
||||
@@ -6,6 +6,8 @@ Date: 18-02-2026
|
||||
#include <stdexcept>
|
||||
#include "MenuItem.h"
|
||||
|
||||
int MenuItem::m_uid = 0;
|
||||
|
||||
int MenuItem::getId() const
|
||||
{
|
||||
return m_id;
|
||||
@@ -17,6 +19,11 @@ std::string MenuItem::getName() const
|
||||
return m_name;
|
||||
}
|
||||
|
||||
std::string MenuItem::getDescription() const
|
||||
{
|
||||
return m_description;
|
||||
}
|
||||
|
||||
double MenuItem::getPrice() const
|
||||
{
|
||||
return m_price;
|
||||
|
||||
@@ -9,19 +9,20 @@ Date: 18-12-2026
|
||||
class MenuItem
|
||||
{
|
||||
private:
|
||||
static int m_uid;
|
||||
int m_id;
|
||||
std::string m_name;
|
||||
std::string m_description;
|
||||
double m_price;
|
||||
public:
|
||||
MenuItem():
|
||||
m_id(0),
|
||||
m_id(++m_uid),
|
||||
m_name(""),
|
||||
m_description(""),
|
||||
m_price(0)
|
||||
{}
|
||||
MenuItem(int id, const std::string& name, const std::string& description, double price):
|
||||
m_id(id),
|
||||
MenuItem(const std::string& name, const std::string& description, double price):
|
||||
m_id(++m_uid),
|
||||
m_name(name),
|
||||
m_description(description),
|
||||
m_price(price)
|
||||
|
||||
@@ -12,6 +12,14 @@ int Restaurant::getId() const
|
||||
return m_id;
|
||||
}
|
||||
|
||||
void Restaurant::addMenuItem(const MenuItem& menuItem)
|
||||
{
|
||||
if (m_menuItems.find(menuItem.getId()) == m_menuItems.end())
|
||||
{
|
||||
m_menuItems[menuItem.getId()] = std::make_shared<MenuItem>(menuItem);
|
||||
}
|
||||
}
|
||||
|
||||
menuItems& Restaurant::getMenuItems()
|
||||
{
|
||||
return m_menuItems;
|
||||
|
||||
@@ -34,6 +34,7 @@ public:
|
||||
m_isOpen(true)
|
||||
{}
|
||||
int getId() const;
|
||||
void addMenuItem(const MenuItem&);
|
||||
menuItems& getMenuItems();
|
||||
const std::string& getName() const;
|
||||
orders& getOrders();
|
||||
|
||||
Reference in New Issue
Block a user