-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cpp
More file actions
73 lines (61 loc) · 1.75 KB
/
Inventory.cpp
File metadata and controls
73 lines (61 loc) · 1.75 KB
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
#ifndef INVENTORY_C
#define INVENTORY_C
#include <iostream>
#include <map>
#include "Equipable.cpp"
#include <algorithm>
class Inventory {
private:
std::map<std::string, Equipable*> equipables;
public:
Inventory() {}
~Inventory() {
for (auto it = equipables.begin(); it != equipables.end(); it++) {
delete it->second;
}
equipables.clear();
}
Equipable* getItem(std::string name) {
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
for (auto it = equipables.begin(); it != equipables.end(); it++) {
std::string itemName = it->first;
std::transform(itemName.begin(), itemName.end(), itemName.begin(), ::tolower);
if (itemName == name) {
return it->second;
}
}
return nullptr;
}
void addItem(Equipable* equipable) {
equipables[equipable->getName()] = equipable;
std::cout << equipable->getName() << " has been added to your inventory\n";
}
void removeItem(Equipable* item) {
std::string name = item->getName();
if (equipables.find(name) == equipables.end()) {
std::cout << "You don't have " << name << " in your inventory\n";
return;
}
delete equipables[name];
equipables.erase(name);
std::cout << name << " has been removed from your inventory\n";
}
void display() {
std::cout << std::endl;
if (equipables.empty()) {
std::cout << "You have nothing in your inventory\n" << std::endl;
return;
}
std::cout << "You have the following items in your inventory:\n";
int count = 1;
for (auto it = equipables.begin(); it != equipables.end(); it++) {
std::cout << count << ". ";
std::cout << it->first << " ";
if (count % 5 == 0) std::cout << std::endl;
count++;
}
std::cout << std::endl;
}
int size() { return (int)this->equipables.size(); }
};
#endif // !INVENTORY_C