void inventory::addWeightAndCount(itemNode* node, const item& newItem) { if(newItem.getWeight() == node->getWeight()) { node->incrementCount(); _weight += newItem.getWeight(); _count++; newItem.printSuccess(); } else { std::cout << "ERROR: tried to add a duplicate item " << "with wrong weight!\n"; } return; }
void inventory::AddItem(const item& newItem) { // We start by checking the weight to see if it will overload us. // If so, we don't do anything and can immediately return. if(newItem.getWeight() + _weight > _maxWeight) { newItem.printFailure(); return; } itemNode *newNode = new itemNode; if(newNode) { newNode->setItem(newItem); itemNode *currentNode = _head; itemNode *previousNode = NULL; // Go to the first node that is greater than newItem. // previousNode will either be NULL, (in which case it's // at the head of the list) equal to newItem, // (in which case it will be incremented) // or less than newItem (in which case newNode will be // put between previousNode and currentNode). while(currentNode && newItem >= currentNode->getItem()) { previousNode = currentNode; currentNode = currentNode->getNext(); } if(previousNode == NULL) { newNode->setNext(_head); _head = newNode; addWeightAndCount(newNode, newItem); } else if(newItem == previousNode->getItem()) { addWeightAndCount(previousNode, newItem); delete newNode; } else { previousNode->setNext(newNode); newNode->setNext(currentNode); addWeightAndCount(newNode, newItem); } } return; }
//Adds the weight of aItem to the inventory's total weight //Returns whether or not it succeeded. bool inventory::incWeight(const item& aItem) { //If the new items weight added to the inventory total weight exceeds the //MAX_WEIGHT value, return a failure. double item_weight = aItem.getWeight(); if((item_weight + weight) > MAX_WEIGHT) { cout << "You're not strong enough to pick up the " << aItem << " with everything else you're carrying." << endl; return false; } else { cout << "You picked up a " << aItem << "." << endl; weight+=item_weight; return true; } }
void inventory::subtractWeightAndCount(const item& newItem) { _weight -= newItem.getWeight(); _count--; }