#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