The key abstractions employed in this program are Inventory, Item, and ItemStack. Complete ADT implementations have been provided for the latter two.
The Inventory class is partially implemented. Your task is to complete the Inventory ADT. The code provided will not run correctly… until you complete all the following methods:
Note this is already provided and complete. Refer to our discussions of the copy-and-swap method.
Once you have completed the Copy Constructor and Destructor, you are done with the Big-3.
The partial implementation provided assumes that each Inventory will keep track of items in a linked list. You must not change this choice of data structure… unless you want a zero.
Inventory.h
#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
Inventory.cpp
#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->slots = 10;
this->occupied = 0;
//std::cerr << Node().data << "\n";
}
//------------------------------------------------------------------------------
Inventory::Inventory(int n)
{
this->head = nullptr;
this->tail = nullptr;
this->slots = n;
this->occupied = 0;
}
//——————————————————————————
Inventory::Inventory(const Inventory &src)
{
// @todo implement this function
}
//——————————————————————————
Inventory::~Inventory()
{
// @todo implement this function
}
//——————————————————————————
bool Inventory::isFull() const
{
// @todo implement this function
//
// If this is more than one line
// in the form “return (boolean expression);”
// you are overthinking the problem
return true; // This line is a placeholder. Remove it.
}
//——————————————————————————
void Inventory::display(std::ostream &outs) const
{
outs << " -Used " << occupied << " of " << slots << " slots" << "\n";
Node* it = head;
while(it != nullptr){
outs << " " << it->data << "\n";
it = it->next;
}
}
//——————————————————————————
Inventory& Inventory::operator=(Inventory rhs)
{
std::swap(*this, rhs);
return *this;
}
//——————————————————————————
void swap(Inventory& lhs, Inventory& rhs)
{
using std::swap;
swap(lhs.head, rhs.head);
swap(lhs.tail, rhs.tail);
swap(lhs.slots, rhs.slots);
swap(lhs.occupied, rhs.occupied);
}
//——————————————————————————
Inventory::Node* Inventory::findMatchingItemStackNode(const ItemStack& itemStack)
{
// @todo implement this function
return nullptr;
}
//——————————————————————————
void Inventory::mergeStacks(ItemStack& lhs, const ItemStack& rhs)
{
// Update lhs… remember rhs is read only
}
//——————————————————————————
void Inventory::addItemStackNoCheck(ItemStack itemStack)
{
// @todo implement this function
}
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.