Here’s the code implemented in C++ using classes and linked lists to manage categories, sub-categories,
and products. The structure includes classes Product, SubCategory, Category, and Inventory, with linked
lists to dynamically store elements.
Code
#include <iostream>
#include <string>
using namespace std;
class Product {
public:
string brand;
string description;
float price;
int sold;
int remaining;
Product* next;
Product(string b, string d, float p, int s, int r)
: brand(b), description(d), price(p), sold(s), remaining(r), next(nullptr) {}
};
class SubCategory {
public:
string name;
Product* productHead;
SubCategory* next;
SubCategory(string n) : name(n), productHead(nullptr), next(nullptr) {}
void addProduct(string brand, string description, float price, int sold, int remaining) {
Product* newProduct = new Product(brand, description, price, sold, remaining);
newProduct->next = productHead;
productHead = newProduct;
void removeProduct(string brand) {
Product* current = productHead;
Product* previous = nullptr;
while (current) {
if (current->brand == brand) {
if (previous) previous->next = current->next;
else productHead = current->next;
delete current;
return;
previous = current;
current = current->next;
}
void updateProduct(string brand, string newDesc, float newPrice, int newSold, int newRemaining) {
Product* current = productHead;
while (current) {
if (current->brand == brand) {
if (!newDesc.empty()) current->description = newDesc;
if (newPrice != -1) current->price = newPrice;
if (newSold != -1) current->sold = newSold;
if (newRemaining != -1) current->remaining = newRemaining;
return;
current = current->next;
void listProducts(string searchTerm = "") {
Product* current = productHead;
while (current) {
if (searchTerm.empty() || current->brand.find(searchTerm) != string::npos || current-
>description.find(searchTerm) != string::npos) {
cout << "Brand: " << current->brand << ", Description: " << current->description
<< ", Price: " << current->price << ", Sold: " << current->sold
<< ", Remaining: " << current->remaining << endl;
current = current->next;
}
}
};
class Category {
public:
string name;
SubCategory* subCategoryHead;
Category* next;
Category(string n) : name(n), subCategoryHead(nullptr), next(nullptr) {}
SubCategory* getSubCategory(string subCatName) {
SubCategory* current = subCategoryHead;
while (current) {
if (current->name == subCatName) return current;
current = current->next;
SubCategory* newSubCat = new SubCategory(subCatName);
newSubCat->next = subCategoryHead;
subCategoryHead = newSubCat;
return newSubCat;
};
class Inventory {
Category* categoryHead;
public:
Inventory() : categoryHead(nullptr) {}
void addProduct(string catName, string subCatName, string brand, string description, float price, int
sold, int remaining) {
Category* cat = getCategory(catName);
SubCategory* subCat = cat->getSubCategory(subCatName);
subCat->addProduct(brand, description, price, sold, remaining);
void removeProduct(string catName, string subCatName, string brand) {
Category* cat = getCategory(catName);
SubCategory* subCat = cat->getSubCategory(subCatName);
subCat->removeProduct(brand);
void updateProduct(string catName, string subCatName, string brand, string newDesc = "", float
newPrice = -1, int newSold = -1, int newRemaining = -1) {
Category* cat = getCategory(catName);
SubCategory* subCat = cat->getSubCategory(subCatName);
subCat->updateProduct(brand, newDesc, newPrice, newSold, newRemaining);
void searchProduct(string catName, string searchTerm) {
Category* cat = getCategory(catName);
SubCategory* currentSubCat = cat->subCategoryHead;
while (currentSubCat) {
currentSubCat->listProducts(searchTerm);
currentSubCat = currentSubCat->next;
void displayMenu() {
int choice;
do {
cout << "1. Add Product\n2. Remove Product\n3. Update Product\n4. Search Product\n5. Exit\
nSelect an option: ";
cin >> choice;
if (choice == 1) {
string cat, subCat, brand, desc;
float price;
int sold, remaining;
cout << "Category: "; cin >> cat;
cout << "Sub-category: "; cin >> subCat;
cout << "Brand: "; cin >> brand;
cout << "Description: "; cin >> desc;
cout << "Price: "; cin >> price;
cout << "Sold: "; cin >> sold;
cout << "Remaining: "; cin >> remaining;
addProduct(cat, subCat, brand, desc, price, sold, remaining);
} else if (choice == 2) {
string cat, subCat, brand;
cout << "Category: "; cin >> cat;
cout << "Sub-category: "; cin >> subCat;
cout << "Brand: "; cin >> brand;
removeProduct(cat, subCat, brand);
} else if (choice == 3) {
string cat, subCat, brand, newDesc;
float newPrice = -1;
int newSold = -1, newRemaining = -1;
cout << "Category: "; cin >> cat;
cout << "Sub-category: "; cin >> subCat;
cout << "Brand: "; cin >> brand;
cout << "New Description (leave empty to skip): "; cin.ignore(); getline(cin, newDesc);
cout << "New Price (enter -1 to skip): "; cin >> newPrice;
cout << "New Sold (enter -1 to skip): "; cin >> newSold;
cout << "New Remaining (enter -1 to skip): "; cin >> newRemaining;
updateProduct(cat, subCat, brand, newDesc, newPrice, newSold, newRemaining);
} else if (choice == 4) {
string cat, searchTerm;
cout << "Category: "; cin >> cat;
cout << "Search term: "; cin >> searchTerm;
searchProduct(cat, searchTerm);
} while (choice != 5);
}
private:
Category* getCategory(string catName) {
Category* current = categoryHead;
while (current) {
if (current->name == catName) return current;
current = current->next;
Category* newCategory = new Category(catName);
newCategory->next = categoryHead;
categoryHead = newCategory;
return newCategory;
};
int main() {
Inventory inventory;
inventory.displayMenu();
return 0;
Explanation
1. Classes and Linked Lists:
Product: Represents a product, with a pointer (next) to the next product in the linked list.
SubCategory: Represents a sub-category containing a linked list of Product objects, managed using the
productHead pointer.
Category: Represents a category containing a linked list of SubCategory objects, managed using the
subCategoryHead pointer.
Inventory: Manages a linked list of Category objects, with categoryHead as the head pointer. It provides
a menu-based interface to interact with the products.
2. Functions:
addProduct(), removeProduct(), updateProduct(), and searchProduct() provide CRUD functionalities.
displayMenu() displays the menu to interact with products.
This approach effectively organizes the code using linked lists within each class to dynamically manage
categories, sub-categories, and products. Let me know if you need further adjustments!