Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3 Your Tasks The key abstr #include #include Inventory.h / / Allow the compiler to define the remaining / / comparison operators using namespace

3 Your Tasks
The key abstr
#include
#include "Inventory.h"
// Allow the compiler to define the remaining
// comparison operators
using namespace std::rel_ops;
//------------------------------------------------------------------------------
Inventory::Node::Node()
:data(Item(0, "Air"),0)
{
this->next = nullptr;
}
//------------------------------------------------------------------------------
Inventory::Node::Node(ItemStack s)
:data(s)
{
this->next = nullptr;
}
//------------------------------------------------------------------------------
Inventory::Inventory()
{
this->head = nullptr;
this->tail = nullptr;
this->slo#ifndef INVENTORY_H_INCLUDED
#define INVENTORY_H_INCLUDED
#include
#include "ItemStack.h"
/**
* An Inventory is composed of n slots. Each slot may store only
* one type of item--specified by *slots*.
*
* Once all slots are filled, no additional Item types may be
* stored. Individual slots may contain any number of the same
* Item.
*/
class Inventory {
private:
/**
* Each Node represents on Inventory slot--i.e., space
*/
struct Node {
ItemStack data; /// One ItemStack
Node* next; /// Next ItemStack Node
/**
* Create an empty *Air* Node
*/
Node();
/**
* Create a Node that contains an ItemStack, *s*
*/
Node(ItemStack s);
};
Node* head; /// First inventory slot
Node* tail; /// Last inventory slot
int slots; /// Capacity
int occupied; /// Number of occupied slots
public:
/**
* Default to 10 slots
*/
Inventory();
/**
* Create an inventory with n slots
*
* @pre n >0
*/
Inventory(int n);
/**
* Duplicate an existing Inventory
*/
Inventory(const Inventory& src);
/**
* Empty all Inventory slots.
*/
~Inventory();
/**
* Add one or more items to the inventory list
*
* @return true if *stack* was added and false otherwise
*/
bool addItems(ItemStack itemStack);
/**
* Check if this inventory is full
*
* @return (occupied slots)//**technically a typo**
*/
bool isFull() const;
/**
* Print a Summary of the Inventory and all Items contained within
*/
void display(std::ostream& outs) const;
/**
*
*/
Inventory& operator=(Inventory rhs);
/**
* Swap the contents of two `Inventory`s
*(Yes this should be spelled "ies"). However, we
* need to recognize that Inventory is the type of both
*`lhs` and `rhs`
*
* I am using a friend function here and only here (under protest)
*
*[Refer here](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom)
*/
friend
void swap(Inventory& lhs, Inventory& rhs);
private:
/**
* Find Node containing and ItemStack with a matching id
*
* @param itemStack ItemStack for which we want a match
*
* @return pointer to a Node containing a matching ItemStack
* or nullptr if no such Node exists
*/
Node* findMatchingItemStackNode(const ItemStack& itemStack);
/**
* Merge two item stacks.
*
* @param lhs item stack where items need to be `add`ed
* @param rhs item stack with the *number* of items to add
*
* @pre lhs.id == rhs.id
*/
void mergeStacks(ItemStack& lhs, const ItemStack& rhs);
/**
* Append a Node.
*
* This is the code we discussed in Review-01
* When this method is invoked all special cases
* have already been covered in `addItems`.
*
* Abstraction and Interfaces
*/
void addItemStackNoCheck(ItemStack itemStack);
};
//------------------------------------------------------------------------------
inline
bool Inventory::addItems(ItemStack itemStack)
{
Node* matchingNode = findMatchingItemStackNode(itemStack);
// A match was found
if(matchingNode != nullptr){
mergeStacks(matchingNode->data, itemStack);
return true;
}
// There is no space for a new type of `ItemStack`
if(this->isFull()){
return false;
}
// This is a new type of item and there is plenty of room
addItemStackNoCheck(itemStack);
return true;
}
/**
* Print the Inventory through use of the display member function
*/
inline
std::ostream& operator(std::ostream& outs, const Inventory& prt)
{
prt.display(outs);
return outs;
}
#endif
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

Develop successful mentoring programs. page 400

Answered: 1 week ago