-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inventory.h
170 lines (142 loc) · 4.22 KB
/
Inventory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef INVENTORY_H_INCLUDED
#define INVENTORY_H_INCLUDED
#include <iostream>
#include "ItemStack.h"
/**
* An Inventory is composed of n slots. Each slot may store only
* one type of item--specified by *slots*.
* <p>
* 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`
* <p>
* I am using a friend function here and only here (under protest)
* <p>
* [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.
* <p>
* This is the code we discussed in Review-01
* When this method is invoked all special cases
* have already been covered in `addItems`.
* <p>
* 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