Determine the different ways the system will be used. This includes main functions the system needs to perform and who will use it.
Warehouse needs to keep track of inventory levels of stocks, manage incoming and outgoing orders and shipments, and optimize storage locations.
Based on the requirements and use cases, identify the main objects of the system...
warehouse, order, shipment, stock, storage
Determine how these objects will interact with each other to fulfill the use cases...
Warehouse is composed of stocks and storage , shipments associate with orders, order associate with stocks and shipments
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...
stock can be an abstract base class that includes fields like stock name and SKU and price for different products.
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
composite, association
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.
public class Car { ... } // Example
#include
#include
#include
#include
using namespace std;
struct Item {
int m_SKU;
string m_name;
};
class Stock : public Item {
int m_quantity;
public:
Stock(int SKU, string name, int quantity = 0) : Item{SKU, name}, m_quantity{quantity} {}
int get_quantity() const {
return m_quantity;
}
void update_quantity(int quantity) {
m_quantity = quantity;
}
};
class Storage {
string m_storage_name;
unordered_map
public:
Storage(string storage_name) : m_storage_name{storage_name} {}
void add_stock(int SKU, shared_ptr
m_stocks[SKU] = stock;
}
void remove_stock(int SKU) {
m_stocks.erase(SKU);
}
};
class Shipment;
class Order{
shared_ptr
string m_status;
int m_timestamp;
public:
Item m_item;
int m_quantity;
bool m_incoming;
Order(Item &item, int quantity, bool incoming, int timestamp) : m_item{item}, m_quantity{quantity}, m_incoming{incoming}, m_shipment{nullptr}, m_status{"received"}, m_timestamp{timestamp} {}
void add_shipment(shared_ptr
m_shipment = shipment;
}
void update_status(string status) {
m_status = status;
}
};
class Shipment{
string m_carrier;
vector
public:
Shipment(string carrier, vector
const vector
return m_orders;
}
};
class Warehouse {
unordered_map
unordered_map
unordered_map
public:
Warehouse(vector
for (string storage_name : storages) {
m_storage.emplace(storage_name, Storage(storage_name));
}
m_storage_name["apple"] = "Fruits";
m_storage_name["shirt"] = "Clothes";
}
Warehouse(initializer_list
for (string storage_name : storages) {
m_storage.emplace(storage_name, Storage(storage_name));
}
m_storage_name["apple"] = "Fruits";
m_storage_name["shirt"] = "Clothes";
}
int check_inventory(int SKU) const {
if (!m_stocks.contains(SKU)) {
return 0;
}
return m_stocks.at(SKU)->get_quantity();
}
void update_inventory(Item& item, int quantity, string storage_name) {
int SKU = item.m_SKU;
string name = item.m_name;
if (!m_stocks.contains(SKU)) {
m_stocks[SKU] = make_shared
m_storage.at(storage_name).add_stock(SKU, m_stocks[SKU]);
} else {
m_stocks[SKU]->update_quantity(quantity);
}
}
void process_shipment(Shipment& shipment) {
for (shared_ptr
update_inventory(order->m_item, order->m_incoming ? order->m_quantity : -order->m_quantity, m_storage_name[order->m_item.m_name]);
}
}
Warehouse(const Warehouse& w) = delete;
Warehouse& operator=(const Warehouse& w) = delete;
Warehouse(const Warehouse&& w) = delete;
Warehouse& operator=(Warehouse&& w) = delete;
~Warehouse() = default;
};
int main() {
// apple: SKU = 1
// shirt: SKU = 2
// eggs: SKU = 3
Warehouse w{"Fruits", "Clothes", "Diary"};
Item apple{1, "apple"};
Item shirt{2, "shirt"};
int timestamp = 10000;
shared_ptr
shared_ptr
vector
Shipment s1("UPS", orders);
w.process_shipment(s1);
cout << w.check_inventory(1) << endl;
cout << w.check_inventory(2) << endl;
return 0;
}
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
S: all classes have one responsibility that matches their names
O: all classes and their functionalities can be extended easily
L: stock can substitute for item easily (only one more field quantity)
I: all classes perform only actions that are needed to fulfill their roles
D: High level modules do not depend on low level modules
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
All classes can be easily extended and scalable
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...
Storage and Stock could potentially be implemented as part of warehouse for better abstraction.