/
inventory.cpp
220 lines (173 loc) · 5.44 KB
/
inventory.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "inventory.h"
inventory::inventory(int maxWeight) {
_head = NULL;
_weight = 0;
_count = 0;
_maxWeight = maxWeight;
return;
}
inventory::inventory(const inventory& originalInventory) {
_head = NULL;
*this = originalInventory;
return;
}
inventory& inventory::operator =(const inventory& originalInventory) {
itemNode *tempNode;
itemNode *otherNode = originalInventory.getHead();
itemNode *currentNode = _head;
// Delete the list that was there before.
chainDelete();
_head = NULL;
tempNode = NULL;
while(otherNode) {
currentNode = new itemNode;
if(currentNode) {
*currentNode = *otherNode; // Using overloaded itemNode operator
// on dereferenced pointers.
}
if(_head == NULL) {
_head = currentNode;
}
if(tempNode) {
tempNode->setNext(currentNode);
}
tempNode = currentNode;
otherNode = otherNode->getNext();
}
_weight = originalInventory.getWeight();
_count = originalInventory.getCount();
return *this;
}
void inventory::chainDelete() {
itemNode *currentNode = _head;
itemNode *tempNode;
while(currentNode) {
tempNode = currentNode->getNext();
delete currentNode;
currentNode = tempNode;
}
_head = NULL;
_weight = 0;
_count = 0;
return;
}
inventory::~inventory() {
chainDelete();
return;
}
itemNode* inventory::getHead() const {
return _head;
}
double inventory::getWeight() const {
return _weight;
}
int inventory::getCount() const {
return _count;
}
void inventory::AddItem(const item& newItem) {
// We start by checking the weight to see if it will overload us.
// If so, we don't do anything and can immediately return.
if(newItem.getWeight() + _weight > _maxWeight) {
newItem.printFailure();
return;
}
itemNode *newNode = new itemNode;
if(newNode) {
newNode->setItem(newItem);
itemNode *currentNode = _head;
itemNode *previousNode = NULL;
// Go to the first node that is greater than newItem.
// previousNode will either be NULL, (in which case it's
// at the head of the list) equal to newItem,
// (in which case it will be incremented)
// or less than newItem (in which case newNode will be
// put between previousNode and currentNode).
while(currentNode && newItem >= currentNode->getItem()) {
previousNode = currentNode;
currentNode = currentNode->getNext();
}
if(previousNode == NULL) {
newNode->setNext(_head);
_head = newNode;
addWeightAndCount(newNode, newItem);
}
else if(newItem == previousNode->getItem()) {
addWeightAndCount(previousNode, newItem);
delete newNode;
}
else {
previousNode->setNext(newNode);
newNode->setNext(currentNode);
addWeightAndCount(newNode, newItem);
}
}
return;
}
void inventory::addWeightAndCount(itemNode* node, const item& newItem) {
if(newItem.getWeight() == node->getWeight()) {
node->incrementCount();
_weight += newItem.getWeight();
_count++;
newItem.printSuccess();
}
else {
std::cout << "ERROR: tried to add a duplicate item "
<< "with wrong weight!\n";
}
return;
}
void inventory::subtractWeightAndCount(const item& newItem) {
_weight -= newItem.getWeight();
_count--;
}
void inventory::RemoveItem(const char* name) {
itemNode *currentNode = _head;
itemNode *previousNode = NULL;
// Loop through the linked list, looking for a match. We store
// the previous node in case we have to entirely remove a node.
while(currentNode &&
CIstrcmp(name, currentNode->getName()) > 0) {
previousNode = currentNode;
currentNode = currentNode->getNext();
}
// We're either at the end of the list, at a match, or beyond the point
// where there could be a match.
if(currentNode == NULL ||
CIstrcmp(name, currentNode->getName()) != 0) {
std::cout << "You don't have a " << name << " in your inventory.\n";
}
else {
std::cout << "You dropped a " << name << ".\n";
currentNode->decrementCount();
subtractWeightAndCount(currentNode->getItem());
// Now we test to see if we have to delete the node.
if(currentNode->getCount() == 0) {
if(previousNode == NULL) { // If it's at the head,
_head = _head->getNext(); // delete the head.
}
else { // Otherwise, we delete the link in the middle.
previousNode->setNext(currentNode->getNext());
}
delete currentNode;
}
}
return;
}
void inventory::PrintInventory() {
itemNode *currentNode = _head;
std::cout << "\nCurrent inventory:\n";
if(currentNode == NULL) {
std::cout << "\t(no items)\n";
}
else {
while(currentNode) {
std::cout << "\t[" << currentNode->getCount() << "] "
<< currentNode->getName() << "\n";
currentNode = currentNode->getNext();
}
std::cout << "Total items: " << _count << "\n";
std::cout << "Total weight: " << _weight << "\n";
}
std::cout << "\n";
return;
}