Header Ads

Suppose you are working as a software developer for a company that sells various types of products.

Suppose you are working as a software developer for a company that
sells various types of products. The company wants to keep track of its
inventory using a linked list data structure. You are asked to develop a C++ program
that will allow the company to add, remove and update products in their
inventory using a linked list.



Requirements:




  • You have to use only the Linked List data structure for this assignment. Your marks will be
    deducted if any other data structure is used

  • Your program should
    perform the below-mentioned operations on the output console:



1.       
Add a new
product to the inventory: The user should be able to add a new product to the
inventory by providing the product name, product ID, quantity, and price.



2.       
Remove a
product from the inventory: The user should be able to remove a product from
the inventory by providing the product ID.



3.       
Update a
product in the inventory: The user should be able to update the quantity or
price of a product in the inventory by providing the product ID.



4.       
Display
the inventory: The user should be able to view the entire inventory, including
the product name, product ID, quantity, and price.



l 
Your
program should be implemented using a linked list data structure, with each
node representing a single product in the inventory. Each node should contain the product
name, product ID, quantity, and price.



Solution Guidelines:



1)     
Understand
and practice the following topics before developing this assignment::



          R Nodes



          R List



          R Linked
List



          RLinked
List Methods



2)      To save product
information, use the Linked List data structure. Each product will be
represented by a node.
Do not use strcut . You have to
implement Node class . Using struct will result in deduction of marks



3)     
Your solution should use these classes:



 




a.      
Node
class
: To save
information of each product



b.     
Inventory
Class
: To save all
products in linked list





Sample Output



See the attached video file (output.mp4) to see
the sample output.

Solution:


#include<iostream>
using namespace std;
class Node{
string name;
int id;
int quantity;
double price;
Node* next;
public:
string getName()const;
int getId()const;
int getQuantity()const;
double getPrice()const;
Node* getNext()const;
void setName(string);
void setId(int);
void setQuantity(int);
void setPrice(double);
void setNext(Node*);
};
void Node::setNext(Node* next){
this->next = next;
}
void Node::setName(string name){
this->name = name;
}
void Node::setId(int id){
this->id = id;
}
void Node::setQuantity(int quantity){
this->quantity = quantity;
}
void Node::setPrice(double price){
this->price = price;
}
Node* Node::getNext()const{
return this->next;
}
string Node::getName()const{
return this->name;
}
int Node::getId()const{
return this->id;
}
int Node::getQuantity()const{
return this->quantity;
}
double Node::getPrice()const{
return this->price;
}
class Inventory{
Node* head;
public:
Inventory();
void addProduct(string name,int id,int quantity,double price);
void removeProduct(int);
void updateProduct(int,int,double);
void displayInventory()const;
};
Inventory::Inventory(){
head = NULL;
}
void Inventory::updateProduct(int id,int quantity,double price){
Node* newNode = head;
while(newNode != NULL){
if(newNode->getId() == id){
newNode->setQuantity(quantity);
newNode->setPrice(price);
cout<<"Product updated successfully"<<endl;
return;
}
newNode = newNode->getNext();
}
}


void Inventory::addProduct(string name,int id,int quantity,double price){
    Node* newNode =  new Node;
    newNode->setName(name);
    newNode->setId(id);
    newNode->setQuantity(quantity);
    newNode->setPrice(price);
    newNode->setNext(NULL);

    if (head == NULL) {
        head = newNode;
    } else {
        Node* last = head;
        while (last->getNext() != NULL) {
            last = last->getNext();
        }
        last->setNext(newNode);
    }

    cout << "Product added successfully." << endl;
}



void Inventory::removeProduct(int id) {
    if (head == NULL) {
        cout << "Inventory is Empty!" << endl;
        return;
    }

    if (head->getId() == id) {
        Node* temp = head;
        head = head->getNext();
        delete temp;
        cout << "Product removed successfully" << endl;
        return;
    }

    Node* newNode = head;
    while (newNode->getNext() != NULL) {
        if (newNode->getNext()->getId() == id) {
            Node* temp = newNode->getNext();
            newNode->setNext(newNode->getNext()->getNext());
            delete temp;
            cout << "Product Removed from inventory" << endl;
            return;
        }
        newNode = newNode->getNext();
    }

    cout << "Product not found in inventory" << endl;
}
void show(){
cout<<"Inventory:"<<endl
<<"Name   ID   Quantity   Price"<<endl;
}
void Inventory::displayInventory()const{
if(head == NULL){
cout<<"Inventory is empty!";
return;
}
show();
Node* newNode = head;
while(newNode != NULL){
cout<<newNode->getName()<<"";
cout<<"\t"<<newNode->getId()<<"";
cout<<"\t"<<newNode->getQuantity()<<"";
cout<<"\t"<<newNode->getPrice()<<"\n";
newNode = newNode->getNext();
}
}
int main(){
Inventory inv;
int choice,id,quantity,products;
string name;
double price;
do{
cout<<"Please choose an action:"<<endl
<<"1. Add product\n"
<<"2. Remove product\n"
<<"3. Update product\n"
<<"4. Display inventory\n"
<<"0. Exit\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1:
cout<<"How many products do you want to add? ";
cin>>products;
for(int i=0; i<products; i++){
cout<<"Enter product name: ";
cin.ignore();
getline(cin,name);
cout<<"Enter product ID: ";
cin>>id;
cout<<"Enter product quantity: ";
cin>>quantity;
cout<<"Enter product price: ";
cin>>price;
inv.addProduct(name,id,quantity,price);
}
break;
case 2:
cout<<"Enter product ID to remove: ";
cin>>id;
inv.removeProduct(id);
break;
case 3:
cout<<"Enter product ID to update: ";
cin>>id;
cout<<"Enter new quantity: ";
cin>>quantity;
cout<<"Enter new price: ";
cin>>price;
inv.updateProduct(id,quantity,price);
break;
case 4:
inv.displayInventory();
break;
case 0:
cout<<"Exiting Program...";
break;
}
cout<<endl;
}while(choice != 0);
return 0;
}




No comments

Powered by Blogger.