jeudi 31 mars 2016

Checking objects before adding C++

I need to update Inventory::add_item() function definition so that before adding a new object to the items array, it first checks whether the identical object already is present and add new item only if it isn’t. I cannot understand how to make comparison between new_item and objects which are already exist using Inventory::find_item function.

#include "Inventory.h"

void Inventory::add_item(std::string modelName, float screenSize, int capacity, iPhone::Colour colour)
{

    if (_count < Inventory::MAX_SIZE)
    {
        iPhone new_item;
        new_item.init( modelName, screenSize,  capacity, colour);
        _items[_count] = new_item;
        _count++;

    }
}

iPhone Inventory::find_item(iPhone &query)
{
    for (size_t i = 0U; i < _count; i++)
    {
        iPhone& item = _items[i];

        //for string type property
        if (query.get_modelName()!= ""
            && query.get_modelName() != item.get_modelName())
            continue;

        // for number type property
        if (query.get_screenSize() != 0
            && query.get_screenSize() != item.get_screenSize())
            continue;
        // for number type property
        if (query.get_capacity() != 0
            && query.get_capacity() != item.get_capacity())
            continue;
        //for string type property
        if (query.get_colour() != iPhone::Colour::ANY
            && query.get_colour()!= item.get_colour())
            continue;
        return item;

    }
    return iPhone{};    // return the default value object (or null object)
}

Here are the other parts of code: Displaying objects which are found in C++

Aucun commentaire:

Enregistrer un commentaire