CS 301p- Assignment 1

CS 301p- Assignment 1
 CS 301p- Assignment 1


Solution:

#include <iostream>
using namespace std;

class Node {
private:
    string name;
    int id;
    int quantity;
    double price;
    Node* next;

public:
    string getName() {
        return name;
    }

    int getId() {
        return id;
    }

    int getQuantity() {
        return quantity;
    }

    double getPrice() {
        return price;
    }

    Node* getNext() {
        return next;
    }

    void setName(string name) {
        this->name = name;
    }

    void setId(int id) {
        this->id = id;
    }

    void setQuantity(int quantity) {
        this->quantity = quantity;
    }

    void setPrice(double price) {
        this->price = price;
    }

    void setNext(Node* next) {
        this->next = next;
    }
};

class Inventory {
private:
    Node* head;

public:
    Inventory() {
        head = NULL;
    }

    void 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* current = head;
            while (current->getNext() != NULL) {
                current = current->getNext();
            }
            current->setNext(newNode);
        }

        cout << "Product added to inventory." << endl;
    }

    void 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 from inventory." << endl;
            return;
        }

        Node* current = head;
        Node* prev = NULL;
        while (current != NULL && current->getId() != id) {
            prev = current;
            current = current->getNext();
        }

        if (current == NULL) {
            cout << "Product not found in inventory." << endl;
            return;
        }

        prev->setNext(current->getNext());
        delete current;
        cout << "Product removed from inventory." << endl;
    }

    void updateProduct(int id, int quantity, double price) {
        Node* current = head;
        while (current != NULL && current->getId() != id) {
            current = current->getNext();
        }

        if (current == NULL) {
            cout << "Product not found in inventory." << endl;
            return;
        }

        current->setQuantity(quantity);
        current->setPrice(price);
        cout << "Product updated." << endl;
    }

    void displayInventory() {
        if (head == NULL) {
            cout << "Inventory is empty." << endl;
            return;
        }

        Node* current = head;
        cout << "Inventory:" << endl;
        cout << "-----------------------------------------" << endl;
        cout << "Product Name\tID\tQuantity\tPrice" << endl;
        cout <<             "-----------------------------------------" << endl;
        while (current != NULL) {
            cout << current->getName() << "\t\t"
                << current->getId() << "\t"
                << current->getQuantity() << "\t\t"
                << current->getPrice() << endl;
            current = current->getNext();
        }
        cout << "-----------------------------------------" << endl;
    }
};

int main() {
    Inventory inventory;
    int choice;

    do {
        cout << "========= Inventory Management =========" << endl;
        cout << "1. Add a product" << endl;
        cout << "2. Remove a product" << endl;
        cout << "3. Update a product" << endl;
        cout << "4. Display inventory" << endl;
        cout << "5. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
            case 1: {
                string name;
                int id, quantity;
                double price;

                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;

                inventory.addProduct(name, id, quantity, price);
                break;
            }
            case 2: {
                int id;

                cout << "Enter product ID to remove: ";
                cin >> id;

                inventory.removeProduct(id);
                break;
            }
            case 3: {
                int id, quantity;
                double price;

                cout << "Enter product ID to update: ";
                cin >> id;
                cout << "Enter new quantity: ";
                cin >> quantity;
                cout << "Enter new price: ";
                cin >> price;

                inventory.updateProduct(id, quantity, price);
                break;
            }
            case 4:
                inventory.displayInventory();
                break;
            case 5:
                cout << "Exiting program." << endl;
                break;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }

        cout << endl;
    } while (choice != 5);
return 0;
}