Question
The key abstractions employed in this program are Item , ItemStack , and Inventory . Complete ADT implementations have been provided for the former two.
The key abstractions employed in this program are Item, ItemStack, and Inventory. Complete ADT implementations have been provided for the former two. A partial implementation has been provided for the Inventory. Your task is to finish the Inventory.
You can implement the Copy Constructor, findMatchingItemStack, and display with loops.
You can, alternatively, use the built-in C++ functions discussed in Review 03 Example 8 (if you like a challenge).
You must implement:
- Copy Constructor
- Assignment Operator
- Note this is already provided and complete. Refer to our discussions of the copy-and-swap method.
- Once you have completed the Copy Constructor, you are done with the Big-3.
- Inventory::isFull - refer to documentation in Inventory.h.
- Inventory::findMatchingItemStackIterator - refer to documentation in Inventory.h.
- Inventory::addItemStackNoCheck - refer to documentation in Inventory.h.
- Inventory::display. This must generate the Inventory summary.
Inventory.cpp
#include
#include "Inventory.h"
// Allow the compiler to define the remaining // comparison operators using namespace std::rel_ops;
//------------------------------------------------------------------------------ Inventory::Inventory() { this->slots = 10; }
//------------------------------------------------------------------------------ Inventory::Inventory(int n) { this->slots = n; //this->allItemStacks.reserve(n); // only works for std::vector }
//------------------------------------------------------------------------------ Inventory::Inventory(const Inventory& src) { const_iterator it = src.begin(); it != src.end(); it++;
// Copy all ItemStack objects from src into _this_ Inventory }
//------------------------------------------------------------------------------ Inventory::~Inventory() {
// Done! Surprised? */ }
//------------------------------------------------------------------------------ int Inventory::utilizedSlots() const { return allItemStacks.size(); }
//------------------------------------------------------------------------------ int Inventory::emptySlots() const { return slots - utilizedSlots(); }
//------------------------------------------------------------------------------ int Inventory::totalSlots() const { return slots; }
//------------------------------------------------------------------------------ bool Inventory::isFull() const { return (utilizedSlots() == slots); }
//------------------------------------------------------------------------------ Inventory::iterator Inventory::begin() { return allItemStacks.begin(); }
//------------------------------------------------------------------------------ Inventory::iterator Inventory::end() { return allItemStacks.end(); }
//------------------------------------------------------------------------------ Inventory::const_iterator Inventory::begin() const { return allItemStacks.begin(); }
//------------------------------------------------------------------------------ Inventory::const_iterator Inventory::end() const { return allItemStacks.end(); }
//------------------------------------------------------------------------------ void Inventory::display(std::ostream &outs) const { // Output each ItemStack. Each ItemStack must be preceded by exactly // two spaces, i.e., " " }
//------------------------------------------------------------------------------ Inventory::iterator Inventory::findMatchingItemStackIterator(const ItemStack& itemStack) {
// Implement this function
}
//------------------------------------------------------------------------------ void Inventory::addItemStackNoCheck(ItemStack itemStack) { allItemStacks.push_back(itemStack); }
//------------------------------------------------------------------------------ Inventory& Inventory::operator=(Inventory rhs) { std::swap(*this, rhs); return *this; }
//------------------------------------------------------------------------------ void swap(Inventory& lhs, Inventory& rhs) { using std::swap;
swap(lhs.allItemStacks, rhs.allItemStacks); swap(lhs.slots, rhs.slots); }
//------------------------------------------------------------------------------ bool operator==(const Inventory& lhs, const Inventory& rhs) { if (lhs.utilizedSlots() != rhs.utilizedSlots()) { return false; }
if (lhs.emptySlots() != rhs.emptySlots()) { return false; }
// The two Inventory objects have the same number of used & unused slots
using const_iterator = Inventory::const_iterator;
const_iterator lhsIt = lhs.begin(); const_iterator rhsIt = rhs.begin();
while (lhsIt != lhs.end() && rhsIt != rhs.end()) { if (*lhsIt != *rhsIt) { return false; }
lhsIt++; rhsIt++; }
// If the two Inventory objects are identical, both iterators // will have reached end positions. return lhsIt == lhs.end() && rhsIt == rhs.end(); }
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 { public:
/** * Aliases for the different possible containers *
* After your code is working, switch between them. * You should have identical results for all of them. */ using ItemStackCollection = std::list
using iterator = ItemStackCollection::iterator; using const_iterator = ItemStackCollection::const_iterator;
private: /** * All `ItemStack`s in _this_ `Inventory` */ ItemStackCollection allItemStacks; int slots; ///< Capacity
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 the number of used/utilized (i.e., non-empty). */ int utilizedSlots() const;
/** * Check the number of unused (i.e., empty) slots. */ int emptySlots() const;
/** * Retrieve the total size (number of slots in total). */ int totalSlots() const;
/** * 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;
// Begin Iterator Support (begin/end) iterator begin(); iterator end();
const_iterator begin() const; const_iterator end() const; // End Iterator Support
/** * */ 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 iterator to the conatiner position with a matching ItemStack * or end() if no such iterator exists */ iterator findMatchingItemStackIterator(const ItemStack& itemStack);
/** * Add a new ItemStack to an empty Inventory slot. *
* This is simliar to the code we discussed in Review-02 * (std::list and std::vector) * * 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) { iterator matchingIterator = findMatchingItemStackIterator(itemStack);
// A match was found if(matchingIterator != this->end()){ ItemStack& matchingStack = (*matchingIterator); matchingStack.addItems(itemStack.size());
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; }
/** * Compare two Inventory objects, without direct access */ bool operator==(const Inventory& lhs, const Inventory& rhs);
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started