Determine the different ways the system will be used. This includes main functions the system needs to perform and who will use it.
Based on the requirements and use cases, identify the main objects of the system...
Determine how these objects will interact with each other to fulfill the use cases...
Design inheritance trees where applicable to promote code reuse and polymorphism. This step involves identifying common attributes and behaviors that can be abstracted into parent classes...
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
Attributes: For each class, define the attributes (data) it will hold...
Methods: Define the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <thread>
#include <mutex>
using namespace std;
enum Payment{
CREDIT = 0,
CASH = 1,
DEBIT = 2
};
static unordered_map<string , vector<pair<User, string>>> Review_Store;
static unordered_map<string, vector<Product>> product_inventory;
static unordered_map<string , vector<Product>> name_product_inventory;
class Product : Review{
public:
int pID;
double price;
string name;
string review;
Product(int pID, double price , string name , string review) : pID(pID) , price(price) , name(name) , review(review){}
bool operator==(const Product& other) const {
return pID == other.pID; // Compare based on pID (or use name if you prefer)
}
void addReview(User& u, const string& reviewText) {
reviewManager.addReview(u, reviewText);
}
void deleteReview(User& u) {
reviewManager.deleteReview(u);
}
};
class Review{
public:
product p;
string review;
Review(Product p):p(p){
review = "";
}
void addReview(User u , string review){
this->review = review;
Review_Store[p.name].push_back({u.name , this->review});
}
void deleteReview(User u , string review){
auto& reviews = Review_Store[p.name];
auto it = find_if(reviews.begin(), reviews.end(), [&u, this](const pair<User, string>& entry) {
return entry.first.name == u.name && entry.second == this->review; // Match by User name and review
});
if (it != reviews.end()) {
reviews.erase(it); // Erase the matching review entry
} else {
cout << "Review not found!" << endl;
}
}
}
class User {
public:
string name;
string email;
string phoneNumber;
string address;
string review;
Payment type;
vector<Product> shoppingCart;
mutex mtx;
User(string n , string e , string p , string a) : name(n) , email(e) , phoneNumber(p) , address(a) , type(CREDIT){}
void addToShoppingCart(string type , Product p){
auto& products = product_inventory[type]; // Access the vector<Product> for the given type
// Find the product in the vector by pID or other criteria
auto it = find_if(products.begin(), products.end(), [&p](const Product& prod) {
return prod.pID == p.pID; // You can change this condition if needed
});
if (it != products.end()) {
mtx.lock();
shoppingCart.push_back(*it);
mtx.unlock();
}
}
void DeleteFromShoppingCart(Product p){
auto it = std::find(shoppingCart.begin(), shoppingCart.end(), p);
if (it != shoppingCart.end()) {
// Erase the element
mtx.lock();
shoppingCart.erase(it);
mtx.unlock();
}
}
};
class AddProduct{
public:
string type;
Product p;
mutex mtx;
AddProduct(string t , Product p): type(t) , p(p){}
void addProductBytype(){
product_inventory[type].push_back(p);
}
void addProductByName(){
name_product_inventory[p.name].push_back(p);
}
void RemovebyType(const Product& p) {
auto& products = product_inventory[type]; // Access the vector<Product> for the given type
// Find the product in the vector by pID or other criteria
auto it = find_if(products.begin(), products.end(), [&p](const Product& prod) {
return prod.pID == p.pID; // You can change this condition if needed
});
// If product is found, erase it
if (it != products.end()) {
products.erase(it); // Erase the product from the vector
mtx.lock();
RemovebyName(p);
mtx.unlock(); // Optional: Additional removal by name (implementation not shown)
} else {
cout << "Product not found in the inventory!" << endl;
}
}
void RemovebyName(const Product& p){
auto& products = name_product_inventory[p.name];
auto it = find(products.begin(), products.end(), p);
if (it != products.end()) {
mtx.lock();
products.erase(it);
mtx.unlock();
} else {
cout << "Product not found by name!" << endl;
}
}
};
enum status {
SUCCESS = 0,
FAIL = 1,
PENDING = 2
};
class Transaction : public User {
public:
status s;
Transaction(string n, string e, string p, string a)
: User(n, e, p, a) {
s = PENDING;
}
// Function to complete the transaction
void completeTransaction() {
shoppingCart.clear();
s = SUCCESS;
cout << "Transaction completed for user: " << name << endl;
}
};
class OnlineShopping{
public:
vector<User> account;
void addAccount(string n , string e , string p , string a){
User user(n , e , p , a);
account.push_back(user);
}
void searchDisplayType(string type){
auto& products = product_inventory[type];
for(auto it : products){
cout << it << endl;
}
}
void seachDisplayName(string name){
auto& products = name_product_inventory[name];
for(auto it: products){
cout << it << endl;
}
}
};
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Critically examine your design for any flaws or areas for future improvement...