增加客户的项目数

时间:2016-10-18 16:03:42

标签: c++

你好我做一个购物清单。我有一个类Customer和一个类Item。我的程序是这样工作的,如果客户不在数据库中,我问客户的名字我添加了这个客户,如果客户是我打印错误,以便说客户存在。之后,我打印了10个产品的列表,并要求客户选择产品并增加该客户的特定项目的数量。 我的程序没有给出任何错误,我尝试做调试,但我没有找到错误,因为该程序不会增加任何数量。

这是我的主要功能:

void DisplayList(Item shopList[10], Customer& custom)
{
    int choose_item = 1;
    while (choose_item != 0)
    {
        cout << "The items you can buy are: (0 to exit)" << endl;
        for (int i = 0; i < 10; i++)
        {
            cout << i + 1 << ". " << shopList[i].getName() << "  " << shopList[i].getUnitPrice() << "  x" << shopList[i].getCount() << endl;
        }
        cout << "What item you would like to buy, Input: ";
        cin >> choose_item;
        custom.addItem(shopList[choose_item - 1]);
    }
}

我的客户标题:

class Customer
{
public:
    Customer(string);
    Customer(){}
    void addItem(Item);
    set<Item> getItems() const;

private:
    string _name;
    set<Item> _items;


};

我的客户cpp

Customer::Customer(string name)
{
    _name = name;
}
Customer::Customer(){  };


void Customer::addItem(Item SpecificItem)
{
    set<Item>::iterator it;
    if ((it = _items.find(SpecificItem)) != _items.end())
    {
        SpecificItem.setCount(SpecificItem.getCount() + 1);
    }
    else
    {
        _items.insert(SpecificItem);
    }

}
set<Item> Customer::getItems() const
{
    return _items;
}

我的项目标题:

class Item
{
public:
    Item(string, string, double);
    ~Item();

    //get and set functions
    string getName() const;
    string getSerialNumber() const;
    int getCount();
    double getUnitPrice() const;

    void setCount(int count);


private:
    string _name;
    string _serialNumber; //consists of 5 numbers
    int _count; //default is 1, can never be less than 1!
    double _unitPrice; //always bigger than 0!

};

我的项目cpp

#include "Item.h"

Item::Item(string name, string serial_number, double price) : _name(name), _serialNumber(serial_number), _unitPrice(price), _count(1)
{
};

Item::~Item(){};

string Item::getName() const
{
    return _name;
}

string Item::getSerialNumber() const
{
    return _serialNumber;
}

int Item::getCount()
{
    return _count;
}

double Item::getUnitPrice() const
{
    return _unitPrice;
}

void Item::setCount(int count)
{
    _count = count;
}

0 个答案:

没有答案