Answered step by step
Verified Expert Solution
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
Your Tasks
The key abstr
#include
#include "Inventory.h
Allow the compiler to define the remaining
comparison operators
using namespace std::relops;
Inventory::Node::Node
:dataItem "Air"
thisnext nullptr;
Inventory::Node::NodeItemStack s
:datas
thisnext nullptr;
Inventory::Inventory
thishead nullptr;
thistail nullptr;
thisslo#ifndef INVENTORYHINCLUDED
#define INVENTORYHINCLUDED
#include
#include "ItemStack.h
An Inventory is composed of n slots. Each slot may store only
one type of itemspecified 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 slotie 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
NodeItemStack s;
;
Node head; First inventory slot
Node tail; Last inventory slot
int slots; Capacity
int occupied; Number of occupied slots
public:
Default to slots
Inventory;
Create an inventory with n slots
@pre n
Inventoryint n;
Duplicate an existing Inventory
Inventoryconst 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 addItemsItemStack itemStack;
Check if this inventory is full
@return occupied slotstechnically a typo
bool isFull const;
Print a Summary of the Inventory and all Items contained within
void displaystd::ostream& outs const;
Inventory& operatorInventory rhs;
Swap the contents of two Inventorys
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 herehttp:stackoverflowcomquestionswhatisthecopyandswapidiom
friend
void swapInventory& 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 findMatchingItemStackNodeconst ItemStack& itemStack;
Merge two item stacks.
@param lhs item stack where items need to be added
@param rhs item stack with the number of items to add
@pre lhsid rhsid
void mergeStacksItemStack& lhs const ItemStack& rhs;
Append a Node.
This is the code we discussed in Review
When this method is invoked all special cases
have already been covered in addItems
Abstraction and Interfaces
void addItemStackNoCheckItemStack itemStack;
;
inline
bool Inventory::addItemsItemStack itemStack
Node matchingNode findMatchingItemStackNodeitemStack;
A match was found
ifmatchingNode nullptr
mergeStacksmatchingNodedata, itemStack;
return true;
There is no space for a new type of ItemStack
ifthisisFull
return false;
This is a new type of item and there is plenty of room
addItemStackNoCheckitemStack;
return true;
Print the Inventory through use of the display member function
inline
std::ostream& operatorstd::ostream& outs, const Inventory& prt
prtdisplayouts;
return outs;
#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