DSA Assignment 2
DSA Assignment 2
and products. The structure includes classes Product, SubCategory, Category, and Inventory, with linked
lists to dynamically store elements.
Code
#include <iostream>
#include <string>
class Product {
public:
string brand;
string description;
float price;
int sold;
int remaining;
Product* next;
};
class SubCategory {
public:
string name;
Product* productHead;
SubCategory* next;
void addProduct(string brand, string description, float price, int sold, int remaining) {
newProduct->next = productHead;
productHead = newProduct;
while (current) {
if (current->brand == brand) {
delete current;
return;
previous = current;
current = current->next;
}
void updateProduct(string brand, string newDesc, float newPrice, int newSold, int newRemaining) {
while (current) {
if (current->brand == brand) {
return;
current = current->next;
while (current) {
cout << "Brand: " << current->brand << ", Description: " << current->description
<< ", Price: " << current->price << ", Sold: " << current->sold
current = current->next;
}
}
};
class Category {
public:
string name;
SubCategory* subCategoryHead;
Category* next;
while (current) {
current = current->next;
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) {
subCat->removeProduct(brand);
void updateProduct(string catName, string subCatName, string brand, string newDesc = "", float
newPrice = -1, int newSold = -1, int newRemaining = -1) {
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: ";
if (choice == 1) {
float price;
} else if (choice == 3) {
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;
} else if (choice == 4) {
searchProduct(cat, searchTerm);
private:
while (current) {
current = current->next;
newCategory->next = categoryHead;
categoryHead = newCategory;
return newCategory;
};
int main() {
Inventory inventory;
inventory.displayMenu();
return 0;
Explanation
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:
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!