Query products: manager can input a product id to query a particular product or input “*” to list all products in the product vector. Output “No product found!” if there is no particular product. (5 marks)
时间: 2024-02-12 19:05:23 浏览: 20
Sure, I understand your question. To implement the product query functionality, we can use a simple if-else condition to check if the input product id matches with any of the product ids in our product vector. Here is the sample code in C++:
```
string productId; // input product id
bool found = false;
if(productId == "*") {
// list all products in the product vector
for(int i = 0; i < products.size(); i++) {
cout << products[i].toString() << endl; // assuming toString() method returns product details
}
} else {
// query a particular product
for(int i = 0; i < products.size(); i++) {
if(products[i].getId() == productId) {
cout << products[i].toString() << endl; // assuming toString() method returns product details
found = true;
break;
}
}
}
if(!found) {
cout << "No product found!" << endl;
}
```
In the above code, we first check if the input product id is "*" (i.e., list all products) or a specific product id. If it is "*", we loop through all the products in our product vector and print their details. Otherwise, we loop through the products and check if any of them match with the input product id. If we find a match, we print the product details and set the `found` flag to true. If we don't find any match, we print the "No product found!" message.