1
class ItemType
private:
string name;
float price;
int quantity;
public:
ItemType() : name(""), price(0.0f), quantity(0) {}
ItemType(const string &name, float price, int quantity) : name(name),
price(price), quantity(quantity) {}
string getName() { return name; }
float getPrice() { return price; }
int getQuantity() { return quantity; }
};
class GroceryList
private:
ItemType *items;
int capacity;
int size;
public:
GroceryList() : items(nullptr), capacity(0), size(0) {}
void addItem(const ItemType &item)
1
if (size == capacity)
int newCapacity = (capacity == 0) ? 1 : capacity * 2;
ItemType *newArray = new ItemType[newCapacity];
for (int i = 0; i < size; ++i)
newArray[i] = items[i];
delete[] items;
items = newArray;
capacity = newCapacity;
items[size++] = item;
void removeItem(string name)
if (size == 0)
cout << "List is empty" << endl;
return;
for (int i = 0; i < size; ++i)
string n = items[i].getName();
if (n == name)
{
1
for (int j = i; j < size - 1; ++j)
items[j] = items[j + 1];
--size;
cout << "Item removed" << endl;
return;
void sortItemsByPrice()
int n = size;
for (int i = 0; i < n - 1; ++i)
int minIndex = i;
for (int j = i + 1; j < n; ++j)
if (items[j].getPrice() < items[minIndex].getPrice())
minIndex = j;
if (minIndex != i)
swap(items[i], items[minIndex]);
}
1
void sortItemsByPriceInsertion()
for (int i = 1; i < size; i++)
ItemType key = items[i];
int j = i - 1;
while (j >= 0 && items[j].getPrice() > key.getPrice())
items[j + 1] = items[j];
j--;
items[j + 1] = key;
void displayItems() const
if (size == 0)
cout << "Grocery List is empty" << endl;
for (int i = 0; i < size; ++i)
cout << "Name: " << items[i].getName() << ", Price: " <<
items[i].getPrice() << ", Quantity: " << items[i].getQuantity() << std::endl;
}
1
~GroceryList()
delete[] items;
};
class Menu
private:
GroceryList groceryList;
int userChoice;
public:
void displayMenu()
while (true) // Loop until user chooses to exit
userChoice = 0;
cout << "*********************" << endl;
cout << "0) Exit: " << endl;
cout << "1) Add an item: " << endl;
cout << "2) Remove an item: " << endl;
cout << "3) Sort the List By Selection Sort: " << endl;
cout << "4) Sort the List By Insertion Sort: " << endl;
cout << "5) Display the Grocery List" << endl;
cout << "*********************" << endl;
cout << "Enter your choice: ";
cin >> userChoice;
switch (userChoice)
1
case 0:
return; // Exit the function and end the program
case 1:
// Get grocery details
string name;
cout << "Enter the name of the grocery item: ";
cin >> name;
float price;
cout << "Enter the price of the item: ";
cin >> price;
int quantity;
cout << "Enter the quantity of the item: ";
cin >> quantity;
ItemType newItem(name, price, quantity);
groceryList.addItem(newItem);
cout << "Added Item: " << endl;
groceryList.displayItems();
break;
case 2:
cout << "Enter the name of item: " << endl;
string choice;
cin >> choice;
groceryList.removeItem(choice);
1
break;
case 3:
groceryList.sortItemsByPrice();
groceryList.displayItems();
break;
case 4:
groceryList.sortItemsByPriceInsertion();
groceryList.displayItems();
break;
case 5:
groceryList.displayItems();
break;
default:
cout << "Invalid choice" << endl;
break;
Menu()
1
displayMenu();
};
int main()
Menu menu;}
Add an item to the list
Remove an item
Display the list
1
Sort the items by insertion Sort
Sort the items by Selection Sort
Display the list