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 |
#include <iostream> #include <queue> #include <vector> #include <string> // Define a structure for a product struct Product { int id; std::string name; }; // Define a class for a manufacturing machine class Machine { public: Machine(const std::string& name) : machineName(name) {} void addProduct(const Product& product) { queue.push(product); } void processProduct() { if (queue.empty()) { std::cout << machineName << " has no products to process.\n"; return; } Product product = queue.front(); queue.pop(); std::cout << machineName << " processed product ID: " << product.id << ", Name: " << product.name << "\n"; } bool hasProducts() const { return !queue.empty(); } private: std::string machineName; std::queue<Product> queue; }; // Define a class for the manufacturing system class ManufacturingSystem { public: void addMachine(const Machine& machine) { machines.push_back(machine); } void addProductToMachine(int machineIndex, const Product& product) { if (machineIndex >= 0 && machineIndex < machines.size()) { machines[machineIndex].addProduct(product); } else { std::cout << "Invalid machine index.\n"; } } void processAllMachines() { for (auto& machine : machines) { machine.processProduct(); } } private: std::vector<Machine> machines; }; int main() { // Create machines Machine machine1("Machine 1"); Machine machine2("Machine 2"); Machine machine3("Machine 3"); // Create a manufacturing system ManufacturingSystem system; system.addMachine(machine1); system.addMachine(machine2); system.addMachine(machine3); // Create and add products to machines system.addProductToMachine(0, {1, "Product A"}); system.addProductToMachine(1, {2, "Product B"}); system.addProductToMachine(2, {3, "Product C"}); system.addProductToMachine(0, {4, "Product D"}); // Process all machines std::cout << "Processing products:\n"; system.processAllMachines(); return 0; } |
Explanation
- Product Structure:
- Represents a product with an
id
and aname
.
- Represents a product with an
- Machine Class:
- Represents a manufacturing machine with a name and a queue to hold products.
- Methods:
addProduct(const Product& product)
: Adds a product to the machine’s queue.processProduct()
: Processes the product at the front of the queue and removes it.hasProducts() const
: Checks if the machine has any products to process.
- ManufacturingSystem Class:
- Manages a collection of
Machine
objects. - Methods:
addMachine(const Machine& machine)
: Adds a machine to the system.addProductToMachine(int machineIndex, const Product& product)
: Adds a product to a specific machine in the system.processAllMachines()
: Processes one product from each machine in the system.
- Manages a collection of
- main Function:
- Creates instances of
Machine
andManufacturingSystem
. - Adds machines to the manufacturing system.
- Creates and adds products to different machines.
- Processes products in all machines and displays the results.
- Creates instances of