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>
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{
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)
}
};
class User{
public:
string name;
string email;
string phoneNumber;
string address;
string review;
Payment type;
vector<Product> shoppingCart;
User(string n , string e , string p , string a) : name(n) , email(e) , phoneNumber(p) , address(a) , type(CREDIT){}
void addReview(const string& reviewText, const User& user) {
pair<User, string> reviewEntry(user, reviewText);
Review_Store[name].push_back(reviewEntry); // Add to the review store
}
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()) {
shoppingCart.push_back(*it);
}
}
void DeleteFromShoppingCart(Product p){
auto it = std::find(shoppingCart.begin(), shoppingCart.end(), p);
if (it != shoppingCart.end()) {
// Erase the element
shoppingCart.erase(it);
}
}
};
class AddProduct{
public:
string type;
Product p;
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
RemovebyName(p); // 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()) {
products.erase(it);
} 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...