dimanche 4 juin 2023

How to design a low-level system using OOP principles to notify users when a previously out-of-stock product becomes available? [closed]

Design a system where if a user want to buy a product that is out of stock then notify the user when product is back in stock.

I tried to implement it following way in C++. Please provide any improvements or any other solution for the problem. Please provide some improvements in term of using data structure or passing arguments.

#include <bits/stdc++.h>
using namespace std;

class User
{
public:
    void sendNotification(string prodName)
    {
        cout << prodName << " Product is Available" << endl;
    }

    void bought(string prodName)
    {
        cout << "Bougth product " << prodName << endl;
    }

    void productNotAvailable(string prodName)
    {
        cout << prodName << " Product is currently out of stock, We will notify you when it will be available." << endl;
    }
};

class Product
{
public:
    int qnt;
    string prodName;
    queue<User> q;

    Product(int qnt, string prodName)
    {
        this->qnt = qnt;
        this->prodName = prodName;
    }

    void notify(User u)
    {
        u.sendNotification(prodName);
    }

    void addProduct(int newQnt)
    {
        if (qnt == 0)
        {
            while (!q.empty())
            {
                notify(q.front());
                q.pop();
            }
        }
        qnt += newQnt;
    }

    void buyProduct(User u)
    {
        if (qnt == 0)
        {
            q.push(u);
            u.productNotAvailable(prodName);
        }
        else
        {
            u.bought(prodName);
            qnt -= 1;
        }
    }
};

int main()
{
    Product iphone = Product(1, "Iphone");
    User u1 = User();
    User u2 = User();
    User u3 = User();

    iphone.buyProduct(u1);
    iphone.buyProduct(u2);
    iphone.buyProduct(u3);

    iphone.addProduct(10);
}

Aucun commentaire:

Enregistrer un commentaire