以下是一个简单的C++仓库库存管理系统的实现思路:
- 首先,定义一个商品结构体,包含商品ID、商品名称、库存量等信息。
struct Product {
int id;
string name;
int stock;
};
- 创建一个仓库类,用于存储商品和进行库存操作。
class Warehouse {
public:
Warehouse() {
products.clear();
}
void addProduct(int id, string name, int stock) {
products.push_back(Product{id, name, stock});
}
void updateProduct(int id, int newStock) {
for (auto it = products.begin(); it != products.end(); ++it) {
if (it->id == id) {
it->stock = newStock;
break;
}
}
}
void removeProduct(int id) {
for (auto it = products.begin(); it != products.end(); ++it) {
if (it->id == id) {
products.erase(it);
break;
}
}
}
int findProductStock(int id) {
for (auto &product : products) {
if (product.id == id) {
return product.stock;
}
}
return 0;
}
void displayAllProducts() {
for (const auto &product : products) {
cout << "ID: " << product.id << ", Name: " << product.name << ", Stock: " << product.stock << endl;
}
}
private:
vector<Product> products;
};
- 创建一个用户界面,用于接收用户的操作输入,并调用仓库类的相应方法进行处理。
int main() {
Warehouse warehouse;
int choice;
while (true) {
cout << "1. Add product" << endl;
cout << "2. Update product" << endl;
cout << "3. Remove product" << endl;
cout << "4. Query product stock" << endl;
cout << "5. Display all products" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int id, stock;
string name;
cout << "Enter product ID, name, and stock: ";
cin >> id >> name >> stock;
warehouse.addProduct(id, name, stock);
break;
}
case 2: {
int id, newStock;
cout << "Enter product ID and new stock: ";
cin >> id >> newStock;
warehouse.updateProduct(id, newStock);
break;
}
case 3: {
int id;
cout << "Enter product ID to remove: ";
cin >> id;
warehouse.removeProduct(id);
break;
}
case 4: {
int id;
cout << "Enter product ID to query: ";
cin >> id;
cout << "Stock: " << warehouse.findProductStock(id) << endl;
break;
}
case 5:
warehouse.displayAllProducts();
break;
case 6:
return 0;
default:
cout << "Invalid choice, please try again." << endl;
}
}
return 0;
}
上述代码只是一个简单的实现示例,您可以根据需求进行扩展和优化。例如,可以添加日志记录功能,保存用户的操作记录;还可以使用数据库存储仓库数据,以便在程序启动时加载现有的库存信息。