-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanItemList.cpp
92 lines (79 loc) · 1.8 KB
/
anItemList.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
#include <iostream>
#include "anItemList.h"
anItemList::anItemList() {
numItems = 0;
}
void anItemList::addItem(anItem item) {
if (numItems < MAX_ITEMS) {
numItems++;
objectS[numItems-1] = item;
}
else if (numItems >= MAX_ITEMS) {
cout << "Array Full!" << endl;
}
}
void anItemList::addItem(string name, string description, bool canTake) {
anItem item;
item.setName(name);
item.setDescription(description);
item.setCanTake(canTake);
addItem(item);
}
anItem anItemList::getItem(int index) {
if (index > 0 && index <= MAX_ITEMS) {
return objectS[index-1];
}
else {
cout << "Error, index out of range." << endl;
anItem item;
return item;
}
}
anItem anItemList::removeItem(int index) {
if (index > 0 && index <= MAX_ITEMS) {
anItem y = objectS[index-1];
anItem item;
objectS[index-1] = item;
return y;
}
else {
cout << "Error, index out of range." << endl;
anItem item;
return item;
}
}
int anItemList::findItem(string name) {
for (int i = 0; i < numItems; i++) {
if (name.compare(objectS[i].getName()) == 0) {
return i;
}
}
return NOT_FOUND;
}
int anItemList::getNumItems() {
return numItems;
}
void anItemList::print() {
cout << "ITEMLIST: numItems " << numItems << endl;
for (int i = 0; i < getNumItems(); i++) {
getItem(i).print();
}
}
void anItemList::displayItems() {
string messPrefix = "You notice the following: ";
string noItemsMess = "You are holding: ";
if (numItems > 0) {
cout << messPrefix;
int i;
for (i = 0; i < numItems-1 && numItems -1 >= 0; i++) {
cout << objectS[i].getName() << ", ";
}
cout << objectS[i].getName();
cout << "." << endl;
}
else if (numItems == 0 && noItemsMess.compare("") != 0) {
cout << noItemsMess;
}
else {
}
}