📜  耐克 - C++ (1)

📅  最后修改于: 2023-12-03 15:11:46.111000             🧑  作者: Mango

耐克 - C++

简介

耐克(Nike)是世界知名的运动鞋、服装、配件制造商。而C++是一种高级编程语言,广泛应用于软件开发领域。这篇文章将介绍如何通过C++来开发一个耐克的商品管理系统。

功能

我们的耐克商品管理系统将包含以下功能:

  • 添加商品
  • 删除商品
  • 显示商品列表
  • 修改商品信息
数据结构

为了实现商品管理系统,我们需要存储商品的信息。这里我们选择使用一个结构体来存储商品的各种属性:

struct Product {
    string name;
    string category;
    double price;
    int quantity;
};

其中,每个商品都有名字、类别、价格和数量四个属性。

添加商品

添加商品的功能实现代码如下:

void addProduct(vector<Product>& products) {
    Product newProduct;
    cout << "Enter product name: ";
    cin >> newProduct.name;
    cout << "Enter product category: ";
    cin >> newProduct.category;
    cout << "Enter product price: ";
    cin >> newProduct.price;
    cout << "Enter product quantity: ";
    cin >> newProduct.quantity;
    products.push_back(newProduct);
}

这里我们使用了一个vector来存储所有的商品对象。

删除商品

删除商品的功能实现代码如下:

void deleteProduct(vector<Product>& products) {
    string productName;
    cout << "Enter product name to delete: ";
    cin >> productName;
    for (int i = 0; i < products.size(); i++) {
        if (products[i].name == productName) {
            products.erase(products.begin() + i);
            cout << "Product deleted." << endl;
            return;
        }
    }
    cout << "Product not found." << endl;
}

这里我们通过商品名称来删除商品对象。如果找到了对应的商品对象,则使用erase函数从vector中删除该对象。

显示商品列表

显示商品列表的功能实现代码如下:

void showProductList(vector<Product>& products) {
    for (int i = 0; i < products.size(); i++) {
        cout << products[i].name << "\t" << products[i].category << "\t" << products[i].price << "\t" << products[i].quantity << endl;
    }
}

这里我们遍历所有的商品对象,并输出它们的属性值。

修改商品信息

修改商品信息的功能实现代码如下:

void modifyProduct(vector<Product>& products) {
    string productName;
    cout << "Enter product name to modify: ";
    cin >> productName;
    for (int i = 0; i < products.size(); i++) {
        if (products[i].name == productName) {
            Product& product = products[i];
            cout << "Enter product name: ";
            cin >> product.name;
            cout << "Enter product category: ";
            cin >> product.category;
            cout << "Enter product price: ";
            cin >> product.price;
            cout << "Enter product quantity: ";
            cin >> product.quantity;
            cout << "Product modified." << endl;
            return;
        }
    }
    cout << "Product not found." << endl;
}

这里我们通过商品名称来查找到对应的商品对象,并允许用户修改商品对象的属性。

完整代码
#include <iostream>
#include <vector>

using namespace std;

struct Product {
    string name;
    string category;
    double price;
    int quantity;
};

void addProduct(vector<Product>& products) {
    Product newProduct;
    cout << "Enter product name: ";
    cin >> newProduct.name;
    cout << "Enter product category: ";
    cin >> newProduct.category;
    cout << "Enter product price: ";
    cin >> newProduct.price;
    cout << "Enter product quantity: ";
    cin >> newProduct.quantity;
    products.push_back(newProduct);
}

void deleteProduct(vector<Product>& products) {
    string productName;
    cout << "Enter product name to delete: ";
    cin >> productName;
    for (int i = 0; i < products.size(); i++) {
        if (products[i].name == productName) {
            products.erase(products.begin() + i);
            cout << "Product deleted." << endl;
            return;
        }
    }
    cout << "Product not found." << endl;
}

void showProductList(vector<Product>& products) {
    for (int i = 0; i < products.size(); i++) {
        cout << products[i].name << "\t" << products[i].category << "\t" << products[i].price << "\t" << products[i].quantity << endl;
    }
}

void modifyProduct(vector<Product>& products) {
    string productName;
    cout << "Enter product name to modify: ";
    cin >> productName;
    for (int i = 0; i < products.size(); i++) {
        if (products[i].name == productName) {
            Product& product = products[i];
            cout << "Enter product name: ";
            cin >> product.name;
            cout << "Enter product category: ";
            cin >> product.category;
            cout << "Enter product price: ";
            cin >> product.price;
            cout << "Enter product quantity: ";
            cin >> product.quantity;
            cout << "Product modified." << endl;
            return;
        }
    }
    cout << "Product not found." << endl;
}

int main() {
    vector<Product> products;
    bool quit = false;
    while (!quit) {
        cout << "1. Add product" << endl;
        cout << "2. Delete product" << endl;
        cout << "3. Show products" << endl;
        cout << "4. Modify product" << endl;
        cout << "5. Quit" << endl;
        int choice;
        cout << "Enter your choice: ";
        cin >> choice;
        switch (choice) {
            case 1:
                addProduct(products);
                break;
            case 2:
                deleteProduct(products);
                break;
            case 3:
                showProductList(products);
                break;
            case 4:
                modifyProduct(products);
                break;
            case 5:
                quit = true;
                break;
            default:
                cout << "Invalid choice." << endl;
        }
    }
    return 0;
}