#include <iostream>
#include <string>
using namespace std;
struct Item {
int id;
string name;
};
// Now these are no longer global variables, they will be passed around.
// int itemCount = 0; // Removed global itemCount
// int itemCapacity = 0; // Removed global itemCapacity
// Function to display a single item
void displayItem(const Item& item) {
cout << "ID: " << [Link] << ", Name: " << [Link] << endl;
}
// Function to display the list of items
// Now takes Item* storeItems, int itemCount as parameters
void displayItems(const Item* storeItems, int itemCount) {
if (itemCount == 0) {
cout << "The store is empty." << endl;
return;
}
cout << "\n--- Store Inventory ---" << endl;
for (int i = 0; i < itemCount; ++i) {
displayItem(storeItems[i]);
}
cout << "-----------------------" << endl;
}
// Function to swap two items
void swapItems(Item& a, Item& b) {
Item temp = a;
a = b;
b = temp;
}
// Function to resize the storeItems array
// Now takes Item*& storeItems (reference to pointer), int& itemCapacity as parameters
void resizeStore(Item*& storeItems, int& itemCapacity, int itemCount, int newCapacity) {
if (newCapacity <= itemCapacity) {
return;
}
Item* newStore = new Item[newCapacity];
for (int i = 0; i < itemCount; ++i) {
newStore[i] = storeItems[i];
}
delete[] storeItems;
storeItems = newStore;
itemCapacity = newCapacity;
}
// Function to add a new item
// Now takes Item*& storeItems, int& itemCount, int& itemCapacity as parameters
void addItem(Item*& storeItems, int& itemCount, int& itemCapacity) {
if (itemCount == itemCapacity) {
// Double the capacity or set to 10 if it's currently 0
resizeStore(storeItems, itemCapacity, itemCount, itemCapacity == 0 ? 10 : itemCapacity * 2);
}
Item newItem;
cout << "Enter item name: ";
cin >> [Link];
cout << "Enter item ID: ";
cin >> [Link];
// Check if ID already exists
for (int i = 0; i < itemCount; ++i) {
if (storeItems[i].id == [Link]) {
cout << "Error: Item with ID " << [Link] << " already exists." << endl;
return;
}
}
storeItems[itemCount++] = newItem;
cout << "Item '" << [Link] << "' added successfully." << endl;
}
// Function to delete an item by ID
// Now takes Item* storeItems, int& itemCount as parameters
void deleteItem(Item* storeItems, int& itemCount) {
int deleteId;
cout << "Enter the ID of the item to delete: ";
cin >> deleteId;
int Index = -1;
for (int i = 0; i < itemCount; ++i) {
if (storeItems[i].id == deleteId) {
Index = i;
break;
}
}
if (Index != -1) {
// Shift elements to fill the gap
for (int i = Index; i < itemCount - 1; ++i) {
storeItems[i] = storeItems[i + 1];
}
itemCount--;
cout << "Item with ID " << deleteId << " deleted successfully." << endl;
} else {
cout << "Error: Item with ID " << deleteId << " not found." << endl;
}
}
// Selection Sort by Name
// Now takes Item* storeItems, int itemCount as parameters
void selectionSortByName(Item* storeItems, int itemCount) {
int n = itemCount;
for (int i = 0; i < n - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (storeItems[j].name < storeItems[minIndex].name) {
minIndex = j;
}
}
if (minIndex != i) {
swapItems(storeItems[i], storeItems[minIndex]);
}
}
cout << "Items sorted by name using Selection Sort." << endl;
}
// Bubble Sort by Name
// Now takes Item* storeItems, int itemCount as parameters
void bubbleSortByName(Item* storeItems, int itemCount) {
int n = itemCount;
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (storeItems[j].name > storeItems[j + 1].name) {
swapItems(storeItems[j], storeItems[j + 1]);
swapped = true;
}
}
if (!swapped) {
break;
}
}
cout << "Items sorted by name using Bubble Sort." << endl;
}
// Insertion Sort by Name
// Now takes Item* storeItems, int itemCount as parameters
void insertionSortByName(Item* storeItems, int itemCount) {
int n = itemCount;
for (int i = 1; i < n; ++i) {
Item key = storeItems[i];
int j = i - 1;
while (j >= 0 && storeItems[j].name > [Link]) {
storeItems[j + 1] = storeItems[j];
j--;
}
storeItems[j + 1] = key;
}
cout << "Items sorted by name using Insertion Sort." << endl;
}
// Selection Sort by ID
// Now takes Item* storeItems, int itemCount as parameters
void selectionSortById(Item* storeItems, int itemCount) {
int n = itemCount;
for (int i = 0; i < n - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (storeItems[j].id < storeItems[minIndex].id) {
minIndex = j;
}
}
if (minIndex != i) {
swapItems(storeItems[i], storeItems[minIndex]);
}
}
cout << "Items sorted by ID using Selection Sort." << endl;
}
// Bubble Sort by ID
// Now takes Item* storeItems, int itemCount as parameters
void bubbleSortById(Item* storeItems, int itemCount) {
int n = itemCount;
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (storeItems[j].id > storeItems[j + 1].id) {
swapItems(storeItems[j], storeItems[j + 1]);
swapped = true;
}
}
if (!swapped) {
break;
}
}
cout << "Items sorted by ID using Bubble Sort." << endl;
}
// Insertion Sort by ID
// Now takes Item* storeItems, int itemCount as parameters
void insertionSortById(Item* storeItems, int itemCount) {
int n = itemCount;
for (int i = 1; i < n; ++i) {
Item key = storeItems[i];
int j = i - 1;
while (j >= 0 && storeItems[j].id > [Link]) {
storeItems[j + 1] = storeItems[j];
j--;
}
storeItems[j + 1] = key;
}
cout << "Items sorted by ID using Insertion Sort." << endl;
}
// Binary Search by ID (Assuming the list is already sorted by ID)
// Now takes const Item* storeItems, int itemCount as parameters
int binarySearchById(const Item* storeItems, int itemCount, int searchId) {
int low = 0;
int high = itemCount - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (storeItems[mid].id == searchId) {
return mid;
} else if (storeItems[mid].id < searchId) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
// Simple bubble sort for sorting by ID (without <algorithm>)
// Now takes Item* storeItems, int itemCount as parameters
void sortByIdWithoutAlgorithm(Item* storeItems, int itemCount) {
int n = itemCount;
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (storeItems[j].id > storeItems[j + 1].id) {
swapItems(storeItems[j], storeItems[j + 1]);
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
int main() {
// These are now local variables within main
Item* storeItems = nullptr;
int itemCount = 0;
int itemCapacity = 0;
string initialNames[] = {
"pen", "book", "ruler", "marker", "eraser",
"laptop", "mouse", "keyboard", "charger", "notebook",
"chair", "desk", "lamp", "shelf", "paper",
"bag", "projector", "whiteboard", "fan", "clock",
"phone", "stapler", "calculator", "tape", "scissors"
};
int numInitialItems = sizeof(initialNames) / sizeof(initialNames[0]);
// Initialize the store with the initial items
// Pass references to storeItems, itemCapacity, itemCount
resizeStore(storeItems, itemCapacity, itemCount, numInitialItems);
for (int i = 0; i < numInitialItems; ++i) {
storeItems[i].id = i + 1;
storeItems[i].name = initialNames[i];
itemCount++;
}
int choice;
do {
cout << "\n--- Store Management System ---" << endl;
cout << "1. Add New Item" << endl;
cout << "2. Sort by Name" << endl;
cout << "3. Sort by ID" << endl;
cout << "4. Display Inventory" << endl;
cout << "5. Delete Item" << endl;
cout << "6. Search by ID (Binary Search)" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
// Pass references to storeItems, itemCount, itemCapacity
addItem(storeItems, itemCount, itemCapacity);
break;
case 2: {
int sortNameChoice;
cout << " 1. Selection Sort" << endl;
cout << " 2. Bubble Sort" << endl;
cout << " 3. Insertion Sort" << endl;
cout << " Enter your choice: ";
cin >> sortNameChoice;
switch (sortNameChoice) {
case 1:
selectionSortByName(storeItems, itemCount);
break;
case 2:
bubbleSortByName(storeItems, itemCount);
break;
case 3:
insertionSortByName(storeItems, itemCount);
break;
default:
cout << "Invalid choice." << endl;
break;
}
break;
}
case 3: {
int sortIdChoice;
cout << " 1. Selection Sort" << endl;
cout << " 2. Bubble Sort" << endl;
cout << " 3. Insertion Sort" << endl;
cout << " Enter your choice: ";
cin >> sortIdChoice;
switch (sortIdChoice) {
case 1:
selectionSortById(storeItems, itemCount);
break;
case 2:
bubbleSortById(storeItems, itemCount);
break;
case 3:
insertionSortById(storeItems, itemCount);
break;
default:
cout << "Invalid choice." << endl;
break;
}
break;
}
case 4:
// Pass storeItems and itemCount
displayItems(storeItems, itemCount);
break;
case 5:
// Pass storeItems and itemCount by reference
deleteItem(storeItems, itemCount);
break;
case 6: {
int searchId;
cout << "Enter the ID to search for: ";
cin >> searchId;
// Sort before binary search, pass storeItems and itemCount
sortByIdWithoutAlgorithm(storeItems, itemCount);
// Pass storeItems, itemCount, and searchId
int index = binarySearchById(storeItems, itemCount, searchId);
if (index != -1) {
cout << "Item found at index " << index << ": ";
displayItem(storeItems[index]);
} else {
cout << "Item with ID " << searchId << " not found." << endl;
}
break;
}
case 0:
cout << "Exiting system." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 0);
// Clean up allocated memory (still in main)
delete[] storeItems;
storeItems = nullptr;
return 0;
}