Fix Create Service Submenu

- Added heading "Create Service" to submenu for clearer user guidance.
- Ensured console clears before service creation for consistent UI behavior.
- Filtered active inventory items before selection to avoid showing inactive items.
- Updated prompts with improved formatting and spacing (e.g., labour cost input, success message).
- Refactored selectInventoryItems:
  - Added "Select Required Items" heading for clarity.
  - Skipped null or inactive items during listing.
  - Improved empty inventory message ("No Items Present, Inventory empty").
  - Enhanced success and error messages with consistent line breaks.
  - Simplified logic for item selection and exit handling.

Fixes #1747
This commit is contained in:
Jissin Mathew
2026-05-26 16:29:03 +05:30
committed by Joel Thomas
parent 3b82648e45
commit 05499e4890
2 changed files with 56 additions and 56 deletions
@@ -386,17 +386,19 @@ Returns:
void AdminMenu::createService() void AdminMenu::createService()
{ {
util::clear(); util::clear();
std::cout << "Create Service\n";
std::string serviceName; std::string serviceName;
double labourCost; double labourCost;
std::cout << "Enter the service name: "; std::cout << "Enter the service name: ";
util::read(serviceName); util::read(serviceName);
util::Map<std::string, const InventoryItem*> currentInventoryItems = m_controller.getInventoryItems(); util::Map<std::string, const InventoryItem*> currentInventoryItems = m_controller.getInventoryItems();
util::Map<std::string, const InventoryItem*> activeInventoryItems = filterActiveItems(currentInventoryItems);
util::Vector<std::string> selectedInventoryItems; util::Vector<std::string> selectedInventoryItems;
selectInventoryItems(currentInventoryItems,selectedInventoryItems); selectInventoryItems(activeInventoryItems,selectedInventoryItems);
std::cout << "Enter the labour cost: "; std::cout << "\nEnter the labour cost: ";
util::read(labourCost); util::read(labourCost);
m_controller.createService(serviceName, selectedInventoryItems, labourCost); m_controller.createService(serviceName, selectedInventoryItems, labourCost);
std::cout << "Service created sucessfully.\n"; std::cout << "\nService created sucessfully.\n\n";
util::pressEnter(); util::pressEnter();
} }
@@ -93,65 +93,63 @@ Returns:
*/ */
inline void selectInventoryItems(util::Map<std::string, const InventoryItem*>& currentInventoryItems, util::Vector<std::string>& selectedInventoryItems) inline void selectInventoryItems(util::Map<std::string, const InventoryItem*>& currentInventoryItems, util::Vector<std::string>& selectedInventoryItems)
{ {
bool doRun = true, hasInventoryItems = false; bool doRun = true;
util::Map<int, const InventoryItem*> currentInventoryMap; util::Map<int, const InventoryItem*> currentInventoryMap;
int currentIndex = 1; int choice;
int choice; if (currentInventoryItems.getSize() == 0)
if (currentInventoryItems.getSize() == 0)
{
std::cout << "Inventory empty.";
}
while (doRun)
{
bool hasInventoryItems = false;
int currentIndex = 1;
currentInventoryMap.clear();
std::cout << std::left
<< std::setw(6) << "Index"
<< std::setw(12) << "Item ID"
<< std::setw(20) << "Part Name"
<< std::setw(10) << "Price"
<< std::setw(10) << "Quantity"
<< std::endl;
for (int iterator = 0; iterator < currentInventoryItems.getSize(); iterator++)
{ {
const InventoryItem* currentInventoryItem = currentInventoryItems.getValueAt(iterator); std::cout << "No Items Present, Inventory empty.\n";
if (currentInventoryItem->getState() == util::State::INACTIVE) return;
{
continue;
}
std::cout << std::left
<< std::setw(6) << currentIndex
<< std::setw(12) << currentInventoryItem->getId()
<< std::setw(20) << currentInventoryItem->getPartName()
<< std::setw(10) << currentInventoryItem->getPrice()
<< std::setw(10) << currentInventoryItem->getQuantity()
<< std::endl;
hasInventoryItems = true;
currentInventoryMap.insert(currentIndex++, currentInventoryItem);
}
if (!hasInventoryItems)
{
std::cout << "No items present in the inventory." << std::endl;
doRun = false;
break;
} }
std::cout << "Select the item (Index) or enter -1 to exit: "; while (doRun)
util::read(choice); {
std::cout << "\nSelect Required Items\n";
if (choice == -1) bool hasInventoryItems = false;
{ int currentIndex = 1;
doRun = false; currentInventoryMap.clear();
std::cout << std::left
<< std::setw(6) << "Index"
<< std::setw(12) << "Item ID"
<< std::setw(20) << "Part Name"
<< std::setw(10) << "Price"
<< std::setw(10) << "Quantity"
<< std::endl;
for (int iterator = 0; iterator < currentInventoryItems.getSize(); iterator++)
{
const InventoryItem* currentInventoryItem = currentInventoryItems.getValueAt(iterator);
if (currentInventoryItem == nullptr || currentInventoryItem->getState() == util::State::INACTIVE)
{
continue;
}
std::cout << std::left
<< std::setw(6) << currentIndex
<< std::setw(12) << currentInventoryItem->getId()
<< std::setw(20) << currentInventoryItem->getPartName()
<< std::setw(10) << currentInventoryItem->getPrice()
<< std::setw(10) << currentInventoryItem->getQuantity()
<< std::endl;
currentInventoryMap.insert(currentIndex++, currentInventoryItem);
hasInventoryItems = true;
} }
else if (currentInventoryMap.find(choice) != -1) if (!hasInventoryItems)
{ {
selectedInventoryItems.push_back(currentInventoryMap.getValueAt(choice)->getId()); break;
std::cout << "Item added successfully." << std::endl; }
std::cout << "Select the item (Index) or enter -1 to exit: ";
util::read(choice);
if (choice == -1)
{
doRun = false;
}
else if (currentInventoryMap.find(choice) != -1)
{
selectedInventoryItems.push_back(currentInventoryMap.getValueAt(currentInventoryMap.find(choice))->getId());
std::cout << "Item added successfully.\n" << std::endl;
} }
else else
{ {
std::cout << "Enter a valid integer." << std::endl; std::cout << "Enter a valid integer.\n" << std::endl;
} }
} }
} }