1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#include <iostream> #include <vector> #include <string> // Class to represent a product class Product { public: Product(const std::string& name, int quantity) : name(name), quantity(quantity) {} // Method to get product information void getInfo() const { std::cout << name << ": " << quantity << " units\n"; } // Method to adjust the quantity void adjustQuantity(int amount) { quantity += amount; } // Method to check if there's enough stock bool hasStock(int amount) const { return quantity >= amount; } private: std::string name; int quantity; }; // Class to represent a supplier class Supplier { public: Supplier(const std::string& name) : name(name) {} // Method to supply products to a warehouse void supply(Product& product, int amount) { std::cout << name << " supplies " << amount << " units of " << product.getName() << "\n"; product.adjustQuantity(amount); } private: std::string name; }; // Class to represent a warehouse class Warehouse { public: Warehouse(const std::string& name) : name(name) {} // Method to store products void store(Product& product, int amount) { std::cout << name << " stores " << amount << " units of " << product.getName() << "\n"; product.adjustQuantity(amount); } // Method to dispatch products to a retailer void dispatch(Product& product, int amount) { if (product.hasStock(amount)) { std::cout << name << " dispatches " << amount << " units of " << product.getName() << "\n"; product.adjustQuantity(-amount); } else { std::cout << name << " does not have enough stock to dispatch " << amount << " units of " << product.getName() << "\n"; } } private: std::string name; }; // Class to represent a retailer class Retailer { public: Retailer(const std::string& name) : name(name) {} // Method to receive products from a warehouse void receive(Product& product, int amount) { std::cout << name << " receives " << amount << " units of " << product.getName() << "\n"; product.adjustQuantity(amount); } private: std::string name; }; int main() { // Create products Product widget("Widget", 0); // Create entities in the supply chain Supplier supplier("Supplier A"); Warehouse warehouse("Warehouse B"); Retailer retailer("Retailer C"); // Simulate the supply chain supplier.supply(widget, 100); warehouse.store(widget, 100); warehouse.dispatch(widget, 50); retailer.receive(widget, 50); warehouse.dispatch(widget, 60); // This will fail due to insufficient stock // Print final stock of the product widget.getInfo(); return 0; } |
Explanation
- Product Class:
- Attributes:
name
: Name of the product.quantity
: Quantity of the product.
- Methods:
getInfo()
: Prints the product’s name and quantity.adjustQuantity(int amount)
: Adjusts the quantity by the given amount.hasStock(int amount)
: Checks if there’s enough stock.
- Attributes:
- Supplier Class:
- Attributes:
name
: Name of the supplier.
- Methods:
supply(Product& product, int amount)
: Supplies products to the warehouse, adjusting the product’s quantity.
- Attributes:
- Warehouse Class:
- Attributes:
name
: Name of the warehouse.
- Methods:
store(Product& product, int amount)
: Stores products, adjusting the quantity.dispatch(Product& product, int amount)
: Dispatches products to the retailer, if stock is sufficient.
- Attributes:
- Retailer Class:
- Attributes:
name
: Name of the retailer.
- Methods:
receive(Product& product, int amount)
: Receives products from the warehouse, adjusting the quantity.
- Attributes:
- Main Function:
- Creates instances of
Product
,Supplier
,Warehouse
, andRetailer
. - Simulates the supply chain by calling methods to supply, store, dispatch, and receive products.
- Prints the final stock of the product.
- Creates instances of
Usage
- Supply Chain Simulation: The simulation demonstrates a basic supply chain process with product handling between a supplier, warehouse, and retailer.
- Stock Management: Adjustments to product quantities are handled based on the operations performed by each entity in the supply chain.