-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inventory.cpp
83 lines (69 loc) · 2.16 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
#include "Inventory.h"
using namespace std;
Inventory::Inventory(int capacity) {
this->capacity = capacity;
this->itemCount = 0;
this->items.reserve(this->capacity);
}
int Inventory::getCapacity() {
return this->capacity;
}
void Inventory::setCapacity(int capacity) {
this->capacity = capacity;
}
int Inventory::getItemCount() {
return this->itemCount;
}
void Inventory::setItemCount(int itemCount) {
this->itemCount = itemCount;
}
vector<Item*> Inventory::getItems() {
return this->items;
}
Item* Inventory::getItem(std::string item_name) {
if (this->isItemInInventory(item_name))
for (unsigned int i = 0; i < this->items.size(); ++i)
if (this->items[i]->getName() == item_name)
return this->items[i];
}
void Inventory::addItem(Item* item) {
this->items.push_back(item);
this->itemCount += 1;
}
void Inventory::removeItem(Item* item) {
for (vector<Item*>::iterator i = this->items.begin();
i != this->items.end(); ++i)
if (*i == item) {
delete *i;
this->items.erase(i);
this->itemCount -= 1;
}
}
void Inventory::removeItem(string name) {
for (unsigned int i = 0; i < this->items.size(); ++i)
if (this->items[i]->getName() == name) {
delete this->items[i];
this->items.erase(this->items.begin() + i);
this->itemCount -= 1;
}
}
void Inventory::printInventory() {
cout << endl << "==================================================";
cout << endl << "This is content of your inventory:" << endl;
cout << "Item name: Strength/Charisma/Speed/Special/Type" << endl;
for (int i = 0; i < this->itemCount; ++i)
this->items[i]->printInfo();
cout << "==================================================" << endl;
cout << endl;
}
bool Inventory::isItemInInventory(string item_name) {
for (int i = 0; i < this->itemCount; ++i)
if (this->items[i]->getName() == item_name)
return true;
return false;
}
Inventory::~Inventory() {
for (unsigned int i = 0; i < this->items.size(); ++i)
delete this->items[i];
this->items.clear();
}